Lots of ways to do this.
For a websocket example (one end)
https://github.com/Links2004/arduinoWeb ... ontrol.inoRGB led control using websockets show how to combine and strip three bytes off of a send.
I have been using UDP on a light switch project. I have a battery operated remote that sends via UDP. The light switch device turns on or off the lamp if the code matches. I am now in the process of adding a web page server to the lamp end so that I can control it from a web page through my tablet. (when I'm too lazy to reach for the remote)
I will add both the UDP remote control and the lamp side here. It has some extra code in it that is not required but it was part of my learning process and I will leave it in. While it might make it more confusing at first I think it will give you some ideas for doing more with it. The remote sender code was a modification of the lamp end and some of that code is not used.
I used Packet Sender program on my PC to send and receive to get the initial relay code working.
https://packetsender.com/The code is set up to connect to a router. I had thought about using the ESP in AP mode but using my router allowed me to use the Packet Sender program for experimentation. I probably will leave it on my network so I can access it from my tablets. (the network credentials shown are not my regular home network)
Since you are running on batteries you might want the two battery devices to go to sleep and then wake and get the latest information from your main unit. Then go to sleep if there is no more tasks for them to do.
If you try the following code you might want to start with the page I started from. It set up a server and used Packet Sender as a client. It is a good example but it didn't show how to set up the client. A small missing part that I had to find out. If you do it this way you need to also do the client end.
http://arduino-esp8266.readthedocs.io/e ... mples.htmlLamp/relay end.
Code: Select all// Some LEDs and a relay. Lamp control end.
// parse packet
// UDP recieve packet. If "pwm\n" then send back current pwm value
// else use value received as integer for pwm update. range 0-1023
#include <WiFiUdp.h>
#include <ESP8266WiFi.h>
const char* ssid = "esp-net"; // your network SSID (name)
const char* pass = "123456789"; // your network password
//IPAddress relayIP = (192,168,100,118);
IPAddress relayIP = (192,168,100,101);
unsigned int localPort = 5000;
int status = WL_IDLE_STATUS;
char packetBuffer[255];
char ReplyBuffer[] = "OK\n";
int pwm = 0;
bool switch0State = 0;
const int switch0 = 0;
const int led2 = 2;
const int led12 = 12;
const int led13 = 13;
const int led14 = 14;
const int led15 = 15;
const int relay = 4;
WiFiUDP Udp;
//------------------------------------------------------------------
void rxUDP() {
int packetSize = Udp.parsePacket(); //Checks for a UDP packet, and reports the size.
//--------------------------------------
if (packetSize) {
IPAddress remoteIp = Udp.remoteIP();
int len = Udp.read(packetBuffer, 255); // read packet into packetBuffer
if (len > 0) {
packetBuffer[len] = 0; // reset length
}
//---
if (strcmp(packetBuffer, "pwm\n") == 0) { // if packet "pwm\n"
char p[5];
String str = String(pwm);
str.toCharArray(p, 5);
Udp.beginPacket(Udp.remoteIP(), Udp.remotePort());
Udp.write(p); // current value for pwm
Udp.endPacket();
}
else { // make number into pwm for output 0-1023
pwm = atoi(packetBuffer);
Serial.print("pwm= ");
Serial.println(pwm);
analogWrite(led15, pwm);
// Udp.beginPacket(Udp.remoteIP(), Udp.remotePort());
// Udp.write(ReplyBuffer); // send 'OK\n'
// Udp.endPacket();
}
//--------------------- serial print details ----------
if (packetSize > 0)
{
Serial.println("----------------------"); //
Serial.print("packetSize=");
Serial.println(packetSize);
Serial.print("Data was -> ");
Serial.print(packetBuffer);
Serial.print(" <- From IP : ");
Serial.print(Udp.remoteIP());
Serial.print(":");
Serial.println(Udp.remotePort());
}
//----------------------------------------------------
// Use a switch statement to examine the first character of the packet buffer
switch (packetBuffer[0]) {
case '1': //
digitalWrite(relay, HIGH);
break;
case '2': //
digitalWrite(relay, LOW);
break;
case '3': //
digitalWrite(led2, HIGH);
break;
case '4': //
digitalWrite(led2, LOW);
break;
case '5': //
digitalWrite(led13, HIGH);
break;
case '6': //
digitalWrite(led13, LOW);
break;
case '7': //
digitalWrite(led14, HIGH);
break;
case '8': //
digitalWrite(led14, LOW);
break;
}
}
}
//==================================================================
void setup() {
pinMode(led2, OUTPUT);
pinMode(led12, OUTPUT);
pinMode(led13, OUTPUT);
pinMode(led14, OUTPUT);
pinMode(led15, OUTPUT);
pinMode(relay, OUTPUT);
pinMode(switch0, INPUT_PULLUP);
Serial.begin(115200);
WiFi.begin(ssid, pass);
while (WiFi.status() != WL_CONNECTED) {
delay(100);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.print("IP: ");
Serial.print(WiFi.localIP());
Serial.print(":");
Serial.println(localPort);
Udp.begin(localPort);
}
//==================================================================
void loop() {
rxUDP(); //check for input and process if available
delay(10);
/*
if ((digitalRead(switch0)) != switch0State)
{
switch0State = !switch0State;
Serial.print("switch0State= ");
Serial.println(switch0State);
Udp.beginPacket(Udp.relayIP, Udp.remotePort());
Udp.write(p); // current value for pwm
Udp.endPacket();
}
*/
}
The remote transmitter end.
Code: Select all// parse packet
// UDP recieve packet. If "pwm\n" then send back current pwm value
// else use value received as integer for pwm update. range 0-1023
#include <WiFiUdp.h>
#include <ESP8266WiFi.h>
const char* ssid = "esp-net"; // your network SSID (name)
const char* pass = "123456789"; // your network password
//IPAddress relayIP = (192, 168, 100, 114);
IPAddress DrelayIP = (192, 168, 100, 101);
IPAddress relayIP (192, 168, 100, 103);
//unsigned int remotePort = 5000;
unsigned int remotePort = 9999;
unsigned int localPort = 5000;
int status = WL_IDLE_STATUS;
char packetBuffer[255];
char ReplyBuffer[] = "OK\n";
int pwm = 0;
bool switch0State = 0;
const int switch5 = 5;
const int switch0 = 0;
const int led2 = 2;
const int led12 = 12;
const int led13 = 13;
const int led14 = 14;
const int led15 = 15;
const int relay = 4;
WiFiUDP Udp;
//------------------------------------------------------------------
void rxUDP() {
int packetSize = Udp.parsePacket(); //Checks for a UDP packet, and reports the size.
//--------------------------------------
if (packetSize) {
IPAddress remoteIp = Udp.remoteIP();
int len = Udp.read(packetBuffer, 255); // read packet into packetBuffer
if (len > 0) {
packetBuffer[len] = 0; // reset length
}
//---
if (strcmp(packetBuffer, "pwm\n") == 0) {
char p[5];
String str = String(pwm);
str.toCharArray(p, 5);
// Udp.beginPacket(Udp.remoteIP(), Udp.remotePort());
// Udp.write(p); // current value for pwm
// Udp.endPacket();
}
else { // make number into pwm for output 0-1023
pwm = atoi(packetBuffer);
Serial.print("pwm= ");
Serial.println(pwm);
analogWrite(led15, pwm);
// Udp.beginPacket(Udp.remoteIP(), Udp.remotePort());
// Udp.write(ReplyBuffer); // send 'OK\n'
// Udp.endPacket();
}
//--------------------- serial print details ----------
if (packetSize > 0)
{
Serial.println("----------------------"); //
Serial.print("packetSize=");
Serial.println(packetSize);
Serial.print("Data was -> ");
Serial.print(packetBuffer);
Serial.print(" <- From IP : ");
Serial.print(Udp.remoteIP());
Serial.print(":");
Serial.println(Udp.remotePort());
}
//----------------------------------------------------
// Use a switch statement to examine the first character of the packet buffer
switch (packetBuffer[0]) {
case '1': //
digitalWrite(relay, HIGH);
break;
case '2': //
digitalWrite(relay, LOW);
break;
case '3': //
digitalWrite(led2, HIGH);
break;
case '4': //
digitalWrite(led2, LOW);
break;
case '5': //
digitalWrite(led13, HIGH);
break;
case '6': //
digitalWrite(led13, LOW);
break;
case '7': //
digitalWrite(led14, HIGH);
break;
case '8': //
digitalWrite(led14, LOW);
break;
}
}
}
//==================================================================
void setup() {
pinMode(led2, OUTPUT);
pinMode(led12, OUTPUT);
pinMode(led13, OUTPUT);
pinMode(led14, OUTPUT);
pinMode(led15, OUTPUT);
pinMode(relay, OUTPUT);
pinMode(switch0, INPUT_PULLUP);
pinMode(switch5, INPUT_PULLUP);
Serial.begin(115200);
WiFi.begin(ssid, pass);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.print("IP: ");
Serial.print(WiFi.localIP());
Serial.print(":");
Serial.println(localPort);
Udp.begin(localPort);
}
//==================================================================
// Udp.beginPacket("192.168.100.103", 5000);
void loop() {
rxUDP(); //check for input and process if available
delay(10);
if ((digitalRead(switch0)) == 0)
{
Serial.println("turn on");
Udp.beginPacket(relayIP, 5000);
Udp.write("1"); // relay on
Udp.endPacket();
delay(500);
Udp.beginPacket(DrelayIP, 5000); // Desk relay
Udp.write("1"); // relay on
Udp.endPacket();
delay(500);
}
if ((digitalRead(switch5)) == 0)
{
Serial.println("turn off");
Udp.beginPacket(relayIP, 5000);
Udp.write("2"); // relay off
Udp.endPacket();
delay(500);
Udp.beginPacket(DrelayIP, 5000); // Desk relay
Udp.write("2"); // relay off
Udp.endPacket();
delay(500);
}
}