This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| et:iot:examples:dhtoled [2023/01/28 18:50] – karllall | et:iot:examples:dhtoled [2024/12/12 09:29] (current) – karllall | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| + | ====== DHT ja OLED näidis ====== | ||
| + | Antud näidis demonstreerib kuidas DHT andur on võimeline saatma temperatuuri ja niiskuse näidu OLED ekraanile kasutades selle jaoks ITT IoT raamistikku. Selle näite jaoks on vaja kahte kontrollerit: | ||
| + | |||
| + | {{: | ||
| + | |||
| + | Kui kood on üles laetud, saadab DHT kontroller temperatuuri ja niiskuse näidu läbi mqtt OLED ekraanile. | ||
| + | |||
| + | Kõigepealt tuleb üks moodul tööle saada ning siis jätame selle mooduli tööle, kustutame arvutist selle mooduli koodi ära (soovi korral säilitame word-i vms dokumendis) ning samasse kohta loome teise mooduli koodi. | ||
| + | |||
| + | Järgnev kood on mõeldud sellele kontrollerile, | ||
| + | Vajaminevad teegid: | ||
| + | < | ||
| + | <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(" | ||
| + | } | ||
| + | } | ||
| + | |||
| + | </ | ||
| + | |||
| + | Järgnev kood on mõeldud kontrollerile, | ||
| + | Vajaminevad teegid: | ||
| + | < | ||
| + | <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 | ||
| + | } | ||
| + | |||
| + | </ | ||