something is wrong with this sketch that is causing udp not to read the messages after some time.
This program sends a struct every second and i can verify that the struct is still being sent and the code is running like normal, then all of a sudden the udp receive part of this code just stops and the rest continues. i have been looking over it for a couple days now but i still have not found the problem. anyone have any idea whats causing this behavior?
i have verified it is not the sender,
main sketch,
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <WiFiUdp.h>
#include <EEPROM.h>
#include <ESP8266WebServer.h>
#include <ESP8266HTTPUpdateServer.h>
#include <ArduinoOTA.h>
#include <FS.h> //Include File System Headers
ESP8266WebServer OTA_httpServer(81);
ESP8266HTTPUpdateServer OTA_httpUpdater;
ESP8266WebServer server(80);
IPAddress sta_local_IP(192, 168, 4, 7);
IPAddress sta_gateway(192, 168, 4, 1);
IPAddress sta_subnet(255, 255, 255, 0);
bool debug = false;
WiFiUDP Udp;
unsigned int ServerPort = 4210;
unsigned int localUdpPort = 4220;
unsigned long now4=0;
const char* sta_ssid = "LCS2";
const char* sta_password = "xxxxxxxx";
void ICACHE_RAM_ATTR PwmPinISR();
void ICACHE_RAM_ATTR getPwmReading();
char incomingPacket[255]; // buffer for incoming _packets
int addr = 0;
const byte WaterSolenoid1 = 12;
const byte WaterSolenoid2 = 14;
const byte FlowSensorInterrupt = 13; // 0 = digital pin 2
byte pulseCount;
struct {
unsigned long SavedTotal;
int maxgallons;
int AlertError;
unsigned long EEPROMWriteCountTotal;
float flowRate;
float FlowSensorCalibrationFactor = 26;
float flowMilliLitres;
float totalMilliLitres;
bool s1 = false;
bool s2 = false;
unsigned long runtime = 0;
bool command = false;
} _packet;
struct {
unsigned long SavedTotal;
int maxgallons;
unsigned long EEPROMWriteCountTotal;
} waterParams;
unsigned long now = 0;
unsigned long now2 = 0;
void sendStruct(IPAddress to, char delimiter[], char delimiter2[]) {
_packet.SavedTotal = waterParams.SavedTotal;
_packet.maxgallons = waterParams.maxgallons;
_packet.EEPROMWriteCountTotal = waterParams.EEPROMWriteCountTotal;
if (digitalRead(WaterSolenoid1) == LOW) {
_packet.s1 = true;
} else {
_packet.s1 = false;
}
if (digitalRead(WaterSolenoid2) == LOW) {
_packet.s2 = true;
} else {
_packet.s2 = false;
}
Udp.beginPacket(to, ServerPort);
Udp.write(delimiter);
Udp.write((uint8_t*)&_packet, sizeof(_packet)); //cast to bytes
Udp.write(delimiter2);
Udp.endPacket();
}
void _writeEEPROM () {
waterParams.EEPROMWriteCountTotal++; EEPROM.put(addr, waterParams);
EEPROM.commit(); //Store data to EEPROM
Serial.println("EEPROM WRITE");
}
void PwmPinISR() {
pulseCount++;
}
void getPwmReading() {
detachInterrupt(digitalPinToInterrupt(FlowSensorInterrupt));
_packet.flowRate = ((1000.0 / (millis() - now)) * pulseCount) / _packet.FlowSensorCalibrationFactor;
_packet.flowMilliLitres = (_packet.flowRate / 60) * 1000;
_packet.totalMilliLitres += _packet.flowMilliLitres;
pulseCount = 0;
attachInterrupt(digitalPinToInterrupt(FlowSensorInterrupt), PwmPinISR, FALLING);
}
#include "watersched.h"
#include "packetHelper.h"
void setup() {
pinMode(WaterSolenoid1, OUTPUT);
pinMode(WaterSolenoid2, OUTPUT);
digitalWrite(WaterSolenoid1, LOW);
digitalWrite(WaterSolenoid2, LOW);
Serial.begin(115200);
SPIFFS.begin();
EEPROM.begin(256); //Initialize EEPROM
EEPROM.get(addr, waterParams);
WiFi.persistent(0);
WiFi.mode(WIFI_STA);
WiFi.begin(sta_ssid, sta_password);
WiFi.config(sta_local_IP, sta_subnet, sta_gateway);
#include "OTA.h"
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
attachInterrupt(digitalPinToInterrupt(FlowSensorInterrupt), PwmPinISR, FALLING);
server.begin();
Udp.begin(localUdpPort);
}
void loop() {
OTA_httpServer.handleClient(); //ota server "htt p://x.x.x.x :81/update"
ArduinoOTA.handle();
server.handleClient();//Checks for web server activity
handleWater();
rec_packet();
if (millis()-now4>=1000){
_packet.command = false;
now4=millis();
}
if (millis() - now >= 1000) {
getPwmReading();
_packet.runtime++;
if (debug) {
Serial.println(F("Realtime Flow Rates,"));
Serial.print(int(_packet.flowRate)); // Print the integer part of the variable
Serial.print(F("."));
unsigned int frac;
frac = (_packet.flowRate - int(_packet.flowRate)) * 10;
Serial.print(frac, DEC) ;
Serial.print(F(" "));
Serial.println(F("L/min"));
Serial.print(_packet.flowMilliLitres);
Serial.print(F(" "));
Serial.println(F("mL/Sec"));
Serial.println(F("Total Quanity,"));
Serial.print(_packet.totalMilliLitres);
Serial.print(F(" "));
Serial.println(F("mL"));
Serial.print(_packet.totalMilliLitres / 3785);
Serial.print(F(" "));
Serial.println(F("Gallons"));
}
sendStruct(sta_gateway, "NODWTR", "RTWODE");
now = millis();
}
if (millis() - now2 >= 1000) {
now2 = millis();
}
}