Place to put your Basic demos and examples

Moderator: Mmiscool

User avatar
By livetv
#57082 So I have two ESP8266 based nodes in a house thousands of miles away monitoring and reporting the status of 14 different sensors. Works pretty well except that both are in a deep, dark basement and while the 12-F stays connected, it appears that the 12-E sees a weaker signal and will, on occasion, disconnect. Once disconnected, the node is isolated yet still running.

So I've conjured up a little code to solve that problem and have it working locally. While there are various ways one might check for connection to a router, I'm going simple. I just look to see if the WGET() that I use to report to my web server is returning a response. If it fails three times in an many minutes, I assume that I've lost connection with the router. While it could be other problems, those cannot be solved by the node and a re-connection is harmless.

Code: Select all' Initialize this once
fail = 0

' Timer triggered reporting code goes here to create a url with command line variable passage.
'   (Once per minute in my case)
rv = wget(url)
if rv == "" then
  fail = fail + 1
  if fail>2 then gosub [reconnect]
  endif
wait

[reconnect]
dim c_inf(6) as string
c_inf(1) = read("WIFIname")
c_inf(2) = read("WIFIpass")
c_inf(3) = read("ipaddress")
c_inf(4) = read("gateway")
c_inf(5) = read("subnetmask")
if c_inf(3) == "" then wifi.connect(c_inf(1),c_inf(2)) else wifi.connect(c_inf(1),c_inf(2),c_inf(3),c_inf(4),c_inf(5))
undim c_inf
fail = 0
return


So this works by reading the router SSID and password and any IP setup from flash (same variables used in the Settings page). Then it uses the right version of the WIFI.CONNECT() function to re-connect. Notice how I use an array so I can DIM and UNDIM it to save on variables? Yeah, that means I read the flash variables every time, but speed is not an issue here.

Now all I have to do is get my cousin (who is barely computer literate) over to the house and talk him through updating the Basic program.
User avatar
By Mmiscool
#57083 You could reduce the lines of code by using the read functions in line instead of reading in to variables.

The same with the wget.

Example.

Code: Select allif wget(url) == "" then
User avatar
By Electroguard
#57086 Thanks for the contribution, and I think I get it... but only if you can confirm that this is intended as a general non node-specific code - to explain what seems an unnecessary IP address check? (presumably you'd know if those nodes would be receiving DHCP addresses or not).
User avatar
By bugs
#57087 I have a couple of devices that sometimes lose connection. They do not connect to the internet - just on a local network and they /may/ be the only devices connected apart from the router.
As they only talk to each other I cannot tell which has lost connection when communication fails.
For this reason I have put in a feature request for a "ping" function to ping the router so that each device can monitor and reconnect if necesary.
I don't /think/ your method would be applicable - my devices must use fixed IPs.