Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
en:iot:examples:smartventilator [2018/05/11 10:08] rim.puksen:iot:examples:smartventilator [Unknown date] (current) – external edit (Unknown date) 127.0.0.1
Line 1: Line 1:
 ====== Smart ventilator example ====== ====== Smart ventilator example ======
-Coming soon+This example shows how to make a temperature sensor controlled ventilator. You will need 2 controller modules. One 
 +with DHT shield attached and the other with relay shield. 
 + 
 +{{:en:iot:examples:20200610_212520.jpg?300|IoT controllers with DHT and relay shields}} 
 + 
 +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. The following is the code for the controller with relay module.
Line 6: Line 12:
 <code>lib_deps = ITTIoT</code> <code>lib_deps = ITTIoT</code>
 <code c> <code c>
-/+// Includes global variables and librarys that the relay uses
-* 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 <Arduino.h>
 #include <ittiot.h> #include <ittiot.h>
  
-#define RELAY_PIN 5 +#define WIFI_NAME "name" 
-float t; //for holding the received float value+#define WIFI_PASSWORD "password" 
 + 
 +// Change it according to the real name of the red IoT module where 
 +// temperature & humidity shield is connected 
 +#define DHT_TOPIC "ESP30" 
 + 
 +#define RELAY_PIN 5 // The relay has been connected to pin 
 +float t; //for holding the received temperature float value
 float tempLimit = 28; //the temperature limit on what the relay will switch float tempLimit = 28; //the temperature limit on what the relay will switch
  
 +// Message received
 void iot_received(String topic, String msg) void iot_received(String topic, String msg)
 { {
-  Serial.print("MSG FROM USER callback, topic: "); +  t=msg.toFloat(); // Converts received temperature value into a real number
-  Serial.print(topic); +
-  Serial.print(" payload: "); +
-  Serial.println(msg); +
-  t=msg.toFloat();+
  
-  if(topic=="DHT/conf")+  // Check if topic contains configuration data (change the temperature limit value) 
 +  if(topic == (DHT_TOPIC"/conf"))
   {   {
-    tempLimit=t; //re-configures the limit temperature+    //re-configures the temperature limit 
 +    tempLimit=t;
   }   }
  
-  if(topic=="DHT/temp")+  // Check if topic contains temperature data 
 +  if(topic == (DHT_TOPIC"/temp"))
   {   {
 +    // Relay switching according to the temperature limit value
     if(t >= tempLimit)     if(t >= tempLimit)
     {     {
Line 53: Line 54:
 } }
  
 +// Function started after the connection to the server is established.
 void iot_connected() void iot_connected()
 { {
 +  // Send message to serial port to show that connection is established
   Serial.println("MQTT connected callback");   Serial.println("MQTT connected callback");
-  iot.subscribe("temp"); +  // Subscribe to get temperature and temperature configuration messages 
-  iot.subscribe("conf");+  iot.subscribe(DHT_TOPIC"/temp"); 
 +  iot.subscribe(DHT_TOPIC"/conf"); 
 +  // Send message to MQTT server to show that connection is established
   iot.log("Ventilator example!");   iot.log("Ventilator example!");
 } }
Line 63: Line 68:
 void setup() void setup()
 { {
-  Serial.begin(115200);+  // Initialize serial port and send message 
 +  Serial.begin(115200); // setting up serial connection parameter
   Serial.println("Booting");   Serial.println("Booting");
  
-  iot.printConfig(); // print json config to serial //Peale Serial.begin ja enne iot.setup +  //iot.setConfig("wname", WIFI_NAME); 
-  iot.setup(); +  //iot.setConfig("wpass", WIFI_PASSWORD); 
-  pinMode(RELAY_PIN, OUTPUT);+  iot.printConfig(); // print IoT json config to serial 
 +  iot.setup(); // Initialize IoT library 
 + 
 +  pinMode(RELAY_PIN, OUTPUT); // The relay pin is defined as output type
 } }
  
 void loop() void loop()
 { {
-  iot.handle(); +  iot.handle(); // IoT behind the plan work, it should be periodically called 
-  delay(200);+  delay(20); // Wait 0.2 second
 } }
 +
 </code> </code>
  
Line 81: Line 91:
 Required libraries: Required libraries:
 <code>lib_deps = ITTIoT, DHT sensor library, Adafruit Unified Sensor</code> <code>lib_deps = ITTIoT, DHT sensor library, Adafruit Unified Sensor</code>
 +<fc #ff0000>After programming, if NAN appears instead of readings, the USB cable should be disconnected and reconnected!</fc>
 <code c> <code c>
-/+// Includes global variables and librarys that the DHT uses
-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 <Arduino.h>
 #include <ittiot.h> #include <ittiot.h>
 +#include <Ticker.h>
 #include <DHT.h> #include <DHT.h>
-#define DHTPIN D4     // what pin we're connected to+ 
 +#define WIFI_NAME "name" 
 +#define WIFI_PASSWORD "password" 
 + 
 +#define DHTPIN D3     // Pin where DHT shield is connected. Change this to D4 if the shield has no legs removed.
 #define DHTTYPE DHT22   // DHT 22  (AM2302) #define DHTTYPE DHT22   // DHT 22  (AM2302)
  
 +// Create an object for DHT sensor
 DHT dht(DHTPIN, DHTTYPE); DHT dht(DHTPIN, DHTTYPE);
  
 +// Create an object for Ticker library
 +Ticker timeTicker;
 +
 +bool sendDataFlag;
 +
 +// Ticker library callback, which will occur 0.5 second interval.
 +void sendData()
 +{
 +  sendDataFlag=true;
 +}
 +
 +// Function started after the connection to the server is established.
 void iot_connected() void iot_connected()
 { {
 +  // Send message to serial port to show that connection is established
   Serial.println("MQTT connected callback");   Serial.println("MQTT connected callback");
 +  // Send message to MQTT server to show that connection is established
   iot.log("IoT DHT example!");   iot.log("IoT DHT example!");
 } }
Line 108: Line 130:
 void setup() void setup()
 { {
-  Serial.begin(115200);+  // Initialize serial port and send message 
 +  Serial.begin(115200); // setting up serial connection parameter
   Serial.println("Booting");   Serial.println("Booting");
-  iot.printConfig(); // print json config to serial //Peale Serial.begin ja enne iot.setup + 
-  iot.setup(); +  //iot.setConfig("wname", WIFI_NAME); 
-  pinMode(16, OUTPUT);+  //iot.setConfig("wpass", WIFI_PASSWORD); 
 +  iot.printConfig(); // print IoT json config to serial 
 +  iot.setup(); // Initialize IoT library 
 + 
 +  // Initialize DHT library
   dht.begin();   dht.begin();
  
 +  // Initialize Ticker interval and callback
 +  timeTicker.attach(1, sendData);
 } }
  
 void loop() void loop()
 { {
-  iot.handle();+  iot.handle(); // IoT behind the plan work, it should be periodically called
  
-  float h = dht.readHumidity(); +  if(sendDataFlag) 
- float t = dht.readTemperature();+  { 
 +    sendDataFlag = false; 
 +    // Read humidity and temperature 
 +    float h = dht.readHumidity(); 
 +    float t = dht.readTemperature();
  
-  char buf[10]; // Put whatever length you need here +    // Create a buffer to store strings to being sent later 
- String(t).toCharArray(buf,10);+    char buf[10];
  
-  digitalWrite(16,HIGH); +    // Convert temperature value messages to strings and send to the MQTT server 
-  iot.publishMsg("temp",buf); +    String(t).toCharArray(buf,10); 
- delay(2000); +    iot.publishMsg("temp",buf);
-  digitalWrite(16,LOW);+
  
- String(h).toCharArray(buf,10); +    // Convert humidity value messages to strings and send to the MQTT server 
-  iot.publishMsg("hum",buf); +    String(h).toCharArray(buf,10); 
- delay(2000);+    iot.publishMsg("hum",buf); 
 +  }
 } }
 +
 </code> </code>
  
en/iot/examples/smartventilator.1526033323.txt.gz · Last modified: 2020/07/20 09:00 (external edit)
CC Attribution-Share Alike 4.0 International
www.chimeric.de Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0