After searching and experimenting on this, I believe the short answer to the question is that you need to send the command string like a buffer of characters AND put your Hex Coded command for the LED controller as an escaped string of characters.
Here is my code (it toggles the LED Controller between Green and Blue every 5 seconds, but you can extend this for other commands and colors as described at http://jpelectron.com/sample/Electronics/WiFi%20LED%20control/):
/*
Send Commands to LED Controller via Wifi Network and Wemos D1 Board
*/
#include <ESP8266WiFi.h>
const char* ssid = "***yourWifiSSID***"; //Change this value for your network
const char* password = "***yourWifiPassword***"; //Change this value for your network
boolean connected = false;
boolean colorToggle = false;
void WiFiEvent(WiFiEvent_t event) {
//Serial.printf("[WiFi-event] event: %d\n", event);
switch(event) {
case 3:
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
connected = true;
break;
case 6:
Serial.println("WiFi lost connection");
connected = false;
break;
}
// 0 : WL_IDLE_STATUS when Wi-Fi is in process of changing between statuses
// 1 : WL_NO_SSID_AVAILin case configured SSID cannot be reached
// 3 : WL_CONNECTED after successful connection is established
// 4 : WL_CONNECT_FAILED if password is incorrect
// 6 : WL_DISCONNECTED if module is not configured in station mode
}
void setup() {
pinMode(LED_BUILTIN, OUTPUT); // Initialize the LED_BUILTIN pin as an output
Serial.begin(115200);
WiFi.disconnect(true);
delay(1000);
WiFi.onEvent(WiFiEvent);
WiFi.begin(ssid, password);
Serial.println("");
// Wait for connection
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.print("Connected to ");
Serial.println(ssid);
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
}
// the loop function runs over and over again forever
void loop() {
//if we are connected
if(connected){
digitalWrite(LED_BUILTIN, LOW); //Turn on LED on the Board
turnLEDSOn();
}
else
{
digitalWrite(LED_BUILTIN, HIGH); //Turn off LED on the Board
setup();
}
}
void turnLEDSOn() {
const uint16_t port = 5577;
const char * host = "192.168.10.2"; //Change to whatever your LED Controller IP is
WiFiClient client;
if (client.connect(host, port)) //Try to connect to TCP Server
{
Serial.println("Connected to LED Controller... ");
if (colorToggle)
{ //Green
char command[] = "\x31\x00\xff\x00\x00\x00\xf0\x0f\x2f";
client.write((uint8_t *)command, sizeof(command));
colorToggle = false;
delay(5000);
}
else
{ //Blue
char command[] = "\x31\x00\x00\xff\x00\x00\xf0\x0f\x2f";
client.write((uint8_t *)command, sizeof(command));
colorToggle = true;
delay(5000);
}
Serial.println("Command sent ... "); // command is the color or animation sent to the LED controller
}
else
{
Serial.println("connection failed ... ");
}
if (client.available())
{
//Read from LED Controller
char c = client.read();
Serial.print(c);
}
if (!client.connected())
{
Serial.println();
Serial.println("disconnecting ... ");
client.stop();
for(;;);
}
}
unsigned long
and send that with one call toprint
. – Johnny Mopp Jul 6 '17 at 17:38