mycontroller-org / MyControllerDevice

MyController supported MQTT ESP8266 library
http://www.mycontroller.org
Apache License 2.0
4 stars 4 forks source link

Sometimes, the before() is not called. #16

Open njbuch opened 6 years ago

njbuch commented 6 years ago

I have realized that the reason for some erronous readings from some sensors, is due to the fact that the sensor-init is never done.

MC[I]: Booting ESP8266 device...
MC[I]: WIFI not connected, trying connection...
MC[I]: Connecting to WiFi with ssid[1421530] and with password............OK
MC[I]: WiFi BSSID:[00:02:61:A8:32:58], RSSI:[-68 dBm, 64 %], IP:[192.168.1.148], StatusCode:[3]
MC[I]: Configuration(NodeEUI:[ESP_2D5A26], Mqtt{mDNS-status:[0], Server:[192.168.1.197], Port:[1883], FeedId:[esp], User:[]})
MC[I]: MQTT settings(Broker:[192.168.1.197], Port:[1883])
MC[I]: MQTT authenticating as anonymous
MC[I]: MQTT connection failed, rc=-4
MC[I]: Initialization done...
MC[I]: MQTT failed! Retryting to connect...
MC[I]: MQTT settings(Broker:[192.168.1.197], Port:[1883])
MC[I]: MQTT authenticating as anonymous
MC[I]: MQTT connected :)
MC[I]: MQTT topic subscribed:[in_esp/ESP_2D5A26/#]
Envoie de Luxmeter:81
Temp2: 93
Requesting temperatures...DONE
Device Address: 0000000000000000 Temp C: -127.00 Temp F: -196.60
Device Address: 0000000000000000 Temp C: -127.00 Temp F: -196.60
Envoie de Luxmeter:81
Temp2: 93

It seems that the device starts probing sensors without the before() or setup() is called. In the above, these two functions are declared:

void setup() {
    MC_SERIAL.print("Core: Setup running... ");
}

and

void before() {
    MC_SERIAL.print("Core: Before running... ");
[init code goes here]
}
njbuch commented 6 years ago

Could be related to the other issue about a failed MQTT connection?

jkandasa commented 6 years ago

@njbuch I will check and update you

jkandasa commented 6 years ago

@njbuch This is the sequence,

  1. Setup
  2. loop-> mc.loop()->before()->loop
  3. loop

In your example, the serial port is no initialized in setup. It is initialized when calling mc.loop()

njbuch commented 6 years ago

I can confirm the sequence you write, when the device is booted first time after flash, but if the device is off and then powered on, the before() is never called and sensors does not work....

njbuch commented 6 years ago

No worries on this one, I have solved it by:

void loop() {

  // This is required for the Mycontroller device works...

  mc.loop();

  //You can add your logic here.

   if(!custom_initdone)

  {
    custom_init();
  }

  sendMeasurements();

}