Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**********************************************************************
- Netatmo display for NodeMCU
- Lorenzo Breda <lorenzo@lbreda.com>
- /**********************************************************************/
- // Display libs
- #include <Adafruit_ST7735.h>
- #include <SPI.h>
- #include "Fonts/LiberationSans_Regular5pt7b.h"
- #include "Fonts/LiberationSans_Regular18pt7b.h"
- #define FONT_SMALL &LiberationSans_Regular5pt7b
- #define FONT_BIG &LiberationSans_Regular18pt7b
- // WiFi libs
- #include <SoftwareSerial.h>
- #include <ESP8266WiFi.h>
- // WiFiManager libs
- #include <DNSServer.h> //Local DNS Server used for redirecting all requests to the configuration portal
- #include <ESP8266WebServer.h> //Local WebServer used to serve the configuration portal
- #include <WiFiManager.h>
- // JSON libs
- #include <ArduinoJson.h>
- // WiFi settings
- #define WIFI_CONFIG_SSID "NetatmoDisplay"
- #define WIFI_SSID "Blatero"
- #define WIFI_PASS "ponyosullascogliera42"
- // Netatmo host
- #define NETATMO_HOST "netatmo.lbreda.com"
- // Display pinout settings
- #define TFT_CS D2 // TFT CS pin is connected to NodeMCU pin D2
- #define TFT_RST D3 // TFT RST pin is connected to NodeMCU pin D3
- #define TFT_DC D4 // TFT DC pin is connected to NodeMCU pin D4
- // initialize TFT library with hardware SPI module
- Adafruit_ST7735 tft = Adafruit_ST7735(TFT_CS, TFT_DC, TFT_RST);
- // Initialize WiFi
- //SoftwareSerial swSerial(sw_serial_rx_pin, sw_serial_tx_pin);
- WiFiClient client;
- // JSON
- DynamicJsonDocument doc(1024);
- const char* intTemp = "--.-";
- const char* intHumidity = "---";
- const char* intPressure = "----.-";
- const char* intCO2 = "----";
- const char* extTemp = "--.-";
- const char* extHumidity = "---";
- // Prints data on display
- 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 = "") {
- tft.fillRect(baseX, baseY, 128, 80, bgColor);
- tft.setTextColor(fgColor);
- tft.setFont(FONT_SMALL);
- tft.setCursor(baseX+1, baseY+9);
- tft.println(title);
- tft.setCursor(baseX+1, baseY+35);
- tft.setFont(FONT_BIG);
- tft.print(temperature);
- tft.drawCircle(tft.getCursorX()+4, baseY+15, 3, fgColor);
- tft.setCursor(tft.getCursorX()+7, baseY+35);
- tft.println("C");
- tft.setCursor(baseX+1, baseY+45);
- tft.setFont(FONT_SMALL);
- tft.print("Hum:"); tft.setCursor(tabX, tft.getCursorY()); tft.print(humidity); tft.println("%");
- if(strcmp(pressure, "") != 0) { tft.print("Press:"); tft.setCursor(tabX, tft.getCursorY()); tft.print(pressure); tft.println("mbar"); }
- if(strcmp(co2, "") != 0) { tft.print("CO2:"); tft.setCursor(tabX, tft.getCursorY()); tft.print(co2); tft.println("ppm"); }
- }
- // Callback for the board config mode
- void configModeCallback (WiFiManager *myWiFiManager) {
- tft.println("Entered config mode");
- tft.print("Connect a PC to the '"); tft.print(WIFI_CONFIG_SSID); tft.println("' WiFi network and open a browser to configure");
- }
- void setup() {
- Serial.begin(9600);
- // Screen initialization
- tft.initR(INITR_BLACKTAB);
- tft.setRotation(0); // 0 - Portrait, 1 - Lanscape
- tft.fillScreen(ST7735_BLACK);
- tft.setTextWrap(true);
- tft.setTextSize(1);
- tft.setFont(&LiberationSans_Regular5pt7b);
- // Wifi connection
- WiFiManager wifiManager;
- wifiManager.setAPCallback(configModeCallback);
- if(!wifiManager.autoConnect(WIFI_CONFIG_SSID)){
- Serial.println("Failed to connect WiFi and hit timeout");
- ESP.reset();
- delay(1000);
- }
- tft.println("WiFi connected.");
- tft.println("IP: ");
- tft.println(WiFi.localIP());
- delay(1000);
- }
- void loop(void) {
- // Connect to the host
- if (!client.connect(NETATMO_HOST, 80)) {
- tft.setCursor(0, 0);
- tft.fillScreen(ST7735_BLACK);
- tft.println("connection failed");
- return;
- }
- // Sends the data
- client.print(String("GET /") + " HTTP/1.1\r\n" +
- "Host: " + NETATMO_HOST + "\r\n" +
- "Connection: close\r\n" +
- "\r\n"
- );
- // Reads the JSON response and fills the variables
- while (client.connected())
- {
- if (client.available())
- {
- String line = client.readStringUntil('\n');
- if (line.startsWith("{")) {
- deserializeJson(doc, line);
- Serial.println(line);
- intTemp = doc["InternalTemperature"].as<char*>();
- intHumidity = doc["InternalHumidity"].as<char*>();
- intPressure = doc["InternalPressure"].as<char*>();
- intCO2 = doc["InternalCO2"].as<char*>();
- extTemp = doc["ExternalTemperature"].as<char*>();
- extHumidity = doc["ExternalHumidity"].as<char*>();
- }
- }
- }
- // Clears the screen
- digitalWrite(D8, HIGH);
- tft.fillScreen(ST77XX_BLACK);
- display_print(0, 0, 40, ((atof(intTemp) > atof(extTemp)) ? ST77XX_ORANGE : ST77XX_CYAN), ST77XX_BLACK, "Internal", intTemp, intHumidity, intPressure, intCO2);
- display_print(0, 80, 40, ((atof(intTemp) <= atof(extTemp)) ? ST77XX_ORANGE : ST77XX_CYAN), ST77XX_BLACK, "External", extTemp, extHumidity);
- delay(5000); digitalWrite(D8, LOW);
- delay(30000);
- }
RAW Paste Data