This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
en:iot:examples:dhtoled [2020/07/20 11:26] – external edit 127.0.0.1 | en:iot:examples:dhtoled [Unknown date] (current) – external edit (Unknown date) 127.0.0.1 | ||
---|---|---|---|
Line 12: | Line 12: | ||
The following is the code for the controller with the DHT shield. Needed libaries: | The following is the code for the controller with the DHT shield. Needed libaries: | ||
- | < | + | < |
+ | <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 24: | Line 30: | ||
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 42: | Line 53: | ||
{ | { | ||
// 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(" |
+ | } | ||
} | } | ||
Line 80: | Line 95: | ||
The following is the code for the controller module with the OLED shield. | The following is the code for the controller module with the OLED shield. | ||
- | < | + | < |
<code c> | <code c> | ||
+ | // Includes global variables and librarys that the OLED display uses | ||
#include < | #include < | ||
#include < | #include < | ||
- | #include <SPI.h> | + | #include <Adafruit_I2CDevice.h> |
- | #include <Wire.h> | + | |
#include < | #include < | ||
#include < | #include < | ||
- | // Change it according to the real name of the red IoT module | + | #define WIFI_NAME " |
- | // DHT shield is connected | + | #define WIFI_PASSWORD " |
- | #define DHT_TOPIC "ESP53" | + | |
+ | // Change it according to the real name of the microcontroller | ||
+ | #define DHT_TOPIC "ESP30" | ||
// OLED reset pin is GPIO0 | // OLED reset pin is GPIO0 | ||
Line 109: | Line 126: | ||
if(topic == (DHT_TOPIC"/ | if(topic == (DHT_TOPIC"/ | ||
{ | { | ||
- | | + | t = msg.toFloat(); |
- | | + | |
} | } | ||
Line 116: | Line 132: | ||
if(topic == (DHT_TOPIC"/ | if(topic == (DHT_TOPIC"/ | ||
{ | { | ||
- | | + | h = msg.toFloat(); |
- | | + | |
} | } | ||
} | } | ||
Line 126: | Line 141: | ||
// Send message to serial port to show that connection is established | // Send message to serial port to show that connection is established | ||
Serial.println(" | Serial.println(" | ||
- | // Subscribe to get temperature and humidity messages | + | // Subscribe |
iot.subscribe(DHT_TOPIC"/ | iot.subscribe(DHT_TOPIC"/ | ||
iot.subscribe(DHT_TOPIC"/ | iot.subscribe(DHT_TOPIC"/ | ||
Line 136: | Line 151: | ||
{ | { | ||
// Initialize serial port and send message | // Initialize serial port and send message | ||
- | Serial.begin(115200); | + | Serial.begin(115200); |
Serial.println(" | Serial.println(" | ||
Line 150: | Line 165: | ||
display.display(); | display.display(); | ||
- | // print IoT json config to serial | + | //iot.setConfig(" |
- | iot.printConfig(); | + | |
- | + | | |
- | // Initialize | + | iot.setup(); |
- | iot.setup(); | + | |
} | } | ||
void loop() | void loop() | ||
{ | { | ||
- | // IoT behind the plan work, it should be periodically called | + | |
- | iot.handle(); | + | |
- | // Display temperature and humidity readings to oLED screen | + | // Display temperature and humidity readings to OLED screen |
- | display.clearDisplay(); | + | display.clearDisplay(); |
- | display.setTextSize(1); | + | display.setTextSize(1); |
- | display.setTextColor(WHITE); | + | display.setTextColor(WHITE); |
- | display.setCursor(0, | + | display.setCursor(0, |
- | display.println(" | + | display.println(" |
- | display.println(t); | + | display.println(t); |
display.setCursor(0, | display.setCursor(0, | ||
display.println(" | display.println(" | ||
Line 174: | Line 187: | ||
display.display(); | display.display(); | ||
- | delay(200); | + | delay(200); |
} | } | ||
+ | |||
</ | </ |