lbreda

Netatmo

Jan 21st, 2021
684
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /**********************************************************************
  2.  
  3.  Netatmo display for NodeMCU
  4.  Lorenzo Breda <lorenzo@lbreda.com>
  5.  
  6. /**********************************************************************/
  7.  
  8. // Display libs
  9. #include <Adafruit_ST7735.h>
  10. #include <SPI.h>
  11. #include "Fonts/LiberationSans_Regular5pt7b.h"
  12. #include "Fonts/LiberationSans_Regular18pt7b.h"
  13. #define FONT_SMALL &LiberationSans_Regular5pt7b
  14. #define FONT_BIG &LiberationSans_Regular18pt7b
  15.  
  16. // WiFi libs
  17. #include <SoftwareSerial.h>
  18. #include <ESP8266WiFi.h>
  19.  
  20. // WiFiManager libs
  21. #include <DNSServer.h>            //Local DNS Server used for redirecting all requests to the configuration portal
  22. #include <ESP8266WebServer.h>     //Local WebServer used to serve the configuration portal
  23. #include <WiFiManager.h>
  24.  
  25. // JSON libs
  26. #include <ArduinoJson.h>
  27.  
  28. // WiFi settings
  29. #define WIFI_CONFIG_SSID "NetatmoDisplay"
  30. #define WIFI_SSID "Blatero"
  31. #define WIFI_PASS "ponyosullascogliera42"
  32.  
  33. // Netatmo host
  34. #define NETATMO_HOST "netatmo.lbreda.com"
  35.  
  36. // Display pinout settings
  37. #define TFT_CS    D2     // TFT CS  pin is connected to NodeMCU pin D2
  38. #define TFT_RST   D3     // TFT RST pin is connected to NodeMCU pin D3
  39. #define TFT_DC    D4     // TFT DC  pin is connected to NodeMCU pin D4
  40.  
  41. // initialize TFT library with hardware SPI module
  42. Adafruit_ST7735 tft = Adafruit_ST7735(TFT_CS, TFT_DC, TFT_RST);
  43.  
  44. // Initialize WiFi
  45. //SoftwareSerial swSerial(sw_serial_rx_pin, sw_serial_tx_pin);
  46. WiFiClient client;
  47.  
  48. // JSON
  49. DynamicJsonDocument doc(1024);
  50. const char* intTemp = "--.-";
  51. const char* intHumidity = "---";
  52. const char* intPressure = "----.-";
  53. const char* intCO2 = "----";
  54. const char* extTemp = "--.-";
  55. const char* extHumidity = "---";
  56.  
  57. // Prints data on display
  58. void display_print(int baseX, int baseY, int tabX, uint16_t bgColor, uint16_t fgColor, char* title, const char* temperature, const char* humidity, const char* pressure = "", const char* co2 = "") {
  59.   tft.fillRect(baseX, baseY, 128, 80, bgColor);
  60.   tft.setTextColor(fgColor);
  61.   tft.setFont(FONT_SMALL);
  62.   tft.setCursor(baseX+1, baseY+9);
  63.   tft.println(title);
  64.   tft.setCursor(baseX+1, baseY+35);
  65.   tft.setFont(FONT_BIG);
  66.   tft.print(temperature);
  67.   tft.drawCircle(tft.getCursorX()+4, baseY+15, 3, fgColor);
  68.   tft.setCursor(tft.getCursorX()+7, baseY+35);
  69.   tft.println("C");
  70.   tft.setCursor(baseX+1, baseY+45);
  71.   tft.setFont(FONT_SMALL);
  72.   tft.print("Hum:"); tft.setCursor(tabX, tft.getCursorY()); tft.print(humidity); tft.println("%");
  73.   if(strcmp(pressure, "") != 0) { tft.print("Press:"); tft.setCursor(tabX, tft.getCursorY()); tft.print(pressure); tft.println("mbar"); }
  74.   if(strcmp(co2, "") != 0) { tft.print("CO2:"); tft.setCursor(tabX, tft.getCursorY()); tft.print(co2); tft.println("ppm"); }
  75. }
  76.  
  77. // Callback for the board config mode
  78. void configModeCallback (WiFiManager *myWiFiManager) {
  79.   tft.println("Entered config mode");
  80.   tft.print("Connect a PC to the '"); tft.print(WIFI_CONFIG_SSID); tft.println("' WiFi network and open a browser to configure");
  81. }
  82.  
  83. void setup() {
  84.   Serial.begin(9600);
  85.  
  86.   // Screen initialization
  87.   tft.initR(INITR_BLACKTAB);
  88.   tft.setRotation(0);  // 0 - Portrait, 1 - Lanscape
  89.   tft.fillScreen(ST7735_BLACK);
  90.   tft.setTextWrap(true);
  91.   tft.setTextSize(1);
  92.   tft.setFont(&LiberationSans_Regular5pt7b);
  93.  
  94.   // Wifi connection
  95.   WiFiManager wifiManager;
  96.   wifiManager.setAPCallback(configModeCallback);
  97.   if(!wifiManager.autoConnect(WIFI_CONFIG_SSID)){
  98.     Serial.println("Failed to connect WiFi and hit timeout");
  99.     ESP.reset();
  100.     delay(1000);
  101.   }
  102.   tft.println("WiFi connected.");
  103.   tft.println("IP: ");
  104.   tft.println(WiFi.localIP());
  105.   delay(1000);
  106. }
  107.  
  108.  
  109. void loop(void) {  
  110.   // Connect to the host
  111.   if (!client.connect(NETATMO_HOST, 80)) {
  112.     tft.setCursor(0, 0);
  113.     tft.fillScreen(ST7735_BLACK);
  114.     tft.println("connection failed");
  115.     return;
  116.   }
  117.   // Sends the data
  118.   client.print(String("GET /") + " HTTP/1.1\r\n" +
  119.            "Host: " + NETATMO_HOST + "\r\n" +
  120.            "Connection: close\r\n" +
  121.            "\r\n"
  122.           );
  123.  
  124.   // Reads the JSON response and fills the variables
  125.   while (client.connected())
  126.   {
  127.     if (client.available())
  128.     {
  129.       String line = client.readStringUntil('\n');
  130.       if (line.startsWith("{")) {
  131.         deserializeJson(doc, line);
  132.         Serial.println(line);
  133.         intTemp = doc["InternalTemperature"].as<char*>();
  134.         intHumidity = doc["InternalHumidity"].as<char*>();
  135.         intPressure = doc["InternalPressure"].as<char*>();
  136.         intCO2 = doc["InternalCO2"].as<char*>();
  137.         extTemp = doc["ExternalTemperature"].as<char*>();
  138.         extHumidity = doc["ExternalHumidity"].as<char*>();
  139.       }
  140.     }
  141.   }
  142.  
  143.   // Clears the screen
  144.   digitalWrite(D8, HIGH);
  145.   tft.fillScreen(ST77XX_BLACK);
  146.  
  147.   display_print(0, 0, 40, ((atof(intTemp) > atof(extTemp)) ? ST77XX_ORANGE : ST77XX_CYAN), ST77XX_BLACK, "Internal", intTemp, intHumidity, intPressure, intCO2);
  148.   display_print(0, 80, 40, ((atof(intTemp) <= atof(extTemp)) ? ST77XX_ORANGE : ST77XX_CYAN), ST77XX_BLACK, "External", extTemp, extHumidity);
  149.  
  150.   delay(5000); digitalWrite(D8, LOW);
  151.    
  152.   delay(30000);
  153. }
RAW Paste Data