Page 1 of 1
Forum

Welcome to the Tweaking4All community forums!
When participating, please keep the Forum Rules in mind!

Topics for particular software or systems: Start your topic link with the name of the application or system.
For example “MacOS X – Your question“, or “MS Word – Your Tip or Trick“.

Please note that switching to another language when reading a post will not bring you to the same post, in Dutch, as there is most likely no translated for the post!




How to monitor 6 te...
 
Share:
Notifications
Clear all

How to monitor 6 temperature sensors


 Hans
(@hans)
Noble Member Admin
Joined: 8 years ago
Posts: 1958
Topic starter  

This is in reply to Reply Francisco Silva's question with the Arduino Temperature Sensor article.
Below his code and questions;
I want to monitorize 6 sensors and show their temperature in a htlm page.
I have all the code, but i just see the temperature of one sensor.
Can you help me?
The code:

#include <OneWire.h>
#include <SPI.h>
#include <WiFi.h>
// IP Fixo:
IPAddress ip(192, 168, 1, 50);
 
char ssid[] = “ZON-6A20”; // Nome rede
char pass[] = “b683a87bade5”; // password
int keyIndex = 0; // Numero rede WEP
int status = WL_IDLE_STATUS;
WiFiServer server(80);
OneWire ds(2); // pino 2
void setup() {
  //Initialize serial and wait for port to open:
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
  }
  // check for the presence of the shield:
  if (WiFi.status() == WL_NO_SHIELD) {
    Serial.println(“WiFi shield not present”);
    // don’t continue:
    while (true);
  }
  WiFi.config(ip);
  
  String fv = WiFi.firmwareVersion();
  if ( fv != “1.1.0” )
    Serial.println(“Please upgrade the firmware”);
  // attempt to connect to Wifi network:
  while ( status != WL_CONNECTED) {
    Serial.print(“Attempting to connect to SSID: “);
    Serial.println(ssid);
    // Connect to WPA/WPA2 network. Change this line if using open or WEP network:
    status = WiFi.begin(ssid, pass);
    // wait 10 seconds for connection:
    delay(100);
  }
  server.begin();
  // you’re connected now, so print out the status:
  printWifiStatus();
}
void loop() {
  byte i;
  byte present = 0;
  byte type_s;
  byte data[12];
  byte addr[8];
  float celsius;
  if ( !ds.search(addr)) {
    Serial.println(“No more addresses.”);
    Serial.println();
    ds.reset_search();
    delay(250);
    return;
  }
  Serial.print(“ROM =”);
  for( i = 0; i < 8; i++) {
    Serial.write(‘ ‘);
    Serial.print(addr, HEX);
  }
  if (OneWire::crc8(addr, 7) != addr[7]) {
      Serial.println(“CRC is not valid!”);
      return;
  }
  Serial.println();
  // the first ROM byte indicates which chip
  switch (addr[0]) {
    case 0x10:
      Serial.println(” Chip = DS18S20″); // or old DS1820
      type_s = 1;
      break;
    case 0x28:
      Serial.println(” Chip = DS18B20″);
      type_s = 0;
      break;
    case 0x22:
      Serial.println(” Chip = DS1822″);
      type_s = 0;
      break;
    default:
      Serial.println(“Device is not a DS18x20 family device.”);
      return;
  } 
  ds.reset();
  ds.select(addr);
  ds.write(0x44); // start conversion, use ds.write(0x44,1) with parasite power on at the end
  delay(1000); // maybe 750ms is enough, maybe not
  // we might do a ds.depower() here, but the reset will take care of it.
  present = ds.reset();
  ds.select(addr);    
  ds.write(0xBE); // Read Scratchpad
  Serial.print(” Data = “);
  Serial.print(present, HEX);
  Serial.print(” “);
  for ( i = 0; i < 9; i++) { // we need 9 bytes
    data = ds.read();
    Serial.print(data, HEX);
    Serial.print(” “);
  }
  Serial.print(” CRC=”);
  Serial.print(OneWire::crc8(data, 8), HEX);
  Serial.println();
  // Convert the data to actual temperature
  // because the result is a 16 bit signed integer, it should
  // be stored to an “int16_t” type, which is always 16 bits
  // even when compiled on a 32 bit processor.
  int16_t raw = (data[1] << 8) | data[0];
  if (type_s) {
    raw = raw << 3; // 9 bit resolution default
    if (data[7] == 0x10) {
      // “count remain” gives full 12 bit resolution
      raw = (raw & 0xFFF0) + 12 – data[6];
    }
  } else {
    byte cfg = (data[4] & 0x60);
    // at lower res, the low bits are undefined, so let’s zero them
    if (cfg == 0x00) raw = raw & ~7; // 9 bit resolution, 93.75 ms
    else if (cfg == 0x20) raw = raw & ~3; // 10 bit res, 187.5 ms
    else if (cfg == 0x40) raw = raw & ~1; // 11 bit res, 375 ms
    //// default is 12 bit resolution, 750 ms conversion time
  }
 celsius = (float)raw / 16.0;
 
  Serial.print(” Temperature = “);
  Serial.print(celsius);
  Serial.print(” Celsius, “);
 
  // listen for incoming clients
  WiFiClient client = server.available();
  if (client) {
    Serial.println(“new client”);
    // an http request ends with a blank line
    boolean currentLineIsBlank = true;
    while (client.connected()) {
      if (client.available()) {
        char c = client.read();
        Serial.write(c);
        // if you’ve gotten to the end of the line (received a newline
        // character) and the line is blank, the http request has ended,
        // so you can send a reply
        if (c == ‘n’ && currentLineIsBlank) {
          // send a standard http response header
          client.println(“HTTP/1.1 200 OK”);
          client.println(“Content-Type: text/html”);
          
          client.println(“Refresh: 5”); // refresh the page automatically every 5 sec
          client.println();
          client.println(“<!DOCTYPE HTML>”);
          client.println(“<html>”);
          
          client.print(“<head>”);
        
  client.print(“<DIV ALIGN=center>”);
   
  client.print(“<title>Monitorizacao temperatura</title>”);
    client.print(“<p>”); 
    client.print(“<br>”); 
 
  client.print(“MONITORIZACAO TEMPERATURA”);
 
  client.print(“<p>”); 
client.print(“<br>”); 
 
client.print(“</head>”);  
client.print(“<body>”);
client.print(“<table border=2 cellpadding=20 cellspacing=3>”);  
client.print(“<tr>”);  
client.print(“<td>Temperatura cilindro 1”);  
  client.println(“<td>”);
  client.println(celsius);
  client.println(“C”);
  client.println(“</td>”);
client.print(“</tr>”);  
client.print(“<tr>”);  
                    
client.print(“<td>Temperatura cilindro 2”);
 client.println(“<td>”);
 client.println(celsius);
 client.println(“C”);
 client.println(“</td>”);
client.print(“</tr>”); 
client.print(“<tr>”);  
client.print(“<td>Temperatura cilindro 3”);  
 client.println(“<td>”);
 client.println(celsius);
 client.println(“C”);
 client.println(“</td>”);
client.print(“</tr>”);  
client.print(“<tr>”);  
                    
client.print(“<td>Temperatura cilindro 4”);
 client.println(“<td>”);
 client.println(celsius);
 client.println(“C”);
 client.println(“</td>”);
client.print(“</tr>”); 
client.print(“<tr>”);  
client.print(“<td>Temperatura cilindro 5”);  
 client.println(“<td>”);
 client.println(celsius);
 client.println(“C”);
 client.println(“</td>”);
client.print(“</tr>”);  
client.print(“<tr>”);  
                    
client.print(“<td>Temperatura cilindro 6”);
 client.println(“<td>”);
 client.println(celsius);
 client.println(“C”);
 client.println(“</td>”);
client.print(“</tr>”); 
client.print(“<td>Pressao cilindros”);
 client.println(“<td>”);
 client.println(celsius);
 client.println(“C”);
 client.println(“</td>”);
client.print(“</tr>”); 
client.print(“</table>”);  
client.print(“</body>”);  
client.print(“<p>”); 
client.print(“<br>”); 
client.print(“Gestamp Cerveira”);
 
client.print(“<br>”);
client.print(“Francisco Silva 2015”);
client.print(“</html>”);  
          
         
            client.println(“<br />”);
          
          client.println(“</html>”);
          break;
        }
        if (c == ‘n’) {
          // you’re starting a new line
          currentLineIsBlank = true;
        }
        else if (c != ‘r’) {
          // you’ve gotten a character on the current line
          currentLineIsBlank = false;
        }
      }
    }
    // give the web browser time to receive the data
    delay(1);
    // close the connection:
    client.stop();
    Serial.println(“client disonnected”);
  }
}
void printWifiStatus() {
  // print the SSID of the network you’re attached to:
  Serial.print(“SSID: “);
  Serial.println(WiFi.SSID());
  // print your WiFi shield’s IP address:
  IPAddress ip = WiFi.localIP();
  Serial.print(“IP Address: “);
  Serial.println(ip);
  // print the received signal strength:
  long rssi = WiFi.RSSI();
  Serial.print(“signal strength (RSSI):”);
  Serial.print(rssi);
  Serial.println(” dBm”);
}

ReplyQuote
 Hans
(@hans)
Noble Member Admin
Joined: 8 years ago
Posts: 1958
Topic starter  

Hi Francisco,

Well, the first thought I had is to look at this article: Arduino Pull Data.

You'll see that I'm basically doing exactly what you're planning to do, I just use Ethernet (vs Wifi) and XML (vs HTML).
I captured the part that generate the temperature overview in a separate procedure (see below).

It goes through all the connected sensors.

Hope this helps ... 

void TemperaturesToXML(void) {
  byte counter;
  byte present = 0;
  byte sensor_type;
  byte data[12];
  byte addr[8];
  float celsius, fahrenheit;
  ds.reset_search();
  while ( ds.search(addr)) {
    client.println("<sensor>");
    // Get Serial number
    client.print(" <serial>");
    Serial.print("n Sensor : ");
    for( counter = 0; counter < 8; counter++) 
    {
      if (addr[counter]<10) client.print("0");
      client.print(String(addr[counter], HEX));
      client.print(" ");
      if (addr[counter]<10) Serial.print("0");
      Serial.print(String(addr[counter], HEX));
      Serial.print(" ");
    }
    Serial.println();
    client.println("</serial>");
    // Check CRC
    if (OneWire::crc8(addr, 7) != addr[7]) 
    {
        client.println(" <status>Invalid CRC</status>n</sensor>");
        Serial.println(" ERRORn");
        return;
    }
    client.println(" <status>OK</status>");
    // Get Chip type (the first ROM byte indicates which chip)
    client.print(" <chip>");
    switch (addr[0]) 
    {
      case 0x10:
        client.println("DS18S20");
        sensor_type = 1;
        break;
      case 0x28:
        client.println("DS18B20");
        sensor_type = 0;
        break;
      case 0x22:
        client.println("DS1822");
        sensor_type = 0;
        break;
      default:
        client.println("undefined</chip>");
        return;
    } 
    client.println("</chip>");
    ds.reset();
    ds.select(addr);
    ds.write(0x44); // start conversion, with regular (non-parasite!) power
    delay(1000); // maybe 750ms is enough, maybe not
    present = ds.reset();
    ds.select(addr);    
    ds.write(0xBE); // Read Scratchpad
    // Get Raw Temp Data, we need 9 bytes
    for ( counter = 0; counter < 9; counter++) 
    {           
      data[counter] = ds.read();
    }
    // Convert the data to actual temperature
    // because the result is a 16 bit signed integer, it should
    // be stored to an "int16_t" type
    int16_t raw = (data[1] << 8) | data[0];
    if (sensor_type) 
    {
      raw = raw << 3; // 9 bit resolution default
      if (data[7] == 0x10) {
        // "count remain" gives full 12 bit resolution
        raw = (raw & 0xFFF0) + 12 - data[6];
      }
    } 
    else 
    {
      // at lower res, the low bits are undefined, so let's zero them
      byte cfg = (data[4] & 0x60);
      //// default is 12 bit resolution, 750 ms conversion time
      if (cfg == 0x00) raw = raw & ~7; // 9 bit resolution, 93.75 ms
      else if (cfg == 0x20) raw = raw & ~3; // 10 bit res, 187.5 ms
      else if (cfg == 0x40) raw = raw & ~1; // 11 bit res, 375 ms
    }
    celsius = (float)raw / 16.0;
    fahrenheit = celsius * 1.8 + 32.0;
    client.print(" <celsius>");
    client.print(celsius);
    client.print("</celsius>n <fahrenheit>");
    client.print(fahrenheit);
    client.println("</fahrenheit>");
    Serial.print(" Temperature: ");
    Serial.print(celsius);
    Serial.print(" C (");
    Serial.print(fahrenheit);
    Serial.println(" F)");
    client.println("</sensor>");
  } 
  return;
}

ReplyQuote
 Hans
(@hans)
Noble Member Admin
Joined: 8 years ago
Posts: 1958
Topic starter  

You'd only need to modify:

....
    client.println("</serial>");
    // Check CRC
    if (OneWire::crc8(addr, 7) != addr[7]) 
    {
        client.println(" <status>Invalid CRC</status>n</sensor>");
        Serial.println(" ERRORn");
        return;
    }
    client.println(" <status>OK</status>");
    // Get Chip type (the first ROM byte indicates which chip)
    client.print(" <chip>"); ....

and

....
    client.print("  <celsius>");
    client.print(celsius);
    client.print("</celsius>n <fahrenheit>");
    client.print(fahrenheit);
    client.println("</fahrenheit>");
    Serial.print(" Temperature: ");
    Serial.print(celsius);
    Serial.print(" C (");
    Serial.print(fahrenheit);
    Serial.println(" F)");
    client.println("</sensor>"); ....

to format it into the table structure you're using in your HTML.


ReplyQuote

Like what you see and you'd like to help out? 

The best way to help is of course by assisting others with their questions here in the forum, but you can also help us out in other ways:

- Do your shopping at Amazon, it will not cost you anything extra but may generate a small commission for us,
- send a cup of coffee through PayPal ($5, $10, $20, or custom amount),
- become a Patreon,
- donate BitCoin (BTC), or BitCoinCash (BCH).

Share: