multiple DS18B20 sensors with esp8266

I want to connect 21 DS18B20 sensors with esp8266 and display the output to a https server, but
I dont have a lot of experience in arduinos or esp8266,
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 21 sensors instead of 3, any help is appriciated

/*************************************************

  • 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("");
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(”<a href="/Tem=ON"">Update Temperature
“);
client.println(”");
delay(1);
Serial.println(“Client disonnected”);
Serial.println("");

}

Multiple_ds18b20_with_nodemcu_esp8266.ino (3.9 KB)

Your code would be a lot shorter and simpler if you used arrays.

It would be easier to read if you used code tags.

sp. “Celsius”, “Fahrenheit”

I tried to make it work with 12 sensors and display only celsius, but i think i didn’t do it right?

/*************************************************
 * 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("

");
  client.print("Sensor-1 Celcius Temperature =");
  client.print(Celsius1);
  client.print("
Sensor-2 Celcius Temperature =");
  client.print(Celsius2);
  client.print("
Sensor-3 Celcius Temperature =");
  client.print(Celsius3);
  client.print("
Sensor-4 Celcius Temperature =");
  client.print(Celsius4);
  client.print("
Sensor-5 Celcius Temperature =");
  client.print(Celsius5);
  client.print("
Sensor-6 Celcius Temperature =");
  client.print(Celsius6);
  client.print("
Sensor-7 Celcius Temperature =");
  client.print(Celsius7);
  client.print("
Sensor-8 Celcius Temperature =");
  client.print(Celsius8);
  client.print("
Sensor-9 Celcius Temperature =");
  client.print(Celsius9);
  client.print("
Sensor-10 Celcius Temperature =");
  client.print(Celsius10);
  client.print("
Sensor-11 Celcius Temperature =");
  client.print(Celsius11);
  client.print("
Sensor-12 Celcius Temperature =");
  client.print(Celsius12);
  
  client.println("

");
  client.println("<a href=\"/Tem=ON\"\"><button>Update Temperature</button></a>
");  
  client.println("</html>");
  delay(1);
  Serial.println("Client disonnected");
  Serial.println("");
      
}

What's wrong with it? Apart from ignoring the advice about arrays.

i will use arrays in my future codes but i need to be done with this project soon,
the code worked fine but the button on the web server to update the temperature is small and i wanted to make it bigger but when i uploaded the new code where i changed it, it didnt work the button just disappeared and i dont know how to make it bigger, any advice?

#include <OneWire.h>
#include <DallasTemperature.h>
#include <ESP8266WiFi.h>
#define ONE_WIRE_BUS 5
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
const char* ssid = "ssid";
const char* password = "password";
int Celsius1=0;
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);  
void setup(){ 
  Serial.begin(115200);
  delay(10);
  sensors.begin();
  delay(10);
  Serial.println();
  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");
  server.begin();
  Serial.println("Server started");
  Serial.print("Use this URL to connect: ");
  Serial.print("http://");
  Serial.print(WiFi.localIP());
  Serial.println("/");
}
void loop(){ 
sensors.requestTemperatures();
  WiFiClient client = server.available();
  if (!client) {
    return;
  } 
  Serial.println("new client");
  while(!client.available()){
    delay(10);
  }
  String request = client.readStringUntil('\r');
  Serial.println(request);
  client.flush();
  if (request.indexOf("/Tem=ON") != -1)  {
  Celsius1=sensors.getTempCByIndex(0);
  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);
  }
  client.println("HTTP/1.1 200 OK");
  client.println("Content-Type: text/html");
  client.println("");
  client.println("<!DOCTYPE HTML>");
  client.println("<html>");
  client.println("<h1>DS18B20 with ESP8266</h1>");
  client.println("

");
  client.print("
 Sensor 1 Temperature= ");
  client.print(Celsius1);
  client.print(" C");
  client.print("
 Sensor 2 Temperature = ");
  client.print(Celsius2);
  client.print(" C");
  client.print("
 Sensor 3 Temperature = ");
  client.print(Celsius3);
  client.print(" C");
  client.print("
 Sensor 4 Temperature = ");
  client.print(Celsius4);
  client.print(" C");
  client.print("
 Sensor 5 Temperature = ");
  client.print(Celsius5);
  client.print(" C");
  client.print("
 Sensor 6 Temperature = ");
  client.print(Celsius6);
  client.print(" C");
  client.print("
 Sensor 7 Temperature = ");
  client.print(Celsius7);
  client.print(" C");
  client.print("
 Sensor 8 Temperature = ");
  client.print(Celsius8);
  client.print(" C");
  client.print("
 Sensor 9 Temperature = ");
  client.print(Celsius9);
  client.print(" C");
  client.print("
Sensor 10 Temperature = ");
  client.print(Celsius10);
  client.print(" C");
  client.print("
Sensor 11 Temperature = ");
  client.print(Celsius11);
  client.print(" C");
  client.print("
Sensor 12 Temperature = ");
  client.print(Celsius12);
  client.print(" C");
  client.println("

");
  client.println("

");
  client.println("<a href=\"/Tem=ON\"\"><button>Update Temperature</button></a>
");
  client.println("</html>");
  delay(10);
  Serial.println("Client disonnected");
  Serial.println("");
      
}