-->
Page 1 of 1

multiple DS18B20 sensors with esp8266

PostPosted: Mon Apr 20, 2020 5:15 pm
by szevlin
I want to connect 12 DS18B20 sensors with esp8266 and display the output to a https server, but
I dont have a lot of experience in esp8266 but,
I found this code and i wanted to make it suitable for my needs but i dont know if it would work or what would i need to change to make it work with 12 sensors instead of 3, any help is appriciated
:D

Code: Select all/*************************************************
 * Written by   : Usman Ali Butt                 *
 * Property off : www.microcontroller-project.com*
 * Dated        : 30 July 2018                   *
 ************************************************/
#include <OneWire.h>
#include <DallasTemperature.h>
#include <ESP8266WiFi.h>

// 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
int Celsius1=0, Fahrenheit1=0;     //Variables to store temperature readings from DS18B20 temperature sensors
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(){
sensors.requestTemperatures(); //Call all sensors on one wire to start calculating the temperature readings

// 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("<!DOCTYPE HTML>");
  client.println("<html>");
  client.println("<h1>Multiple Ds18b20 with Nodemcu</h1>");
  client.println("<br><br>");
  client.print("Sensor-1 Celcius Temperature =");
  client.print(Celsius1);
  client.println("<br>Sensor-1 Farenheight Temperature =");
  client.print(Fahrenheit1);
 
  client.print("<br>Sensor-2 Celcius Temperature =");
  client.print(Celsius2);
  client.println("<br>Sensor-2 Farenheight Temperature =");
  client.print(Fahrenheit2);
 
  client.print("<br>Sensor-3 Celcius Temperature =");
  client.print(Celsius3);
  client.println("<br>Sensor-3 Farenheight Temperature =");
  client.print(Fahrenheit3);
 
  client.println("<br><br>");
  client.println("<a href=\"/Tem=ON\"\"><button>Update Temperature</button></a><br />");
  client.println("</html>");
  delay(1);
  Serial.println("Client disonnected");
  Serial.println("");
     
}

Re: multiple DS18B20 sensors with esp8266

PostPosted: Tue Apr 21, 2020 2:41 am
by AcmeUK
This might explain what the sketch is doing:-
https://lastminuteengineers.com/multiple-ds18b20-arduino-tutorial/

Re: multiple DS18B20 sensors with esp8266

PostPosted: Tue Apr 21, 2020 5:37 am
by szevlin
thank you, i understand it a bit better now, i tried to change the code to work with 12 sensors, and only display celsius but i think i didnt do it right?
Code: Select all/*************************************************
 * Written by   : Usman Ali Butt                 *
 * Property off : www.microcontroller-project.com*
 * Dated        : 30 July 2018                   *
 ************************************************/
#include <OneWire.h>
#include <DallasTemperature.h>
#include <ESP8266WiFi.h>

// 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
int Celsius1=0;     //Variables to store temperature readings from DS18B20 temperature sensors
int Celsius2=0;
int Celsius3=0;
int Celsius4=0;
int Celsius5=0;
int Celsius6=0;
int Celsius7=0;
int Celsius8=0;
int Celsius9=0;
int Celsius10=0;
int Celsius11=0;
int Celsius12=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(){
sensors.requestTemperatures(); //Call all sensors on one wire to start calculating the temperature readings

// 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
  Celsius2=sensors.getTempCByIndex(1);
  Celsius3=sensors.getTempCByIndex(2);
  Celsius4=sensors.getTempCByIndex(3);
  Celsius5=sensors.getTempCByIndex(4);
  Celsius6=sensors.getTempCByIndex(5);
  Celsius7=sensors.getTempCByIndex(6);
  Celsius8=sensors.getTempCByIndex(7);
  Celsius9=sensors.getTempCByIndex(8);
  Celsius10=sensors.getTempCByIndex(9);
  Celsius11=sensors.getTempCByIndex(10);
  Celsius12=sensors.getTempCByIndex(11);
                   
  }
 
  // 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("<!DOCTYPE HTML>");
  client.println("<html>");
  client.println("<h1>Multiple Ds18b20 with Nodemcu</h1>");
  client.println("<br><br>");
  client.print("Sensor-1 Celcius Temperature =");
  client.print(Celsius1);
  client.print("<br>Sensor-2 Celcius Temperature =");
  client.print(Celsius2);
  client.print("<br>Sensor-3 Celcius Temperature =");
  client.print(Celsius3);
  client.print("<br>Sensor-4 Celcius Temperature =");
  client.print(Celsius4);
  client.print("<br>Sensor-5 Celcius Temperature =");
  client.print(Celsius5);
  client.print("<br>Sensor-6 Celcius Temperature =");
  client.print(Celsius6);
  client.print("<br>Sensor-7 Celcius Temperature =");
  client.print(Celsius7);
  client.print("<br>Sensor-8 Celcius Temperature =");
  client.print(Celsius8);
  client.print("<br>Sensor-9 Celcius Temperature =");
  client.print(Celsius9);
  client.print("<br>Sensor-10 Celcius Temperature =");
  client.print(Celsius10);
  client.print("<br>Sensor-11 Celcius Temperature =");
  client.print(Celsius11);
  client.print("<br>Sensor-12 Celcius Temperature =");
  client.print(Celsius12);
 
  client.println("<br><br>");
  client.println("<a href=\"/Tem=ON\"\"><button>Update Temperature</button></a><br />"); 
  client.println("</html>");
  delay(1);
  Serial.println("Client disonnected");
  Serial.println("");
     
}

PostPosted: Tue Apr 21, 2020 8:14 am
by szevlin
.