Tell me what you want, What you really, really want.

Moderator: Mmiscool

User avatar
By heckler
#65455 Here is an example of how I have a PIC microcontroller and an ESP module communicate serially.

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 >>
Code: Select allbaudrate 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 >>
Code: Select all            ' 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 >>
Code: Select all            ' 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 >>
Code: Select all 
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 >>
Code: Select allmemclear
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
User avatar
By dubpetr
#65467 Hi heckler, in fact, I have no experience in PIC programming - I've made a complete (lights, temperature, garage door, windows screens, recuperation unit) "inteligent home" myself based on 1wire network, but it is PC-controlled, so no "lowlevel" programming was needed. However there is no easy way to connect ultrasonic device to such a network, since 1wire itself is too slow. ESP seems to be fast enough to be able to measure pulses in, say, 20us precision (about 3mm in distance). I understand, that it is outside the possibilities of ESP Basic, and this was the reason of my question.
In fact, I wanted to try it to check it first, if the sensor would withstand the environment... As ardhuru noted, it would probably be the main problem.
Thank you very much for your help (I will maybe use it later :-)).
User avatar
By dubpetr
#65468 Hi ardhuru,
ad 1) OK, I just hoped, that ESP processor is fast enough :-)
ad 2) I don't think, this is the right approach. Simple capacitor charging and discharging could work on short distances, but the farer you are, the worse precision you get.
ad 3) This is, what I wanted to test - nearly 100% humidity and cca 7°C is not the environment the electronic like (I would not like it too :D ). I wanted to use some paint to protect it...
ad 4) This is a great idea. I always thought, that such devices work on short distances only (say a meter and less). My tank is 2,5 meter deep, however the maximum water level is about 2 meters. What type of sensors did you buy (if it is not secret :-))?
User avatar
By ardhuru
#65497 Hello Dubpetr,

2) I realized this; the pic measuring the capacitive probe is very close to the sensor (about 6 inches), and then transmits the reading to the controller over the UART. But still, more work needs to be done!

3) Yikes! For that environment, I'd pretty much rule out the HC-SR04. The paint might (or, not) help the pcb, but the transducers themselves have a fine grill, which would still expose the element to the humidity. I suspect you'll get reliable operation only for a few weeks, if that.

4) No secrets on this forum! Here's the kind of unit I have; http://www.ebay.com/itm/Parking-4-Senso ... Swx2dYEyMN

Please note that there are a number of OEMs making these, and the signal protocols might be slightly different for each. Nothing that a logic analyzer or a PC based oscilloscope cant handle! Usually, there's a wire going to the display unit that carries all the distance information, for all 4 transducers.

Alternately, this looks promising too, though I havent used one yet. http://www.ebay.com/itm/Ultrasonic-Modu ... Sw6BtVUbL4

Regards,

Anand