Post about your Basic project here

Moderator: Mmiscool

User avatar
By russm
#56813 Updated the code to do a few things which you can see in the code comments section. Just added some code to update the DST on the proper date for the United States where it is followed/desired. New code also has a nicer web page to update settings. I may add a setting in the DST/Timezone setup to enable/disable using DST for locations that don't follow. I'll leave it up to anyone interested to update the code to reflect their particular DST dates. Here's the code:

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

timesetup(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]
 timesetup(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

timesetup(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

User avatar
By forlotto
#56885 Oh wow auto DST a first great addition ;)

Thanks for sharing :)
User avatar
By forlotto
#57807 Way cool ! good work. Helpful for many projects really anything dealing with time !