- Blog entry posted in 'Electronics and Other Ramblings...', May 31, 2014.
As a follow-up to the previous blog, I proceeded to implement the DS18B20 for temperature sensing within the same Arduino board. It is now able to report to the Sparkfun Data Server. In and of itself this is no big deal, but this is a really good beginning for going into displaying stuff around the house.
Updated code follows...
Code:
/*
Sparkfun Date Web client
This sketch connects to the Sparkfun Data feed (http://data.sparkfun.com).
It uses the Arduino Wiznet Ethernet shield.
Senses temperature using DS18B20 and pushes this data to Sparkfun Data feed.
Circuit:
* Ethernet shield attached to pins 10, 11, 12, 13
* DS18B20 attached to pin 2
created 30 May 2014 by @languer
*/
#include <SPI.h>
#include <Ethernet.h>
#include <OneWire.h>
// Enter a MAC address for your controller below.
byte mac = {0xDE, 0xAD, 0xBE, 0xEF, 0xFF, 0x01};
char server = "data.sparkfun.com";
char publicKey = "/input/Jxyjr7DmxwTD5dG1D1Kv";
char privateKey = "?private_key=gzgnB4VazkIg7GN1g1qA";
// Initialize the Ethernet client library
EthernetClient client;
// DS18B20 pin
OneWire ds(2); // on pin 2 (a 4.7K resistor is necessary)
void setup()
{
Serial.begin(9600);
delay(2500);
Serial.println("Sparkfun Web Client");
// Open serial communications and wait for port to open:
Ethernet.begin(mac);
sendFirstMessage();
}
void loop()
{
// if connection was successful, send data to display
if (client.connect(server, 80))
{
String var = "";
var = var + readDS18B20();
Serial.println("sending data...");
client.print("GET ");
client.print(publicKey);
client.print(privateKey);
client.println(var);
client.println("Host: data.sparkfun.com");
client.println("Connection: close");
client.println();
Serial.println("data sent...");
}
// read available server data
delay(15000); // give server 15secs to respond
if (client.available())
{
Serial.println("reading data...");
do
{
char c = client.read();
Serial.print(c);
} while (client.available());
Serial.println("data read...");
}
// if the server's disconnected, stop the client:
if (!client.connected())
{
Serial.println();
Serial.println("disconnecting.");
client.stop();
// do nothing forevermore - remove to continuously send data
//while(true);
}
// repeat every 15secs
delay(15000);
}
void sendFirstMessage()
{
String s = "&brewTemp=Live%20From%20Arduino";
if (client.connect(server, 80))
{
Serial.println("sending data...");
client.print("GET ");
client.print(publicKey);
client.print(privateKey);
client.println(s);
client.println("Host: data.sparkfun.com");
client.println("Connection: close");
client.println();
Serial.println("data sent...");
Serial.println("disconnecting.");
client.stop();
}
// read available server data
delay(15000); // give server 15secs to respond
if (client.available())
{
Serial.println("reading data...");
do
{
char c = client.read();
} while (client.available());
}
if (!client.connected())
{
Serial.println("disconnecting.");
client.stop();
}
}
String readDS18B20()
{
byte i;
byte data;
String var;
// conversion routine
ds.reset();
ds.skip();
ds.write(0x44, 1); // start conversion, with parasite power on at the end
delay(1000); // delay to allow for conversion
// read temperature
ds.reset();
ds.skip();
ds.write(0xBE); // Read Scratchpad
for ( i = 0; i < 9; i++) // temperature data consists of 9 bytes
{
data = 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, which is always 16 bits
// even when compiled on a 32 bit processor.
int16_t raw = (data << 8) | data;
byte cfg = (data & 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
long c_2dec, f_2dec;
// calculate celsius to 2 decimal places
c_2dec = raw;
c_2dec = (c_2dec * 100) / 16;
// calculate farenheit to 2 decimal places
f_2dec = ((c_2dec * 9) / 5) + 3200;
String c_temp, f_temp;
c_temp = long2String(c_2dec);
f_temp = long2String(f_2dec);
var = "&brewTemp=";
var = var + c_temp + "degC,%20";
var = var + f_temp + "degF";
return var;
}
String long2String(long d)
{
String s;
long d1, d2;
d1 = d / 100;
s = String(d1) + ".";
d2 = d % 100;
if (d2 < 10)
{
s = s + "0" + String(d2);
}
else
{
s = s + String(d2);
}
return s;
}
languer, May 31, 2014
For anyone who is mildly interested in this, remember the Sparkfun Data Server is completely free. It is a service they do for the community (like "you scratch my back, I'll scratch yours"). They figure the easier they make it for people to get into electronics, the better their business. One of my favorite companies for sure (also have to plug Adafruit and Pololu).
languer, June 08, 2014
I attempted to do a quick VirtualWire implementation to the sensors I had available which already contain a PIC controller. This resulted in much more work than I was anticipating. So instead I am translating the code I already have working on my OOK Sensors ( [url]http://www.electro-tech-online.com/blog-entries/ook-wireless-transmit-receive-pair-–-simple-pro.266/[/url] ) into the Arduino which is providing the Web interface. So far a direct interface between PIC and Arduino (no special encoding just yet) is working quite good.
languer, August 23, 2014
Follow-on post on [url]http://www.electro-tech-online.com/blog-entries/arduino-internet-of-things-continued-3.749/[/url]