http://things.m31.ch/?p=232
I couldn't find any library for this so I'be been working on this code to try to send socket messages using the format defined there, but no matter what, it's not working. Anyone has any idea what could be wrong ?
I don't know if it's the way I'm building the message (packet) or the way I'm sending it but my TV doesn't receive anything at all. Maybe someone can help me ? You will be able to see in the code both way's I tried to build the message, the base64 encoding library I used and the 3 diferent ways of sending the message I used so far without luck.
// Definiciones para TV por SOCKET
#include <rBase64.h> // TEST Base64 para Socket TV
#include <ESP8266WiFi.h>
const char* ssid = "...";
const char* password = "...";
const char* host = "192.168.0.230";
// Definiciones para TV por SOCKET
String TV_IP = "192.168.0.230";
String ESP_IP = "192.168.0.32";
String ESP_Mac = "00-0c-29-3e-b1-4f";
String Appstring = "ESP C - Arduino IDE";
String TVAppstring = "TV 3D Samsung";
String RemoteName = "ESP Samsung Remote";
String MSG0 = ""; // Auth
String MSG0_PAYLOAD = ""; // Auth Payload
String MSG1 = ""; // Auth
String MSG1_PAYLOAD = ""; // Auth Payload
void setup() {
Serial.begin(9600);
delay(10);
// We start by connecting to a WiFi network
Serial.println();
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
// Armo el MSG para TV por Socket
String ESP_IP_b64 = rbase64.encode(ESP_IP);
String ESP_Mac_b64 = rbase64.encode(ESP_Mac);
String RemoteName_b64 = rbase64.encode(RemoteName);
// authenticationMessage = "${(char)remoteNameLength}${(char)0x00}${remoteName}"
// MSG0_PAYLOAD = (char)0x64 + (char)0x00 + (char)(ESP_IP_b64.length()) + (char)0x00 + ESP_IP_b64 + (char)(ESP_Mac_b64.length()) + (char)0x00 + ESP_Mac_b64 + (char)(RemoteName_b64.length()) + (char)0x00 + RemoteName_b64;
MSG0_PAYLOAD += (char)0x00;
MSG0_PAYLOAD += (char)(ESP_IP_b64.length());
MSG0_PAYLOAD += (char)0x00;
MSG0_PAYLOAD += ESP_IP_b64;
MSG0_PAYLOAD += (char)(ESP_Mac_b64.length());
MSG0_PAYLOAD += (char)0x00;
MSG0_PAYLOAD += ESP_Mac_b64;
MSG0_PAYLOAD += (char)(RemoteName_b64.length());
MSG0_PAYLOAD += (char)0x00;
MSG0_PAYLOAD += RemoteName_b64;
// authenticationPacket = "${(char)0x00}${(char)appStringLength}${(char)0x00}${appString}${(char)authenticationMessageLength}${(char)0x00}${authenticationMessage}"
// MSG0 = (char)0x00 + (char)(Appstring.length()) + (char)0x00 + Appstring + (char)(MSG0_PAYLOAD.length()) + (char)0x00 + MSG0_PAYLOAD;
MSG0 += (char)0x00;
MSG0 += (char)(Appstring.length());
MSG0 += (char)0x00;
MSG0 += Appstring;
MSG0 += (char)(MSG0_PAYLOAD.length());
MSG0 += (char)0x00;
MSG0 += MSG0_PAYLOAD;
}
int value = 0;
void loop() {
delay(5000);
++value;
Serial.print("connecting to ");
Serial.println(host);
// Use WiFiClient class to create TCP connections
WiFiClient client;
const int httpPort = 55000;
if (!client.connect(host, httpPort)) {
Serial.println("connection failed");
return;
}
// This will send the request to the server
// client.print(String("GET ") + url + " HTTP/1.1\r\n" + "Host: " + host + "\r\n" + "Connection: close\r\n\r\n");
client.print(MSG0);
client.print(MSG1);
client.print(String("GET ") + MSG0 + " HTTP/1.1\r\n" + "Host: " + host + "\r\n" + "Connection: close\r\n\r\n");
client.print(String("GET ") + MSG1 + " HTTP/1.1\r\n" + "Host: " + host + "\r\n" + "Connection: close\r\n\r\n");
client.print(String("POST ") + MSG0 + " HTTP/1.1\r\n" + "Host: " + host + "\r\n" + "Connection: close\r\n\r\n");
client.print(String("POST ") + MSG1 + " HTTP/1.1\r\n" + "Host: " + host + "\r\n" + "Connection: close\r\n\r\n");
unsigned long timeout = millis();
while (client.available() == 0) {
if (millis() - timeout > 5000) {
Serial.println(">>> Client Timeout !");
client.stop();
return;
}
}
// Read all the lines of the reply from server and print them to Serial
while(client.available()){
String line = client.readStringUntil('\r');
Serial.print(line);
}
Serial.println();
Serial.println("closing connection");
}
Thanks.