web based clock

Post details of your projects here.
basicchip
Posts: 985
Joined: Fri Oct 19, 2012 2:39 am
Location: Lake Tahoe, CA
Contact:

Re: web based clock

Post by basicchip » Sun Dec 30, 2018 1:20 am

Add this code to the clock display. It enables the PMOSFET to turn on and off regulating brightness. Right now this is not synced to the software "drawing" the digits, but at 10x that rate it should be fine. May requires some fine tuning later. Right now each digit is displayed for 1 millisecond.

Code: Select all

#define EMC0	4
#define EMC1	6
#define EMC2	8

sub setupPWM (cycleTime, highTime)		' times in micro-seconds
	DIM prescale as integer

	prescale = 48		' as the peripherals run at 48 MHz

	while (cycleTime >= &H10000) 
		cycleTime = cycleTime >> 1
		highTime = highTime >> 1
		prescale = prescale << 1
	loop
	prescale = prescale - 1				' counts to n-1
	cycleTime = cycleTime - 1			' counts to n-1
		
	' enable counters CT16B0 and CT16B1
	SYSCON_SYSAHBCLKCTRL = SYSCON_SYSAHBCLKCTRL or (1<<7) or (1<<8)

	T3_TCR =	0
	
	IOCON_PIO0_21 = IOCON_PIO0_21 or &H00000001				'P0.21

	T3_MR0 = cycleTime - highTime + 1
	T3_EMR = T3_EMR or ((1<<EMC0) or 1)
	T3_PWMC = T3_PWMC or &H09
	T3_PR =	prescale
	T3_TC =	0
	T3_MCR = 1<<10 'Reset on MR3
	T3_MR3 = cycleTime
	T3_TCR =	1' Enable counter and PWM
end sub

main:

	setupPWM(100,2)					' near full bright for now

Will need to add variable PWM which without prescale (which is not changed currently) just needs to change T3_MR0



TodWulff
Posts: 51
Joined: Fri Oct 19, 2012 4:03 am
Location: The Mitten State - Shores of Lake Huron

Re: web based clock

Post by TodWulff » Sun Dec 30, 2018 2:31 am

A nice bit of progress in a short amount of time. Kudos.

Speaking of time, and the accuracy of same, I note that either the forum server needs its time updated, or you're being hosted out in the middle of the Atlantic: https://i.imgur.com/IDpCoS3.png

EDIT: Or is it my profile.?. :oops:
EDIT 2: Nope, I was only in Nova Scotia. Server time is off, I perceive.
EdiT 3: I'm drowning. :mrgreen:
EDIT 4: Ahhh, all is right in the World: https://i.imgur.com/ZpIBch8.png
Last edited by TodWulff on Sun Dec 30, 2018 2:49 am, edited 4 times in total.

basicchip
Posts: 985
Joined: Fri Oct 19, 2012 2:39 am
Location: Lake Tahoe, CA
Contact:

Re: web based clock

Post by basicchip » Sun Dec 30, 2018 2:38 am

I believe the forum runs on GMT.

And thanks, basically throwing a lot of existing code pieces together.

EDIT seems to have my time right, you must be out in the Atlantic. Need a paddle?

basicchip
Posts: 985
Joined: Fri Oct 19, 2012 2:39 am
Location: Lake Tahoe, CA
Contact:

Re: web based clock

Post by basicchip » Sun Dec 30, 2018 5:15 am

Hooked up PWM and experimented with it. Nothing happened, weird, the drive waveform looked correct. But looked at the 5V power that was suppose to be going away, and yes it started down the RC slope. But there is too much capacitance on the board, mainly to support the clock chip that is no longer there. I will have to remove some of those. ... Manyana

But as this is suppose to be instructive as well as useful, I made the whole display digits interrupt driven. The PWM timer now cycles through the digit display, so for the most part the CPU is free. I guess I could wiggle IOs to see how much time that is, but it is probably most of the time.

Here is the code now

Code: Select all


'	web clock display drive

#include "LPC11U3x.bas"

' globals

hr = 0		' define hour
min = 0		' define minute

#define SEG_0	&HBB00		
#define SEG_1	&H1800 		
#define SEG_2	&HD300 		
#define SEG_3	&HD900 		
#define SEG_4	&H7800 		
#define SEG_5	&HE900 		
#define SEG_6	&HEB00 		
#define SEG_7	&H9800 		
#define SEG_8	&HFB00 		
#define SEG_9	&HF800 		
                	
#define SEG_o	&H4B00 		
#define SEG_f	&HE200 		

const DIGarray = { SEG_0, SEG_1, SEG_2, SEG_3, SEG_4, SEG_5, SEG_6, SEG_7, SEG_8, SEG_9, SEG_o, SEG_f }

#define US_TIME

' user TIMER1 (32 bit) to interrupt each minute

INTERRUPT SUB TIMER1IRQ
	T1_IR = 1	      ' Clear interrupt
  
  	min += 1
	if min > 59 then
		min = 0
		hr += 1
		if hr > 23 then
			hr = 0
		endif
	endif

ENDSUB

SUB ON_TIMER ( max_cnt, dothis )
	TIMER1_ISR   = dothis + 1              'set function of VIC   -- need the +1 for Thumb operation
	SYSCON_SYSAHBCLKCTRL OR= (1<<10)		  ' enable TIMER1
	T1_PR  = 0	                         'no prescale -- will adjust the value for more accurate time
	VICIntEnable OR= (1<<TIMER1_IRQn)  	 'Enable interrupt
	T1_MR0 = max_cnt-1 ' set up match number of ms
	T1_MCR = 3      ' Interrupt and Reset on MR0
	T1_IR  = 1      ' clear interrupt
	T1_TC  = 0      ' clear timer counter
	T1_TCR = 1      ' TIMER1 Enable
	
ENDSUB

'	Timer3 interrupt will now drive the character, using PWM_count to index through the 4 digits

PWM_count = 0		' globals used in TIMER3 interrupt
PWM_toggle = 0

const DIGIT_IO = { 45, 46, 47, 48 }

INTERRUPT SUB TIMER3IRQ
	dim dig as integer
	
	T3_IR = 8	      ' Clear interrupt
	
#ifdef US_TIME
	if hr > 12 then hr -= 12
	if hr = 0  then hr = 12
#endif	

	select PWM_count
		CASE 0
			INPUT ( DIGIT_IO(3))
			dig = hr /10
			
			if dig then
				IO( DIGIT_IO(0)) = 0
			endif
		
			GPIO_DIR(0) = (GPIO_DIR(0) & &HFFFF00FF) + DIGarray(dig)
			GPIO_CLR(0) = DIGarray(dig)
			
			PWM_count = 1
		CASE 1
			INPUT( DIGIT_IO(0))
		
			dig = hr MOD 10
			IO( DIGIT_IO(1)) = 0
	
			GPIO_DIR(0) = (GPIO_DIR(0) & &HFFFF00FF) + DIGarray(dig) + IF(PWM_toggle , &H400 , 0)
			GPIO_CLR(0) = DIGarray(dig) + IF(PWM_toggle , &H400 , 0)
			
			PWM_toggle = IF(PWM_toggle , 0 , 1)		' the colon is TOO bright so display at 50%

			PWM_count = 2
		CASE 2
			INPUT( DIGIT_IO(1))
		
			dig = min / 10
			IO( DIGIT_IO(2)) = 0

			GPIO_DIR(0) = (GPIO_DIR(0) & &HFFFF00FF) + DIGarray(dig)
			GPIO_CLR(0) = DIGarray(dig)
			
			PWM_count = 3
		CASE 3
			INPUT( DIGIT_IO(2))
		
			dig = min MOD 10
			IO( DIGIT_IO(3)) = 0

			GPIO_DIR(0) = (GPIO_DIR(0) & &HFFFF00FF) + DIGarray(dig)
			GPIO_CLR(0) = DIGarray(dig)

			PWM_count = 0
		CASE ELSE
			PWM_count = 0		' should not be needed
	END SELECT
	
ENDSUB


#define EMC0	4
#define EMC1	6
#define EMC2	8

sub setupPWM (cycleTime, highTime)		' times in micro-seconds
	DIM prescale as integer

	prescale = 48		' as the peripherals run at 48 MHz

	' treat them all like 16 bits
	
	while (cycleTime >= &H10000) 
		cycleTime = cycleTime >> 1
		highTime = highTime >> 1
		prescale = prescale << 1
	loop
	prescale = prescale - 1				' counts to n-1
	cycleTime = cycleTime - 1			' counts to n-1
		
	' enable counters CT16B0 and CT16B1
	SYSCON_SYSAHBCLKCTRL = SYSCON_SYSAHBCLKCTRL or (1<<7) or (1<<8)

	T3_TCR =	0
	
	IOCON_PIO0_21 = IOCON_PIO0_21 or &H00000401				'  P0.21  MAT function and open drain

	T3_MR0 = cycleTime - highTime + 1
	T3_EMR = T3_EMR or ((1<<EMC0) or 1)
	T3_PWMC = T3_PWMC or &H09
	T3_PR =	prescale
	T3_TC =	0
'	T3_MCR = 1<<10 		'Reset on MR3
	T3_MCR = 3<<9 		'Interrupt and Reset on MR3
	T3_MR3 = cycleTime
	T3_TCR =	1		' Enable counter and PWM
	
	T3_IR = 8			' clear any interrupt
	PWM_count = 0
	PWM_toggle = 0

	TIMER3_ISR   = ADDRESSOF ( TIMER3IRQ ) + 1      'set function of VIC   -- need the +1 for Thumb operation
	VICIntEnable OR= (1<<TIMER3_IRQn)  				'Enable interrupt

end sub

		
#define MINUT_PCLK	2880000000				' 60 seconds at 48 MHz	
	
	
#define SCAN_TIME	1000			' 5000 visible flicker, and 2000 maybe visible

main:

	setupPWM(SCAN_TIME,SCAN_TIME - 50)					' near full bright for now

	hr = 9
	min = 4

	ON_TIMER(MINUT_PCLK , ADDRESSOF TIMER1IRQ)		
'	ON_TIMER(MINUT_PCLK / 100, ADDRESSOF TIMER1IRQ)		' testing for now

' now that everything is interrupt drive need to keep running
	
	while 1
	loop

olzeke51
Posts: 369
Joined: Sat May 17, 2014 4:22 pm
Location: South Carolina

Re: web based clock

Post by olzeke51 » Sun Dec 30, 2018 1:37 pm

Deadbugging - has lost its meaning I fear, in this modern day of SMT parts.
There are no legs to poke up and solder wires to !!!
deadbug.jpg
deadbug.jpg (49.65 KiB) Viewed 2937 times
'
Olzeke
Just wanted the new-timers to appreciate our heritage!
This is not totally the original concept. -- when a modification was needed or a part defective,
you would put the new chip on top of the old one (usually legs up) and solder the needed pins.

basicchip
Posts: 985
Joined: Fri Oct 19, 2012 2:39 am
Location: Lake Tahoe, CA
Contact:

Re: web based clock

Post by basicchip » Sun Dec 30, 2018 6:02 pm

OK Olzeke, I think dead bugging is worth a blog post, next year... 2 days from now

A quick thought on accuracy, the crystal is rated at 50 ppm, which is good to 2 minutes a month. Watch crystals are typically twice as accurate. So I'll check the time daily and adjust the minute timer up or down accordingly, and save that value in Flash. Over time (like maybe years it will get more accurate).

basicchip
Posts: 985
Joined: Fri Oct 19, 2012 2:39 am
Location: Lake Tahoe, CA
Contact:

Re: web based clock

Post by basicchip » Mon Dec 31, 2018 4:23 pm

Happy New Year.

Well I thought I was going to finish it up yesterday. Pulled the caps off the LED board, so PWM working fine. Though to auto-dim it I am waiting for a photo transistor to come in from Digikey, probably later this week (with other goodies too).

So it was back to the ESP8266, as I had seen it work once doing an HTTP GET from AT commands via TclTerm, seems like I was lucky. (better to be lucky than good they say). Anyway I found a number of issues with the ESP8266 running in AT command mode.
  • I have many different varieties of ESP8266 from about 3-4 years ago and not all accept Expressif's latest firmware
  • Some come up saying they have connected to my WiFi and have an IP, but they were not doing TCP and turns out they had some off the wall IP that I had to force to a normal one via AT -- and then they work
  • AT commands require CR-LF, but when CIPSEND'ing to an HTTP GET, that seems to only work with LF, which is actually a violation of the TCP/IP protocol according to my network expert - Mike
  • All are real slow, but for what I am expecting them to do that is fine, but AT commands are painful, but I had hoped that would be the lowest common denominator
  • There are so many websites talking about doing it this way or that bug or ... really tough to sort out
  • And whoever heard of coming up at 75Kbaud, then switching to 115Kbaud??
So today going to order some new ones, and look into Lua which hopefully might be my answer

EDIT: So far so good with Lua, took one of the ESP8266 with only 4Mbit of Flash and loaded Lua, and interpreting away. This one failed to update with the more modern and complex loader! So for now I am using version 0.9.6 of the firmware.

https://www.electrodragon.com/w/File:No ... rmware.zip

and the OLD and SIMPLE esp8266_flasher -- downloaded so many things, not sure where it came from, but I will put a copy up at the website (KISS !)

basicchip
Posts: 985
Joined: Fri Oct 19, 2012 2:39 am
Location: Lake Tahoe, CA
Contact:

Re: web based clock

Post by basicchip » Mon Dec 31, 2018 5:35 pm

OK Lua is WAY too easy, and the Adafruit tutorial is great.

https://learn.adafruit.com/adafruit-huz ... odemcu-lua

I spent all day yesterday trying to do this that took 5 minutes in Lua

Code: Select all

> wifi.sta.config("your_SSID","your_PASSWORD")
> wifi.sta.connect()
...
> sk=net.createConnection(net.TCP, 0)
> sk:on("receive", function(sck, c) print(c) end )
> sk:connect(80,"coridium.us")
> sk:send("GET /time.php HTTP/1.1\r\nHost: coridium.us\r\nConnection: keep-alive\r\nAccept: */*\r\n\r\n")
And it returned

Code: Select all

> HTTP/1.1 200 OK
Date: Mon, 31 Dec 2018 17:38:49 GMT
Server: Apache
X-Powered-By: PHP/7.0.33
Keep-Alive: timeout=5, max=100
Connection: Keep-Alive
Transfer-Encoding: chunked
Content-Type: text/html; charset=UTF-8

8f
<!DOCTYPE html>
<html lang=en>
<head>
<title>Coridium Time Server</title>
</head>
<body>
time is-09:38:49<br>date is-12/31/2018</body>
</html>

basicchip
Posts: 985
Joined: Fri Oct 19, 2012 2:39 am
Location: Lake Tahoe, CA
Contact:

Re: web based clock

Post by basicchip » Fri Jan 04, 2019 5:05 am

OK up and limping.

Need to make some enclosure changes, just to make it easier to work on when I go back in.

No real error recovery yet. ESP8266 still a bit finicky, and the latest node_mcu does not run on some of my earlier parts. Basically I tried scripts on the ESP8266, but those seemed to fail, not sure why. I could step through he process slowly so that is what the BASIC program does now.

Need to add light sensor dimmer.

But it is up and running in the bedroom, so I can see the clock now.

Here is the clock display program

Code: Select all


'	web clock display drive

#include "LPC11U3x.bas"

' globals

hr = 0		' define hour
min = 0		' define minute

#define OFFLINE -1
#define WEB_ERR	-2


'      -15-
'    |      |
'    13     12
'    |      |
'      -14-    colon/alarm 10
'    |      |
'    9      11
'    |      |
'      -8-

#define SEG_0	&HBB00		
#define SEG_1	&H1800 		
#define SEG_2	&HD300 		
#define SEG_3	&HD900 		
#define SEG_4	&H7800 		
#define SEG_5	&HE900 		
#define SEG_6	&HEB00 		
#define SEG_7	&H9800 		
#define SEG_8	&HFB00 		
#define SEG_9	&HF800 		
                	
#define SEG_o	&H4B00 		
#define SEG_f	&HE200 		
#define SEG_n	&H4A00 		
#define SEG_E	&HE300 		
#define SEG_r	&H4200 		

const DIGarray = { SEG_0, SEG_1, SEG_2, SEG_3, SEG_4, SEG_5, SEG_6, SEG_7, SEG_8, SEG_9 }

#ifdef TIME_LORD
each_minute:
	gosub do_each_minute
	return
#endif

' rather than gosub -- set a callback flag on minute
minute_callback=0

#define US_TIME

' user TIMER1 (32 bit) to interrupt each minute

INTERRUPT SUB TIMER1IRQ
	T1_IR = 1	      ' Clear interrupt
	
	if hr < 0 then return
  
  	min += 1
	if min > 59 then
		min = 0
		hr += 1
		if hr > 23 then
			hr = 0
		endif
	endif
	
	minute_callback=1

#ifdef TIME_LORD
'	gosub each_minute				' call back to network program each minute
#endif
ENDSUB

SUB ON_TIMER ( max_cnt, dothis )
	TIMER1_ISR   = dothis + 1              'set function of VIC   -- need the +1 for Thumb operation
	SYSCON_SYSAHBCLKCTRL OR= (1<<10)		  ' enable TIMER1
	T1_PR  = 0	                         'no prescale -- will adjust the value for more accurate time
	VICIntEnable OR= (1<<TIMER1_IRQn)  	 'Enable interrupt
	T1_MR0 = max_cnt-1 ' set up match number of ms
	T1_MCR = 3      ' Interrupt and Reset on MR0
	T1_IR  = 1      ' clear interrupt
	T1_TC  = 0      ' clear timer counter
	T1_TCR = 1      ' TIMER1 Enable
	
ENDSUB

'	Timer3 interrupt will now drive the character, using PWM_count to index through the 4 digits

PWM_count = 0		' globals used in TIMER3 interrupt
PWM_toggle = 0		

const DIGIT_IO = { 45, 46, 47, 48 }

INTERRUPT SUB TIMER3IRQ
	dim dig as integer
	
	T3_IR = 8	      ' Clear interrupt
	
#ifdef US_TIME
	if hr > 12 then hr -= 12
	if hr = 0  then hr = 12
#endif	

	select PWM_count
		CASE 0
			INPUT ( DIGIT_IO(3))
			
			if hr = OFFLINE then 
				dig = DIGarray(0)
			elseif hr = WEB_ERR then 
				dig = SEG_n
			else 
				dig = DIGarray(hr /10)
			endif
			
			if dig <> DIGarray(0) then					' if leading digit is 0, display blank
				IO( DIGIT_IO(0)) = 0
			endif
		
			GPIO_DIR(0) = (GPIO_DIR(0) & &HFFFF00FF) + dig
			GPIO_CLR(0) = dig
			
			PWM_count = 1
		CASE 1
			INPUT( DIGIT_IO(0))
		
			if hr = OFFLINE then 
				dig = SEG_o
			elseif hr = WEB_ERR then 
				dig = SEG_o
			else 
				dig = DIGarray(hr MOD 10)
			endif
			
			IO( DIGIT_IO(1)) = 0
	
			GPIO_DIR(0) = (GPIO_DIR(0) & &HFFFF00FF) + dig + IF(PWM_toggle , &H400 , 0)
			GPIO_CLR(0) = dig + IF(PWM_toggle , &H400 , 0)
			
			PWM_toggle = IF(PWM_toggle , 0 , 1)		' the colon is TOO bright so display at 50%

			PWM_count = 2
		CASE 2
			INPUT( DIGIT_IO(1))
		
			if hr = OFFLINE then dig = SEG_f else dig = DIGarray(min / 10)
			
			if hr <> WEB_ERR then IO( DIGIT_IO(2)) = 0		' display no n  -- skip this digit

			GPIO_DIR(0) = (GPIO_DIR(0) & &HFFFF00FF) + dig
			GPIO_CLR(0) = &HFF00
			
			PWM_count = 3
		CASE 3
			INPUT( DIGIT_IO(2))
		
			if hr = OFFLINE then 
				dig = SEG_f
			elseif hr = WEB_ERR then 
				dig = SEG_n
			else 
				dig = DIGarray(min MOD 10)
			endif
			
			IO( DIGIT_IO(3)) = 0

			GPIO_DIR(0) = (GPIO_DIR(0) & &HFFFF00FF) + dig
			GPIO_CLR(0) = &HFF00

			PWM_count = 0
		CASE ELSE
			PWM_count = 0		' should not be needed
	END SELECT
	
ENDSUB


#define EMC0	4		' bit positions for external match control
#define EMC1	6
#define EMC2	8

sub setupPWM (cycleTime, highTime)		' times in micro-seconds
	DIM prescale as integer

	prescale = 48		' as the peripherals run at 48 MHz

	' treat them all like 16 bits
	
	while (cycleTime >= &H10000) 
		cycleTime = cycleTime >> 1
		highTime = highTime >> 1
		prescale = prescale << 1
	loop
	prescale = prescale - 1				' counts to n-1
	cycleTime = cycleTime - 1			' counts to n-1
		
	' enable counters CT16B0 and CT16B1
	SYSCON_SYSAHBCLKCTRL = SYSCON_SYSAHBCLKCTRL or (1<<7) or (1<<8)

	T3_TCR =	0
	
	IOCON_PIO0_21 = IOCON_PIO0_21 or &H00000401				'  P0.21  MAT function and open drain

	T3_MR0 = cycleTime - highTime + 1
	T3_EMR = T3_EMR or ((1<<EMC0) or 1)
	T3_PWMC = T3_PWMC or &H09
	T3_PR =	prescale
	T3_TC =	0
'	T3_MCR = 1<<10 		'Reset on MR3
	T3_MCR = 3<<9 		'Interrupt and Reset on MR3
	T3_MR3 = cycleTime
	T3_TCR =	1		' Enable counter and PWM
	
	T3_IR = 8			' clear any interrupt
	PWM_count = 0
	PWM_toggle = 0

	TIMER3_ISR   = ADDRESSOF ( TIMER3IRQ ) + 1      'set function of VIC   -- need the +1 for Thumb operation
	VICIntEnable OR= (1<<TIMER3_IRQn)  				'Enable interrupt

end sub

		
#define MINUT_PCLK	2880000000				' 60 seconds at 48 MHz	

'	the crystal is about 50 ppm accurate -- which is about 2 minutes / month
'	about once a day I will check the time and adjust this value +/- 100 and save in Flash
	
	
#define SCAN_TIME	1000			' in microseconds - 5000 visible flicker, and 2000 maybe visible

#define MAX_BRIGHT	990
#define MAX_DIM		250

sub set_brite(x)
	T3_MR0 = x
endsub


#ifdef TIME_LORD				' this has been included by the main program == otherwise make it standalone
sub init_scan()
#else
main:
#endif
	setupPWM(SCAN_TIME,MAX_BRIGHT)					' near full bright for now

	hr = OFFLINE
'	hr = 17
	min = 25

	ON_TIMER(MINUT_PCLK , ADDRESSOF TIMER1IRQ)		
'	ON_TIMER(MINUT_PCLK / 100, ADDRESSOF TIMER1IRQ)		' testing for now

#ifdef TIME_LORD
end sub
#else
' now that everything is interrupt drive need to keep running
	
	while 1
		for lo = 90 to 990 step 100
			T3_MR0 = lo
'			print lo
			wait (2000)
		next
	loop
#endif
And the web get time program (the main in this case)

Code: Select all


#include "WiFiLogin.bas"

#include <STRING.bas>

#define TIME_LORD

#define OFFLINE		-1
#define NO_NET		-2

#define LPC11U37			' fake out pre-processor while testing on LPC824

#include "displayCLOCK.bas"

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)
	loop

	TXD(1) = CR
	TXD(1) = LF
	
	while incremental_gets(1) = 0
	loop
	print build_gets
	wait(10)
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


sub wifi_gettime
	dim start 
	
	hr = 	OFFLINE
	min = 	OFFLINE			
	
	build_idx_gets = 0	
	build_idx_debug = 0	
	
	Baud(1)=75000
	
	IO(ESP_RESET)=0
	wait(100)
	IO(ESP_RESET)=1		' reset ESP8266
	wait(100)
	
	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	
	
	CMDstr = "wifi.sta.config(" + DQUOTE + SSID + DQUOTE + COMMA + DQUOTE + PASSWORD + DQUOTE + ")"
	
	sendLua(CMDstr)					' connect to my Wifi
	sendLua("wifi.sta.connect()")
	sendLua("print(wifi.sta.status())")
	while incremental_gets(1) = 0
	loop
	print build_gets	
	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 < 5000000)
		if incremental_gets(1) = 1 then				' dump whatever comes out
			print build_gets
			if strstr(build_gets, "time is-") = 0 then
				hr = build_gets(8) - "0"
				if build_gets(9) = ":" then
					min = (build_gets(10) - "0")*10
					min += build_gets(11) - "0"
				else
					hr = hr * 10 + build_gets(9) - "0"
					min = (build_gets(11) - "0")*10
					min += build_gets(12) - "0"			
				endif
			endif
		endif
	loop
	
	sendLua("wifi.sta.disconnect()")

endsub

init_scan()

do_each_minute:					' callback from minute timer tick
	if hr>=21 and min>30 then set_brite(MAX_DIM)			' until I get a light sensor
	if hr>=8  and min>30 then set_brite(MAX_BRIGHT)
return

main:

hr = OFFLINE
min = 0

#ifndef LPC824
init_scan()
set_brite(MAX_BRIGHT)
#endif

wifi_gettime

print hr, min

if hr=OFFLINE then hr=WEB_ERR

while 1
	if minute_callback then
		if hr>=21 and min>30 then 
			set_brite(MAX_DIM)			' until I get a light sensor
		elseif hr>=9  and min>30 then 
			set_brite(MAX_BRIGHT)
		endif
		
		minute_callback = 0
	endif
loop

basicchip
Posts: 985
Joined: Fri Oct 19, 2012 2:39 am
Location: Lake Tahoe, CA
Contact:

Re: web based clock

Post by basicchip » Fri Jan 04, 2019 6:21 am

And what is the second PCB?
clockA.jpg
clockA.jpg (68.84 KiB) Viewed 2898 times
It is the LPC824 board without the 824. The ESP8266 can require 200 mA or more in bursts, maybe too much for the ARMstamp supply, so that LPC824 board has another 5V to 3.3V regulator,

Post Reply