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.
Revision:
3:7bf41989ff8f
Parent:
2:f7d19812bdc5
Child:
4:55f0c303f56a
--- a/main.cpp	Sun Mar 31 00:53:59 2019 +0000
+++ b/main.cpp	Sun Mar 31 03:08:51 2019 +0000
@@ -3,6 +3,10 @@
 #include "ESP8266Interface.h"
 #include "TCPSocketConnection.h"
 #include "rtos.h"
+#include "MbedJSONValue.h"
+#include <string>
+
+#define BUF_SIZE 4096
 
 // We need this for being able to reset the MBED (similar to watch dogs)
 extern "C" void mbed_reset();
@@ -30,7 +34,12 @@
 
 // Weather
 Thread weather_thread;
-char* forecast_link;
+char weather_api_key[256];
+
+// Location
+char ip_api_key[256];
+double latitude = 0;
+double longitude = 0;
 
 void time_updater() {
     // We're not an interrupt, so take as much time as we need. Infinite loop
@@ -70,8 +79,34 @@
 
 void weather_updater() {
     // We can take as long as we want
+    return;
     while (true) {
         // get the weather
+        // first get the current weather
+        // Weather data is _long_
+        
+        char forecast_buf[2];
+        TCPSocketConnection sck;
+        // http://api.openweathermap.org/data/2.5/forecast?lat=33.7485&lon=-84.3871&appid=6971e1ebfcc60f29c8dcc617c532b1b6
+        sck.connect("api.openweathermap.org", 80);
+        char cmd[1024];
+        sprintf(cmd,
+            "GET /data/2.5/weather?lat=%0.4f&lon=%0.4f&APPID=%s\r\nHost: api.openweathermap.org\r\n\r\n",
+            latitude, longitude, weather_api_key);
+        sck.send_all(cmd, strlen(cmd));
+        wait(10);
+        int buf_len = wifi.recv(forecast_buf, 16384 - 1, 0);
+        forecast_buf[buf_len] = '\0';
+        
+        // Get current weather
+        char current_buf[8192];
+        sprintf(cmd,
+            "GET /data/2.5/forecast?lat=%0.4f&lon=%0.4f&APPID=%s\r\nHost: api.openweathermap.org\r\n\r\n",
+            latitude, longitude, weather_api_key);
+        sck.send_all(cmd, strlen(cmd));
+        wait(10);
+        buf_len = wifi.recv(current_buf, 8192 - 1, 0);
+        current_buf[buf_len] = '\0';
         // we'll always want to update the LCD - don't worry about the previous
         // weather
         int curr_temp = 0;
@@ -87,7 +122,6 @@
         uLCD.text_string(buf, 0, 5, FONT_7X8, WHITE);
         // done! unlock
         lcd_lock.unlock();
-        Thread::wait(3600);
     }
 }
 
@@ -134,6 +168,7 @@
     char ssid[256];
     char pass[256];
     char api_key[256];
+    
     if (true) {
         
     } else if (fid != NULL) {
@@ -235,7 +270,10 @@
         //fprintf(fid, "%s\n%s\n%s\n", ssid, pass, api_key);
         //fclose(fid);
     }
-    
+    char* tmp = "af9319bf6435ddd9bb640f763ff64d34";
+    for (int i = 0; i < strlen(tmp); i++) {
+        api_key[i] = tmp[i];
+    }
     bool res = wifi.init();
     //dev.printf("Reset: %d\n", res);
     // Set up the WiFi Access Point
@@ -249,8 +287,57 @@
     dev.printf("Connected with IP Address: %s\n", wifi.getIPAddress());
     
     // Get the time & location
-    dev.printf("Getting the current time & location...\n");
+    dev.printf("Getting the current location...\n");
+    TCPSocketConnection tcp;
+    tcp.connect("api.ipstack.com", 80);
+    
+    char tcp_cmd[256];
+    sprintf(tcp_cmd, 
+        "GET /check?access_key=af9319bf6435ddd9bb640f763ff64d34 HTTP/1.1\r\nHost: api.ipstack.com\r\n\r\n");
+    tcp.send_all(tcp_cmd, strlen(tcp_cmd));
+    
+    wait(10);
+    
+    char buffer[BUF_SIZE];
+    int read_len = wifi.recv(buffer, BUF_SIZE - 1, 0);
+    buffer[read_len] = '\0';
+    
+    MbedJSONValue parser;
+    parse(parser, buffer);
+    // for now, just print...
     
+    latitude = parser["latitude"].get<double>();
+    longitude = parser["longitude"].get<double>();
+
+    /*
+    TCPSocketConnection weather_sck;
+    //http://api.openweathermap.org/data/2.5/weather?q=London,uk&APPID=6971e1ebfcc60f29c8dcc617c532b1b6
+    //http://api.openweathermap.org/data/2.5/forecast?lat=33.7485&lon=-84.3871&appid=6971e1ebfcc60f29c8dcc617c532b1b6
+    weather_sck.connect("api.openweathermap.org", 80);
+    sprintf(tcp_cmd,
+        "GET /data/2.5/weather?lat=%0.4f&lon=%0.4f&APPID=%s\r\nHost: api.openweathermap.org\r\n\r\n",
+        lat, lng, "6971e1ebfcc60f29c8dcc617c532b1b6");
+    sprintf(weather_api_key,
+        "%s", "6971e1ebfcc60f29c8dcc617c532b1b6");
+    weather_sck.send_all(tcp_cmd, strlen(tcp_cmd));
+    wait(15);
+    int read_len = wifi.recv(buffer, BUF_SIZE - 1, 0);
+    buffer[read_len] = '\0';
+    dev.printf(buffer);
+    */
+    // dev.printf("Connection: %d\n", con_res);
+    //sprintf
+    //int con_res = weather_sck.connect("api.weather.gov", 443);
+    //    dev.printf("Connection: %d\n", con_res);
+    //sprintf(tcp_cmd,
+    //    "GET /points/%0.4f,%0.4f HTTP/1.1\r\nHost: api.weather.gov\r\n\r\n",
+    //    lat, lng);
+    //weather_sck.send_all(tcp_cmd, strlen(tcp_cmd));
+    //wait(5);
+    //wait(10);
+    //int read_len = wifi.recv(buffer, BUF_SIZE - 1, 0);
+    //buffer[read_len] = '\0';
+    //dev.printf(buffer);
     set_time(1256729737);
     
     // Clear the LCD Screen first
@@ -262,53 +349,25 @@
     time_thread.start(time_updater);
     
     // Make the request to get the forecast link
-    TCPSocketConnection tcp;
-    int tcp_res = tcp.connect("alexhrao.com", 80);
-    dev.printf("Connection: %d\n", tcp_res);
-    //int sent = tcp.send("GET / HTTP/1.1\r\nHost: alexhrao.com\r\nUser-Agent: curl/7.54.0\r\nAccept: */*", 73);
-    tcp.set_blocking(true, 10000);
-    char* cmd = "GET / HTTP/1.1\r\nHost: alexhrao.com\r\n\r\n";
-    int sent = tcp.send_all(cmd, strlen(cmd));
-    dev.printf("Sent %d\n", sent);
-    char buf[128];
-    for (int i = 0; i < 32; i++) {
-        buf[i] = 0;
-    }
-//    wifi.send(cmd, strlen(cmd));
-//    wifi.recv(buf, 2048);
-//wait(15);
-    //tcp.receive(buf, 16);
-    int to_rec = 7;
-    //wifi.getc();
-    while (true) {
-        wifi.recv(buf, &to_rec);
-        dev.printf("To Receive: %d\n", to_rec);
-        to_rec = 7;
-        buf[7] = '\0';
-        dev.printf("%s", buf);
-        wait(15);
-    }
-    wifi.recv(buf, &to_rec);
-    dev.printf("%s\n", buf);
-    wifi.recv(buf, &to_rec);
-    int recv2 = tcp.receive_all(buf, 4);
-    dev.printf("Received (%d):\n%s\n", recv2, buf);
-    // Where are we & What time is it?
-    // Create the TCP connection, request from ipstack. Get the request,
-    // and store the current time (FORMAT?)
-    set_time(1256729737);
-    // Now that we know what time it is, set up our Time Thread
-    time_thread.start(time_updater);
+
     // Now, make a single request to nws and get the forecast link - we can 
     // store this link for later!
     
     // Start up weather service!
-    weather_thread.start(weather_updater);
+    //weather_thread.start(weather_updater);
     
     //weather_ticker.attach(&weather_tick, 900000.0f);
     // Listen on bluetooth.
     dev_thread.start(dev_recv);
+    dev.printf("Hello, World!\n");
+    time_t prev_time = time(NULL);
     while(true) {
+        time_t curr_time = time(NULL);
+        // if it's been an hour (divide by 3600 >= 60), pause and get weather:
+        if (((int)(curr_time - prev_time) / 3600) >= 60) {
+            //weather_updater();
+            prev_time = curr_time;
+        }
         err_led = !err_led;
         //dev.printf("Connection Status: %d\n", wifi.is_connected());
         wait(0.5);