Skip to content

Instantly share code, notes, and snippets.

@milankragujevic
Created January 13, 2019 13:21
Show Gist options
  • Save milankragujevic/b152f826bda60adff5aefa4a50d9df44 to your computer and use it in GitHub Desktop.
Save milankragujevic/b152f826bda60adff5aefa4a50d9df44 to your computer and use it in GitHub Desktop.
Bulsatcom IR WiFi Bridge with ESP8266 (2019)
#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
#include <ESP8266mDNS.h>
#include <IRremoteESP8266.h>
#include <IRsend.h>
#include <WiFiClient.h>
MDNSResponder mdns;
IRsend irsend(14);
ESP8266WebServer server(80);
//IPAddress ip(192, 168, 8, 250);
//IPAddress gateway(192, 168, 8, 1);
//IPAddress subnet(255, 255, 255, 0);
const char* ssid = "HUAWEI-B310-XXXX";
const char* password = "XXXXXXXXXXX";
const char* mDNSName = "ESP_2BC290";
const uint32_t btnPwr = 0x20250AF;
const uint32_t btn1 = 0x202807F;
const uint32_t btn2 = 0x20240BF;
const uint32_t btn3 = 0x202C03F;
const uint32_t btn4 = 0x20220DF;
const uint32_t btn5 = 0x202A05F;
const uint32_t btn6 = 0x202609F;
const uint32_t btn7 = 0x202E01F;
const uint32_t btn8 = 0x20210EF;
const uint32_t btn9 = 0x202906F;
const uint32_t btn0 = 0x20200FF;
const uint32_t volUp = 0x202A857;
const uint32_t volDown = 0x20238C7;
const uint32_t chUp = 0x202F807;
const uint32_t chDown = 0x2027887;
const uint32_t btnMute = 0x20230CF;
const uint32_t btnMenu = 0x202F00F;
const uint32_t btnUp = 0x202D02F;
const uint32_t btnDown = 0x202708F;
const uint32_t btnLeft = 0x20208F7;
const uint32_t btnRight = 0x2028877;
const uint32_t btnOK = 0x202B04F;
void led() {
digitalWrite(16, HIGH);
delay(50);
digitalWrite(16, LOW);
delay(50);
digitalWrite(16, HIGH);
delay(50);
digitalWrite(16, LOW);
delay(50);
digitalWrite(16, HIGH);
}
// server.arg("code")
bool processCommand(String code) {
if(code == "btn") {
return false;
} else {
if(code == "btnPwr") {
irsend.sendNEC(btnPwr, 32);
return true;
} else if(code == "btn1") {
irsend.sendNEC(btn1, 32);
return true;
} else if(code == "btn2") {
irsend.sendNEC(btn2, 32);
return true;
} else if(code == "btn3") {
irsend.sendNEC(btn3, 32);
return true;
} else if(code == "btn4") {
irsend.sendNEC(btn4, 32);
return true;
} else if(code == "btn5") {
irsend.sendNEC(btn5, 32);
return true;
} else if(code == "btn6") {
irsend.sendNEC(btn6, 32);
return true;
} else if(code == "btn7") {
irsend.sendNEC(btn7, 32);
return true;
} else if(code == "btn8") {
irsend.sendNEC(btn8, 32);
return true;
} else if(code == "btn9") {
irsend.sendNEC(btn9, 32);
return true;
} else if(code == "btn0") {
irsend.sendNEC(btn0, 32);
return true;
} else if(code == "volUp") {
irsend.sendNEC(volUp, 32);
return true;
} else if(code == "volDown") {
irsend.sendNEC(volDown, 32);
return true;
} else if(code == "chUp") {
irsend.sendNEC(chUp, 32);
return true;
} else if(code == "chDown") {
irsend.sendNEC(chDown, 32);
return true;
} else if(code == "btnMute") {
irsend.sendNEC(btnMute, 32);
return true;
} else if(code == "btnDown") {
irsend.sendNEC(btnDown, 32);
return true;
} else if(code == "btnUp") {
irsend.sendNEC(btnUp, 32);
return true;
} else if(code == "btnLeft") {
irsend.sendNEC(btnLeft, 32);
return true;
} else if(code == "btnRight") {
irsend.sendNEC(btnRight, 32);
return true;
} else if(code == "btnOK") {
irsend.sendNEC(btnOK, 32);
return true;
} else if(code == "btnMenu") {
irsend.sendNEC(btnMenu, 32);
return true;
}
return false;
}
return false;
}
String getValue(String data, char separator, int index)
{
int found = 0;
int strIndex[] = { 0, -1 };
int maxIndex = data.length() - 1;
for (int i = 0; i <= maxIndex && found <= index; i++) {
if (data.charAt(i) == separator || i == maxIndex) {
found++;
strIndex[0] = strIndex[1] + 1;
strIndex[1] = (i == maxIndex) ? i+1 : i;
}
}
return found > index ? data.substring(strIndex[0], strIndex[1]) : "";
}
int MAX_SUBSTRINGS = 5;
bool processCommandMulti(String multicode) {
Serial.print("Multicode is: ");
Serial.print(multicode);
Serial.println("");
for (int i = 0; i < MAX_SUBSTRINGS; i++) {
String code = getValue(multicode, ',', i);
Serial.println(code);
if (code != "") {
if(processCommand(code)) {
Serial.print("Successfully sent command: ");
Serial.print(code);
Serial.println("");
} else {
Serial.println("Error sending command. ");
Serial.print("Tried to send command: ");
Serial.print(code);
Serial.println("");
}
led();
}
}
return true;
}
void handleRequest() {
String message = "";
String code = server.arg("code");
String multicode = server.arg("multicode");
if(multicode != "") {
if(processCommandMulti(multicode)) {
message = "OK_MULTI";
} else {
message = "INVALID_PARAMETER_MULTICODE";
}
} else {
if(processCommand(code)) {
Serial.print("Successfully sent command: ");
Serial.print(code);
Serial.println("");
message = "OK";
} else {
Serial.println("Error sending command. ");
Serial.print("Tried to send command: ");
Serial.print(code);
Serial.println("");
message = "INVALID_PARAMETER_CODE";
}
}
server.sendHeader("Access-Control-Allow-Origin", "*");
server.send(200, "text/plain", message);
led();
}
void setup() {
pinMode(16, OUTPUT);
digitalWrite(16, HIGH);
Serial.begin(115200);
delay(100);
irsend.begin();
Serial.println("-------------------------------");
Serial.println("ZHD 210 DVB-S2 Single IR Bridge");
Serial.println("-------------------------------");
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
//WiFi.config(ip, gateway, subnet);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
Serial.println("");
if(mdns.begin(mDNSName)) {
Serial.println("MDNS responder started");
String identifyMessage = "Identified as ";
identifyMessage += mDNSName;
Serial.println(identifyMessage);
} else {
Serial.println("Couldn't start MDNS responder!");
}
server.on("/", handleRequest);
server.onNotFound([](){
server.sendHeader("Access-Control-Allow-Origin", "*");
server.send(404, "text/plain", "PAGE_NOT_FOUND");
led();
});
server.begin();
Serial.println("Server started on port 80");
}
void loop() {
server.handleClient();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment