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/04 07:34] – 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. | ||
+ | Required libraries: | ||
+ | < | ||
+ | <code c> | ||
+ | // Includes global variables and librarys that the relay uses | ||
+ | #include < | ||
+ | #include < | ||
+ | |||
+ | #define WIFI_NAME " | ||
+ | #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 5 // The relay has been connected to pin 5 | ||
+ | float t; //for holding the received temperature float value | ||
+ | float tempLimit = 28; //the temperature limit on what the relay will switch | ||
+ | |||
+ | // Message received | ||
+ | void iot_received(String topic, String msg) | ||
+ | { | ||
+ | t=msg.toFloat(); | ||
+ | |||
+ | // Check if topic contains configuration data (change the temperature limit value) | ||
+ | if(topic == (DHT_TOPIC"/ | ||
+ | { | ||
+ | // | ||
+ | tempLimit=t; | ||
+ | } | ||
+ | |||
+ | // Check if topic contains temperature data | ||
+ | if(topic == (DHT_TOPIC"/ | ||
+ | { | ||
+ | // Relay switching according to the temperature limit value | ||
+ | if(t >= tempLimit) | ||
+ | { | ||
+ | digitalWrite(RELAY_PIN, | ||
+ | } | ||
+ | else | ||
+ | { | ||
+ | digitalWrite(RELAY_PIN, | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | |||
+ | // Function started after the connection to the server is established. | ||
+ | void iot_connected() | ||
+ | { | ||
+ | // Send message to serial port to show that connection is established | ||
+ | Serial.println(" | ||
+ | // Subscribe to get temperature and temperature configuration messages | ||
+ | iot.subscribe(DHT_TOPIC"/ | ||
+ | iot.subscribe(DHT_TOPIC"/ | ||
+ | // Send message to MQTT server to show that connection is established | ||
+ | iot.log(" | ||
+ | } | ||
+ | |||
+ | void setup() | ||
+ | { | ||
+ | // Initialize serial port and send message | ||
+ | Serial.begin(115200); | ||
+ | Serial.println(" | ||
+ | |||
+ | // | ||
+ | // | ||
+ | iot.printConfig(); | ||
+ | iot.setup(); | ||
+ | |||
+ | pinMode(RELAY_PIN, | ||
+ | } | ||
+ | |||
+ | void loop() | ||
+ | { | ||
+ | iot.handle(); | ||
+ | delay(20); // Wait 0.2 second | ||
+ | } | ||
+ | |||
+ | </ | ||
+ | |||
+ | The following is the program code for the controller with DHT module. | ||
+ | Required libraries: | ||
+ | < | ||
+ | <fc # | ||
+ | <code c> | ||
+ | // Includes global variables and librarys that the DHT uses | ||
+ | #include < | ||
+ | #include < | ||
+ | #include < | ||
+ | #include < | ||
+ | |||
+ | #define WIFI_NAME " | ||
+ | #define WIFI_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) | ||
+ | |||
+ | // Create an object for DHT sensor | ||
+ | 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() | ||
+ | { | ||
+ | // Send message to serial port to show that connection is established | ||
+ | Serial.println(" | ||
+ | // Send message to MQTT server to show that connection is established | ||
+ | iot.log(" | ||
+ | } | ||
+ | |||
+ | void setup() | ||
+ | { | ||
+ | // Initialize serial port and send message | ||
+ | Serial.begin(115200); | ||
+ | Serial.println(" | ||
+ | |||
+ | // | ||
+ | // | ||
+ | iot.printConfig(); | ||
+ | iot.setup(); | ||
+ | |||
+ | // Initialize DHT library | ||
+ | dht.begin(); | ||
+ | |||
+ | // Initialize Ticker interval and callback | ||
+ | timeTicker.attach(1, | ||
+ | } | ||
+ | |||
+ | void loop() | ||
+ | { | ||
+ | iot.handle(); | ||
+ | |||
+ | if(sendDataFlag) | ||
+ | { | ||
+ | sendDataFlag = false; | ||
+ | // Read humidity and temperature | ||
+ | float h = dht.readHumidity(); | ||
+ | float t = dht.readTemperature(); | ||
+ | |||
+ | // Create a buffer to store strings to being sent later | ||
+ | char buf[10]; | ||
+ | |||
+ | // Convert temperature value messages to strings and send to the MQTT server | ||
+ | String(t).toCharArray(buf, | ||
+ | iot.publishMsg(" | ||
+ | |||
+ | // Convert humidity value messages to strings and send to the MQTT server | ||
+ | String(h).toCharArray(buf, | ||
+ | iot.publishMsg(" | ||
+ | } | ||
+ | } | ||
+ | |||
+ | </ |