Happy for you to look at it. Stripped down to the essentials, here it is. There are no calls to malloc or new in any of the code I’ve suppressed, nor anything else that consumes resources (just reads some of the pins).
TBH I don’t see how the router can do anything to affect a UDP socket. Even if I turn it off, the multicast gets transmitted, but nobody is listening (well, actually the other hosts on the same WiFi network can be). Unless it’s something in the cloud code which is burning resources somewhere. I haven’t tried turning that off.
/*
*send_message - send a UDP multicast message, and send it also to the serial port
*/
void send_message(char *buffer, int sz)
{
int status = udp.sendPacket(buffer, sz, remoteIP, udp_port);
Serial.printlnf("status %5d %5d msg %s", status, errno, buffer);
}
/*
* make_status_message - create a status message in the given buffer
*
* Format is:
*
* unit_id message_type(=1) sequence_no time power-voltage pin-status
*/
int make_status_message(char *buffer, int bufsz)
{
bool dummy;
int result = snprintf(buffer, bufsz, "%d %d %d %d %d %04x",
my_id, MSG_SENSOR_STATUS, sequence, millis(), get_voltage(), detector::get_all());
++sequence;
return result;
}
/*
* send_status - send a UDP message with the detector status
*/
bool status_repeat_pending = false;
int32_t last_send_time = 0;
void read_and_send_status()
{
bool changed;
detector::get_all(changed);
if (changed || status_repeat_pending || millis() - last_send_time > MESSAGE_INTERVAL) {
status_repeat_pending = changed;
int sz = make_status_message(buffer, buffer_size);
send_message(buffer, sz);
last_send_time = millis();
the_led.blink();
}
}
/*
* setup code
*/
int detector_list[] = { D0, D1, D2, D3, D4, D5, D6, A2, A3, A4, A5, A6, -1 };
void setup() {
pinMode(voltage_pin, INPUT);
udp.begin(udp_port);
Serial.begin(9600);
//Serial.println("IP address: ", WiFi.localIP());
detector::setup(detector_list);
get_id();
}
/*
* main loop
*/
void loop() {
detector::poll_all();
const size_t bufferSize = 1024;
char buf[bufferSize];
read_and_send_status();
the_led.action();
delay(loop_interval);
}