#include <OneWire.h>
int DS18S20_Pin = 2; //DS18S20 Signal pin on digital 2
//Temperature chip i/o
OneWire ds(DS18S20_Pin); // on digital pin 2
void setup(void) {
Serial.begin(9600);
}
void loop(void) {
float temperature = getTemp();
Serial.println(temperature);
delay(100); //just here to slow down the output so it is easier to read
}
float getTemp(){
//returns the temperature from one DS18S20 in DEG Celsius
byte data[12];
byte addr[8];
if ( !ds.search(addr)) {
//no more sensors on chain, reset search
ds.reset_search();
return -1000;
}
if ( OneWire::crc8( addr, 7) != addr[7]) {
Serial.println("CRC is not valid!");
return -1000;
}
if ( addr[0] != 0x10 && addr[0] != 0x28) {
Serial.print("Device is not recognized");
return -1000;
}
ds.reset();
ds.select(addr);
ds.write(0x44,1); // start conversion, with parasite power on at the end
byte present = ds.reset();
ds.select(addr);
ds.write(0xBE); // Read Scratchpad
for (int i = 0; i < 9; i++) { // we need 9 bytes
data[i] = ds.read();
}
ds.reset_search();
byte MSB = data[1];
byte LSB = data[0];
float tempRead = ((MSB << 8) | LSB); //using two's compliment
float TemperatureSum = tempRead / 16;
return TemperatureSum;
}
i then setup webserver with Mega and Ethernet shield in an attempt to display the temperature. but i do not know how to incorporate the HTML to "Client.print" the temperature to display on the webpage.
can someone please guide me in right direction (the HTML part is new to me actually)?
/*--------------------------------------------------------------
Program: eth_websrv_switch
Description: Arduino web server shows the state of a switch
on a web page. Does not use the SD card.
Hardware: Arduino Uno and official Arduino Ethernet
shield. Should work with other Arduinos and
compatible Ethernet shields.
Software: Developed using Arduino 1.0.3 software
Should be compatible with Arduino 1.0 +
References: - WebServer example by David A. Mellis and
modified by Tom Igoe
- Ethernet library documentation:
http://arduino.cc/en/Reference/Ethernet
Date: 12 January 2013
Author: W.A. Smith, http://startingelectronics.com
--------------------------------------------------------------*/
#include <OneWire.h> //##############################
#include <SPI.h>
#include <Ethernet.h>
int DS18S20_Pin = 22; //DS18S20 Signal pin on digital 2 //##############################
//Temperature chip i/o//##############################
OneWire ds(DS18S20_Pin); // on digital pin 21//##############################
// MAC address from Ethernet shield sticker under board
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(169, 254, 126, 222); // IP address, may need to change depending on network
EthernetServer server(80); // create a server at port 80
void setup()
{
Ethernet.begin(mac, ip); // initialize Ethernet device
server.begin(); // start to listen for clients
pinMode(3, INPUT); // input pin for switch
Serial.begin(9600);//##############################
}
void loop()
{
EthernetClient client = server.available(); // try to get client
if (client) { // got client?
boolean currentLineIsBlank = true;
while (client.connected()) {
if (client.available()) { // client data available to read
char c = client.read(); // read 1 byte (character) from client
// last line of client request is blank and ends with \n
// respond to client only after last line received
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("Connnection: close");
client.println();
// send web page
client.println("<!DOCTYPE html>");
client.println("<html>");
client.println("<head>");
client.println("<title>Arduino Read Switch State</title>");
client.println("<meta http-equiv=\"refresh\" content=\"1\">");
client.println("</head>");
client.println("<body>");
client.println("<h1>Switch</h1>");
client.println("<p>State of switch is:</p>");
GetSwitchState(client);
GetSwitchState1(client);// i added this line************************
client.println("temperature");// then this :(
client.println("</body>");
client.println("</html>");
break;
}
// every line of text received from the client ends with \r\n
if (c == '\n') {
// last character on line of received text
// starting new line with next character read
currentLineIsBlank = true;
}
else if (c != '\r') {
// a text character was received from client
currentLineIsBlank = false;
}
} // end if (client.available())
} // end while (client.connected())
delay(1); // give the web browser time to receive the data
client.stop(); // close the connection
} // end if (client)
float temperature = getTemp();//##############################
Serial.println(temperature);//##############################
delay(100); //just here to slow down the output so it is easier to read//##############################
}
void GetSwitchState1(EthernetClient cl)// i added this to return to the "web page" ************************
{ cl.println("<p>temperature</p>");
}
void GetSwitchState(EthernetClient cl)
{
if (digitalRead(3)) {
cl.println("<p>ON</p>");
}
else {
cl.println("<p>OFF</p>");
}
}
float getTemp(){//##############################
//returns the temperature from one DS18S20 in DEG Celsius
byte data[12];//##############################
byte addr[8];//##############################
if ( !ds.search(addr)) {
//no more sensors on chain, reset search
ds.reset_search();//##############################
return -1000;//##############################
}
if ( OneWire::crc8( addr, 7) != addr[7]) {//##############################
Serial.println("CRC is not valid!");//##############################
return -1000;//##############################
}
if ( addr[0] != 0x10 && addr[0] != 0x28) {//##############################
Serial.print("Device is not recognized");//##############################
return -1000;//##############################
}
ds.reset();//##############################
ds.select(addr);//##############################
ds.write(0x44,1); // start conversion, with parasite power on at the end//##############################
byte present = ds.reset();//##############################
ds.select(addr); //##############################
ds.write(0xBE); // Read Scratchpad//##############################
for (int i = 0; i < 9; i++) { // we need 9 bytes//##############################
data[i] = ds.read();//##############################
}
ds.reset_search();//##############################
byte MSB = data[1];//##############################
byte LSB = data[0];//##############################
float tempRead = ((MSB << 8) | LSB); //using two's compliment//##############################
float TemperatureSum = tempRead / 16;//##############################
return TemperatureSum;//##############################
}
Writing to a serial output or to a webserver is actually the same.
When you view the page in the webbrower, have a look at the html source code. I think Ctrl+U in Opera browser.
You will see the HTML code that you made yourself in the sketch with the "" and the "" and so on.
So if you want the print the text temperature, do this: client.println("temperature");
But if you want to print the actual temperature, you print the variable: client.println(temperature);
However, you do have a problem with the declaration of the float temperature.
You do that somewhere at the bottom of loop().
Please declare the temperature at the top of loop().
client.println("Hello, I am a webserver");
client.print("The temperature is ");
client.print(temperature);
client.println("°C");
if (temperature < 20.0)
client.println("It is cold.");
else
client.println("It is warm.");
SamsungACProject:
Thank you guys for the assistance.
i just declared it at the beginning of the loop and it worked - how cool (stupid of me) is that.
Have a look at my Arduino/Ethermega home automation system at http://219.88.69.69/2WG/ (exact syntax important) for an Arduino project running six temperature/humidity sensors and publishing hourly statistics plus daily minimums and maximums for the previous seven days (all using arrays with backup and reloading upon restart using an SD card).
At http://219.88.69.69/PUBLIC.DIR/ there are some files you can browse that will help understand how the system is currently working. I intend putting code in that directory showing the application's key functionality including its html web publishing, its cookie based web security and the SD card directory browsing and security functionality.
CatweazleNZ that is expert level - will still need to look at it again..
OK, so initially i had the issue where with displaying the 'temperature' on the webserver (arduino) which was fixed by using "client.println(temperature);" and declaring properly.
However, now i made a simple webpage on SD card and i have the same problem again- i cannot display temperature on wepage (on micro SD card in the ethernet shield).
most of what i come across is not for SD card webserver. Please provide guidance how to:
-SD card webserver read and display a variable from arduino (temperature - in my case)
-use the GET command when a button is pressed (by client) on server page (on SD) to turn a digital pin Low/High.
Arduino code (note the ##'s are lines from the temperature sensor code example i added)
#include <SPI.h>
#include <Ethernet.h>
#include <SD.h>
#include <OneWire.h> //##############################
// MAC address from Ethernet shield sticker under board
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(169,254,126, 222); // IP address, may need to change depending on network
EthernetServer server(80); // create a server at port 80
int DS18S20_Pin = 22; //DS18S20 Signal pin on digital 2 //##############################
//Temperature chip i/o//##############################
OneWire ds(DS18S20_Pin); // on digital pin 21//##############################
File webFile;
void setup()
{
float temperature = getTemp();//##############################
Serial.println(temperature);//##############################
delay(100); //just here to slow down the output so it is easier to read//##############################
Ethernet.begin(mac, ip); // initialize Ethernet device
server.begin(); // start to listen for clients
Serial.begin(9600); // for debugging
// initialize SD card
Serial.println("Initializing SD card...");
if (!SD.begin(4)) {
Serial.println("ERROR - SD card initialization failed!");
return; // init failed
}
Serial.println("SUCCESS - SD card initialized.");
// check for index.htm file
if (!SD.exists("INDEXH~1.TXT")) {
Serial.println("ERROR - Can't find index.htm file!");
return; // can't find index file
}
Serial.println("SUCCESS - Found index.htm file.");
}
void loop()
{
EthernetClient client = server.available(); // try to get client
if (client) { // got client?
boolean currentLineIsBlank = true;
while (client.connected()) {
if (client.available()) { // client data available to read
char c = client.read(); // read 1 byte (character) from client
// last line of client request is blank and ends with \n
// respond to client only after last line received
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("Connection: close");
client.println();
// send web page
webFile = SD.open("INDEXH~1.TXT"); // open web page file
if (webFile) {
while(webFile.available()) {
client.write(webFile.read()); // send web page to client
}
webFile.close();
}
break;
}
// every line of text received from the client ends with \r\n
if (c == '\n') {
// last character on line of received text
// starting new line with next character read
currentLineIsBlank = true;
}
else if (c != '\r') {
// a text character was received from client
currentLineIsBlank = false;
}
} // end if (client.available())
} // end while (client.connected())
delay(1); // give the web browser time to receive the data
client.stop(); // close the connection
} // end if (client)
}
float getTemp(){//##############################
//returns the temperature from one DS18S20 in DEG Celsius
byte data[12];//##############################
byte addr[8];//##############################
if ( !ds.search(addr)) {
//no more sensors on chain, reset search
ds.reset_search();//##############################
return -1000;//##############################
}
if ( OneWire::crc8( addr, 7) != addr[7]) {//##############################
Serial.println("CRC is not valid!");//##############################
return -1000;//##############################
}
if ( addr[0] != 0x10 && addr[0] != 0x28) {//##############################
Serial.print("Device is not recognized");//##############################
return -1000;//##############################
}
ds.reset();//##############################
ds.select(addr);//##############################
ds.write(0x44,1); // start conversion, with parasite power on at the end//##############################
byte present = ds.reset();//##############################
ds.select(addr); //##############################
ds.write(0xBE); // Read Scratchpad//##############################
for (int i = 0; i < 9; i++) { // we need 9 bytes//##############################
data[i] = ds.read();//##############################
}
ds.reset_search();//##############################
byte MSB = data[1];//##############################
byte LSB = data[0];//##############################
float tempRead = ((MSB << 8) | LSB); //using two's compliment//##############################
float TemperatureSum = tempRead / 16;//##############################
return TemperatureSum;//##############################
}
HTML code (see right at bottom 2 lines before )
<!DOCTYPE html>
<html>
<head>
<title>Arduino SD Card Web Page</title>
</head>
<body>
<h2>Hello from the Arduino SD Card!</h2>
<H5>Time and note PC recognise this as "INDEXH~1.TXT"</H3>
<!--"Comment: script to display the time"-->
<body onload="startTime()">
<div id="txt"></div>
<script>
function startTime()
{
var today=new Date();
var h=today.getHours();
var m=today.getMinutes();
var s=today.getSeconds();
// add a zero in front of numbers<10
m=checkTime(m);
s=checkTime(s);
document.getElementById('txt').innerHTML=h+":"+m+":"+s;
t=setTimeout(function(){startTime()},500);
}
function checkTime(i)
{
if (i<10)
{
i="0" + i;
}
return i;
}
</script>
<!--Comment: this script is run when on/ off buttons are pressed to change the value of the label-->
<script type="text/javascript">
function changePriceon(){ document.getElementById('price11').innerHTML = 'ON';}
function changePriceoff(){ document.getElementById('price11').innerHTML = 'OFF';}
</script>
<h2> Room Lights</h2>
<!--"Comment: Create table-->
<table border=2>
<!--"Comment: create new row-->
<tr>
<th>Room 1</th>
</tr>
<!--"Comment: create new row with 2 buttons and a label-->
<tr>
<td>
<button name=b value=1 type=submit onclick=changePriceon() style=height:40px;width:40px>LED on</button>
<button name=b value=6 type=submit onclick=changePriceoff() style=height:40px;width:40px>LED off</button>
<label for=price1 id=price11>off</label>
</td>
</tr>
</table>
<h2>Aircon</h2>
<table border="2">
<tr>
<th>Room 1</th>
</tr>
<tr>
<td> + </td>
<td> hgf </td>
</tr>
<tr>
<td> - </td>
</tr>
<tr>
<td> <input type="radio" name="L1" value="ON">ON </td>
</tr>
<tr>
<td> <input type="radio" name="L1" value="OFF">OFF</td>
</tr>
</table>
<h2>Temperature </h2>
<table border="2">
<tr>
<th>Room 1</th>
</tr>
<tr>
<td> x degrees </td>
</tr>
</table>
<h2>Motion Sensor </h2>
<table border=2>
<tr>
<th>Room 1</th>
</tr>
<tr>
<td> No motion/ Motion </td>
</tr>
</table>
<h3>X </h3>
<p>temperature</p>
client.println(temperature);// then this :(
</body>
</html>
You are reading text from the SD card and sending the text to the client. What you have is equivalent to:
client.print("client.print(temperature);");
which, as you can imagine, is useless for sending the temperature to the client.
What you need is a way for the Arduino to know that it needs to substitute a value. You could have something like:
The temperature is %temperature%.
in the file on the SD card. The Arduino would then read a complete record, and check for %. No %, and the record would be send to the client. If there is a %, find the next one, determine what value should be substituted, perform the substitution, and send the resulting string to the client.
I have a webpage on the microSD card, as "INDEX.HTM"
In that webpage I have the text "$A" and "$B" and "$C".
I use '$' as special character, but you can use an other one, like '%'.
This line: "client.write(webFile.read());" reads a character and writes it to the webpage.
I test the character from the microSD card first if it is a '$'. If so, I read the next character.
That next character defines what I will write to the webpage from the sketch.
For example '$A' could inject the temperature in the webpage.
uint8_t data = webFile.read();
if (data == '
) // test the read character
{
data = webFile.read(); // read next character
switch (data)
{
case 'A':
client.print (getTemperature());
break;
case 'B':
..........
}
}
else
{
// A normal character for the webpage
client.write(data);
}
Caltoa:
CatweazleNZ, that is very cool.
I have some remarks:
Can you use the ° for the temperature.
What kind of security is that, when you put the status online ?
Thanks for the heads up on the ° character.
What part of online do you think unwise? Without knowing the password and logging in you cannot tell if the system is running in alarm mode or not. You also don't know how the alarm is triggered and what happens when the alarm is activated. You might guess that I am not home from the PIR reading - you can also make a good guess on that by seeing if my car is parked in the drive or knocking on the from door.
Is displaying the fact that an alarm exists the same as a sticker on the front door that says "These premises are protected by electronic security". I think advertising the existence of an alarm is a reasonable deterrent against the casual burglar.
In that is the case, you can add fake things like "dog barking level", or "lawn vibrations" or "drone infrared human detector".
But seriously, I would never publish the garage door status and other things like that.
Caltoa:
In that is the case, you can add fake things like "dog barking level", or "lawn vibrations" or "drone infrared human detector".
But seriously, I would never publish the garage door status and other things like that.
My bathroom functionality is currently fake - unimplemented functionality to get to in a couple of months. But I might consider the fake idea further. I could randomly update the PIR sensor times for external browsers to create the impression that someone is home and moving around the house. And I guess I could do that with the garage door sensors - or I might yet suppress the information as you suggest.
But anyway, if the garage says it is closed what would someone gain? Either I am home and if I am not the place is likely secure with an alarm system running.
If the garage is open you might assume that I am home or not - so you wait to see if the door is still open in ten or thirty minutes. But ... depending on the garage operating mode the door will automatically close after four minutes anyway OR it will email my iPhone using a push email after four minutes and repeatedly thereafter that until I close the garage door remotely. (I did leave the door open once and was saved by an email.)
Incidentally - I often leave the garage door open for an hour or more when I am out the front working on my car or doing some gardening. If any local web browser person notes that my garage door has been open for say an hour and comes around to check out what is in my garage then I will be at the front of the house to greet them.
for simplicity (as my arduino is long) i refer to the digital temp sensor code. How can the "int DS18S20_Pin = 2;" part of the code be manipulated (maybe in a For loop?) to read temp on other inputs as well i.e from digital pin 31 to 40 ?
i did it for D31 in my arduino code and the webpage update every second. i need it for D31-D40 but need Help please........
digital temp sensor code
#include <OneWire.h>
int DS18S20_Pin = 2; //DS18S20 Signal pin on digital 2
//Temperature chip i/o
OneWire ds(DS18S20_Pin); // on digital pin 2
void setup(void) {
Serial.begin(9600);
}
void loop(void) {
float temperature = getTemp();
Serial.println(temperature);
delay(100); //just here to slow down the output so it is easier to read
}
float getTemp(){
//returns the temperature from one DS18S20 in DEG Celsius
byte data[12];
byte addr[8];
if ( !ds.search(addr)) {
//no more sensors on chain, reset search
ds.reset_search();
return -1000;
}
if ( OneWire::crc8( addr, 7) != addr[7]) {
Serial.println("CRC is not valid!");
return -1000;
}
if ( addr[0] != 0x10 && addr[0] != 0x28) {
Serial.print("Device is not recognized");
return -1000;
}
ds.reset();
ds.select(addr);
ds.write(0x44,1); // start conversion, with parasite power on at the end
byte present = ds.reset();
ds.select(addr);
ds.write(0xBE); // Read Scratchpad
for (int i = 0; i < 9; i++) { // we need 9 bytes
data[i] = ds.read();
}
ds.reset_search();
byte MSB = data[1];
byte LSB = data[0];
float tempRead = ((MSB << 8) | LSB); //using two's compliment
float TemperatureSum = tempRead / 16;
return TemperatureSum;
}
in the arduino code [see lower part of void XML_response(EthernetClient cl) ] float temperature = getTemp() is called, however int DS18S20_Pin = 31 is declared (in beginning of code) and will therefore only read the temperature on pin 31.
how can the code be changed to read temperature from all pins 31-40, and not only 31?
How can the "int DS18S20_Pin = 2;" part of the code be manipulated (maybe in a For loop?) to read temp on other inputs as well i.e from digital pin 31 to 40 ?
Why do this when one of the advantages of that sensor is that you can attach more than one on a single pin?
thank you guys for the responses. @wildbill. i am new to arduino and at this stage prefer to have 1 temp sensor per pin.
i Managed to read and display 2 temperatures from 2 arduino pins (D31 and D32) and display on webserver (see image-note i only have temp sensor on D31)..
However the code is quite long and i still need to repeat for 10 arduino pins so it's tedious i guess XD... It would be appreciated if someone can advise how to shorten the modified temp code. i have also added the original temp code for reference.
modified temp code (pins 31 and 32)
(ignore the //xml comments as this was used to send the temp reading to the browser)
#include <OneWire.h>
//int DS18S20_Pin = 2; //DS18S20 Signal pin on digital 2 - removed from origional example
//Temperature chip i/o
//OneWire ds(DS18S20_Pin); // on digital pin 2 - removed from origional example
int DS18S20_Pin31 = 31; // Zoomkat proposal
OneWire ds31(DS18S20_Pin31); // Zoomkat proposal
int DS18S20_Pin32 = 32;
OneWire ds32(DS18S20_Pin32);
void setup(void) {
Serial.begin(9600);
}
void loop(void) {
float temperature31 = getTemp31(); // added this to call the function
Serial.println("temperature31");// added this to display in the serial what is being monitored
Serial.println(temperature31);// added this to then display specific port
delay(1000); //just here to slow down the output so it is easier to read
//xml cl.print("<temp>");
//xml cl.print(temperature31);
//xml cl.println("</temp>");
float temperature32 = getTemp32();
Serial.println("temperature32");
Serial.println(temperature32);
delay(1000); //just here to slow down the output so it is easier to read
//xml cl.print("<temp>");
//xml cl.print(temperature32);
//xml cl.println("</temp>");
}
float getTemp31(){
//returns the temperature from one DS18S20 in DEG Celsius
byte data[12];
byte addr[8];
if ( !ds31.search(addr)) {
//no more sensors on chain, reset search
ds31.reset_search();
return -1000;
}
if ( OneWire::crc8( addr, 7) != addr[7]) {
Serial.println("CRC is not valid!");
return -1000;
}
if ( addr[0] != 0x10 && addr[0] != 0x28) {
Serial.print("Device is not recognized");
return -1000;
}
ds31.reset();
ds31.select(addr);
ds31.write(0x44,1); // start conversion, with parasite power on at the end
byte present = ds31.reset();
ds31.select(addr);
ds31.write(0xBE); // Read Scratchpad
for (int i = 0; i < 9; i++) { // we need 9 bytes
data[i] = ds31.read();
}
ds31.reset_search();
byte MSB = data[1];
byte LSB = data[0];
float tempRead = ((MSB << 8) | LSB); //using two's compliment
float TemperatureSum = tempRead / 16;
return TemperatureSum;
}
float getTemp32(){
//returns the temperature from one DS18S20 in DEG Celsius
byte data[12];
byte addr[8];
if ( !ds32.search(addr)) {
//no more sensors on chain, reset search
ds32.reset_search();
return -1000;
}
if ( OneWire::crc8( addr, 7) != addr[7]) {
Serial.println("CRC is not valid!");
return -1000;
}
if ( addr[0] != 0x10 && addr[0] != 0x28) {
Serial.print("Device is not recognized");
return -1000;
}
ds32.reset();
ds32.select(addr);
ds32.write(0x44,1); // start conversion, with parasite power on at the end
byte present = ds32.reset();
ds32.select(addr);
ds32.write(0xBE); // Read Scratchpad
for (int i = 0; i < 9; i++) { // we need 9 bytes
data[i] = ds32.read();
}
ds32.reset_search();
byte MSB = data[1];
byte LSB = data[0];
float tempRead = ((MSB << 8) | LSB); //using two's compliment
float TemperatureSum = tempRead / 16;
return TemperatureSum;
}
original temp code example (pin 2)
#include <OneWire.h>
int DS18S20_Pin = 2; //DS18S20 Signal pin on digital 2
//Temperature chip i/o
OneWire ds(DS18S20_Pin); // on digital pin 2
void setup(void) {
Serial.begin(9600);
}
void loop(void) {
float temperature = getTemp();
Serial.println(temperature);
delay(100); //just here to slow down the output so it is easier to read
}
float getTemp(){
//returns the temperature from one DS18S20 in DEG Celsius
byte data[12];
byte addr[8];
if ( !ds.search(addr)) {
//no more sensors on chain, reset search
ds.reset_search();
return -1000;
}
if ( OneWire::crc8( addr, 7) != addr[7]) {
Serial.println("CRC is not valid!");
return -1000;
}
if ( addr[0] != 0x10 && addr[0] != 0x28) {
Serial.print("Device is not recognized");
return -1000;
}
ds.reset();
ds.select(addr);
ds.write(0x44,1); // start conversion, with parasite power on at the end
byte present = ds.reset();
ds.select(addr);
ds.write(0xBE); // Read Scratchpad
for (int i = 0; i < 9; i++) { // we need 9 bytes
data[i] = ds.read();
}
ds.reset_search();
byte MSB = data[1];
byte LSB = data[0];
float tempRead = ((MSB << 8) | LSB); //using two's compliment
float TemperatureSum = tempRead / 16;
return TemperatureSum;
}
hi,
thanks for all the help so far everyone. this is to update and possibly help others trying to do the same.
i proceeded (with the tedious manner) to display 10 x temperature sensor values on webpage - with the image attached i just have 1 x sensor connected currently.