In this receive code the sprintf() is causing a wdt crash.
. connected
Now listening at IP 192.168.4.100, UDP port 4220
Received 12 bytes from 192.168.4.1, port 4210
UDP packet contents: NODECMCMNODE
Received 12 bytes from 192.168.4.1, port 4210
Exception (9):
epc1=0x402025b5 epc2=0x00000000 epc3=0x00000000 excvaddr=0x00454493 depc=0x00000000
>>>stack>>>
ctx: cont
sp: 3ffffdc0 end: 3fffffc0 offset: 01a0
3fffff60: 00000000 00000000 00000000 402041c0
3fffff70: 0104a8c0 3ffe84dc 3ffee528 40202530
3fffff80: 3ffef3c4 00000000 00000000 402041c0
3fffff90: 00000000 00000000 00000001 3ffee5a8
3fffffa0: 3fffdad0 00000000 3ffee578 40203d58
3fffffb0: feefeffe feefeffe 3ffe8514 40100869
<<<stack<<<
ets Jan 8 2013,rst cause:2, boot mode:(1,6)
ets Jan 8 2013,rst cause:4, boot mode:(1,6)
wdt reset
This is the receiving code
#include <ESP8266WiFi.h>
#include <WiFiUdp.h>
IPAddress Server(192, 168, 4, 1);
WiFiUDP Udp;
const char* ssid = "udpserver";
const char* password = "12345678";
char verifyStart[7];
char verifyEnd[7];
unsigned int localUdpPort = 4220; // local port to listen on
char incomingPacket[255]; // buffer for incoming packets
int ServerPort = 4210;
unsigned long now = 0;
unsigned long now2 = 0;
unsigned long now3 = 0;
struct dataStruct {
int CounterUP = 0;
int CounterDN = 0;
unsigned long duration = 5050;
};
dataStruct localData;
void setup()
{
pinMode(5, OUTPUT);
digitalWrite(5, LOW);
pinMode(4, OUTPUT);
digitalWrite(4, LOW);
Serial.begin(115200);
Serial.println();
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
Serial.print(".");
}
Serial.println(" connected");
Udp.begin(localUdpPort);
Serial.printf("Now listening at IP %s, UDP port %d\n", WiFi.localIP().toString().c_str(), localUdpPort);
}
void loop()
{
int packetSize = Udp.parsePacket();
if (packetSize) {
Serial.printf("Received %d bytes from %s, port %d\n", packetSize, Udp.remoteIP().toString().c_str(), Udp.remotePort());
int len = Udp.read(incomingPacket, 800);
sprintf (verifyStart, (char*)incomingPacket, 7 );
//sprintf (verifyEnd, (char *)incomingPacket + len - 6 , 7 );
Serial.printf("UDP packet contents: %s\n", incomingPacket);
digitalWrite(5, HIGH);
now3 = millis();
}
}
here is the sending code,
#include <ESP8266WiFi.h>
#include <WiFiUdp.h>
IPAddress local_IP(192, 168, 4, 1);
IPAddress gateway(192, 168, 4, 1);
IPAddress subnet(255, 255, 255, 0);
IPAddress Client(192, 168, 4, 100);
const char* ssid = "udpserver";
const char* password = "12345678";
unsigned long now = 0;
unsigned long now2 = 0;
WiFiUDP Udp;
unsigned int localUdpPort = 4210; // local port to listen on
unsigned int clientPort = 4220;
char incomingPacket[800]; // buffer for incoming packets
char replyPacket[] = "Hi there! Got the message :-)"; // a reply string to send back
char verifyStart[7];
char verifyEnd[7];
void setup()
{
Serial.begin(115200);
Serial.println();
pinMode(5, OUTPUT);
pinMode(2, OUTPUT);
pinMode(4, OUTPUT);
pinMode(0, OUTPUT);
pinMode(14, OUTPUT);
digitalWrite(14, LOW);
digitalWrite(4, LOW);
digitalWrite(0, LOW);
digitalWrite(2, LOW);
digitalWrite(5, LOW);
//Serial.printf("Connecting to %s ", ssid);
//WiFi.begin(ssid, password);
while (!WiFi.softAP(ssid, password, 6, false, 15)) {
delay(500);
Serial.print(".");
}
Serial.println(WiFi.softAPConfig(local_IP, gateway, subnet) ? "Ready" : "Failed!");
Serial.println(" connected");
Udp.begin(localUdpPort);
Serial.printf("Now listening at IP %s, UDP port %d\n", WiFi.softAPIP().toString().c_str(), localUdpPort);
}
void loop()
{
if (millis() - now >= 200) {
digitalWrite(5, LOW);
digitalWrite(2, LOW);
digitalWrite(0, LOW);
digitalWrite(4, LOW);
digitalWrite(14, LOW);
now = millis();
}
if (millis() - now2 >= 1000) {
const char delimiter[] = "NODECM";
const char delimiter2[] = "CMNODE";
Udp.write((const uint8_t *)delimiter, sizeof(delimiter) - 1);
//Udp.write((const uint8_t *)&localData, sizeof(localData));
Udp.write((const uint8_t *)delimiter2, sizeof(delimiter2) - 1);
Udp.beginPacket(Client, clientPort);
//Udp.write((char*)&buffer, sizeof(buffer));
Udp.endPacket();
now2 = millis();
digitalWrite(5, HIGH);
}
}
the first sprintf() crashes the esp the second one does not?
sprintf (verifyStart, (char*)incomingPacket, 7 ); //crashes program
sprintf (verifyEnd, (char *)incomingPacket + len - 6 , 7 );//does not crash program
EDIT: I just noticed the UDP.Writes are in the wrong place. ill fixt hat and see if it helps
EDIT2: Did not help..