A Basic Interpreter written from scratch for the ESP8266

Moderator: Mmiscool

User avatar
By jimg
#63842 Thank you.

I kinda knew that it only had to be done once, but wanted the program to be able to work with fresh modules. I assume the five second rule is something you came up with by experimentation as I couldn't find any documentation. Probably better than doing a hundred time requests in a row, which could have an effect on the ntp time servers if everyone did it.

This only seems to affect getting from output of zero to some actual time, but has no effect on getting daylight savings time working. I still only get standard time.
User avatar
By heckler
#63851 Make sure that your esp module has access to the internet. If it's in AP mode then it will not be able to acess an NTP server. Although I notice that the time.setup() function in the docs says something about specifying an alternate time server so you may be able to use a private server inside your own network.

PS It seems that both time.setup() and timesetup() works although I haven't tested it in the last couple of weeks.

This works for me...
Code: Select alltimesetup(-7,0)    'Mountain Std Time, DST off
delay 3000
[SyncTime]                 'arrive here to re-sync clock to NTP time source
bla = time()
print bla
shh = mid(bla,12,2)         'hour  this statment extracts the hours value
smm = mid(bla,15,2)         'min   this statment extracts the minutes
hh = val(shh)               ' this converts the hours from string to numeric
mm = val(smm)               ' this converts the minutes from string to numeric
wait
User avatar
By jimg
#63852 Yes, thanks. I'm getting the time from the server, the only problem remaining is getting it in daylight savings time. Everything I've tried only returns standard time, not DST.

I'm going to use this to do things like turn on appliances at a specific time, and I plan on making a sprinkler timer/controller I can trigger from my cell phone while I'm out in the yard. All this hinges upon getting the correct time of day.

Someone out there must know the secret to this DST thing. I went through all the projects posted and didn't see anything that worked automatically upon time change.
User avatar
By heckler
#63862 This code below (not mine... see the link at the end of the code) works like a champ!
Load it up on one of your modules (no need for any external connections) and go to its web page and check it out. Then you can see how he did the dst thing.

dwight

Code: Select all'********************************************************
'* Analog Panel Meter Clock                             *
'*   by Russ McIntire, 10/19/16                         *
'*   D0 = Hours                                         *
'*   D1 = Minutes                                       *
'*   D2 = Seconds                                       *
'*   D4 = can vary intensity to show clock running      *
'*        currently commented out in code               *
'*                                                      *
'* Shows hours/minutes/seconds on 3 panel displays      *
'* Allows Timezone and DST settings from webpage        *
'* Requires Internet connection for NTP time retrieval  *
'* Sends UDP broadcast of current time                  *
'* Can update UDP Port in settings, default is 5001     *
'* Added WIFI configuration and reboot capability       *
'* Added last NTP update retrieved to main web page     *
'* Added calculation and update of DST for U.S.         *
'********************************************************
'* When powered up first time, go to http://192.168.4.1 *
'* 1) Enter timezone and DST (1=true, 0=false) first    *
'* 2) Save timezone information                         *
'* 3) Change UDP port if desired and save               *
'* 4) Enter SSID and password and save                  *
'* 5) Click reboot button                               *
'*                                                      *
'* Clock will restart with new settings within a minute *
'* If desired, note the MAC address shown when first    *
'* connecting.  Set this in your routers static DHCP    *
'* If reconnection is desired for DST change, go to the *
'* clock's new IP address. It will be necessary to find *
'* it if the DHCP setting above is not performed.       *
'* Scanning programs are available and they should show *
'* the name as ESPXXXXX                                 *
'********************************************************

memclear

'Get DST and Timezone info from memory
DST = read.val(DaylightSavings)
timezone = read.val(TZ)
udpportval = read.val(UdpPort)
wifiName = read(WIFIname)
wifiPass = read(WIFIpass)

if DST = 1 then
 tz = timezone + 1
else
 tz = timezone
endif

if udpportval = 0 then udpportval = 5001
udpbegin udpportval

time.setup(tz,DST)
delay 5000

hour = 0
min = 0
sec = 0
M = " "
udptimer = 0
hourpwm = 0
minpwm = 0
secpwm = 0
updated = 0
updatetimer = 0
cts = time("hour:min:sec")
lastNTP = "First boot "&time("month. day, year  hour:min:sec")
uvsr = "Running..."

'Determine broadcast address
let localIP = ip()
pos = instrrev(localIP,".")
netIP = left(localIP,pos)
nodeIP = mid(localIP,pos+1)
let broadcastaddr = netIP & "255"

'GUI setup
cls
wprint |<HTML>|
wprint |<HEAD>|
wprint |<style> h1{text-shadow: 2px 2px 5px blue} </style>|
wprint |<h1>Analog Clock Setup</h1>|
wprint |</HEAD>|
wprint "The time is currently: "
wprint htmlvar(cts)  ' time("hour:min:sec")
wprint "<br>"
wprint "Last NTP update:  "
wprint htmlvar(lastNTP)
wprint "<br><br>"
textbox timezone
cssid htmlid(),"position: absolute; left: 110px; display:block;width:40px"
wprint "Timezone: "
wprint "<br>"
textbox DST
cssid htmlid(),"position: absolute; left: 110px; display:block;width:40px"
wprint "DST (1=DST):"
wprint "<br><br>"
button "Time Info Update", [timezone_update]
wprint "<br><br>"

textbox udpportval
cssid htmlid(),"position: absolute; left: 110px; display:block;width:60px"
wprint "UDP Port:"
wprint "<br><br>"
button "UDPPort Update", [udpport_update]
wprint "<br><br>"
textbox wifiName
cssid htmlid(),"position: absolute; left: 110px; display:block;width:110px"
wprint "WIFI Name:"
wprint "<br>"
textbox wifiPass
cssid htmlid(),"position: absolute; left: 110px; display:block;width:110px"
wprint "WIFI Pass:"
wprint "<br><br>"
button "Store WIFI Settings", [wifi_update]
wprint "<br> <br>"
button "Reboot Clock", [reboot_clock]
wprint "<br><br>"
wprint htmlvar(uvsr)
wprint "<br><br>"

wprint "-------------------------------------------- <br>"
n = wifi.scan()
wprint "Number of networks found = "&n&"<br>"
wprint "-------------------------------------------- <br>"

wprint "RSSI == BSSID == SSID <br>"

for i = 1 to n
 wprint wifi.rssi(i)&" == "&wifi.bssid(i)&" == "&wifi.ssid(i)&"<br>"
next n


timer 1000, [updatetime]
wait
end

[updatetime]
'Get time values from NTP server
 hour = val(time("hour"))
 min = val(time("min"))
 sec = val(time("sec"))

'For calibration (max meter swing)
'hour = 0
'min = 0
'sec = 59

 cts = time("hour:min:sec")

'Set 12 hour time and determine AM/PM
 if hour >= 12 then
  hour = hour - 12
  M = "PM"
 else
  M = "AM"
 endif

'Panel PWM calculation
'hourpwm = 85.33 * (hour)                 'divide 1024/12
hourpwm = (85.33 * hour) + (1.446 * min)   'divide 1024/12 add proportion of minutes (1.445/min)
minpwm = 17.36 * min                      'divide 1024/59
'minpwm = (17.36 * min) + (.289 * sec)     '- don't recommend - divide 1024/59 add proportion of seconds (.289/sec)
secpwm = 17.36 * sec                      'divide 1024/59

'Panel output
io(pwo,d0,hourpwm)
io(pwo,d1,minpwm)
io(pwo,d2,secpwm)

'Daily NTP refresh at 2:00 AM
if ((hour=3) and (min=0) and (sec>0) and (sec<5) and (M="AM")) then
 gosub [calculate_DST]
 time.setup(tz,DST)
 delay 5000
 lastNTP = "Last NTP "&time("month. day, year  hour:min:sec")
endif

gosub [udptimer]
wait

[timezone_update]
'Persist timezone information and UDPPort
write(DaylightSavings,str(DST))
write(TZ,str(timezone))

if DST = 1 then
 tz = timezone + 1
else
 tz = timezone
endif

time.setup(tz,DST)
delay 5000

'serialprintln "Timezone Updated"
'serialprintln "The Updated time is: "
'serialprintln time("hour:min")

cts = time("hour:min:sec")
uvsr = "<mark> Updated... </mark>"
updated = 1
returngui
wait

[udpport_update]
write(UdpPort,str(udpportval))
udpbegin udpportval
cts = time("hour:min:sec")
uvsr = "<mark> Updated... </mark>"
updated = 1
returngui
wait

[wifi_update]
write(WIFIname,wifiName)
write(WIFIpass,wifiPass)
cts = time("hour:min:sec")
uvsr = "<mark> Updated, requires a reboot to take effect... </mark>"
updated = 1
returngui
wait

[udptimer]
'Send UDP broadcast message out for current time every 30 seconds
 udptimer = udptimer + 1
' io(pwo,d4,1024-udptimer*273)     'vary d4 intensity to show clock operating, can comment out if desired
 if udptimer > 30 then
  if (M = "PM" and hour = 0) then hour = 12
  ct = str(hour)&":"&time("min:sec")&" "&M
  udpwrite broadcastaddr, 5001, ct
  udptimer = 0
 endif

'Check to see if Timezone or DST was updated
 if updated = 1 then
  updatetimer = updatetimer + 1
    if updatetimer > 9 then
        uvsr = "Running..."
        updated = 0
        updatetimer = 0
        returngui
    endif
 endif     
return

[reboot_clock]
cls
print "REBOOTING..."
delay 1000
reboot
wait

[calculate_DST]
dow = time("dow")
day = time("day")
month = time("month")
hour = time("hour")

if ((DST = 1) and (dow = "Sun") and (month = "Nov") and (day >= 1) and (day < 8)) then
 DST = 0
 write(DaylightSavings,str(DST))
endif

if ((DST = 0) and (dow = "Sun") and (month = "Mar") and (day >=8) and (day < 15)) then
 DST = 1
 write(DaylightSavings,str(DST))
endif

return

- See more at: http://www.esp8266.com/viewtopic.php?f=41&t=11518&start=32#sthash.nbm3X642.dpuf