DS18B20 Temperature Sensor
Ds18b20 is a 1-wire temperature sensor. From 1-wire it means the sensor require only one digital pin for its operation. Since each ds18b20 has a unique address and only one wire is required to communicate with them hence we can connect multiple ds18b20 sensors to a single 1-wire and call them one by one with their addresses. Each DS18B20 contains a unique 64-bit address etched in to it. Ds18b20 provides 9-bit or 12-bit precision.
Ds18b20 has 3 pins. Two are power pins. Vcc is +ve power pin and GND is -ve power pin. The third pin is DQ pin. Its our data line. We have to connect this line to our microcontroller or control system. If we are using multiple ds18b20 sensors in our project then we need to commonly connect all the sensors DQ pins to a single point/wire.
|
Project circuit
Nodemcu works on 3.3 v TTL logic and ds18b20 working range is between 3 to 5 volts. So we can power ds18b20 with nodemcu power output rails and i powered the sensors with nodemcu power output.
Please enter your WiFi SSID and password before moving any further. Enter the SSID and password in the double quotations.
const char* ssid = “Your SSID”; //Enter your WiFi SSID Here
const char* password = “Your Wifi Password”; //Enter your WiFi Password Here
In the setup function first the arduino serial monitor communication channel is initialed at 115200 baud rate. Then ds18b20 sensors on 1-wire bus are commanded to start the operations. After wards nodemcu is requesting the WiFi router for an IP allotment. When IP is allotted by your router nodemcu starts its server.
/************************************************* * Written by : Usman Ali Butt * * Property off : www.microcontroller-project.com* * Dated : 30 July 2018 * ************************************************/ #include #include #include // Data output of DS18B20 is connected to nodemcu GPIO 5 or D1 #define ONE_WIRE_BUS 5 // Setting a one wire instance OneWire oneWire(ONE_WIRE_BUS); // Passing onewire instance to Dallas Temperature sensor library DallasTemperature sensors(&oneWire); const char* ssid = "Your SSID"; //Enter your WiFi SSID Here const char* password = "Your Wifi Password"; //Enter your WiFi Password Here //Variables to store temperature readings from DS18B20 temperature sensors int Celsius1=0, Fahrenheit1=0; int Celsius2=0, Fahrenheit2=0; int Celsius3=0, Fahrenheit3=0; WiFiServer server(80); //Web server default port void setup(){ Serial.begin(115200); //initialize serial communication delay(10); sensors.begin(); // Begin the DS18B20 initialization delay(10); Serial.println(); Serial.println(); Serial.print("Connecting to "); // Connect to WiFi network Serial.println(ssid); WiFi.begin(ssid, password); while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); } Serial.println(""); Serial.println("WiFi connected"); // Start the server server.begin(); Serial.println("Server started"); // Print the IP address on serial monitor Serial.print("Use this URL to connect: "); Serial.print("http://"); //URL IP to be typed in mobile/desktop browser Serial.print(WiFi.localIP()); Serial.println("/"); } void loop(){ //Call all sensors on one wire to start calculating the temperature readings sensors.requestTemperatures(); // Check if a client has connected WiFiClient client = server.available(); if (!client) { return; } // Wait until the client sends some data Serial.println("new client"); while(!client.available()){ delay(1); } // Read the first line of the request String request = client.readStringUntil('\r'); Serial.println(request); client.flush(); // Match the request if (request.indexOf("/Tem=ON") != -1) { Celsius1=sensors.getTempCByIndex(0); //Get temperature reading from sensor 0 in celsius scale Fahrenheit1=sensors.getTempFByIndex(0);//Get temperature reading from sensor 0 in fahrenheit scale Celsius2=sensors.getTempCByIndex(1); //Get temperature reading from sensor 0 in celsius scale Fahrenheit2=sensors.getTempFByIndex(1);//Get temperature reading from sensor 0 in fahrenheit scale Celsius3=sensors.getTempCByIndex(2); //Get temperature reading from sensor 0 in celsius scale Fahrenheit3=sensors.getTempFByIndex(2);//Get temperature reading from sensor 0 in fahrenheit scale } // Return the response client.println("HTTP/1.1 200 OK"); client.println("Content-Type: text/html"); client.println(""); // do not forget this one client.println(""); client.println(""); client.println("Multiple Ds18b20 with Nodemcu
"); client.println("
"); client.print("Sensor-1 Celcius Temperature ="); client.print(Celsius1); client.println("
Sensor-1 Farenheight Temperature ="); client.print(Fahrenheit1); client.print("
Sensor-2 Celcius Temperature ="); client.print(Celsius2); client.println("
Sensor-2 Farenheight Temperature ="); client.print(Fahrenheit2); client.print("
Sensor-3 Celcius Temperature ="); client.print(Celsius3); client.println("
Sensor-3 Farenheight Temperature ="); client.print(Fahrenheit3); client.println("
"); client.println("
"); client.println(""); delay(1); Serial.println("Client disonnected"); Serial.println(""); } !doctype>
You will see in serial nodemcu requesting your router for an IP allotment. After successful IP allotment it will show you its server status. Once the sever starts nodemcu will print the server address on serial monitor. You have to enter this server address in your client browser to access the ds18b20 control and temperature display page.
Note: Both the server(nodemcu) and client(mobile, desktop, laptop or notebook) must be connected to same network in order to communicate with each other. You have to connect your client to same WiFi whose ssid and password you entered in the code above or to which you connected your nodemcu. |
In loop function nodemcu is always waiting for the client request. When the request arrives it matches it with the request format it has “/Tem=ON“. If the request matches nodemcu updates the temperature readings. Updating statements are
Celsius1=sensors.getTempCByIndex(0); //Get temperature reading from sensor 0 in celsius scale
Fahrenheit1=sensors.getTempFByIndex(0); //Get temperature reading from sensor 0 in fahrenheit scale
Rest of the code in loop function contains HTML code to be displayed as a web page in browser of client. After calculating the updated temperature values nodemcu before closing the request replies back with the HTML format. Its actually the updated web page. I web page only the temperature readings are changed rest of the page remains same.
Future Work:
I connected only three ds18b20 temperatures sensors on bus. For future you can connect more to the 1-wire bus and test them. Web page is not static it refreshes every time one presses the “Update Temperature” button. For future you can make a static web page using J Query or AJAX. Multiple sensors can increases the temperature readings by averaging all the senors temperature readings.
Filed Under: ESP8266, Microcontroller Projects
Questions related to this article?
👉Ask and discuss on Electro-Tech-Online.com and EDAboard.com forums.
Tell Us What You Think!!
You must be logged in to post a comment.