Displays the current weather, date, and time

Dependencies:   mbed MbedJSONValue mbed-rtos 4DGL-uLCD-SE ESP8266NodeMCUInterface

Welcome to the Smart Weather Clock, which uses WiFi and Bluetooth to give the user a clear date, time, and weather for the day.

This wiki serves to document

  • What components you'll need
  • How these components will be connected
  • How the code gets all the information it needs

Components

LCD Screen

Components / 4D Systems 128 by 128 Smart Color LCD uLCD-144-G2
128 by 128 Color Smart LCD with serial interface and SD card slot

The LCD screen is a 128x128 pixel full color LCD by 4D Systems. A wonderful introduction can be found here.

In the simplest case, we'll only use one screen, but we can (and perhaps should) augment the design to include two.

Import library4DGL-uLCD-SE

Fork of 4DGL lib for uLCD-144-G2. Different command values needed. See https://mbed.org/users/4180_1/notebook/ulcd-144-g2-128-by-128-color-lcd/ for instructions and demo code.

You can buy it from sparkfun.

5V Adapter

The adapter allows us to feed much more power to the MBED and, critically, decouples us from a computer. It's important to remember that on the barrel jack adapter (i.e., the part that goes in the breadboard), the pin closest to the jack is 5V, and the back pin is Ground.

You can get the adapter here, and the barrel jack adapter here

Bluetooth UART Module

This module allows us to interface with the mbed via bluetooth and the ever-helpful AdaFruit Bluetooth App. A great introduction can be found here.

We'll use it to allow the user to set some defaults and API keys. This is stored in persistent flash storage on the LPC 1768 (See the Local File System, but you'll need an SD card if you're using a different MBED device.

You can get the Bluetooth module here.

WiFi Module

The WiFi module is also from AdaFruit, like the Bluetooth module. This allows us to interface with different WiFi networks for internet connectivity. A nice introduction is here.

You can get the WiFi module here.

WiFi Interface!

The current WiFi interface (found here) has some major problems - we're using an augmented version in our code!

Connections

5V Input for MBED

Before you connect the MBED to 5V from the wall, Make sure you have connected it correctly! If you don't, you could brick the MBED.

Thankfully, this is easy to test - when you think you have hooked up the 5V Jack correctly, try lighting a single LED (with a 10K resistor in series). If it lights up - you're good!

Alternatively, you could just plug in the MBED via USB, and just not have the MBED run on the +5V. You'll still need the Wall Adapter, however.

WiFi Module

The WiFi Module has two Grounds - you must connect both!

MBED5V Barrel Jack
Vin+5V (back pin)
GndGnd (pin closest to jack)
MBEDuLCD Cable
5V5V
GndGnd
P09TX
P10RX
P11Reset
MBEDBluetooth UART
5VVin
GndGnd
GndCTS
P27TXO
P28RXI
MBEDWiFi Module
5VV+
GndGnd
GndGnd
P13RX
P14TX
P15Reset

Code

The code works in a fairly logical order:

  1. Connect to the given WiFi, and check that we have all of our API keys
  2. Get the current location via the IP API
  3. Get the current time via the TimeZoneDB
  4. Get the current weather via the OpenWeatherMap API
  5. Continually update the weather, once per minute

Some caveats:

  • Memory seems to be an issue. If I had to guess, it would be the MbedJSONValue JSON parser - instantiating a third one seems to always lead to an MBED OS Fault or an MBED Memory Allocation fault. More insight is needed here.
  • The off-the-shelf ESP8226 Interface doesn't handle receiving correctly - I had to augment the library with recv and recv2. It works in most simple practical use cases.
Committer:
alexhrao
Date:
Fri Mar 29 13:12:49 2019 +0000
Revision:
1:0620ba35d2e8
Parent:
0:4ffa136585a2
Child:
2:f7d19812bdc5
Get TCP Connection to work

Who changed what in which revision?

UserRevisionLine numberNew contents of line
alexhrao 0:4ffa136585a2 1 #include "mbed.h"
alexhrao 1:0620ba35d2e8 2 #include "uLCD_4DGL.h"
alexhrao 1:0620ba35d2e8 3 #include "ESP8266Interface.h"
alexhrao 1:0620ba35d2e8 4 #include "TCPSocketConnection.h"
alexhrao 1:0620ba35d2e8 5 #include "rtos.h"
alexhrao 1:0620ba35d2e8 6
alexhrao 1:0620ba35d2e8 7 // We need this for being able to reset the MBED (similar to watch dogs)
alexhrao 1:0620ba35d2e8 8 extern "C" void mbed_reset();
alexhrao 1:0620ba35d2e8 9
alexhrao 1:0620ba35d2e8 10 // LCD Screen
alexhrao 1:0620ba35d2e8 11 uLCD_4DGL uLCD(p9, p10, p11);
alexhrao 1:0620ba35d2e8 12 Mutex lcd_lock;
alexhrao 1:0620ba35d2e8 13
alexhrao 1:0620ba35d2e8 14 // File System
alexhrao 1:0620ba35d2e8 15 LocalFileSystem fs("local");
alexhrao 1:0620ba35d2e8 16
alexhrao 1:0620ba35d2e8 17 // Bluetooth
alexhrao 1:0620ba35d2e8 18 RawSerial pc(USBTX, USBRX);
alexhrao 1:0620ba35d2e8 19 RawSerial dev(p28,p27);
alexhrao 1:0620ba35d2e8 20 Thread dev_thread;
alexhrao 1:0620ba35d2e8 21
alexhrao 1:0620ba35d2e8 22 // Error LED
alexhrao 1:0620ba35d2e8 23 DigitalOut err_led(LED1);
alexhrao 1:0620ba35d2e8 24
alexhrao 1:0620ba35d2e8 25 // WiFi
alexhrao 1:0620ba35d2e8 26 ESP8266Interface wifi(p13, p14, p15, 9600, 10000);
alexhrao 1:0620ba35d2e8 27
alexhrao 1:0620ba35d2e8 28 // Time
alexhrao 1:0620ba35d2e8 29 Thread time_thread;
alexhrao 1:0620ba35d2e8 30
alexhrao 1:0620ba35d2e8 31 // Weather
alexhrao 1:0620ba35d2e8 32 Thread weather_thread;
alexhrao 1:0620ba35d2e8 33 char* forecast_link;
alexhrao 0:4ffa136585a2 34
alexhrao 1:0620ba35d2e8 35 void time_updater() {
alexhrao 1:0620ba35d2e8 36 // We're not an interrupt, so take as much time as we need. Infinite loop
alexhrao 1:0620ba35d2e8 37 // but wait 1 second between each loop
alexhrao 1:0620ba35d2e8 38 struct tm* ltm;
alexhrao 1:0620ba35d2e8 39 time_t now;
alexhrao 1:0620ba35d2e8 40
alexhrao 1:0620ba35d2e8 41 now = time(NULL);
alexhrao 1:0620ba35d2e8 42 ltm = localtime(&now);
alexhrao 1:0620ba35d2e8 43
alexhrao 1:0620ba35d2e8 44 // Buffer for time string. Max length is 23:59 + \0
alexhrao 1:0620ba35d2e8 45 int max_time_len = 6;
alexhrao 1:0620ba35d2e8 46 char ftime[max_time_len];
alexhrao 1:0620ba35d2e8 47
alexhrao 1:0620ba35d2e8 48 int min = -1;
alexhrao 1:0620ba35d2e8 49
alexhrao 1:0620ba35d2e8 50 while (true) {
alexhrao 1:0620ba35d2e8 51 // if the minute has changed, update.
alexhrao 1:0620ba35d2e8 52 now = time(NULL);
alexhrao 1:0620ba35d2e8 53 ltm = localtime(&now);
alexhrao 1:0620ba35d2e8 54 if(ltm->tm_min != min) {
alexhrao 1:0620ba35d2e8 55 // Get the new time
alexhrao 1:0620ba35d2e8 56 strftime(ftime, max_time_len, "%H:%M", ltm);
alexhrao 1:0620ba35d2e8 57 // Update time! Lock the lcd mutex
alexhrao 1:0620ba35d2e8 58 lcd_lock.lock();
alexhrao 1:0620ba35d2e8 59 uLCD.text_width(2);
alexhrao 1:0620ba35d2e8 60 uLCD.text_height(2);
alexhrao 1:0620ba35d2e8 61 uLCD.text_string(ftime, 0, 2, FONT_8X8, GREEN);
alexhrao 1:0620ba35d2e8 62 // Done updating - unlock!
alexhrao 1:0620ba35d2e8 63 lcd_lock.unlock();
alexhrao 1:0620ba35d2e8 64 min = ltm->tm_min;
alexhrao 1:0620ba35d2e8 65 }
alexhrao 1:0620ba35d2e8 66 // Wait 1 second
alexhrao 1:0620ba35d2e8 67 Thread::wait(1.0f);
alexhrao 1:0620ba35d2e8 68 }
alexhrao 1:0620ba35d2e8 69 }
alexhrao 1:0620ba35d2e8 70
alexhrao 1:0620ba35d2e8 71 void weather_updater() {
alexhrao 1:0620ba35d2e8 72 // We can take as long as we want
alexhrao 1:0620ba35d2e8 73 while (true) {
alexhrao 1:0620ba35d2e8 74 // get the weather
alexhrao 1:0620ba35d2e8 75 // we'll always want to update the LCD - don't worry about the previous
alexhrao 1:0620ba35d2e8 76 // weather
alexhrao 1:0620ba35d2e8 77 int curr_temp = 0;
alexhrao 1:0620ba35d2e8 78 int high_temp = 0;
alexhrao 1:0620ba35d2e8 79 int low_temp = 0;
alexhrao 1:0620ba35d2e8 80 char buf[12];
alexhrao 1:0620ba35d2e8 81 sprintf(buf, "%d %d/%d", curr_temp, high_temp, low_temp);
alexhrao 1:0620ba35d2e8 82 // lock
alexhrao 1:0620ba35d2e8 83 lcd_lock.lock();
alexhrao 1:0620ba35d2e8 84 uLCD.text_width(2);
alexhrao 1:0620ba35d2e8 85 uLCD.text_height(2);
alexhrao 1:0620ba35d2e8 86 // include null!
alexhrao 1:0620ba35d2e8 87 uLCD.text_string(buf, 0, 5, FONT_7X8, WHITE);
alexhrao 1:0620ba35d2e8 88 // done! unlock
alexhrao 1:0620ba35d2e8 89 lcd_lock.unlock();
alexhrao 1:0620ba35d2e8 90 Thread::wait(3600);
alexhrao 1:0620ba35d2e8 91 }
alexhrao 1:0620ba35d2e8 92 }
alexhrao 1:0620ba35d2e8 93
alexhrao 1:0620ba35d2e8 94 void dev_recv() {
alexhrao 1:0620ba35d2e8 95 // Continually check if we have stuff...
alexhrao 1:0620ba35d2e8 96 char buf[1024];
alexhrao 1:0620ba35d2e8 97 int ind = 0;
alexhrao 1:0620ba35d2e8 98 while (true) {
alexhrao 1:0620ba35d2e8 99 while (true) {
alexhrao 1:0620ba35d2e8 100 // get stuff. If we encounter \r or \n, that's a complete command!
alexhrao 1:0620ba35d2e8 101 char tmp = dev.getc();
alexhrao 1:0620ba35d2e8 102 if (tmp == '\n' || tmp == '\r') {
alexhrao 1:0620ba35d2e8 103 break;
alexhrao 1:0620ba35d2e8 104 }
alexhrao 1:0620ba35d2e8 105 buf[ind++] = tmp;
alexhrao 1:0620ba35d2e8 106 Thread::wait(0.01);
alexhrao 1:0620ba35d2e8 107 }
alexhrao 1:0620ba35d2e8 108 buf[ind] = '\0';
alexhrao 1:0620ba35d2e8 109 // read command and respond
alexhrao 1:0620ba35d2e8 110 if (strcmp(buf, "reset") == 0) {
alexhrao 1:0620ba35d2e8 111 dev.printf("Are you sure? y/[n]\n");
alexhrao 1:0620ba35d2e8 112 }
alexhrao 1:0620ba35d2e8 113 buf[0] = '\0';
alexhrao 1:0620ba35d2e8 114 ind = 0;
alexhrao 1:0620ba35d2e8 115 //if (strcmp(buf, "reset") != 0) {
alexhrao 1:0620ba35d2e8 116 Thread::wait(0.01);
alexhrao 1:0620ba35d2e8 117 }
alexhrao 1:0620ba35d2e8 118 }
alexhrao 0:4ffa136585a2 119
alexhrao 0:4ffa136585a2 120 int main() {
alexhrao 1:0620ba35d2e8 121 // Set up bluetooth
alexhrao 1:0620ba35d2e8 122 dev.baud(9600);
alexhrao 1:0620ba35d2e8 123 pc.baud(9600);
alexhrao 1:0620ba35d2e8 124 // Tell user we're initializing...
alexhrao 1:0620ba35d2e8 125 lcd_lock.lock();
alexhrao 1:0620ba35d2e8 126 uLCD.cls();
alexhrao 1:0620ba35d2e8 127 uLCD.text_height(2);
alexhrao 1:0620ba35d2e8 128 uLCD.text_string("PLEASE WAIT", 0, 2, FONT_7X8, WHITE);
alexhrao 1:0620ba35d2e8 129 lcd_lock.unlock();
alexhrao 1:0620ba35d2e8 130 // Need to get wifi settings. If we don't have local file, then ask!
alexhrao 1:0620ba35d2e8 131 FILE* fid = NULL;//fopen("/local/settings.txt", "r");
alexhrao 1:0620ba35d2e8 132 // TODO: Get username and password
alexhrao 1:0620ba35d2e8 133 char ssid[256];
alexhrao 1:0620ba35d2e8 134 char pass[256];
alexhrao 1:0620ba35d2e8 135 if (fid == NULL) {
alexhrao 1:0620ba35d2e8 136
alexhrao 1:0620ba35d2e8 137 //fclose(fid);
alexhrao 1:0620ba35d2e8 138 } else {
alexhrao 1:0620ba35d2e8 139 lcd_lock.lock();
alexhrao 1:0620ba35d2e8 140 uLCD.cls();
alexhrao 1:0620ba35d2e8 141 uLCD.text_height(2);
alexhrao 1:0620ba35d2e8 142 uLCD.text_width(2);
alexhrao 1:0620ba35d2e8 143 uLCD.text_string("SEE", 0, 2, FONT_7X8, RED);
alexhrao 1:0620ba35d2e8 144 uLCD.text_string("DEVICE", 0, 5, FONT_7X8, RED);
alexhrao 1:0620ba35d2e8 145 lcd_lock.unlock();
alexhrao 1:0620ba35d2e8 146 // Ask!
alexhrao 1:0620ba35d2e8 147 dev.printf("Please provide the name of a WiFi Network\n");
alexhrao 1:0620ba35d2e8 148 // Wait for them to respond
alexhrao 1:0620ba35d2e8 149 while (!dev.readable()) {
alexhrao 1:0620ba35d2e8 150 wait(0.001);
alexhrao 1:0620ba35d2e8 151 }
alexhrao 1:0620ba35d2e8 152 int ind = 0;
alexhrao 1:0620ba35d2e8 153 // Read response
alexhrao 1:0620ba35d2e8 154 while (ind < 255) {
alexhrao 1:0620ba35d2e8 155 char tmp = dev.getc();
alexhrao 1:0620ba35d2e8 156 if (tmp == '\n' || tmp == '\r') {
alexhrao 1:0620ba35d2e8 157 break;
alexhrao 1:0620ba35d2e8 158 }
alexhrao 1:0620ba35d2e8 159 ssid[ind++] = tmp;
alexhrao 1:0620ba35d2e8 160 wait(0.01);
alexhrao 1:0620ba35d2e8 161 }
alexhrao 1:0620ba35d2e8 162 ssid[ind] = '\0';
alexhrao 1:0620ba35d2e8 163
alexhrao 1:0620ba35d2e8 164 // flush device
alexhrao 1:0620ba35d2e8 165 while (dev.readable()) {
alexhrao 1:0620ba35d2e8 166 dev.getc();
alexhrao 1:0620ba35d2e8 167 wait(0.01);
alexhrao 1:0620ba35d2e8 168 }
alexhrao 1:0620ba35d2e8 169
alexhrao 1:0620ba35d2e8 170 dev.printf("Please provide the password\n");
alexhrao 1:0620ba35d2e8 171 while (!dev.readable()) {
alexhrao 1:0620ba35d2e8 172 wait(0.001);
alexhrao 1:0620ba35d2e8 173 }
alexhrao 1:0620ba35d2e8 174 ind = 0;
alexhrao 1:0620ba35d2e8 175 while (ind < 255) {
alexhrao 1:0620ba35d2e8 176 char tmp = dev.getc();
alexhrao 1:0620ba35d2e8 177 if (tmp == '\n' || tmp == '\r') {
alexhrao 1:0620ba35d2e8 178 break;
alexhrao 1:0620ba35d2e8 179 }
alexhrao 1:0620ba35d2e8 180 pass[ind++] = tmp;
alexhrao 1:0620ba35d2e8 181 wait(0.01);
alexhrao 1:0620ba35d2e8 182 }
alexhrao 1:0620ba35d2e8 183 pass[ind] = '\0';
alexhrao 1:0620ba35d2e8 184 // Because this is a simple proof of concept, we store the password in
alexhrao 1:0620ba35d2e8 185 // plaintext. It should be noted, however, that you **should never do
alexhrao 1:0620ba35d2e8 186 // this in "real life"**
alexhrao 1:0620ba35d2e8 187 //fid = fopen("/local/settings.txt", "w");
alexhrao 1:0620ba35d2e8 188 //fprintf(fid, "ssid: %s\npass: %s\n", ssid, pass);
alexhrao 1:0620ba35d2e8 189 //fclose(fid);
alexhrao 1:0620ba35d2e8 190 }
alexhrao 1:0620ba35d2e8 191
alexhrao 1:0620ba35d2e8 192 bool res = wifi.init();
alexhrao 1:0620ba35d2e8 193 //dev.printf("Reset: %d\n", res);
alexhrao 1:0620ba35d2e8 194 // Set up the WiFi Access Point
alexhrao 1:0620ba35d2e8 195 dev.printf("Connecting to WiFi %s with Password %s\n", ssid, pass);
alexhrao 1:0620ba35d2e8 196 res = wifi.connect("Alex's iPhone", "mbedlookhere");
alexhrao 1:0620ba35d2e8 197 //if (!res) {
alexhrao 1:0620ba35d2e8 198 // dev.printf("Connection Failed... Resetting Device\n");
alexhrao 1:0620ba35d2e8 199 // mbed_reset();
alexhrao 1:0620ba35d2e8 200 //}
alexhrao 1:0620ba35d2e8 201 dev.printf("Is Connected: %d\n", wifi.is_connected());
alexhrao 1:0620ba35d2e8 202 dev.printf("IP Address: %s\n", wifi.getIPAddress());
alexhrao 1:0620ba35d2e8 203 TCPSocketConnection tcp;
alexhrao 1:0620ba35d2e8 204 int tcp_res = tcp.connect("13.58.105.158", 80);
alexhrao 1:0620ba35d2e8 205 dev.printf("Connection: %d\n", tcp_res);
alexhrao 1:0620ba35d2e8 206 // Where are we & What time is it?
alexhrao 1:0620ba35d2e8 207 // Create the TCP connection, request from ipstack. Get the request,
alexhrao 1:0620ba35d2e8 208 // and store the current time (FORMAT?)
alexhrao 1:0620ba35d2e8 209 set_time(1256729737);
alexhrao 1:0620ba35d2e8 210 // Now that we know what time it is, set up our Time Thread
alexhrao 1:0620ba35d2e8 211 time_thread.start(time_updater);
alexhrao 1:0620ba35d2e8 212 // Now, make a single request to nws and get the forecast link - we can
alexhrao 1:0620ba35d2e8 213 // store this link for later!
alexhrao 1:0620ba35d2e8 214 lcd_lock.lock();
alexhrao 1:0620ba35d2e8 215 uLCD.cls();
alexhrao 1:0620ba35d2e8 216 lcd_lock.unlock();
alexhrao 1:0620ba35d2e8 217
alexhrao 1:0620ba35d2e8 218 // Start up weather service!
alexhrao 1:0620ba35d2e8 219 weather_thread.start(weather_updater);
alexhrao 1:0620ba35d2e8 220
alexhrao 1:0620ba35d2e8 221 //weather_ticker.attach(&weather_tick, 900000.0f);
alexhrao 1:0620ba35d2e8 222 // Listen on bluetooth.
alexhrao 1:0620ba35d2e8 223 dev_thread.start(dev_recv);
alexhrao 1:0620ba35d2e8 224 while(true) {
alexhrao 1:0620ba35d2e8 225 err_led = !err_led;
alexhrao 1:0620ba35d2e8 226 //dev.printf("Connection Status: %d\n", wifi.is_connected());
alexhrao 1:0620ba35d2e8 227 wait(0.5);
alexhrao 0:4ffa136585a2 228 }
alexhrao 0:4ffa136585a2 229 }