In this post I will show you guys, how to get temperature and humidity values on your Android Phone, from DHT11 sensor.
If you are new to NodeMcu modules, I would recommend to read our old posts.
Getting Started with Nodemcu in Arduino IDE
Getting Temperature and Humidity Values on Phone
Controlling Devices Over WiFi using Phone
Controlling Devices Over WiFi Using Phone without WiFi Router
Posting DHT11 Values to ThingSpeak Using Nodemcu
Oh, you already saw the name of the post "Get Temperature and Humidity Values on Phone", then you might be thinking, what's new in this post.
The answer for the above question is, Everything.
Actually the last post uses local area network to get the temperature and humidity data and due to this limitation, you can get data only when your Android Phone and NodeMCU are on same network, but with this post you will be able to get temperature and humidity data from any corner of the world, just make sure you that have active internet connection.
Introduction
The NodeMcu module uses DHT11(Temperature and Humidity sensor), and after measuring the temperature and humidity it publishes this data to a server, and this server then publish this data to all the subscribers.
For publishing and subscribing, I am going to use MQTT at all the ends (NodeMcu, Android Phone, Server).
If you are new to MQTT please have a look at the following posts, it will you some idea, plus it will also introduces you with the free CloudMQTT broker which we are going to use.
Getting Started with MQTT on Windows PC
Getting Started with MQTT Using Python
The main challenge here, is to get the public MQTT Server or MQTT broker, which can help us to subscribe the temperature and humidity messages from any corner of the world.
Since we don't have money and we always wanted to test everything for free, I found one popular MQTT broker, which is free to use with few limitations, and once you like it and wanted to explore more, you can purchase the paid accounts without limitations, and the name of the broker is CloudMQTT.
Hardware
The following is the list of components required to complete this project.
Software
We need some libraries to complete this project and they are as given below.
The DHT Sensor Library will be used to measure the temperature and humidity data from DHT11 sensor, while the MQTT Library will be used to publish and subscribe the topics.
In this project, NodeMcu will Publish the temperature and humidity data, while it will subscribe the led topic and based on payload parameter, the on board led, on NodeMcu will get turned on or off.
With the help of this simple application we laid the foundation for Home Automation project and we will slowly move towards a professional way of controlling home appliances over internet.
Code Snippets:
Enter you WiFi router information here, this will help NodeMcu to establish connection with your router and connect itself with internet to Publish and Subscribe topics.
Update the following information with your CloudMQTT instance details.
The following entered information is from my free account, so it is better to create yours, as I will delete this after testing, so it will not work.
The following function are for connecting with server, publishing messages and subscribing topics.
The complete NodeMcu code written in Arduiono IDE is as follow:
Watch this YouTube Video, to see how the system works.
Click Here to get the Arduino IDE Source Code and Android APK for testing.
Note:
Android Application is tested on three Android Phone, and it is working fine, one limitation in Android Application is that, you have to press enter key, once you enter any information, otherwise information will not get updated.
Conclusion
Now you can get your temperature and humidity directly on your phone from any corner of the world, and you can also control led from anywhere, with this setup.
As a next step, we have created a application on Windows PC, which will display the temperature and humidity values and can also turn on Led directly using a Toggle Button.
If you want to read the post, please follow the link given below.
Get Temperature and Humidity Data on PC Using Python
If you are new to NodeMcu modules, I would recommend to read our old posts.
Getting Started with Nodemcu in Arduino IDE
Getting Temperature and Humidity Values on Phone
Controlling Devices Over WiFi using Phone
Controlling Devices Over WiFi Using Phone without WiFi Router
Posting DHT11 Values to ThingSpeak Using Nodemcu
Oh, you already saw the name of the post "Get Temperature and Humidity Values on Phone", then you might be thinking, what's new in this post.
The answer for the above question is, Everything.
Actually the last post uses local area network to get the temperature and humidity data and due to this limitation, you can get data only when your Android Phone and NodeMCU are on same network, but with this post you will be able to get temperature and humidity data from any corner of the world, just make sure you that have active internet connection.
Android Application |
Introduction
The NodeMcu module uses DHT11(Temperature and Humidity sensor), and after measuring the temperature and humidity it publishes this data to a server, and this server then publish this data to all the subscribers.
For publishing and subscribing, I am going to use MQTT at all the ends (NodeMcu, Android Phone, Server).
If you are new to MQTT please have a look at the following posts, it will you some idea, plus it will also introduces you with the free CloudMQTT broker which we are going to use.
Getting Started with MQTT on Windows PC
Getting Started with MQTT Using Python
The main challenge here, is to get the public MQTT Server or MQTT broker, which can help us to subscribe the temperature and humidity messages from any corner of the world.
Since we don't have money and we always wanted to test everything for free, I found one popular MQTT broker, which is free to use with few limitations, and once you like it and wanted to explore more, you can purchase the paid accounts without limitations, and the name of the broker is CloudMQTT.
Hardware
The following is the list of components required to complete this project.
Connection Diagram |
- NodeMcu Module
- DHT11 Sensor
- Cables to connect DHT11 with NodeMcu
- Usb Cable to Upload Firmware
- Android Phone
System Working High Level |
We need some libraries to complete this project and they are as given below.
The DHT Sensor Library will be used to measure the temperature and humidity data from DHT11 sensor, while the MQTT Library will be used to publish and subscribe the topics.
In this project, NodeMcu will Publish the temperature and humidity data, while it will subscribe the led topic and based on payload parameter, the on board led, on NodeMcu will get turned on or off.
With the help of this simple application we laid the foundation for Home Automation project and we will slowly move towards a professional way of controlling home appliances over internet.
Code Snippets:
Enter you WiFi router information here, this will help NodeMcu to establish connection with your router and connect itself with internet to Publish and Subscribe topics.
const char* ssid = "wifi ssid"; // WiFi Name const char* password = "wifi password"; // WiFi Password
Update the following information with your CloudMQTT instance details.
The following entered information is from my free account, so it is better to create yours, as I will delete this after testing, so it will not work.
const char* mqtt_server = "m14.cloudmqtt.com"; // MQTT Server Name const int mqtt_port = 18410; // MQTT Server Port const char* user_name = "setsmjwc"; // MQTT Server Instance User Name const char* mqtt_pswd = "apDnKqHRgAjA"; // MQTT Server Instance Password
The following function are for connecting with server, publishing messages and subscribing topics.
/*Connect with Client using the following lines*/ client.connect(client_id, username, password); /*Subscribe to Topics Using the following line*/ client.subscribe("led/#");
/*Publish "msg", with topic name as "home", using the following lines*/
client.publish("home", msg);
The complete NodeMcu code written in Arduiono IDE is as follow:
#include <DHT.h> #include <DHT_U.h> #include <ESP8266WiFi.h> #include <PubSubClient.h> // DHT11 Sensor Data Pin #define DHTPIN D4 // DHT Sensor Type #define DHTTYPE DHT11 // DHT 11 //#define DHTTYPE DHT22 // DHT 22 //#define DHTTYPE DHT21 // DHT 21 // Initialize DHT Sensor DHT dht( DHTPIN, DHTTYPE); // Update these Information const char* ssid = "wifi ssid"; // WiFi Name const char* password = "wifi password"; // WiFi Password const char* mqtt_server = "m14.cloudmqtt.com"; // MQTT Server Name const int mqtt_port = 18410; // MQTT Server Port const char* user_name = "setsmjwc"; // MQTT Server Instance User Name const char* mqtt_pswd = "apDnKqHRgAjA"; // MQTT Server Instance Password WiFiClient espClient; PubSubClient client(espClient); uint32_t lastMsg = 0; #define MSG_BUFFER_LEN 10u char msg[MSG_BUFFER_LEN]; // Setup WiFi Connection void setup_wifi() { delay(10); // We start by connecting to a WiFi network Serial.println(); Serial.print("Connecting to "); Serial.println(ssid); WiFi.begin(ssid, password); while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); } Serial.println(""); Serial.println("WiFi connected"); Serial.println("IP address: "); Serial.println(WiFi.localIP()); } // Callback Function void callback(char* topic, byte* payload, uint16_t length) { Serial.print("Message arrived ["); Serial.print(topic); Serial.print("] "); for (uint16_t i = 0; i < length; i++) { Serial.print((char)payload[i], HEX); } Serial.println(); // Switch on the LED if an 1 was received as first character if ((char)payload[0] == 0x01) { digitalWrite(BUILTIN_LED, LOW); // Turn the LED on (Note that LOW is the voltage level // but actually the LED is on; this is because // it is acive low on the ESP-01) } else { digitalWrite(BUILTIN_LED, HIGH); // Turn the LED off by making the voltage HIGH } } // Reconnect with Server Function void reconnect() { // Loop until we're reconnected while (!client.connected()) { Serial.print("Attempting CloudMQTT connection..."); String clientId = "EmbeddedLab"; Serial.print("Client Id: "); Serial.println(clientId); // Attempt to connect // if (client.connect(clientId.c_str())) if ( client.connect(clientId.c_str(), user__name, mqtt_pswd) ) { Serial.println("connected"); // Once connected, publish an announcement... // client.publish("Broadcast", "Connected with MQTT Server"); // ... and resubscribe (Topic is "LED", to control the on board LED) client.subscribe("led/#"); } else { Serial.print("failed, rc="); Serial.print(client.state()); Serial.println(" try again in 5 seconds"); // Wait 5 seconds before retrying delay(5000); } } } void setup() { pinMode(BUILTIN_LED, OUTPUT); // Initialize the BUILTIN_LED pin as an output Serial.begin(115200); setup_wifi(); client.setServer(mqtt_server, mqtt_port); client.setCallback(callback); } void loop() { if (!client.connected()) { reconnect(); } client.loop(); uint32_t now = millis(); // Publish Message After another 15 seconds if (now - lastMsg > 15000u) { lastMsg = now; // Read Humidity float h = dht.readHumidity(); // Read temperature as Celsius (the default) float t = dht.readTemperature(); // Check if any reads failed if( isnan(h) || isnan(t) ) { // Don't do anything, if data is invalid } else { uint16_t temp = (uint16_t)(t); uint16_t humid= (uint16_t)(h); snprintf(msg, 7,"%.2d,%.2d", temp, humid); Serial.print("Publish message: "); Serial.println(msg); client.publish("home", msg); } } }
Watch this YouTube Video, to see how the system works.
Click Here to get the Arduino IDE Source Code and Android APK for testing.
Note:
Android Application is tested on three Android Phone, and it is working fine, one limitation in Android Application is that, you have to press enter key, once you enter any information, otherwise information will not get updated.
Conclusion
Now you can get your temperature and humidity directly on your phone from any corner of the world, and you can also control led from anywhere, with this setup.
As a next step, we have created a application on Windows PC, which will display the temperature and humidity values and can also turn on Led directly using a Toggle Button.
If you want to read the post, please follow the link given below.
Get Temperature and Humidity Data on PC Using Python
Nice to read your article! I am looking forward to sharing your adventures and experiences. Temperature Humidity Test Chamber
ReplyDeleteThanks for the blog post buddy! Keep them coming... Temperature Humidity Test Chamber
ReplyDelete