-
Notifications
You must be signed in to change notification settings - Fork 836
/
Copy pathIRGCTCPServer.ino
136 lines (121 loc) · 4.38 KB
/
IRGCTCPServer.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/*
* IRremoteESP8266: IRGCTCPServer - send Global Cache-formatted codes via TCP.
* An IR emitter must be connected to GPIO pin 4.
* Version 0.2 May, 2017
* Copyright 2016 Hisham Khalifa, http://www.hishamkhalifa.com
* Copyright 2017 David Conran
*
* Example command - Samsung TV power toggle: 38000,1,1,170,170,20,63,20,63,20,63,20,20,20,20,20,20,20,20,20,20,20,63,20,63,20,63,20,20,20,20,20,20,20,20,20,20,20,20,20,63,20,20,20,20,20,20,20,20,20,20,20,20,20,63,20,20,20,63,20,63,20,63,20,63,20,63,20,63,20,1798\r\n
* For more codes, visit: https://irdb.globalcache.com/
*
* How to use this program:
* 1) Update "kSsid" and "kPassword" below for your WIFI network.
* 2) Compile and upload the sketch to your ESP8266 module.
* 3) (Optional) Use the serial connection to confirm it started and get the
* IP address.
* 4) From a client machine, connect to port 4998 on the ESP8266, using
* 'telnet', 'nc' (netcat), 'putty' or similar command, etc.
* You may need to install one.
* Unix/OSX:
* Start a shell. Then type:
* telnet <esp8266deviceIPaddress> 4998
* Windows:
* Start a new CMD window, then type:
* telnet <esp8266deviceIPaddress> 4998
*
* 5) Enter a Global Cache-formatted code, starting at the frequency,
* and then a return/enter at the end. No spaces. e.g.:
*
* 38000,1,1,170,170,20,63,20,63,20,63,20,20,20,20,20,20,20,20,20,20,20,63,20,63,20,63,20,20,20,20,20,20,20,20,20,20,20,20,20,63,20,20,20,20,20,20,20,20,20,20,20,20,20,63,20,20,20,63,20,63,20,63,20,63,20,63,20,63,20,1798
*
* To exit the 'telnet' command:
* press <control> + <]> at the same time, then press 'q', and then <return>.
* or:
* <control> + <d> might work.
*
* This program will display the ESP's IP address on the serial console, or you
* can check your wifi router for it's address.
*/
#include <Arduino.h>
#if defined(ESP8266)
#include <ESP8266WiFi.h>
#endif // ESP8266
#if defined(ESP32)
#include <WiFi.h>
#endif // ESP32
#include <IRremoteESP8266.h>
#include <IRsend.h>
#include <WiFiClient.h>
#include <WiFiServer.h>
const char* kSsid = "..."; // Put your WIFI SSID here.
const char* kPassword = "..."; // Put your WIFI Password here.
WiFiServer server(4998); // Uses port 4998.
WiFiClient client;
uint16_t *code_array;
#define IR_LED 4 // ESP8266 GPIO pin to use. Recommended: 4 (D2).
IRsend irsend(IR_LED); // Set the GPIO to be used to sending the message.
void sendGCString(String str) {
int16_t index;
uint16_t count;
// Find out how many items there are in the string.
index = -1;
count = 1;
do {
index = str.indexOf(',', index + 1);
count++;
} while (index != -1);
// Now we know how many there are, allocate the memory to store them all.
code_array = reinterpret_cast<uint16_t*>(malloc(count * sizeof(uint16_t)));
// Check we malloc'ed successfully.
if (code_array == NULL) { // malloc failed, so give up.
Serial.printf("\nCan't allocate %d bytes. (%d bytes free)\n",
count * sizeof(uint16_t), ESP.getFreeHeap());
Serial.println("Giving up & forcing a reboot.");
ESP.restart(); // Reboot.
delay(500); // Wait for the restart to happen.
return; // Should never get here, but just in case.
}
// Now convert the strings to integers and place them in code_array.
count = 0;
uint16_t start_from = 0;
do {
index = str.indexOf(',', start_from);
code_array[count] = str.substring(start_from, index).toInt();
start_from = index + 1;
count++;
} while (index != -1);
#if SEND_GLOBALCACHE
irsend.sendGC(code_array, count); // All done. Send it.
#endif // SEND_GLOBALCACHE
free(code_array); // Free up the memory allocated.
}
void setup() {
// initialize serial:
Serial.begin(115200);
delay(100);
Serial.println(" ");
Serial.println("IR TCP Server");
WiFi.begin(kSsid, kPassword);
while (WiFi.status() != WL_CONNECTED) {
delay(900);
Serial.print(".");
}
server.begin();
IPAddress myAddress = WiFi.localIP();
Serial.println(myAddress.toString());
irsend.begin();
}
void loop() {
while (!client)
client = server.available();
while (!client.connected()) {
delay(900);
client = server.available();
}
if (client.available()) {
String ir_code_str = client.readStringUntil('\r'); // Exclusive of \r
client.readStringUntil('\n'); // Skip new line as well
client.flush();
sendGCString(ir_code_str);
}
}