Forum - MCS Electronics

 

FAQFAQ SearchSearch RegisterRegister Log inLog in

ESP8266 WIFI to serial board gets NTP time

 
Post new topic   Reply to topic    www.mcselec.com Forum Index -> BASCOM-AVR
View previous topic :: View next topic  
Author Message
Wilfred

Bascom Member



Joined: 15 Jul 2010
Posts: 40

netherlands.gif
PostPosted: Sun Jan 04, 2015 6:34 pm    Post subject: ESP8266 WIFI to serial board gets NTP time Reply with quote

Quote:
Hello friends,

Here a first try with my newest little toy. this thing is so simple, even I can write some code for it to get it going Very Happy

There is no code for when something goes wrong in the communication. (when the NTP server says huh?, the program stops)
but for a quick try out this does the thing.

My xmega's internal clock is waaay to fast (several minutes every hour) . so I have to come up with a solution to slow it down

Have fun!
Wilfred.


Code:

' ESP8266  pinout
' top view
' CH_PD to GND  to activate (Chip powers down (when high))
'
'    _   _   _   ________
'   | |_| |_| |_|                antenna on top
'
'  GND  GPIO2 GPIO0   RX
'   |     |     |     |
'
'   |     |     |     |
'  TX   CH_PD  RST   Vcc
'
'VCC=3.3V !

$regfile = "xm128a3udef.dat"
$crystal = 32000000
$hwstack = 64
$swstack = 40
$framesize = 64



Const Ssid = "Your ssid"
Const Passwort = "your password"
Const Offset_gmt = 3600                 'in seconds. I live in GMT+1




Dim Sec_to_ntp_update As Integer

Config Osc = Enabled , 32mhzosc = Enabled       ' enable 2 MHz and 32 MHz internal oscillators
'Select the oscillator source for the system clock and prescaler (this must match with $crystal = XXXXXX).
'The following configure the internal 32MHz oscillator as system clock without prescaler so the system
'clock is 32MHz which Match With $crystal = 32000000
Config Sysclock = 32mhz , Prescalea = 1 , Prescalebc = 1_1

$lib "datetime.lbx"
$lib "xmega.lib" : $external _xmegafix_clear : $external _xmegafix_rol_r1014


'First Enable The Osc Of Your Choice , make sure to enable 32 KHz clock or use an external 32 KHz clock
Config Osc = Enabled , 32mhzosc = Enabled , 32khzosc = Enabled
'For the CLOCK we use the RTC so make sure the 32 KHZ osc is enabled!!!

'configure the systemclock
Config Sysclock = 32mhz , Prescalea = 1 , Prescalebc = 1_1

Config Clock = Soft , Rtc = 1khz_int32khz_ulp , Gosub = Sectic       ' we select the internal 1 KHz clock from the 32KHz internal oscillator
Config Priority = Static , Vector = Application , Lo = Enabled       ' the RTC uses LO priority interrupts so these must be enabled !!!


' COM1 --> Usartc0 --> RX=pc2(pin18)  TX=pc3(pin19)
Config Com1 = 9600 , Mode = Asynchroneous , Parity = None , Stopbits = 1 , Databits = 8
Open "COM1:" For Random As #1

'pin nummer                    40              41              42              43            39             38
Config Lcdpin = Pin , Db4 = Porte.4 , Db5 = Porte.5 , Db6 = Porte.6 , Db7 = Porte.7 , E = Porte.3 , Rs = Porte.2
Config Lcd = 16 * 2

Config Pina.2 = Output
Led1 Alias Porta.2

Config Date = Dmy , Separator = Minus

Dim Rxstring As String * 100
Dim Check As Bit


' Ntp Stuff
Dim Tt(4) As String * 1
Dim Ss(4) As Byte
Dim L1 As Long At Ss Overlay                                ' Overlay a long variable to receive-string
Dim L2 As Long                          ' with overlay you need no transfer from the byte-array to a long-variable


Declare Sub Init_esp8266
Declare Sub Ntp
Declare Sub Wait_for_string(byval B As String)
Declare Function Read_string() As String

Enable Interrupts
'==================================================================================
'init
Sec_to_ntp_update = 600

Cls
Locate 1 , 1 : Lcd "init ESP"
Init_esp8266

Locate 1 , 1 : Lcd "Getting NTP time"
Ntp                                     ' we don't have the correct time so lets start with that

Cls
Do
   If Sec_to_ntp_update = 0 Then        ' update time every hour. At least when you set the seconds to 3600
      Ntp
      Sec_to_ntp_update = 600
   End If
   Locate 1 , 9 : Lcd Time$
   Locate 2 , 9 : Lcd Date$
   Locate 1 , 1 : Lcd Sec_to_ntp_update
Loop

Sectic:
   Decr Sec_to_ntp_update
Return

'***********************   Ask Time From Timeserver Esp8266:  ******************************************

Sub Ntp
   Print "AT+CIPMUX=1"
   Waitms 100
   Print "AT+CIPSTART=1," ; Chr(34) ; "UDP" ; Chr(34) ; "," ; Chr(34) ; "193.67.79.202" ; Chr(34) ; ",37"
   Waitms 100
   Print "AT+CIPSEND=1,3"
   Waitms 100
   Print "X"                            'send capital X to ntp server and you obtain the time
   Do
     Wait_for_string "+IPD,1,4:"        'behind the : will be the time....  4 bytes
     Reset Check
     Rxstring = Read_string()
     Wait_for_string "OK"               'reply from ESP8266
   Loop Until Check = 1                 'check is set in wait_for string
   Reset Check

   Tt(1) = Mid(rxstring , 4 , 1)        'from big endian to little endian
   Tt(2) = Mid(rxstring , 3 , 1)        'swap the bytes
   Tt(3) = Mid(rxstring , 2 , 1)
   Tt(4) = Mid(rxstring , 1 , 1)
   Ss(1) = Asc(tt(1))                   'this is  L1 split in 4s by overlay
   Ss(2) = Asc(tt(2))
   Ss(3) = Asc(tt(3))
   Ss(4) = Asc(tt(4))
   L2 = L1 + 1139293696
   L2 = L2 + Offset_gmt
   Time$ = Time(l2)                     'set the time
   Date$ = Date(l2)                     'set the date
   Waitms 250
   Print "AT+CIPCLOSE=1"  
End Sub

'*******************************************************************************************************
Sub Init_esp8266
'init the ESP8266
'AT+RST
'AT+CIPMUX=1
'AT+CWMODE=3
'AT+CIPSERVER=1,23
'AT+CWLAP    'we don't do this one because we allready know our wifi network ssid and password
'AT+CWJAP="ssid","password"

   Print "AT+RST"                       'reset the ESP8266
   Do
      Wait_for_string "ready"           'reply from ESP8266
   Loop Until Check = 1
   Reset Check

   Print "AT+CIPMUX=1"
   Waitms 100                           'the answer here is "link is builded" but i don't know if that's changed in another version

   Print "AT+CWMODE=3"
   Waitms 100

   Print "AT+CIPSERVER=1,23"
   Do
      Wait_for_string "OK"              'reply from ESP8266
   Loop Until Check = 1
   Reset Check

   Print "AT+CWJAP=" ; Chr(34) ; Ssid ; Chr(34) ; "," ; Chr(34) ; Passwort ; Chr(34)
   Do
      Wait_for_string "OK"              'reply from ESP8266
   Loop Until Check = 1
   Reset Check
   Waitms 250


End Sub
'*******************************************************************************
Sub Wait_for_string(byval B As String) As String
   Local Ii As Byte
   Local Cc As Byte
      Ii = 1
      M1:
      Cc = Inkey()
      If Cc <> Mid(b , Ii , 1) Then
         Goto M1
      Else
         Incr Ii
         If Ii > Len(b) Then Goto M2
         Goto M1
      End If
   M2:
   Set Check
End Sub
'*******************************************************************************
Function Read_string() As String
   Local Cc As Byte
   Local Sss As String * 100
   Local Stringlen As Integer
      Sss = ""
      While Cc <> Chr(13)
         Cc = Inkey()
         If Cc = Chr(10) Then Cc = " "
         Sss = Sss + Chr(cc)
      Wend
      Stringlen = Len(sss) - 1
      Read_string = Left(sss , Stringlen)
End Function

End
 

(BASCOM-AVR version : 2.0.7.7 , Latest : 2.0.7.8 )
Back to top
View user's profile Visit poster's website
Flyby123

Bascom Member



Joined: 05 Jul 2008
Posts: 73

blank.gif
PostPosted: Wed Jan 07, 2015 11:49 am    Post subject: Reply with quote

Hi Wilfred, what/which firmware did you use for (ESP-01?)?
Nice work, thanks for posting.


Jim
Back to top
View user's profile
Wilfred

Bascom Member



Joined: 15 Jul 2010
Posts: 40

netherlands.gif
PostPosted: Wed Jan 07, 2015 3:16 pm    Post subject: Reply with quote

Hi Jim,

thanks Smile

I don't know yet, as I am not at home until tomorrow
But If you look at the AT commands, and the AT commands of your version, and they match
you can probably use this code.

Wilfred
Back to top
View user's profile Visit poster's website
Wilfred

Bascom Member



Joined: 15 Jul 2010
Posts: 40

netherlands.gif
PostPosted: Thu Jan 08, 2015 8:34 pm    Post subject: Reply with quote

Jim,
the version is 0.9.2.4
Back to top
View user's profile Visit poster's website
bzijlstra

Bascom Ambassador



Joined: 30 Dec 2004
Posts: 1179
Location: Tilburg - Netherlands

netherlands.gif
PostPosted: Sun Feb 22, 2015 8:36 pm    Post subject: small miracle... Reply with quote

Wilfred. Now I got my own module I see what a small miracle this esp8266 is. Thanks for your code. It gives a good head start.

Have fun
Ben Zijlstra
Back to top
View user's profile Visit poster's website
Evert :-)

Bascom Expert



Joined: 18 Feb 2005
Posts: 2107

netherlands.gif
PostPosted: Sun Feb 22, 2015 11:01 pm    Post subject: Reply with quote

Hi Ben,

Here's some more info https://nurdspace.nl/ESP8266

_________________
www.evertdekker.com Bascom code vault
Back to top
View user's profile Visit poster's website
Wilfred

Bascom Member



Joined: 15 Jul 2010
Posts: 40

netherlands.gif
PostPosted: Tue Feb 24, 2015 10:17 am    Post subject: Reply with quote

Hi Ben,

Nice to do something back (after hunting your website for years Very Happy )

that was exactly what this code was intended for.
To get you started.


73, Wilfred
Back to top
View user's profile Visit poster's website
Evert :-)

Bascom Expert



Joined: 18 Feb 2005
Posts: 2107

netherlands.gif
PostPosted: Tue Feb 24, 2015 3:07 pm    Post subject: Reply with quote

This very nice, https://importhack.wordpress.com/2014/11/22/how-to-use-ep8266-esp-01-as-a-sensor-web-client/ with the LUA firmware you don't need an external mcu anymore.
Just program it direct.

_________________
www.evertdekker.com Bascom code vault
Back to top
View user's profile Visit poster's website
Dave

Bascom Member



Joined: 05 Feb 2005
Posts: 267
Location: McMinnville, OR

usa.gif
PostPosted: Thu Mar 05, 2015 5:25 am    Post subject: Reply with quote

Wilfred,
Thanks for posting. That will be a big help.

Evert,
I've been playing with an esp-01 running Lua. So far I can run only about two pages of code before it's out of memory. I hope that is something I'm doing wrong but many people out there complain about memory so I don't know. Still I have it sending me an email when ever some event happens. That by it's self is useful.



The esp8266 comes in several flavors. The esp-01, esp-02, esp-03... I've tried the esp-01 and esp-03. The esp-01 is working as described but the two esp-03 that I tried don't work. AT commands did not work at all. I tried loading Lua and that sort of works with lot of problems. I'm taking a chance and ordered five esp-12. All for about 18 bucks from a US seller. The seller claims they have all been tested. The esp-12 have the io that makes the most sense. So i'll keep you posted.

Dave
Back to top
View user's profile
hg7kaz

Bascom Member



Joined: 15 Aug 2007
Posts: 6

hungary.gif
PostPosted: Sat Mar 14, 2015 3:04 pm    Post subject: Re: ESP8266 WIFI to serial board gets NTP time Reply with quote

I want to try this program on my xm256a3b board.
Here are the program's first some rows:

Code:
$regfile = "xm256a3bdef.dat"
$crystal = 32000000

$hwstack = 64
$swstack = 40
$framesize = 40

$lib "xmega.lib"
$lib "i2c_twi.lbx" '
$external _xmegafix_clear
$external _xmegafix_rol_r1014

Config Osc = Enabled , 32mhzosc = Enabled , 32khzosc = Enabled

Config Sysclock = 32mhz , Prescalea = 1 , Prescalebc = 1_1

Config Clock = Soft , Rtc = 1KHZ_INT32KHZ_ULP , gosub = sectic

Config Priority = Static , Vector = Application , Lo = Enabled
Enable Interrupts

Dim Sec_to_ntp_update As Integer


I receive many error messages after compiling the program.
These errors show signs of one single row: config clock = soft, rtc = 1KHZ_INT32KHZ_ULP, gosub = sectic

What's wrong?
Back to top
View user's profile
bzijlstra

Bascom Ambassador



Joined: 30 Dec 2004
Posts: 1179
Location: Tilburg - Netherlands

netherlands.gif
PostPosted: Sat Mar 14, 2015 6:07 pm    Post subject: wiki Reply with quote

There is a item in the wiki. 7 inch display using i2c.
Back to top
View user's profile Visit poster's website
Tubeampman

Bascom Member



Joined: 27 Feb 2006
Posts: 95
Location: Bodo

norway.gif
PostPosted: Tue Mar 31, 2015 11:18 pm    Post subject: Reply with quote

Hi
thanx for sharing Your code Smile

Am into making a selfadjusting alarmclock and was suposed to use a gps, but this module makes the solution far better.

Your program Works Perfect, but now i want to correct a timekeeping DS3221.
Am able to do this by getting the byte variables _sec _min _ and _day which comes With the config clock = soft.

Is there a way to get lets say these bytevariables web_sec web_min and web_hour directly from L2 without using the config clock ?.


Best regards
Řyvind
Back to top
View user's profile
Display posts from previous:   
Post new topic   Reply to topic    www.mcselec.com Forum Index -> BASCOM-AVR All times are GMT + 1 Hour
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You cannot download files in this forum