ArduinoArduino UnoExact chip

Live in ARGYNIX Cloud

A DHT22 sends to your Connect dashboards; Connect switches the LED.

Live in ARGYNIX Cloud in Argynix Lab

The code

sketch.inoC++
// This board, live in ARGYNIX Cloud. Link it (Live in Cloud, top right) to a device
// from Connect, press Run, and its readings reach that device's dashboards. A switch
// in Connect sending "setLed" turns the LED on pin 13 on and off.
#include <DHT.h>
#include <ArgynixCloud.h>

DHT dht(2, DHT22);
ArgynixCloud cloud;
const int LED = 13;
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, on ? HIGH : LOW);
    cloud.add("led", on);
    cloud.publish();
    Serial.println(on ? "LED on, from the cloud" : "LED off, from the cloud");
  }
}

void setup() {
  Serial.begin(9600);
  pinMode(LED, OUTPUT);
  dht.begin();
  cloud.onCommand(onCommand);
  Serial.println("Sending a reading every 5 s");
}

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)) {
      cloud.add("temperature", t, 1);
      cloud.add("humidity", h, 1);
      cloud.publish();
      Serial.print("Sent ");
      Serial.print(t, 1);
      Serial.print(" C, ");
      Serial.print(h, 0);
      Serial.println(" %");
    }
  }
}