#include "WiFiLogin.bas" #include #define TIME_LORD #define OFFLINE -1 #define NO_NET -2 #define LPC11U37 ' fake out pre-processor while testing on LPC824 ' globals hr = 0 ' define hour min = 0 ' define minute retry_count=0 ' retries during time check 3:39 AM and initial startup check_time = 0 sub each_minute ' check for time drift / daylight savings -- this is inside an interrupt so set a flag to check the time ' if (min and 3) = 0 then check_time = 1 ' enable this one for checking each 4 minutes if hr=3 and min = 39 then check_time = 1 endsub #ifdef LPC824 sub set_brite(x) endsub #else #include "displayCLOCK.bas" #endif dim CMDstr(200) as string dim RESPONSEdata (3000) as byte #define LF 10 #define CR 13 #define COMMA "," #define PROMPT ">" #define DQUOTE chr(34) dim build_gets (255) as string build_idx_gets = 0 ' return empty string until CR/LF seen -- maybe timeout too function incremental_gets (wate) dim c c=RXD(1) if c = -1 then return 0 if c = LF then build_gets(build_idx_gets)=0 build_idx_gets = 0 return 1 endif if build_idx_gets=0 then if c = PROMPT then build_gets(build_idx_gets)=c wait (10) ' try to read in the space c = RXD(1) if c > 0 then build_gets(build_idx_gets+1)=c build_idx_gets += 1 endif build_gets(build_idx_gets+1)=0 build_idx_gets = 0 return 1 endif endif build_gets(build_idx_gets) = c build_idx_gets += 1 if wate = 0 then print chr(c); return 0 endif return 0 end function sub sendLua (byref ATstr() as string) dim i, c wait(1000) i=0 c = ATstr(i) while (c) TXD(1)=c i=i+1 c = ATstr(i) ' wait(20) loop TXD(1) = CR TXD(1) = LF while incremental_gets(1) = 0 loop print build_gets wait(50) end sub dim build_debug (255) as string build_idx_debug = 0 ' return empty string until CR/LF seen -- maybe timeout too function incremental_debug () dim c c=RXD(0) if c = -1 then return 0 if c = CR then build_debug(build_idx_debug)=CR build_idx_debug += 1 build_debug(build_idx_debug)=0 sendLua(build_debug) build_idx_debug = 0 return 0 endif build_debug(build_idx_debug) = c build_idx_debug += 1 return 0 end function #ifdef LPC824 #define ESP_RESET 15 #else #define ESP_RESET 58 #endif web_hr = 0 web_min = 0 sub wifi_gettime dim start web_hr = OFFLINE web_min = OFFLINE Baud(1)=75000 ' UART used for ESP8266 connection build_idx_gets = 0 build_idx_debug = 0 print "RESET ESP8266" IO(ESP_RESET)=0 wait(500) IO(ESP_RESET)=1 ' reset ESP8266 wait(1000) start = TIMER while (TIMER - start < 100000) incremental_gets(0) ' dump whatever comes out if strstr (build_gets, "csum") = 0 then exit loop Baud(1)=9600 wait(1000) sendLua("wifi.setmode(wifi.STATION)") if retry_count and 1 then CMDstr = "wifi.sta.config(" + DQUOTE + SSID1 + DQUOTE + COMMA + DQUOTE + PASSWORD1 + DQUOTE + ")" else CMDstr = "wifi.sta.config(" + DQUOTE + SSID2 + DQUOTE + COMMA + DQUOTE + PASSWORD2 + DQUOTE + ")" endif print "connect to WiFi" sendLua(CMDstr) ' connect to my Wifi sendLua("wifi.sta.connect()") wait(1000) sendLua("print(wifi.sta.status())") while incremental_gets(1) = 0 loop print build_gets print "loop2" while incremental_gets(1) = 0 loop print build_gets sendLua("sk=net.createConnection(net.TCP, 0)") CMDstr = "sk:on(" + DQUOTE + "receive" + DQUOTE + ", function(sck, c) print(c) end )" sendLua(CMDstr) CMDstr = "sk:connect(80," + DQUOTE + "coridium.us" + DQUOTE + ")" sendLua(CMDstr) CMDstr = "sk:send("+ DQUOTE + "GET /time.php HTTP/1.1\r\nHost: coridium.us\r\n\r\n" + DQUOTE + ")" sendLua(CMDstr) start = TIMER while (TIMER - start < 9000000) if incremental_gets(1) = 1 then ' dump whatever comes out print build_gets if strstr(build_gets, "time is-") = 0 then web_hr = build_gets(8) - "0" if build_gets(9) = ":" then web_min = (build_gets(10) - "0")*10 web_min += build_gets(11) - "0" else web_hr = web_hr * 10 + build_gets(9) - "0" web_min = (build_gets(11) - "0")*10 web_min += build_gets(12) - "0" endif exit endif endif loop wait(1000) sendLua("wifi.sta.disconnect()") start = TIMER while (TIMER - start < 2000000) if incremental_gets(1) = 1 then ' dump whatever comes out print build_gets endif loop print "done wifi_gettime" endsub main: hr = OFFLINE min = 0 IO(51) = 1 ' this is thermostat wait(2000) #ifndef LPC824 init_scan() set_brite(MAX_BRIGHT) #endif for retry_count=0 to 9 wait (retry_count=0 * 5000) ' initially don't wait, then up to 45 seconds of retries wifi_gettime if web_min <> OFFLINE then hr = web_hr min = web_min exit endif next retry_count=0 #ifdef LPC824 print hr,min,sec end #endif if hr=OFFLINE then hr=WEB_ERR while 1 if check_time then print "check time" retry_count=0 while 1 wifi_gettime if web_min <> OFFLINE then hr = web_hr min = web_min exit endif retry_count += 1 wait(1000) loop check_time = 0 retry_count=0 print "update complete" endif x = AD(6) >> 6 set_brite(x) wait(5000) loop