This is an old revision of the document!
This example shows how to make a temperature sensor controller ventilator. You will need 2 controller modules. One with DHT shield attached and the other with relay shield.
Once the code has been uploaded, the DHT controller will publish the temperature and humidity values to “temp” and “hum” every 2 seconds. The relay module subscribes to topics “temp” and “conf”. The relay controller will switch the relay if the temperature rises above the set limit (default:28 degrees). To change the set limit, send a new value to the “conf” topic. (For example: “25.5”)
The following is the code for the controller with relay module. Required libraries:
lib_deps = ITTIoT
/* * Ventilator example - relay shield program * * This program is for the controller with relay shield. It is meant to work together with * the Ventilator - DHT shield program. * The controller subscribes to topics "temp" and "conf". The "temp" topic is meant for temperature data. * If the received temperature is higher then tempLimit (default 28 degrees) it will switch on the relay. * If the temperature goes under the limit again, the relay is switched off. * For changing the tempLimit, send new limit to topic "conf" (for example: "26.3"). * * Author: Rim Puks * May 2018 */ #include <Arduino.h> #include <ittiot.h> #define RELAY_PIN 5 float t; //for holding the received float value float tempLimit = 28; //the temperature limit on what the relay will switch void iot_received(String topic, String msg) { Serial.print("MSG FROM USER callback, topic: "); Serial.print(topic); Serial.print(" payload: "); Serial.println(msg); t=msg.toFloat(); if(topic=="DHT/conf") { tempLimit=t; //re-configures the limit temperature } if(topic=="DHT/temp") { if(t >= tempLimit) { digitalWrite(RELAY_PIN, HIGH); } else { digitalWrite(RELAY_PIN, LOW); } } } void iot_connected() { Serial.println("MQTT connected callback"); iot.subscribe("temp"); iot.subscribe("conf"); iot.log("Ventilator example!"); } void setup() { Serial.begin(115200); Serial.println("Booting"); iot.printConfig(); // print json config to serial //Peale Serial.begin ja enne iot.setup iot.setup(); pinMode(RELAY_PIN, OUTPUT); } void loop() { iot.handle(); delay(200); }
The following is the program code for the controller with DHT module. Required libraries:
lib_deps = ITTIoT, DHT sensor library, Adafruit Unified Sensor
/* Smart ventilator - DHT shield program code This program publishes temperature and humidity values once every 2 seconds. Data is published to "hum" and "temp" accordingly. Author: Heiko Pikner May 2018 */ #include <Arduino.h> #include <ittiot.h> #include <DHT.h> #define DHTPIN D4 // what pin we're connected to #define DHTTYPE DHT22 // DHT 22 (AM2302) DHT dht(DHTPIN, DHTTYPE); void iot_connected() { Serial.println("MQTT connected callback"); iot.log("IoT DHT example!"); } void setup() { Serial.begin(115200); Serial.println("Booting"); iot.printConfig(); // print json config to serial //Peale Serial.begin ja enne iot.setup iot.setup(); pinMode(16, OUTPUT); dht.begin(); } void loop() { iot.handle(); float h = dht.readHumidity(); float t = dht.readTemperature(); char buf[10]; // Put whatever length you need here String(t).toCharArray(buf,10); digitalWrite(16,HIGH); iot.publishMsg("temp",buf); delay(2000); digitalWrite(16,LOW); String(h).toCharArray(buf,10); iot.publishMsg("hum",buf); delay(2000); }