Chat freely about anything...

User avatar
By scargill
#7122 I tried to help out tonight - wrote a queue - but I can't get your publish callback to work - in the init, it's all setup
MQTT_OnPublished(&mqttClient, mqttPublishedCb);

But when publishing - nothing happens - the info text in that callback is not showing in the serial and if I add code in there - it's not being run.

tuanpm wrote:You right Peter,
If you wait and poll for published event, it will block os threads.
I will add publish buffer and publish queue soon

Tuan PM
User avatar
By gicho
#7129 Am I the only one that gets that "MQTT:Published." print?

23:15:59.745> connected with OpenWrt, channel 1
23:15:59.745> dhcp client start...
23:16:00.183> STATION_IDLE
23:16:00.556> ip:192.168.1.8,mask:255.255.255.0,gw:192.168.1.1
23:16:00.681> TCP: Connect to ip 192.168.1.1:1883
23:16:00.743> MQTT: Connected to broker 192.168.1.1:1883
23:16:00.743> MQTT: Send mqtt connection info, to broker 192.168.1.1:1883
23:16:00.743> TCP: Sent
23:16:00.743> TCP: data received
23:16:00.743> MQTT: Connected to 192.168.1.1:1883
23:16:00.743> MQTT: Connected
23:16:00.743> MQTT: subscribe, topic"/test/control/sleeptime" at broker 192.168.1.1:1883
23:16:00.743> TCP: Sent
23:16:00.743> TCP: data received
23:16:00.743> MQTT: Subscribed
23:16:00.743> MQTT: sending publish...
23:16:00.743> MQTT: Subscribe successful
23:16:00.743> TCP: Sent
23:16:00.743> MQTT: Published
23:16:00.743> TCP: data received
23:16:00.743> MQTT topic: /test/control/sleeptime40, data: 40
23:16:01.243> Guard timer elapsed, going to sleep for 40 secondsrm match
23:16:01.243> pm close 7 0 0/1530383
23:16:01.243> TCP: Reconnect to 192.168.1.1:1883
23:16:01.306> deep sleep 40s

Timestamps are from my serial terminal, guard timer is not relevant here, just protection if something fails in any of the states. At the end it ensures 500ms to get any subscribed messages (like the sleeptime).

/test/control/sleeptime holds a retained message that sets the sensor sleep period (40s in this case).

I have added also "subscribed" callback to be able to tick my state machine completely on events. No need of queues, no delays on polling ;) Just effective event programming.

I also made some trials with wifi_set_sleep_type(), it works but I need to measure and document the results like Tuan has done it in the other topic (scope pictures).
User avatar
By scargill
#7146 I now get the published message - I was using code a couple of weeks old that didn't have this. I'm using the callback routine to implement a queue and it works a treat with no discernable delays. Detailed in my blog.

gicho wrote:Am I the only one that gets that "MQTT:Published." print?

23:15:59.745> connected with OpenWrt, channel 1
23:15:59.745> dhcp client start...
23:16:00.183> STATION_IDLE
23:16:00.556> ip:192.168.1.8,mask:255.255.255.0,gw:192.168.1.1
23:16:00.681> TCP: Connect to ip 192.168.1.1:1883
23:16:00.743> MQTT: Connected to broker 192.168.1.1:1883
23:16:00.743> MQTT: Send mqtt connection info, to broker 192.168.1.1:1883
23:16:00.743> TCP: Sent
23:16:00.743> TCP: data received
23:16:00.743> MQTT: Connected to 192.168.1.1:1883
23:16:00.743> MQTT: Connected
23:16:00.743> MQTT: subscribe, topic"/test/control/sleeptime" at broker 192.168.1.1:1883
23:16:00.743> TCP: Sent
23:16:00.743> TCP: data received
23:16:00.743> MQTT: Subscribed
23:16:00.743> MQTT: sending publish...
23:16:00.743> MQTT: Subscribe successful
23:16:00.743> TCP: Sent
23:16:00.743> MQTT: Published
23:16:00.743> TCP: data received
23:16:00.743> MQTT topic: /test/control/sleeptime40, data: 40
23:16:01.243> Guard timer elapsed, going to sleep for 40 secondsrm match
23:16:01.243> pm close 7 0 0/1530383
23:16:01.243> TCP: Reconnect to 192.168.1.1:1883
23:16:01.306> deep sleep 40s

Timestamps are from my serial terminal, guard timer is not relevant here, just protection if something fails in any of the states. At the end it ensures 500ms to get any subscribed messages (like the sleeptime).

/test/control/sleeptime holds a retained message that sets the sensor sleep period (40s in this case).

I have added also "subscribed" callback to be able to tick my state machine completely on events. No need of queues, no delays on polling ;) Just effective event programming.

I also made some trials with wifi_set_sleep_type(), it works but I need to measure and document the results like Tuan has done it in the other topic (scope pictures).
User avatar
By mhwlng
#7190
scargill wrote:
In any real world stand alone application we need to know the time.


I used sntp.c, sntp.h, time_utils.c, time_utils.h from here :

https://github.com/TomerCo/ESP8266-AT/t ... 0.9.3/user

to get an accurate timestamp from an NTP time server (resynced every hour)

Code: Select all   
void wifiConnectCb(uint8_t status)
{
  if(status == STATION_GOT_IP){
    sntp_init();
    MQTT_Connect(&mqttClient);
  }
}


Code: Select allif (sntp_time > 0)
{
  char tmp[100];
  os_sprintf(tmp,"Time: %s\n",epoch_to_str(sntp_time));
  console_printf(tmp);
}


NOTE that I had to add :
#define LWIP_OPEN_SRC
to user_config.h

NOTE that this ntp library doesn't figure out daylight savings,
so I removed the timezone (sntp_tz) from the code and always use UTC timestamp.
(and only figure out local time in the server)