martin-ger / esp_mqtt

MQTT Broker/Bridge on the ESP8266
MIT License
297 stars 70 forks source link

set ntp publish interval #47

Open nopnop2002 opened 5 years ago

nopnop2002 commented 5 years ago

Thank you for useful Firmware.

After NTP sync has been completed successfully once, the local time will be published every second under the topic "$SYS/broker/time" in the format "hh:mm:ss".

I want to set publish interval.

set ntp_local_interval1 600 set ntp_local_interval2 3600 set ntp_local_interval3 12:00:00 set ntp_local_interval4 0:00:00

the local time will be published every 10 minute under the topic "$SYS/broker/time1" in the format "hh:mm:ss".

the local time will be published every 60 minute under the topic "$SYS/broker/time2" in the format "hh:mm:ss".

the local time will be published at 12:00:00 under the topic "$SYS/broker/time3" in the format "hh:mm:ss".

the local time will be published at 00:00:00 under the topic "$SYS/broker/time4" in the format "hh:mm:ss".

martin-ger commented 5 years ago

This is quite a specific request. This could be done in the code, but in order to keep that somewhat generic, I would suggest to use a script with two timers and two alarms.

nopnop2002 commented 5 years ago

Thank you very much for your answer. I'll use a script.

martin-ger commented 5 years ago

As it might be soewhat tricky, here is a script that does the job:

% Example for some regular timers

% Config params, overwrite any previous settings from the commandline
config ntp_server   1.pool.ntp.org
config ntp_timezone 1

% Now the initialization, this is done once after booting
on init
do
    setvar $t_prefix = "$SYS/broker/time"
    setvar $tt1 = $t_prefix | "1"
    setvar $tt2 = $t_prefix | "2"
    setvar $tt3 = $t_prefix | "3"
    setvar $tt4 = $t_prefix | "4"

    % Starts, when time is synced
    subscribe local $t_prefix

% Now the events, checked whenever something happens

on topic local $t_prefix
do
    % Wait for sync
    unsubscribe local $t_prefix
    settimer 1 1000
    setalarm 1 "12:00:00"
    setalarm 2 "00:00:00"

on timer 1
do
    % Check for full 10 minutes
    if (substr($timestamp,4,4) = "0:00") then
        publish local $tt1 $timestamp
            % Check for full hour
            if (csvstr($timestamp,1,":") = "00") then
            publish local $tt2 $timestamp
            endif
    endif
    settimer 1 1000

on alarm 1
do
    publish local $tt3 "12:00:00"

on alarm 2
do
    publish local $tt4 "00:00:00"
nopnop2002 commented 5 years ago

I worried about this

the local time will be published every 10 minute 8:10 8:20 8:30 8:40 .......

the local time will be published every 60 minute 9:00 10:00 11:00 12:00 ......

Thank you.

martin-ger commented 5 years ago

Not what you requested for? Do you want an event exactly 10 min after sync?

nopnop2002 commented 5 years ago

Your script works perfectly.

Wait for WiFi....WiFi connected
IP address: 192.168.4.2
subnetMask: 255.255.255.0
gatewayIP: 192.168.4.1
Connect to 192.168.4.1
clientid=ESP8266-cb120b
Attempting MQTT connection...connected as subscriber
Message arrived [$SYS/broker/time1] 07:00:00
Message arrived [$SYS/broker/time2] 07:00:00
Message arrived [$SYS/broker/time1] 07:10:00
Message arrived [$SYS/broker/time1] 07:20:00
Message arrived [$SYS/broker/time1] 07:30:00
Message arrived [$SYS/broker/time1] 07:40:00
Message arrived [$SYS/broker/time1] 07:50:00
Message arrived [$SYS/broker/time1] 08:00:00
Message arrived [$SYS/broker/time2] 08:00:00

But I have more questions.

Q1) I wrote firmware in the same ESP-12 again. But my script and uplink-AP setting still stays.

Q2) When a reset button of ST(WeMos as MQTT Client) is pressed during a ST connection, esp_mqtt also resets at the same time. When this occurs, ST can't be connected to Broker. This isn't usual, it sometimes occurs.

connected with aterm-e625c0-g, channel 11
dhcp client start...
connect to ssid aterm-e625c0-g, channel 11
ip:192.168.10.186,mask:255.255.255.0,gw:192.168.10.1
ip:192.168.10.186,mask:255.255.255.0,gw:192.168.10.1,dns:192.168.10.1
Got NTP server: 133.243.238.163
add 1
aid 1
station: 68:c6:3a:cb:12:0b join, AID = 1
station: 68:c6:3a:cb:12:0b join, AID = 1
station: 68:c6:3a:cb:12:0b leave, AID = 1
rm 1
station: 68:c6:3a:cb:12:0b leave, AID = 1
add 1
aid 1
station: 68:c6:3a:cb:12:0b join, AID = 1
station: 68:c6:3a:cb:12:0b join, AID = 1
err already associed!
station: 68:c6:3a:cb:12:0b leave, AID = 1
rm 1
station: 68:c6:3a:cb:12:0b leave, AID = 1
add 1
aid 1
station: 68:c6:3a:cb:12:0b join, AID = 1
station: 68:c6:3a:cb:12:0b join, AID = 1
Fatal exception 28(LoadProhibitedCause):
epc1=0x402504c6, epc2=0x00000000, epc3=0x00000000, excvaddr=0x0000000c, depc=0x0000000
 ets Jan  8 2013,rst cause:2, boot mode:(3,6)

load 0x40100000, len 30196, room 16
tail 4
chksum 0xe0
load 0x3ffe8000, len 2124, room 4
tail 8
chksum 0xac
load 0x3ffe8850, len 10024, room 0
tail 8
chksum 0x31
csum 0x31

Japan will be the New Year holiday now. So I can't response till end of holiday. Thank you.

martin-ger commented 5 years ago

Q1: That't correct, the script and the WiFi-Settings are stored independently from the code. Clear flash (or write any file to 0x00000 - at least 64 KB). This will reset the settings as well.

Q2: Don't know yet...

Happy New Year!