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”);
}