ArduinoESP32 DevKit V1Lab virtual

ESP32: weather to the cloud

Wi-Fi, a DHT22 and an I2C LCD; readings to ARGYNIX Cloud, its LED switched from Connect.

ESP32: weather to the cloud in Argynix Lab

The code

sketch.inoC++
// ESP32 on Wi-Fi, live in ARGYNIX Cloud. A DHT22 on GPIO 4 is read every 5 s, shown on
// an I2C LCD (SDA 21, SCL 22) and sent to the cloud. Link the project (Live in Cloud,
// top right) to a device from Connect; a switch there sending "setLed" lights GPIO 2.
#include <WiFi.h>
#include <DHT.h>
#include <LiquidCrystal_I2C.h>
#include <ArgynixCloud.h>

DHT dht(4, DHT22);
LiquidCrystal_I2C lcd(0x27, 16, 2);
ArgynixCloud cloud;
unsigned long last = 0;

void onCommand(const char *method, const char *params) {
  if (strcmp(method, "setLed") == 0) {
    bool on = strcmp(params, "true") == 0 || strcmp(params, "1") == 0;
    digitalWrite(LED_BUILTIN, on);
    cloud.add("led", on);
    cloud.publish();
  }
}

void setup() {
  Serial.begin(115200);
  pinMode(LED_BUILTIN, OUTPUT);
  lcd.init();
  lcd.backlight();
  lcd.print("Joining Wi-Fi...");
  WiFi.begin("ARGYNIX-Lab", "");
  while (WiFi.status() != WL_CONNECTED) delay(100);
  Serial.print("Wi-Fi up, IP ");
  Serial.println(WiFi.localIP());
  dht.begin();
  cloud.onCommand(onCommand);
}

void loop() {
  cloud.loop();
  if (last == 0 || millis() - last >= 5000) {
    last = millis() | 1;
    float t = dht.readTemperature();
    float h = dht.readHumidity();
    if (!isnan(t) && !isnan(h)) {
      lcd.clear();
      lcd.print("Temp ");
      lcd.print(t, 1);
      lcd.print((char)223);
      lcd.print("C");
      lcd.setCursor(0, 1);
      lcd.print("Humidity ");
      lcd.print(h, 0);
      lcd.print("%");
      cloud.add("temperature", t, 1);
      cloud.add("humidity", h, 1);
      cloud.add("rssi", (long)WiFi.RSSI());
      cloud.publish();
    }
  }
  delay(10);
}