http://www.codetorment.com/wp-content/uploads/DSCN9593-1.JPG
(รูปวงจร)
#include <OneWire.h>
#include <string.h>
#include <Ethernet.h>
int count = 0;
OneWire ds(8);
int HighByte, LowByte, TReading, SignBit, Tc_100, Tf_100, Whole, Fract;
int Tcount = 1;
int W[6];
int F[6];
int photocellPin = 0; // the cell and 10K pulldown are connected to a0
int light; // the analog reading from the sensor divider
byte mac[] = {
0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip[] = {
192, 168, 1, 36};
byte server[] = { 77, 222, 78, 32};
// String buffer
char buffer[256];
Client client(server, 80);
void setup()
{
Ethernet.begin(mac, ip);
Serial.begin(9600);
delay(1000);
// Serial.println("connecting...");
}
void loop()
{
byte i;
byte present = 0;
byte data[12];
byte addr[8];
if ( !ds.search(addr)) {
ds.reset_search();
return;
}
ds.reset();
ds.select(addr);
ds.write(0x44,1); // start conversion, with parasite power on at the end
delay(1000);
present = ds.reset();
ds.select(addr);
ds.write(0xBE); // Read Scratchpad
light = analogRead(photocellPin);
Serial.print("light intensity = ");
Serial.print(light);
Serial.print("\n");
for ( i = 0; i < 9; i++) { // we need 9 bytes
data
= ds.read();
}
LowByte = data[0];
HighByte = data[1];
TReading = (HighByte << 8) + LowByte;
SignBit = TReading & 0x8000; // test most sig bit
if (SignBit) // negative
{
TReading = (TReading ^ 0xffff) + 1; // 2's comp
}
Tc_100 = (6 * TReading) + TReading / 4; // multiply by (100 * 0.0625) or 6.25
Whole = Tc_100 / 100; // separate off the whole and fractional portions
Fract = Tc_100 % 100;
if (SignBit) // If its negative
{
Serial.print("-");
}
Serial.print(Whole);
Serial.print(".");
if (Fract < 10)
{
Serial.print("0");
}
Serial.print(Fract);
Serial.print(" C\n");
if (client.connect()) {
Serial.println("connected");
sprintf(buffer, "HEAD /path/to/script/newtemp.php?count=%d&temp=%d.%d&light=%d HTTP/1.1", Tcount, Whole, Fract, photocell);
client.println(buffer);
client.println("Host: yourdomain.com");
client.println("Connection: close");
client.println();
client.stop();
Tcount++;
}
else {
Serial.println("connection failed");
}
delay(60000); // wait 1 minute before sending new data
}
(code ของโปรแกรม)
arduino-วัดอุณภูมิ-DS1820 โค้ดรันไม่ได้ต้องทำยังไง
(รูปวงจร)
#include <OneWire.h>
#include <string.h>
#include <Ethernet.h>
int count = 0;
OneWire ds(8);
int HighByte, LowByte, TReading, SignBit, Tc_100, Tf_100, Whole, Fract;
int Tcount = 1;
int W[6];
int F[6];
int photocellPin = 0; // the cell and 10K pulldown are connected to a0
int light; // the analog reading from the sensor divider
byte mac[] = {
0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip[] = {
192, 168, 1, 36};
byte server[] = { 77, 222, 78, 32};
// String buffer
char buffer[256];
Client client(server, 80);
void setup()
{
Ethernet.begin(mac, ip);
Serial.begin(9600);
delay(1000);
// Serial.println("connecting...");
}
void loop()
{
byte i;
byte present = 0;
byte data[12];
byte addr[8];
if ( !ds.search(addr)) {
ds.reset_search();
return;
}
ds.reset();
ds.select(addr);
ds.write(0x44,1); // start conversion, with parasite power on at the end
delay(1000);
present = ds.reset();
ds.select(addr);
ds.write(0xBE); // Read Scratchpad
light = analogRead(photocellPin);
Serial.print("light intensity = ");
Serial.print(light);
Serial.print("\n");
for ( i = 0; i < 9; i++) { // we need 9 bytes
data = ds.read();
}
LowByte = data[0];
HighByte = data[1];
TReading = (HighByte << 8) + LowByte;
SignBit = TReading & 0x8000; // test most sig bit
if (SignBit) // negative
{
TReading = (TReading ^ 0xffff) + 1; // 2's comp
}
Tc_100 = (6 * TReading) + TReading / 4; // multiply by (100 * 0.0625) or 6.25
Whole = Tc_100 / 100; // separate off the whole and fractional portions
Fract = Tc_100 % 100;
if (SignBit) // If its negative
{
Serial.print("-");
}
Serial.print(Whole);
Serial.print(".");
if (Fract < 10)
{
Serial.print("0");
}
Serial.print(Fract);
Serial.print(" C\n");
if (client.connect()) {
Serial.println("connected");
sprintf(buffer, "HEAD /path/to/script/newtemp.php?count=%d&temp=%d.%d&light=%d HTTP/1.1", Tcount, Whole, Fract, photocell);
client.println(buffer);
client.println("Host: yourdomain.com");
client.println("Connection: close");
client.println();
client.stop();
Tcount++;
}
else {
Serial.println("connection failed");
}
delay(60000); // wait 1 minute before sending new data
}
(code ของโปรแกรม)