This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
en:iot:examples:smartventilator [2020/07/20 11:26] – external edit 127.0.0.1 | en:iot:examples:smartventilator [Unknown date] (current) – external edit (Unknown date) 127.0.0.1 | ||
---|---|---|---|
Line 10: | Line 10: | ||
The following is the code for the controller with relay module. | The following is the code for the controller with relay module. | ||
Required libraries: | Required libraries: | ||
- | < | + | < |
<code c> | <code c> | ||
+ | // Includes global variables and librarys that the relay uses | ||
#include < | #include < | ||
#include < | #include < | ||
+ | |||
+ | #define WIFI_NAME " | ||
+ | #define WIFI_PASSWORD " | ||
// Change it according to the real name of the red IoT module where | // Change it according to the real name of the red IoT module where | ||
- | // DHT shield is connected | + | // temperature & humidity |
- | #define DHT_TOPIC "ESP53" | + | #define DHT_TOPIC "ESP30" |
- | #define RELAY_PIN 5 | + | #define RELAY_PIN |
- | float t; //for holding the received float value | + | 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 | ||
Line 26: | Line 30: | ||
void iot_received(String topic, String msg) | void iot_received(String topic, String msg) | ||
{ | { | ||
- | t=msg.toFloat(); | + | t=msg.toFloat(); |
- | // Check if topic contains | + | // Check if topic contains |
if(topic == (DHT_TOPIC"/ | if(topic == (DHT_TOPIC"/ | ||
{ | { | ||
- | // | + | // |
tempLimit=t; | tempLimit=t; | ||
} | } | ||
Line 65: | Line 69: | ||
{ | { | ||
// Initialize serial port and send message | // Initialize serial port and send message | ||
- | Serial.begin(115200); | + | Serial.begin(115200); |
Serial.println(" | Serial.println(" | ||
- | // print IoT json config to serial | + | //iot.setConfig(" |
- | iot.printConfig(); | + | |
- | // Initialize | + | |
- | iot.setup(); | + | iot.setup(); |
- | | + | pinMode(RELAY_PIN, |
- | | + | |
} | } | ||
void loop() | void loop() | ||
{ | { | ||
- | // IoT behind the plan work, it should be periodically called | + | |
- | iot.handle(); | + | delay(20); // Wait 0.2 second |
- | delay(200); | + | |
} | } | ||
+ | |||
</ | </ | ||
The following is the program code for the controller with DHT module. | The following is the program code for the controller with DHT module. | ||
Required libraries: | Required libraries: | ||
- | < | + | < |
+ | <fc # | ||
<code c> | <code c> | ||
+ | // Includes global variables and librarys that the DHT uses | ||
#include < | #include < | ||
#include < | #include < | ||
+ | #include < | ||
#include < | #include < | ||
- | #define DHTPIN D3 // Pin where DHT shield is connected. Change this to D4 if your shield has no legs removed. | + | #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) | #define DHTTYPE DHT22 // DHT 22 (AM2302) | ||
Line 99: | Line 108: | ||
DHT dht(DHTPIN, DHTTYPE); | DHT dht(DHTPIN, DHTTYPE); | ||
- | // Message received | + | // Create an object for Ticker library |
- | void iot_received(String topic, String msg) | + | Ticker timeTicker; |
+ | |||
+ | bool sendDataFlag; | ||
+ | |||
+ | // Ticker library callback, which will occur 0.5 second interval. | ||
+ | void sendData() | ||
{ | { | ||
+ | sendDataFlag=true; | ||
} | } | ||
Line 117: | Line 131: | ||
{ | { | ||
// Initialize serial port and send message | // Initialize serial port and send message | ||
- | Serial.begin(115200); | + | Serial.begin(115200); |
Serial.println(" | Serial.println(" | ||
- | // print IoT json config to serial | + | //iot.setConfig(" |
- | iot.printConfig(); | + | |
- | + | | |
- | // Initialize | + | iot.setup(); |
- | iot.setup(); | + | |
// Initialize DHT library | // Initialize DHT library | ||
dht.begin(); | dht.begin(); | ||
+ | |||
+ | // Initialize Ticker interval and callback | ||
+ | timeTicker.attach(1, | ||
} | } | ||
void loop() | void loop() | ||
{ | { | ||
- | // IoT behind the plan work, it should be periodically called | + | |
- | iot.handle(); | + | |
- | // Read humidity and temperature | + | |
- | float h = dht.readHumidity(); | + | { |
- | float t = dht.readTemperature(); | + | sendDataFlag = false; |
+ | | ||
+ | float h = dht.readHumidity(); | ||
+ | float t = dht.readTemperature(); | ||
- | | + | |
- | char buf[10]; | + | char buf[10]; |
- | | + | |
- | String(t).toCharArray(buf, | + | String(t).toCharArray(buf, |
- | iot.publishMsg(" | + | iot.publishMsg(" |
- | delay(1000); | + | |
- | String(h).toCharArray(buf, | + | // Convert humidity value messages to strings and send to the MQTT server |
- | iot.publishMsg(" | + | |
- | delay(1000); | + | iot.publishMsg(" |
+ | } | ||
} | } | ||
+ | |||
</ | </ | ||