This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| en:iot:examples:dhtoled [2020/06/10 20:09] – heikopikner | en:iot:examples:dhtoled [Unknown date] (current) – external edit (Unknown date) 127.0.0.1 | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| + | ====== DHT and OLED example ====== | ||
| + | This example demonstrates how DHT senor can send temperature and humidity readings to the OLED screen with the ITT IoT framework. | ||
| + | For this example you will need two controllers. One with the OLED shield and the other with the DHT sensor shield. | ||
| + | |||
| + | {{: | ||
| + | |||
| + | Once the code has been uploaded the DHT controller will send temperature and humidity messages via MQTT to the OLED module. | ||
| + | |||
| + | To upload the code you need to create two projects in PlatformIO environment. | ||
| + | One for the controller with the OLED shield and the other for the DHT sensor shield. | ||
| + | |||
| + | The following is the code for the controller with the DHT shield. Needed libaries: | ||
| + | < | ||
| + | <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(" | ||
| + | } | ||
| + | } | ||
| + | |||
| + | </ | ||
| + | |||
| + | The following is the code for the controller module with the OLED shield. | ||
| + | < | ||
| + | <code c> | ||
| + | // Includes global variables and librarys that the OLED display uses | ||
| + | #include < | ||
| + | #include < | ||
| + | #include < | ||
| + | #include < | ||
| + | #include < | ||
| + | |||
| + | #define WIFI_NAME " | ||
| + | #define WIFI_PASSWORD " | ||
| + | |||
| + | // Change it according to the real name of the microcontroller where DHT shield is connected | ||
| + | #define DHT_TOPIC " | ||
| + | |||
| + | // OLED reset pin is GPIO0 | ||
| + | #define OLED_RESET 0 | ||
| + | |||
| + | // Create an object for OLED screen | ||
| + | Adafruit_SSD1306 display(OLED_RESET); | ||
| + | |||
| + | // Define variables to store humidity and temperature values | ||
| + | float h; | ||
| + | float t; | ||
| + | |||
| + | // Message received | ||
| + | void iot_received(String topic, String msg) | ||
| + | { | ||
| + | // Check if topic contains temperature data | ||
| + | if(topic == (DHT_TOPIC"/ | ||
| + | { | ||
| + | t = msg.toFloat(); | ||
| + | } | ||
| + | |||
| + | // Check if topic contains humidity data | ||
| + | if(topic == (DHT_TOPIC"/ | ||
| + | { | ||
| + | h = msg.toFloat(); | ||
| + | } | ||
| + | } | ||
| + | |||
| + | // 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 topics to get temperature and humidity 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(" | ||
| + | |||
| + | // Initialize OLED with the I2C addr 0x3C (for the 64x48) | ||
| + | display.begin(SSD1306_SWITCHCAPVCC, | ||
| + | |||
| + | // Display " | ||
| + | display.clearDisplay(); | ||
| + | display.setTextSize(1); | ||
| + | display.setTextColor(WHITE); | ||
| + | display.setCursor(0, | ||
| + | display.println(" | ||
| + | display.display(); | ||
| + | |||
| + | // | ||
| + | // | ||
| + | iot.printConfig(); | ||
| + | iot.setup(); | ||
| + | } | ||
| + | |||
| + | void loop() | ||
| + | { | ||
| + | iot.handle();// | ||
| + | |||
| + | // Display temperature and humidity readings to OLED screen | ||
| + | display.clearDisplay(); | ||
| + | display.setTextSize(1); | ||
| + | display.setTextColor(WHITE); | ||
| + | display.setCursor(0, | ||
| + | display.println(" | ||
| + | display.println(t); | ||
| + | display.setCursor(0, | ||
| + | display.println(" | ||
| + | display.println(h); | ||
| + | display.display(); | ||
| + | |||
| + | delay(200); // Waiting 0.2 second | ||
| + | } | ||
| + | |||
| + | </ | ||