External Links

Creative Science Centre

 

Web Server

There is also now the micro_web that has been built on the experience with these pages, but it does require an SD card to be on the system. I would check out Micro_Web first.

This is an update to the original web server information. The reason for the update is to accommodate the new ESP8266 device.

Scope

This applies to devices that have an SD Card, this includes the MX170 processors running the SD Card firmware. Also the IoT kit can be fitted with an SD Card.

Warning

Before you begin it must be said that the current version of ESP8266 firmware only has a small I/O buffer, about 100 bytes and so this makes communication with a web page quite slow. Sometimes so slow that it gives up. Having said that don't be put off it still works well for simple web pages.

Firmware

This is written in sections so that it can be built up slowly in order:

  • main.bas
  • mx170_rookie3_part1.bas or mx370_rookie3_part1.bas
  • mx170_rooke3_part2.bas or mx370_rookie3_part2.bas
  • datetime.bas
  • ESP8266_508.bas or ESP8266_509.bas
  • http_server_sd_a.bas
  • Application

The BV502, the DIL IC (BV500) and BV508 can all use the MX170 or BV508 firmware. The BV509 should use the BV509 firmware. The only real difference for this application is that the BV509 has an RTC and the BV508 has a 'pretend' RTC. The MX170 does actually have RTC hardware but unfortunately the pin for the clock crytsal is used for something else.

main.bas

This is responsible for checking that the modules ar loaded into flash and if not, load them in. If for example flclear(0) is typed in to clear the flash, then, when the ByPic resets main will load the file into flash again. The MX170 firmware uses short file names to save space (in flash not on the SD Card) and the MX370 uses long file names, this can be observed in the main.bas file where MX170_rookie3_part1.bas becomes MX170_~1.BAS

Rookie

This is just the standard rookie, now version 3 which is much more efficient.

datetime.bas

This is a collection of date and time routines that can manipulate the string output format. There are also some other usefull routines that enable the calculation of date and time from a certain point. Its not that easy to calculate the number of seconds between say now and tomorrow at 13:20. The file is not absolutely required but it is good to have.

ESP8266_508/9

The only difference between the 508 and 509 is the constants at the top of the file because the ESP8266 is connected to different pins.

====== Up to this point we have a Wi-Fi communication platform, from here we can take several directions, however as the title suggests we will look at the HTTP protocol.

http_server_sd_a.bas

This file will take some explaining as it behaves as a web server, serving pages to a browser. There is however an added complication in that it is capable of serving dynamic pages. To explain further; a simple web server will when requested for a page will send that page back to the browser.

This is easy work for the server, of course if the page does not exist then it send a page back saying not found.

Level 1

An extension of this is to send some extra information in the URL, so for example if the URL was 192.168.11.50/page1, the server would find page 1 and send that back (this assumes that the server is listening on 192.16.11.50 port 80). Suppose now we wanted to say switch on an LED at the server. This could be done in 2 ways:

1) page 1 switches the led off and page 2 switches the led on

2) Use a form on the browser, not to go into too much detail but a form will send a request for a page and also a query string, it looks something like this:

192.168.11.50/ledpage?LED1=on

Here we have 2 bits of information the page "ledpage" and the query string "LED1=on"

The web server can use this information to display the ledpage and turn on LED1, we could easily program some functions to do that.

Level 2

What about if we need to tell the user, via the browser something. The temperature for example. It is easy for the web server hardware to read a temperature device but how would this get back onto the browser? A different page for each possible temperature - not really. This is where dynamic pages come in. Instead of sending back a static page, the server will create a page based on the page it has stored and the request mad be the browser.

As an example we have a page that simply sends back the temperature. The page may look something like this:

<html>

<body>

<p>The temperature is <? bypic getTemp ?></p>

</body>

</html>

[TABLE 1]


The bit to note is this "<? bypic getTemp ?>" The webserver will read the page and when i comes accross this string will call a function called getTemp(). The function will get the temperature and place it in a global variable called echo$ which will be output instead of "<? bypic getTemp ?>" so the resulting page that will be sent back to the browser will be something like:

<html>

<body>

<p>The temperature is 27.3</p>

</body>

</html>

[TABLE 2]

Armed with this information the way http_server_sd_a works is as follows:

1) server_loop(): This is a perpetual loop that the server will wait in until information is received from the ESP8266 vi UART1. This will be as a result of a browser making a request. The server_loop with then break this information into a page (page$) and a query string (qs$). A page is not really a page but a function!

Now, the http_server_sd_a.bas file is loaded before the page functions exists, this is to make it more generic so that an application can be built on top of the server. Normally ByPic would complain that the function did not exist so the lookup technique is used, which, because it is not actually done until run time it does not matter that the future function does not exists. As a bonus it makes the server loop very simple, the alternative would be a large select, case statement. The downside is that parameters cannot be used, this is why page$ and qs$ are global.

If a function is found (in some future module) then the server loop makes a jump to that function which is executed. The page functions should all be contained within the application module (it doesnt have to be called application).

To go with the example of returning a temperature we have two functions, one for the page and one for actually getting the temperature:

function tempPage()
   create_tempFile("/web/temp.htm") // {1}
   send_page(chan,tempFile$) // {2}
endf

Variables chan and tempFile$ are global and set by http_server_sd_a. {1} here we have a file on the SD card in the directory web called temp.htm that is exactly like table 1 above. Create_tempFile() will take the file as an input and create a temporary file on the SD Card whose name is in tempFile$. It will be an exact copy of temp.htm except where <?bypic ?> is encountered. When it cones across this it will extract the function name and run that function and substitute the contents of echo$ in between the <? and ?>.

function getTemp$()
   echo$="27.3"
endf

The resulting temporary file will now look exactly like that of table 2.

The application is also responsible for starting up the ESP8266 and starting the watch dog is required. The watch dog is definitely recommended as the wi-fi can be a bit temperamental.

A Practical Example

For this to work the wi-fi, ESP8266 will need to have your ssid and password set using the join("myssid","mypassord") command that is in the ESP8266 library.

Trying to keep this as simple as possible a BV508 is used with an SD Card connected and a LED connected to B0 and a bit of wire connected to B1, this will be an input with a weak pull up so connecting it to ground will change its state.

 

The web site looks like this

  • Zip file containing all files for this simple project

Unzipping the above file, placing on an SD card and then resetting the device will result in the creation of a web server, with the serial still connected you will be able to see the IP address, in the browser simply put in the IP address and press enter and hopefully the above page will appear.

For development I would modify the main.bas file so that it does not run application.bas, I would load this through the IDE, pressing F4 so that some experimentation can be carried out.

Conclusion

If you are having difficulty getting the web server to run then strip it back to where the ESP8266 library is loaded and go from there, something like:

wf_start()
mode(1)
join("myssid","mypassword")
info() // you should see an IP address that you can use in the browser, may need to wait and do it again.
server(80) // this makes the wi-fi listen on port 80
see1()

Now send something from a web server by typing the IP address in and use see1() again to see if anything has arrived. If not that's your problem right there as they say.

On my system this works very well, the main downside is that the current firmware on the ESP8266 does not allow a static IP address for when the device is in station mode and so you have no idea what the IP address is unless you do info or look on the router as to what address it has been given. There is a later version of the firmware that addresses this I believe.

If you have a public web service, they used to come with an internet provider I don't know if they still do then the device can publish its address to that periodically. Another alternative I think is perhaps to use a service like noip