Here is the partial pertinent code that runs on the esp module. waiting for data requests from the PIC micro which is also running BASIC (PIC BASIC PRO from Melabs.com)
(The complete esp8266 code is at the bottom.)
<< ESP8266 CODE >>
baudrate 9600
serialtimeout 2000
timer 500, [PicSer]
wait
'
[PicSer]
serialflush
input picin
picin = mid(picin,1,5)
if picin == "T?" then gosub [gettime]
if picin == "I?" then gosub [SendIP]
if picin == "D?" then gosub [SendData]
if picin == "X" then goto [Exit]
wait
'
[gettime]
bla = time()
dy = mid(bla,1,3) 'dow
mh = mid(bla,5,3) 'month
dd = mid(bla,9,2) 'date
hh = mid(bla,12,2) 'hour
mm = mid(bla,15,2) 'min
ss = mid(bla,18,2) 'sec
yr = mid(bla,21,4) 'year
'
if dy == "Sun" then dow = "1/"
if dy == "Mon" then dow = "2/"
if dy == "Tue" then dow = "3/"
if dy == "Wed" then dow = "4/"
if dy == "Thu" then dow = "5/"
if dy == "Fri" then dow = "6/"
if dy == "Sat" then dow = "7/"
'
if mh == "Jan" then mth = "01/"
if mh == "Feb" then mth = "02/"
if mh == "Mar" then mth = "03/"
if mh == "Apr" then mth = "04/"
if mh == "May" then mth = "05/"
if mh == "Jun" then mth = "06/"
if mh == "Jul" then mth = "07/"
if mh == "Aug" then mth = "08/"
if mh == "Sep" then mth = "09/"
if mh == "Oct" then mth = "10/"
if mh == "Nov" then mth = "11/"
if mh == "Dec" then mth = "12/"
'
picout = dow & mth
picout = picout & dd
picout = picout & "/"
picout = picout & hh
picout = picout & mm
picout = picout & ss
'
serialprint picout
return
'
[SendIP]
serialprint ip()
return
'
[SendData]
serialprint Doff
serialprint "/"
serialprint Don
serialprint "/"
if Mtime == "hhmm" then goto [SkipMtime]
Mhrs = left(Mtime,2)
serialprint Mhrs
serialprint ":"
Mmin = right(Mtime,2)
serialprint Mmin
return
[SkipMtime]
serialprint "00:00"
return'
And the following code snippets is the associated code running on my PIC microcontroller that communicates with the ESP module. It asks the esp module for 3 different strings of data.
This first bit of code is how the PIC asks the esp module for the date and time
<< PIC CODE >>
' Wait for >,CR,LF from ESP module
serin2 rxe,espbaud,2000,SerTmout,[wait(">"),wait(13),wait(10)]
' ESP module expects "T?" before it will give the date/time stirng
serout2 txe,espbaud,["T?"]
serin2 rxe,espbaud,3000,SerTmout,_
[dec1 dow,wait("/"),dec2 mth,wait("/"),dec2 date,_
wait("/"),dec2 Hours,dec2 Minutes,dec2 Seconds]
This bit of code is where the PIC asks for the IP address of the esp module so I can display it to the user.
<< PIC CODE >>
' Wait for >,CR,LF from ESP module
serin2 rxe,espbaud,2000,SerTmout,[wait(">"),wait(13),wait(10)]
' ESP module expects "I?" before it will give IP address
serout2 txe,espbaud,["I?"] 'ask esp8266 for IP address
serin2 rxe,espbaud,3000,SerTmout,[str ip \15 \">"]
In this bit of code the PIC is asking for some other data parameters that the user inputs to a web page and the PIC needs this data from the esp module.
<< PIC CODE >>
serin2 rxe,espbaud,2000,SerTmout,[wait(">"),wait(13),wait(10)]
serout2 txe,espbaud,["D?"] 'ask for other data
serin2 rxe,espbaud,3000,SerTmout,_
[dec2 DHoff,dec2 DMoff,wait("/"),dec2 DHon,dec2 DMon,_
wait("/"),dec2 Mhrs,wait(":"),dec2 Mmin]
When the complete code below is running on an esp module, the esp module is constantly outputting a "> CR LF" and then the PIC waits to see these three characters before it sends the request to the esp module.
If the esp module doesn't get a request from the PIC within the 2 second timeout it outputs another ">" and waits again for a request for data.
The PIC can ask for 3 different strings of data...
T? will get back a formatted date/time string.
I? will get back the IP address of the esp module
D? will get back other data from the esp module
this has worked flawless for me for over a year.
In my case the esp module is providing time and parameters to a nixie clock that used to be controlled by an TI MSP430 microcontroller. I wanted to use a PIC as I had no experience with the 430 and I allready had a PICBASIC compiler.
here is the complete esp code...
If you load it up on a module and run it, then connect to it with a serial terminal 9600baud you will see the esp module outputting a string of > > > >.
If you just type any of the commands (T?, I?, or D?) you will get back a string of data to the terminal program. You will have to be fast as the timeout is 2 seconds. Note that the T,I,D are capitals. You can change these as needed.
Be sure to also load up the web page and interact with it there and then you can see how the data string changes when you interact with it serially.
<< COMPLETE ESP8266 CODE >>
memclear
cls
timezone = read.val(TZ)
wifiName = read(WIFIname)
wifiPass = read(WIFIpass)
Doff = "2215"
Don = "0600"
Mtime = "hhmm"
time.setup(timezone,0)
baudrate 9600
serialtimeout 2000
delay 3000
wprint "PIC Nixie Clock<br><br>"
wprint "Enter your home WiFi Network credentials below...<br>"
wprint "Case Sensitive!!<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>"
button "Store WIFI Settings", [wifi_update]
wprint " <--- ONLY Press this button if changing WiFi<br>"
wprint "IMPORTANT: If module fails to connect to your home network<br>"
wprint " then use cellphone WiFi to connect to NIXIE_clock<br>"
wprint "then use browser to connect to 192.168.4.1<br>"
wprint "then correct above WiFi Name and WiFi Pass.<br><br>"
wprint "Enter your Time Zone --->"
textbox timezone
wprint "<br>Example Time Zone: Eastern:-5, Mountain:-7, Pacific:-6<br>"
button "Time Zone Update", [timezone_update]
wprint "<br><br>Enter HH MM to blank the display...<br>"
wprint "Use Military time. For 10:15pm enter 2215 <br>"
wprint " for 6:00 AM enter 0600 (always 4 digits, no colon)<br>"
wprint "Enter 0000 in both for always on<br>"
wprint "Turn off display at > "
textbox Doff
wprint " and back on at > "
textbox Don
wprint "<br><br>"
wprint "Manual time set value (only use if internet not available)-->"
textbox Mtime
wprint "<br>Leave as hhmm to use Internet Time<br>"
wprint "after entering manual time then press lower right button on clock"
wprint "<br> <br>"
wprint "<br>"
button "Exit", [Exit]
timer 500, [PicSer]
wait
[PicSer]
serialflush
input picin
picin = mid(picin,1,2)
if picin == "T?" then gosub [gettime]
if picin == "I?" then gosub [SendIP]
if picin == "D?" then gosub [SendData]
if picin == "X" then goto [Exit]
wait
'
[gettime]
bla = time()
dy = mid(bla,1,3) 'dow
mh = mid(bla,5,3) 'month
dd = mid(bla,9,2) 'date
hh = mid(bla,12,2) 'hour
mm = mid(bla,15,2) 'min
ss = mid(bla,18,2) 'sec
yr = mid(bla,21,4) 'year
'
if dy == "Sun" then dow = "1/"
if dy == "Mon" then dow = "2/"
if dy == "Tue" then dow = "3/"
if dy == "Wed" then dow = "4/"
if dy == "Thu" then dow = "5/"
if dy == "Fri" then dow = "6/"
if dy == "Sat" then dow = "7/"
'
if mh == "Jan" then mth = "01/"
if mh == "Feb" then mth = "02/"
if mh == "Mar" then mth = "03/"
if mh == "Apr" then mth = "04/"
if mh == "May" then mth = "05/"
if mh == "Jun" then mth = "06/"
if mh == "Jul" then mth = "07/"
if mh == "Aug" then mth = "08/"
if mh == "Sep" then mth = "09/"
if mh == "Oct" then mth = "10/"
if mh == "Nov" then mth = "11/"
if mh == "Dec" then mth = "12/"
'
picout = dow & mth
picout = picout & dd
picout = picout & "/"
picout = picout & hh
picout = picout & mm
picout = picout & ss
'
serialprint picout
return
'
[SendIP]
serialprint ip()
return
'
[SendData]
serialprint Doff
serialprint "/"
serialprint Don
serialprint "/"
if Mtime == "hhmm" then goto [SkipMtime]
Mhrs = left(Mtime,2)
serialprint Mhrs
serialprint ":"
Mmin = right(Mtime,2)
serialprint Mmin
return
[SkipMtime]
serialprint "00:00"
return
'
[wifi_update]
write(WIFIname,wifiName)
write(WIFIpass,wifiPass)
returngui
cls
print "REBOOTING..."
print "Clock will attempt to connect to new network settings..."
delay 3000
reboot
wait
'
[timezone_update]
write(TZ,str(timezone))
time.setup(timezone,0)
delay 5000
returngui
wait
'
[Exit]
end
Load it up on a module and give it a try, it is pretty fun to see how easy it is to interact with an esp module serially.
regards
dwight