External Links

Creative Science Centre

 

IoT Wi-Fi Discovery Kit

This text is now obsolete, the ESP8266 now uses ByVac firmware which does not use the AT commands and so the functions used in this text will not work. The updated text is here.

Getting Started

IoT (Internet of Things). This text describes the use and how to use the kit and how it works so that all or part of it can be incorporated into other projects.

The kit comes with the software already pre-loaded and works with a ByPic Microcontroller. (if you have an existing BV502 (MX170 version), download the software is here.

http://www.byvac.com/mBlib/flb/Library/wifi/ESP8266_DHT.script (details)

These controllers talk to the outside world via the 5 pin serial interface and so the first thing to do is to download the IDE on this page.

Because the software is pre-loaded you should see the menu. ** If you see a slightly different menu then simply clear the flash (flclear(0)) and reload the script above using CRTL+SHIFT+R **

Remove the jumpers as described in the kit instructions for S1,S2 and S3 to get to this screen. The jumpers simply tell ByPic to start running the client or server immediately, we don't want that as we need to get to this menu.

Setting up a Server (1)

A server is used in this context to provide a web page for a browser, so the kit will effectively be a HTTP server providing (all be it one) a web page to any browser that is connected. A server is useful for controlling things such as lights and heating as it can be contacted by any browser (potentially anywhere) and will respond straight away.

Select 3 to run info:

The important bits have been highlighted in white. This tells us that the current mode of operation is as an access point and if we were to scan on another device (laptop, phone etc.) we would see 'LINK3' as an open (the last 0) access point that we could connect to. We can communicate with it via the IP address as shown. This is 192.168.4.1 and cannot currently be changed.

If Wi_Fi mode is not 2 then press 4 as we need the module to be an Access Point (AP). Scanning for lINK3 (in this example) will not work until theESP8266 is in AP mode.

Now press 9 to run the HTTP server. Go to the laptop or other device and connect to the SSID as shown in info, in this case LINK3. Because of the way the kit is set up you will also need to place a shorting link between C9 on the Mini-Max and ground. Removing the link enables you to get out of the continuous server loop.

This is a dongle device on Windows XP, windows 7 and laptops normally have an icon bottom right. Use setting for Android devices.

Troubleshooting:

  1. The ESP8266 is quite slow at giving out an IP addresses and so it may take more than a minute, you can't brows until this happens.
  2. Some devices (Android) have a habit of reverting back to the original Wi-Fi network. If this is causing problems, temporarily disable (forget) the one you normally use.

All being well you should be able to brows to something like this:

Clicking the links should show results. This is an example of being able to control something - turning the LED on and off and being able to monitor something, getting the temperature and RH.

The serial terminal will show something like this when the browser is clicked:

To stop the server running remove the link from C9 and ground.

Setting Up a Client

A client will require an existing Access Point SSID and password. This will definitely NOT be set for your home network, it only needs doing once as the ESP8266 will remember it.

Set the SSID and password using option 6

Now set to be a station by using option 5

Use option 3 to check that it is in mode 1

Here we can see that it is in mode 1 and the IP address has been given to the ESP8266 by the home access point, in this case 192.168.11.54.

To test this use option 8 or get the current date, option 11. Option 8 will contact google and google will in turn send back information from its server. The information will be in the form of an HTTP 'head' and so will not be very meaningful but you should be able to make out details such as the server name etc.

Need a Server

The hard part about using this mode is that a server is required and this is not a standard option on any Windows platform, possibly on Linux. so to make thing easier initially you can use a public server that has been set up for this purpose.

With C9 connected to ground Use option 10

The serial connection will show something like:

In a browser navigate to  http://www.byvac.com/wifi/wfdemo.php

And you should get a page that looks similar to this:

The client is active about once every 30 seconds or so, when not active it powers down and uses less than 1mA (about 350uA)*. In this example it shows how a client can report back to a server and also how that server can influence the client. The led will come on if the button is pressed but only when the client next connects to the server.

The main advantage of this set up is that the client can power down between contacting the server, using the common 18650 Li-Ion battery could give several months possibly a year or more depending on how often the client contacts the server.

Applications for this include wild life monitoring, remote weather stations etc. The client could also contact the server at a specific time for turning on light for example and then the server could tell the client when next to contact.

The actual files on the server are here.

To get back to the main menu disconnect the link at C9 and wait for the client to power up.

How the client and server interact

There are three files on the server side, logger.php, wifidmo.php and wifidemoLED.php. The client (BV508) will send a GET request to the server, the request is sent to logger.php as follows:

GET http://www.byvac.com/wifi/logger.php?tmp=21.5&rh=44.56&dp=7.903

The values for the temperature, relative humidity and dew point are calculated just before being sent. The logger.php file will receive those values and store them in a text file on the server. The file name used employs the IP address of the client which will in most cases be different for each client. So each time the client makes a GET request a new set of values (up to 10) is stored in the file.

When the client makes the GET request, it expects a response from the server, the logger.php will provide this response with a very simple:

RESPONSE:L1 or RESPONSE:L0

This will tell the client (BV508) to either switch the led on or off. Logger.php gets this (L1 or L0) value from another file on the server that is written by wifidemo.php. Wifidemo.php is the file that the user sees, it displays the contents of the file created by logger.php and it also creates the file used by logger.php when a button is pressed.

The set up is meant to be for demonstration and is very easy to abuse, if this happens it will have to be shut down so do not create a project that relies on it being there.

To extend the usefulness of this system then Google Docs, specifically a google spreadsheet could be used to hold the data as the data to one of these spreadsheets can be sent by a client similar to this. It only needs an account and a key.

NOTES:

*With the current software the BV508 will consume about 4.5mA. This is probably due to a peripheral being left switched on or perhaps a port being left in input mode. The figure of 350uA is for the BV502 with the ESP8266 on a separate power supply.

For serious long battery life applications attention needs to be paid to the power supply. In fact for the arrangement we have in this kit the power supply will consume far more than the CPU and ESP8266 in sleep mode.

For this example to work easily a public server has been set up. Do not rely on this being in place indefinitely, you should set up your own server.

Client(2) Sending an Email

This is an exercise for you to do. The BV509 will, instead of sending data to the public browser as above will send an email to any email box of your choosing.

In order to do this you will need a google account, here are the step by step instructions:

  1. Log into www.pushingbox.com using your google account
  2. Create a Service called Email service, with the email address that you want to send data to, so for example if you want to send an email to fred@basset.com then use that email address.
  3. Now using the menu options in pushingbox create a Scenario by adding a scenario to your list, give it any name, say Emailer.
  4. Add an action to this scenario and use the Emailer service you created in 2)
  5. Give it a subject name, this will be on the subject line of the email when sent by pushingbox.
  6. In the body enter this text 'Temperature is $tmp$ Humidity is $rh$ and Dew Point is $dp$' and submit. The words bounded by $ are variables that we will send as part of the GET URL. Make sure for this example that you use exactly those variable names.
  7. Make a note of the device ID vC0123456789F355 (you will have a different ID)
  8. Test by using a browser, enter the following: http://api.pushingbox.com/pushingbox?devid=vC0123456789F355&tmp=22&rh=44&dp=3.77 in the address bar, obviously substituting your ID for the one shown here.
  9. You will now receive an email which will read in the body of the text:
    Temperature is 22 Humidity is 44 and Dew Point is 3.77
  10. If not then check in the junk mail, this does work and if not in your case then check the above and do it again.

So far so good, all we need to do now is to create a client that will send an email rather than sending it to the server as in the client example preceding this.

First get out of the menu system by pressing 0 (or any none numeric key for that matter). Copy this code to a new sheet in PSPad:

function test(clientID$[30])
dim a$[160]
    a$= "pushingbox?devid="+clientID$+"&tmp=22.0&rh=44&dp=3.7"
    get("api.pushingbox.com",a$,80)
endf

Send it to the BV508 by pressing F4 and type at the black screen:

test("vC0123456789F355") // You will need to enter your own client ID * Carefully, it may be easier to use this code:

function test()
dim a$[160]
    a$= "pushingbox?devid=vC0123456789F355&tmp=22.0&rh=44&dp=3.7"
    get("api.pushingbox.com",a$,80)
endf

and past in the Client ID as it is very easy to make a mistake. Then just type test()

If this works copy this code to a blank PSPad sheet

constant DEVICEID$ "vC0123456789F355"
// *****************************************************************************
// creates a query string to send to pushingbox
// *****************************************************************************
function qs2$(devID$[30])
dim x$[180] = "pushingbox?devid="+devID$+"&"
dim t$,r$,v,t#,rh#,dp#
    dht11_init() // for temperature measurments
    v = dht11_read()
    t$=get_temp$(v)
    r$=get_rh$(v)
    x$=x$+"tmp="+t$
    x$=x$+"&rh="+r$
    t#=str2float(get_temp$(v))
    rh#=str2float(get_rh$(v))
    dp# = dewpoint#(t#,rh#)
    x$=x$+"&dp="+float2str(dp#)
    return x$
endf
// *****************************************************************************
// client function talkes to a webserver
// *** Before starting this will need connecting to an existing access point
//     using join and the wi-fi initialising
// This will send an email to pushingbox
// *****************************************************************************
function HTTPclient2()
dim msg$, j
    io_pinMode(pSERVER,SERVER,IN,WPU) // run client when low
    if io_read(pSERVER,SERVER)  <> 0 then
        print "\nThis will not run without putting a link between C9"
        print "\nand ground - do it now"
    endif
    print "\nTo stop the server remove the link between C9 and ground\n"
    mux(0) // needs to be single for this app
    mode(1) // station mode
    // ip address is aquired from remote DHCP
    if isIP() = 0 then
        print "\nUnable to get IP address"
        return
    endif
    while io_read(pSERVER,SERVER)  = 0
        wfPower(1)
        isIP() // takes some time to get ip address back
        get("api.pushingbox.com",qs2$(DEVICEID$),cPORT) // send
        // no need to do anyting with response but could check if it is okay
        // for now just print it out
        wait(1000) // get all of it
        see1()
        // power down here
        wfPower(0) // wifi power down
        @WDTSET = WDSW // turn on watch dog (about 16 sec)
        for j = 1 to 4 // increase time a bit dont want an email every 30sec
            powersave(1) // sleep mode for CPU (about 15-16 sec)
        next
        @WDTCLR = WDSW // watchdog off
    wend
    // power back up before leaving
    wfPower(1)
endf

Change the constant for your device ID and press F4 to send to the BV508, don't forget to connect up the 'C9 switch' and type HTTPclient2() to get it going. You will receive an email ever minute or so.

The qs2$() function builds up the query string based on real values, this of course can be changed to reflect anything but the pushingbox scenario must be also changed if the variables change. In a practical situation I would take several readings and perhaps send them all at once at the end of the day.

If necessary you can save the above code to flash by typing flsave(""). If you want it to run at switch on instead of the menu then rename HTTPclient2() to main() or create a function main() that calls HTTPclient2() before typing flsave("").

At start up ByPic will look for main() and run that, if there is more than one main() in flash, it will run the last saved one. In fact by default if there are functions with the same name then the last defined function will be run.

Setting up a Server (2)

In part 1 of setting up a server, the ESP8266 was used as an access point. This was convenient because it meant that ANY device could link with it and control it. However it is more likely or convenient that the ESP8266 is connected to an existing network, yet can still be a server.

Select option 3 and make a note of the IP address, also make sure that it is in mode 1 (station)

Select option 9 but this time brows to the IP address given in info, in the case of above this will be 192.168.11.54

You should be able to now connect without reconnecting to another access point. This is the most useful application for controlling a home network. For example powering up/down computer equipment, turning lights on and off. Even though it needs to be powered all of the time the power is very small about 1/3 Watt on average which is probably much less then most equipment on standby.

How it Works

The firmware consists mainly of the ESP8266 library that has its own page and examples of how to use the library, selecting 0 on the menu system will exit to ByPic so the library can be accessed directly, as an example, press 0 to exit and then type info().

In addition to the ESP8266 library there is the DHT11 plug-in so that the temp and RH can be obtained and also a HTTP_client.bas and HTTP_server.bas. The URL to both files are here:

http://byvac.com/mBlib/flb/Library/wifi/http_client.bas

http://byvac.com/mBlib/flb/Library/wifi/http_server.bas

The best way to look at these is to copy the url to the clipboard and then user the editor script "open URL in clip". It can be seen that most of the work is done in the ESP8266 library.

Resources

The underlying commands set and data sheets for the ESP8266 is very well described here:

https://nurdspace.nl/ESP8266  There are also plenty of references for this module, search for ESP8266

ESP8266 ByPic library

HTTP client and server software

http://byvac.com/mBlib/flb/Library/wifi/http_client.bas

http://byvac.com/mBlib/flb/Library/wifi/http_server.bas

DHT11 Drivers software

FAQ

I have come out of the menu system, how do I get the menu options up again

Type menu()