Life Clock
Clock with Internet synced LCD time and date display, and touch screen display used for weather but functionality can be expanded.
The WiFi Setup
The Wifi Setup¶
A primary functionality of Life Clock is its internet connectivity, with auto time updates and preferably a weather summary as well. We used the ESP8266 wifi module to provide this. Below I summarise the steps involved with setting this up.
Wiring up the module¶
We found the data page on https://cdn.instructables.com/FV1/45EH/IC4MPIMS/FV145EHIC4MPIMS.MEDIUM.jpg useful to wire it up to the mbed.
Rx and Tx Pins These have to go to designated Rx and Tx ports on the board. One important difficulty was realizing that the receiver (Rx) pin from the module connects to the transmitter (Tx) pin from the board and vice versa. CHPD and RST these pins can be wired to any digital output. The CHPD is effectively the on/off switch, use the RST pin to trigger a reset by switching off, waiting 2s and switching on. (see reset() in our code)
Powering the module was tricky. It receives 3.3V but requires around 150mA, higher than is available from the board's 3.3V outputs. After initially trying to do so via the on board power supply we moved to using a regulator circuit to connect the 3.3V input to the board's 5V output, before finally using a seperate 3.3V power supply with an external power input.
Coding the module¶
The full code is given here:
Import programWifiReceiver
Communicates with the WiFi - be very careful when using as it is tempremental.
The majority of the coding effort went into understanding how to get the board to speak to the module. The mbed page from a previous Oxford CWM team at https://developer.mbed.org/teams/Oxford-CWM-Team/wiki/esp8266-mbed-setup-and-control was very helpful in doing so. This takes the form of establishing a Serial to communicate with the PC and another RawSerial to do so with the board.
After doing this I used the .attach() method to set routines to listen for communication in both directions from the ESP and the PC, as shown below
Attach prompts to the PC and ESP inputs via the functions pc_recv and esp_recv
pc.attach(&pc_recv, Serial::RxIrq); // attach the two interrupt services esp.attach(&esp_recv, Serial::RxIrq);
The esp_recv and pc_recv methods to enable bidirectional communication
void esp_recv() { while(esp.readable()) { pc.putc(esp.getc()); //wait_us(1); } } void pc_recv() { char c; while(pc.readable()) { c=pc.getc(); esp.putc(c); if(c==13) { esp.putc(10); // send the linefeed to complement the carriage return generated by return key on the pc pc.putc(10); //ie makes enter perform as expected } } }
After this we had full manual control of the ESP and were able to begin sending commands while the programme was running.
The ESP commands are all listed in this datasheet https://cdn.sparkfun.com/assets/learn_tutorials/4/0/3/4A-ESP8266__AT_Instruction_Set__EN_v0.30.pdf, of which we used the following sequence to start the module:
Commands to initialise the ESP
char* startCommands[10]= { "AT", "AT", //check we are live "AT", "AT+RST", //reset "AT+CWMODE=1", "AT+CWJAP=\"epgmdm\",\"3F7540BC59\"", //these are the SSID and password of your wifi "AT+CIFSR", //request the IP of the connection "AT+CIPMUX=1", //select multiple connection mode (single connection mode doesn't work well) "AT+CIPSERVER=1,8080", "AT+CIPSEND=0," };
As shown in the Datasheet linked above, sending AT simply prompts the ESP to respond with OK, proving that it is alive and well.
AT+RST prompts a full reset, switching the module off and on again. After performing this step, wait for the ESP to send Wifi Connected and IP Obtained before sending anything further. I had a lot of trouble from sending an extra Return after each command, which caused the ESP to simply respond with busy s....
Connecting to a website¶
Having setup bidirectional communication with the module, the challenge was to send requests to and receive data from a webpage. We decided to focus on extracting only the time from the web, and so our target page was http://time.jsontest.com
Connecting to this webpage was a simple matter of sending the command "AT+CIPSTART=1,\"TCP\",\"172.217.23.19\",80" where the =1 indicates our arbitrary connection number and the IP of the website was found by pinging it from a terminal. Port 80 is used for any HTTP website. The ESP responds with something like Connected, at which point you are ready to send and receive data.
Retreiving Time¶
We used the following code to get our time from jsontest.
Code to connect to time.jsontest.com
char *msg=(char *)malloc(50); char *msg2= "GET / HTTP/1.1\r\nhost: time.jsontest.com\r\n\r\n"; sprintf(msg,"AT+CIPSEND=1,%d",strlen(msg2)); pc.printf("%s\r\n",msg); esp.printf("%s\r\n",msg); pc.printf("%s\r\n",msg2); esp.printf("%s\r\n",msg2);
Jsontest responds to this with a large message, including its HTML content.
Retreiving time from message data¶
After extracting this data, we plan to add functionality to move the characters into a string from which the time can then be extracted. Sadly, time constraints did not allow us to do this.