This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| en:iot:examples:smartventilator [2018/05/11 10:08] – rim.puks | en: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. | ||
| + | |||
| + | {{: | ||
| + | |||
| + | Once the code has been uploaded, the DHT controller will publish the temperature and humidity values to " | ||
| 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 c> | <code c> | ||
| - | /* | + | // Includes global variables |
| - | * 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 " | + | |
| - | * 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 " | + | |
| - | * | + | |
| - | * Author: Rim Puks | + | |
| - | * May 2018 | + | |
| - | */ | + | |
| #include < | #include < | ||
| #include < | #include < | ||
| - | #define RELAY_PIN 5 | + | #define WIFI_NAME " |
| - | float t; //for holding the received float value | + | #define WIFI_PASSWORD " |
| + | |||
| + | // Change it according to the real name of the red IoT module where | ||
| + | // temperature & humidity shield is connected | ||
| + | #define DHT_TOPIC " | ||
| + | |||
| + | #define RELAY_PIN | ||
| + | float t; //for holding the received | ||
| 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) | ||
| { | { | ||
| - | | + | t=msg.toFloat(); |
| - | Serial.print(topic); | + | |
| - | Serial.print(" | + | |
| - | Serial.println(msg); | + | |
| - | | + | |
| - | if(topic==" | + | |
| + | | ||
| { | { | ||
| - | | + | // |
| + | tempLimit=t; | ||
| } | } | ||
| - | if(topic==" | + | |
| + | | ||
| { | { | ||
| + | // 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(" | Serial.println(" | ||
| - | iot.subscribe(" | + | |
| - | iot.subscribe(" | + | |
| + | iot.subscribe(DHT_TOPIC"/conf" | ||
| + | // Send message to MQTT server to show that connection is established | ||
| iot.log(" | iot.log(" | ||
| } | } | ||
| Line 63: | Line 68: | ||
| void setup() | void setup() | ||
| { | { | ||
| - | Serial.begin(115200); | + | |
| + | | ||
| Serial.println(" | Serial.println(" | ||
| - | iot.printConfig(); | + | |
| - | iot.setup(); | + | // |
| - | pinMode(RELAY_PIN, | + | |
| + | iot.setup(); | ||
| + | |||
| + | pinMode(RELAY_PIN, | ||
| } | } | ||
| void loop() | void loop() | ||
| { | { | ||
| - | iot.handle(); | + | iot.handle(); |
| - | delay(200); | + | delay(20); // Wait 0.2 second |
| } | } | ||
| + | |||
| </ | </ | ||
| Line 81: | Line 91: | ||
| Required libraries: | Required libraries: | ||
| < | < | ||
| + | <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 " | + | |
| - | + | ||
| - | Author: Heiko Pikner | + | |
| - | May 2018 | + | |
| - | */ | + | |
| #include < | #include < | ||
| #include < | #include < | ||
| + | #include < | ||
| #include < | #include < | ||
| - | #define DHTPIN | + | |
| + | #define WIFI_NAME " | ||
| + | #define WIFI_PASSWORD " | ||
| + | |||
| + | #define DHTPIN | ||
| #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(" | Serial.println(" | ||
| + | // Send message to MQTT server to show that connection is established | ||
| iot.log(" | iot.log(" | ||
| } | } | ||
| Line 108: | Line 130: | ||
| void setup() | void setup() | ||
| { | { | ||
| - | Serial.begin(115200); | + | |
| + | | ||
| Serial.println(" | Serial.println(" | ||
| - | | + | |
| - | iot.setup(); | + | // |
| - | | + | // |
| + | | ||
| + | iot.setup(); | ||
| + | |||
| + | | ||
| dht.begin(); | dht.begin(); | ||
| + | // Initialize Ticker interval and callback | ||
| + | timeTicker.attach(1, | ||
| } | } | ||
| void loop() | void loop() | ||
| { | { | ||
| - | iot.handle(); | + | iot.handle(); |
| - | float h = dht.readHumidity(); | + | |
| - | float t = dht.readTemperature(); | + | { |
| + | sendDataFlag = false; | ||
| + | // Read humidity and temperature | ||
| + | | ||
| + | float t = dht.readTemperature(); | ||
| - | | + | // Create a buffer to store strings to being sent later |
| - | String(t).toCharArray(buf, | + | |
| - | digitalWrite(16,HIGH); | + | // Convert temperature value messages to strings and send to the MQTT server |
| - | iot.publishMsg(" | + | String(t).toCharArray(buf,10); |
| - | delay(2000); | + | iot.publishMsg(" |
| - | digitalWrite(16, | + | |
| - | String(h).toCharArray(buf, | + | // Convert humidity value messages to strings and send to the MQTT server |
| - | iot.publishMsg(" | + | |
| - | delay(2000); | + | iot.publishMsg(" |
| + | } | ||
| } | } | ||
| + | |||
| </ | </ | ||