I am not sure why, but I have a the LED set to flash each packet it receives. Another node sends “ping” packets 2x a second. The weird thing is, when I see it’s flashing less than twice a second, I reset the receiver and boom we’re back in business. It is powered through the USB cable current connected to PC. Why does resetting the receiver “fix” the problem?
I can only think of it possibly being a memory issue. Please review the code for sender and receiver.
While I know UDP is no guaranteed delivery, the protocol suits my needs. I also want to have some advice on UDP data integrity, clearly if packets don’t get through, I could multiply the sends and add -01 -02 for each iteration. That way it ignores subsequent packets of same event multiplied. I could also add an unique code as an acknowledgment that notes the original packet sent. If sender doesn’t get a reply back, it will re-transmit, up to a reasonable number.
Sender
// ** Node 5 ***
int code, i, x;
#include <Arduino.h>
#include <ESP8266WiFi.h>
#include <WiFiUdp.h>
IPAddress ip(192, 168, 1, 135);
IPAddress ipTarget(192, 168, 1, 139);
const char* ssid = "JMR";
const char* password = "crystal2965";
// buffers for receiving and sending data
char packetBuffer[32]; //buffer to hold incoming packet,
// An EthernetUDP instance to let us send and receive packets over UDP
WiFiUDP Udp;
const byte numChars = 8;
char receivedChars[numChars]; // an array to store the received data
char codeChar[numChars];
boolean newData = false;
int bo5 = D1;
int dr5 = D2;
int bo5Last, dr5Last, dr5Now, bo5Now;
void setup() {
// start the Ethernet and UDP:
Serial.begin(115200);
Serial.print("Node 5 Starting...");
WiFi.mode(WIFI_STA);
pinMode(bo5, INPUT_PULLUP);
pinMode(dr5, INPUT_PULLUP);
IPAddress gw(192, 168, 1, 254);
IPAddress dns(192, 168, 1, 254);
IPAddress sn(255, 255, 255, 0);
WiFi.config(ip, gw, sn, dns);
WiFi.begin(ssid, password);
Serial.println("");
// Wait for connection
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.print("Node5: Connected to ");
Serial.println(ssid);
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
x = Udp.begin(8888);
if (x == 0) {
Serial.print("UDP.Begin: No sockets availible");
}
}
void loop() {
dr5Now = digitalRead(dr5);
bo5Now = digitalRead(bo5);
if (dr5Now != dr5Last) { //If door state changes
if (dr5Now == HIGH) {
if (bo5Now == HIGH) {
sendUDP("5005"); //bolt break
}
sendUDP("5001"); //door open
}
if (dr5Now == LOW) {
sendUDP("5002"); //door closed
}
}
if (bo5Now != bo5Last) { //if bolt state changes
if (bo5Now == HIGH) { sendUDP("5009"); }
if (bo5Now == LOW) { sendUDP("5010"); }
}
dr5Last = dr5Now;
bo5Last = bo5Now;
recvWithEndMarker();
showNewData();
delay(100);
i++;
if (i == 10) {
//Send ping!
sendUDP("5102");
i = 0;
}
udpRead();
}
void sendUDP(char* udpDataOut) {
//Inital Clearance Period
if (millis() < 5000) return;
x = Udp.beginPacket(ipTarget, 8888);
if (x == 1) {
}
else {
Serial.print("beginPacket status: FAILURE ");
}
Serial.printf("Sending UDP: %s", udpDataOut);
x = Udp.write(udpDataOut);
x = Udp.endPacket();
if (x == 1) {
}
else {
Serial.print("Packet Send status: FAILURE ");
}
}
void udpRead() {
int packetSize = Udp.parsePacket();
if (packetSize > 3) {
// read the packet into packetBufffer
Udp.read(packetBuffer, UDP_TX_PACKET_MAX_SIZE);
Serial.println("From B->A: ");
Serial.println(packetBuffer);
}
}
void recvWithEndMarker() {
static byte ndx = 0;
char endMarker = '\n';
char rc;
//Serial.print("recvWithEndMarker");
while (Serial.available() > 0 && newData == false) {
rc = Serial.read();
if (rc != endMarker) {
receivedChars[ndx] = rc;
ndx++;
if (ndx >= numChars) {
ndx = numChars - 1;
}
}
else {
receivedChars[ndx] = '\0'; // terminate the string
ndx = 0;
newData = true;
}
}
}
void showNewData() {
if (newData == true) {
sendUDP(receivedChars);
Serial.println("A->B: ");
Serial.println(receivedChars);
newData = false;
}
}/code]
My reciever sketch is too large to display in-line. Please download using link to review it.
node5-0.ino (3.41 KB)