I have a waterproofed ds18b20 (Waterproof 1-Wire DS18B20 Digital temperature sensor : ID 381 : $9.95 : Adafruit Industries, Unique & fun DIY electronics and kits) that I'm using to take the temperature of an aquarium. I have it wired in parasitic mode and taking the temperature every 5 minutes.
At 5 minute intervals I query for the temp and then upload the variable to a webpage.
For the most part this works except every once in a while, I will get spikes on the webpage of 5000 and then drops to 0 (the normal value is 78).
Where do you think the problem lies?
Here is the code I'm using to get and send the data. This isn't all the code I'm running currently but I don't think the rest of it should be affecting it.
/*
DNS and DHCP-based Web client
This sketch connects to a website (http://www.google.com)
using an Arduino Wiznet Ethernet shield.
Circuit:
* Ethernet shield attached to pins 10, 11, 12, 13
created 18 Dec 2009
by David A. Mellis
modified 12 April 2011
by Tom Igoe, based on work by Adrian McEwen
*/
#include <SPI.h>
#include <Ethernet.h>
#include <OneWire.h>
// Enter a MAC address for your controller below.
// Newer Ethernet shields have a MAC address printed on a sticker on the shield
byte mac[] = { 0x00, 0xAA, 0xBB, 0xCC, 0xDE, 0x02 };
char serverName[] = "belk.jomber.com";
OneWire ds(2);
float getTemp();
// Initialize the Ethernet client library
// with the IP address and port of the server
// that you want to connect to (port 80 is default for HTTP):
EthernetClient client;
void setup() {
// start the serial library:
Serial.begin(9600);
// start the Ethernet connection:
if (Ethernet.begin(mac) == 0) {
Serial.println("Failed to configure Ethernet using DHCP");
// no point in carrying on, so do nothing forevermore:
while(true);
}
// give the Ethernet shield a second to initialize:
delay(1000);
Serial.println("connecting...");
// if you get a connection, report back via serial:
}
void loop()
{
if (client.connect(serverName, 80)) {
Serial.println("connected");
int tempValue = 12;
// Make a HTTP request:
client.print("GET /track.php?type=temp&user=nathan&value");
client.print("=");
client.print(getTemp());
client.println(" HTTP/1.1");
client.println("Host: belk.jomber.com");
client.println("User-Agent: Arduino");
client.println("Accept: text/html");
Serial.println("Sent stuff to server");
client.println();
}
else {
// kf you didn't get a connection to the server:
Serial.println("connection failed");
}
// if there are incoming bytes available
// from the server, read them and print them:
//if (client.available()) {
//char c = client.read();
//Serial.print(c);
while(client.connected())
{
// stay in this loop until the server closes the connection
// that is the signal it is finished sending
//Serial.print("Printing Available Data to Serial");
while(client.available()) {
char c = client.read();
Serial.write(c);
}
}
// }
// if the server's disconnected, stop the client:
if (!client.connected()) {
Serial.println();
Serial.println("disconnecting.");
client.stop();
// do nothing forevermore:
}
delay(300000);
}
float getTemp()
{
byte i;
byte present = 0;
byte type_s;
byte data[12];
byte addr[8];
float celsius, fahrenheit;
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[i], 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 NULL;
}
ds.reset();
ds.select(addr);
ds.write(0x44,1); // start conversion, 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[i] = ds.read();
// Serial.print(data[i], HEX);
// Serial.print(" ");
}
//Serial.print(" CRC=");
//Serial.print(OneWire::crc8(data, 8), HEX);
// Serial.println();
// convert the data to actual temperature
unsigned int 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);
if (cfg == 0x00) raw = raw << 3; // 9 bit resolution, 93.75 ms
else if (cfg == 0x20) raw = raw << 2; // 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;
fahrenheit = celsius * 1.8 + 32.0;
return fahrenheit;
//Serial.print(" Temperature = ");
//Serial.print(celsius);
//Serial.print(" Celsius, ");
//Serial.print(fahrenheit);
//Serial.println(" Fahrenheit");
}