After 2h or weeks normal work - main loop OK or Crash, anyway - Solid Green Light, No wifi connection

Solid Green Light, No wifi connection. My router (try two of them) don’t see device after 2h-weeks of normal work.
Firmware version is 0.5.2.
System thread is enabled
Then this trouble happens - application thread is running loop() as expected. Photon control my devices, but it don’t send information on my local mqtt server and it don’t receive command.
I use SYSTEM_MODE(MANUAL), but don’t use cloud functions. In setup call WiFi.on() & WiFi.connect(); Everything look fine, but after 2h-weeks of work - it stuck.
I got 3 photon(1 using i2c, 2-nd use i2c and serial, 3-d use only analog read\ digital write) - this trouble occurre on any of them

Any solid color (unless directly controled via RGB.color()) indicates some kind of lock condition in the system thread, which usually is caused by user code blocking some shared resources.
To find the possible cause we might need to see your code or you try to locate the places where your code accessed shared recources and put some debug statements around them.

1 Like
#include "MQTT/MQTT.h"

SYSTEM_THREAD(ENABLED);
SYSTEM_MODE(MANUAL);
STARTUP(WiFi.selectAntenna(ANT_INTERNAL));

void callback(char* topic, byte* payload, unsigned int length);

byte server[] = { 192,168,2,101};
MQTT client(server, 1883, callback);

volatile int vent[2][6] = { {A4, 0, -1, 0, A6, 0}, {A5, 0, -1, 0, A7, 0} };//control_pin : goal : now : taho : taho_pin : taho_count
String vent_name[2] = { "input", "output" }; 

volatile int flap[8][4] = { 
                            {0, 0, 0, 0}, 
                            {0, 0, 0, 0}, 
                            {0, 0, 0, 0}, 
                            {0, 0, 0, 0}, 
                            {0, 0, 0, 0}, 
                            {0, 0, 0, 1}, 
                            {0, 0, 0, 0},
                            {0, 0, 0, 0}, 
                          }; //flap_now, flap_goal, state, invert

unsigned long previous_conected = 100000; // start on first loop
unsigned long previous_wifi_uptime = 100000; // start on first loop 
unsigned long wifi_uptime = 0;
unsigned long previous_vent_iterate = 0;
unsigned long previous_check_taho = 0;

bool publish_message(const char* t, int p, bool retain) 
{   
    char buf5[12];
    int n = sprintf(buf5,"%d",p);
    return client.publish(t, (uint8_t*)buf5, n, retain);
}

// recieve message
void callback(char* topic, byte* payload, unsigned int length) {
    char p[length + 1];
    memcpy(p, payload, length);
    p[length] = NULL;
    String message(p);
    String t(topic);

    if (t.equals("home/vent/spark/set"))
    {
        if (message.equalsIgnoreCase("1"))
        {
            Particle.connect();
            if (waitFor(Particle.connected, 10000)) 
                {publish_message("home/vent/spark", 1, false);}
            else
                {Particle.disconnect(); publish_message("home/vent/spark", 0, false);}
        }
        else
        {
            Particle.disconnect();
            publish_message("home/vent/spark", 0, false);
        }
        
    }
    else if (t.equals("home/vent/input/pwm/set"))
    {
        int m = message.toInt();
        (m > -1 & m < 255) ? vent[0][1] = m : publish_message("home/vent/input/pwm", vent[0][2], true);
    }
    else if (t.equals("home/vent/output/pwm/set"))
    {
        int m = message.toInt();
        (m > -1 & m < 255) ? vent[1][1] = m : publish_message("home/vent/output/pwm", vent[1][2], true);
    }
    else if (t.startsWith("home/vent/flap/"))
    {
        int m = message.toInt();
        int x = t.substring(15,16).toInt();
        if (m > -1 && m <= 120 && x > -1 && x <8) 
        {
            flap[x][1] = m;
        }
        else
        {
            publish_message("home/vent/flap/" + t.substring(15,16),  flap[x][0] , true);
        }
    }
}

void setup() {
    WiFi.on();
    WiFi.connect();
    Wire.begin(); 
    pinMode(vent[0][0], OUTPUT); //turbine control
    pinMode(vent[1][0], OUTPUT); //turbine control
    pinMode(vent[0][4], INPUT); //rpm read with interrupt
    pinMode(vent[1][4], INPUT); //rpm read with interrupt
    attachInterrupt(vent[0][4], tic0, RISING );
    attachInterrupt(vent[1][4], tic1, RISING );
    //read last flap state
    for (int i=0; i < 8; i++) 
    {
        int addr = i * 10;
        int value;
        EEPROM.get(addr, value);
        flap[i][0] = value;
        flap[i][1] = value;
    }
    if (waitFor(WiFi.ready, 5000))  { wifi_uptime = 60; }
}

void loop() {
    unsigned long currentMillis = millis();
    // check network and connect MQTT brocker
    if (currentMillis - previous_conected >= 30000 || previous_conected > currentMillis)
    {
        previous_conected = currentMillis;
        if (!client.isConnected() & wifi_uptime > 60)
        {
            if (client.connect("vent")) 
            {
                client.subscribe("home/vent/spark/set");
                client.subscribe("home/vent/flap/+/set");
                client.subscribe("home/vent/+/pwm/set"); 
                publish_message("home/vent/spark", Particle.connected() ? 1 : 0, false);
            }
        }
        publish_message("home/vent/rssi", WiFi.RSSI(), true);
    }
    if (currentMillis - previous_vent_iterate >= 200 || previous_vent_iterate > currentMillis)
    {
        previous_vent_iterate = currentMillis;
        for (int i=0; i <= 1; i++) 
        {
            if (vent[i][1] != vent[i][2])
            {
                (vent[i][1] > vent[i][2]) ? vent[i][2]+=1 : vent[i][2]-=1 ;
                analogWrite(vent[i][0], vent[i][2]);
                if (vent[i][1] == vent[i][2])
                {
                    publish_message("home/vent/" + vent_name[i] + "/pwm", vent[i][2], true);
                }
            }
        }
    }
    if (currentMillis - previous_check_taho >= 30000 || previous_check_taho > currentMillis)
    {
        previous_check_taho = currentMillis;
        for (int i=0; i <= 1; i++) 
        {
            char buf5[12];
            publish_message("home/vent/" + vent_name[i]  + "/rpm", (vent[i][5] + vent[i][3]), true);
            vent[i][3] = (vent[i][5] + vent[i][3]) / 2 ; 
            vent[i][5] = 0;
        }
    }
    if (currentMillis - previous_wifi_uptime >= 1000 || previous_wifi_uptime > currentMillis)
    {
        previous_wifi_uptime = currentMillis;
        WiFi.ready() ? wifi_uptime++ : wifi_uptime = 0;
        
        for (int i=0; i < 8; i++) 
        {
            if (flap[i][2] != 0)
            {
                flap[i][0] += flap[i][2];
                char buf18[18]; sprintf(buf18,"home/vent/flap/%d", i);
                if (flap[i][0] == flap[i][1])
                {
                    flap[i][2] = 0;
                    Wire.beginTransmission(4); // transmit to device #4
                    Wire.write(char(i));        // sends flap number
                    Wire.write('0');             
                    Wire.endTransmission();    // stop transmitting
                    publish_message(buf18 , flap[i][0], true);
                    int addr = i * 10;
                    int value = flap[i][0];
                    EEPROM.put(addr, value);
                }
                else
                {
                    if (flap[i][0] % 5 == 0)
                    {
                        publish_message(buf18 , flap[i][0], true);
                    }
                }
            }

            if (flap[i][0] != flap[i][1])
            {
                if (flap[i][0] < flap[i][1] && flap[i][2] != 1)
                {
                    flap[i][2] = 1;
                    Wire.beginTransmission(4);          // transmit to device #4
                    Wire.write(char(i));              // sends flap number
                    if (flap[i][3] == 0) {Wire.write('+');}
                        else {Wire.write('-');}
                    Wire.endTransmission();    // stop transmitting
                }
                if (flap[i][0] > flap[i][1] && flap[i][2] != -1)
                {
                    flap[i][2] = -1;
                    Wire.beginTransmission(4); // transmit to device #4
                    Wire.write(char(i));              // sends flap number
                    if (flap[i][3] == 0) {Wire.write('-');}
                        else {Wire.write('+');}
                    Wire.endTransmission();    // stop transmitting
                }
            }
        }
    }
    Particle.process();
    client.loop();
}

void tic0() {vent[0][5] +=1 ;}
void tic1() {vent[1][5] +=1 ;}

This is all code, I use mqtt lib from web IDE https://github.com/hirotakaster/MQTT

[quote=“ScruffR, post:2, topic:26181”]
user code blocking some shared resources.
[/quote] no good - I was thinking that application layer in sandbox and can’t effect System thread.

@Godz, I haven’t done a deep analysis of your code but there are some obvious issues.

  • In setup(), the line if (waitFor(WiFi.ready, 5000)) { wifi_uptime = 60; } only checks for a successful connection. What if the connection fails?
  • Nowhere in your code do you look for and recover from a wifi disconnection.

The last point is why you may be seeing a solid green. The MQTT library may be stuck in a loop assuming the TCP connection is working when it is not.

Since you are uses MANUAL mode, the firmware will not recover a lost wifi connection for you. You need to check for wifi connectivity and recover that connection yourself. Some users will attempt reconnection a few times and if that fails, do a System.reset() to try and recover the connection. I believe the system firmware will re-connect to previously open TCP sockets once wifi is reconnected. @ScruffR can you check me on this?

1 Like

if OK - I try to connetct to mqtt server in first loop cycle.
if NOT - main loop will try to connect every 30 seconds

Yes, and its correct. I made command - WiFi.connect();
And never say - WiFi.disconnect();
So even in manual mode particle will search wifi network continuously. I reboot my router many times and allways wifi connection recover automaticaly.

No, it recover now.

Trouble - I try use watchdog, or check - WiFi.Ready() - it didn't help. Photon report WiFi.Ready=OK or report nothing.

Today first photon full stop - even main loop() don’t work.
Try to turn off //SYSTEM_THREAD(ENABLED); - may be it help.

Didn’t help, hang after 18 hours of work, but photon now stop fully - even pwm in output’s didn’t work.
Try 0.5.3. - no way to be worse.