the model is arduino UNO
#include <Ethernet.h>
#include <SPI.h>
// DS18B20
#include <OneWire.h>
#include <DallasTemperature.h>
// Data wire is plugged into pin 3 on the Arduino
#define ONE_WIRE_BUS 3
// Setup a oneWire instance to communicate with any OneWire devices
OneWire oneWire(ONE_WIRE_BUS);
// Pass our oneWire reference to Dallas Temperature.
DallasTemperature sensors(&oneWire);
// Assign the addresses of your 1-Wire temp sensors.
DeviceAddress insideThermometer = {
0x28, 0x67, 0xE7, 0x47, 0x04, 0x00, 0x00, 0x2B };
//dataino.it
// remember to chang mac!!!
// this must be unique
byte mac[] = {
0x90,0xA2,0xDA,0x0D,0x82,0x39};
// change to your network settings
IPAddress ip(192,168,6,91);
IPAddress gateway(192, 168, 6, 90);
IPAddress subnet(255, 255, 255, 0);
// LOCAL HOST SERVER
//IPAddress server(192,168,6,90); //localhost
//String API_key = "41uX-u50e-84d4-8f0u-kP0s";
//~ ONLINE SERVER !!!
IPAddress server(94,23,64,3); //dataino.it
String API_key = "68bS-4p6r-r8FR-3W0X-bZ68";
String cmd_find = "/dataino.it/api/find_value.php?api-KEY=";
String cmd_add = "/dataino.it/api/add_value.php?api-KEY=";
String cmd_read = "/dataino.it/api/read_value.php?api-KEY=";
String cmd_delete = "/dataino.it/api/delete_value.php?api-KEY=";
String cmd_set = "/dataino.it/api/set_value.php?api-KEY=";
String command = "";
String var[]={
"Temp="};
int totalCount = 0;
int loopCount = 0;
char pageAdd[128];
EthernetClient client;
// S E T U P
void setup(void)
{
// start serial port
Serial.begin(9600);
sensors.begin();
// set the resolution to 10 bit (good enough?)
sensors.setResolution(insideThermometer, 10);
// Start ethernet
Serial.println(F("Starting ethernet..."));
Ethernet.begin(mac, ip, gateway, gateway, subnet);
// If using dhcp, comment out the line above
// and uncomment the next 2 lines
// if(!Ethernet.begin(mac)) Serial.println(F("failed"));
// else Serial.println(F("ok"));
//digitalWrite(10,HIGH);
Serial.println(Ethernet.localIP());
delay(2000);
Serial.println(F("Ready"));
}
// PRINT TEMP
void printTemperature(DeviceAddress deviceAddress)
{
// DS18B20
float tempC = sensors.getTempC(deviceAddress);
if (tempC == -127.00) {
Serial.print("Error getting temperature");
}
else {
Serial.print("C: ");
Serial.print(tempC);
Serial.print(" F: ");
Serial.print(DallasTemperature::toFahrenheit(tempC));
// dataino.it
String val0= ftoa(tempC,2,5);
command = cmd_add + API_key + "&" + var[0] + val0 + "\0";
command.toCharArray(pageAdd, 128);
}
}
// DS18B20
void loop(void)
{
if(loopCount < 50)
{
// if loopCount is less than 50, just delay a 100 millisecond
delay(100);
Serial.print(".");
}
else
{
// every tree seconds this runs
loopCount = 0;
Serial.print("\n\r");
// DS18B20
Serial.print("Getting temperatures...\n\r");
sensors.requestTemperatures();
Serial.print("Inside temperature is: ");
printTemperature(insideThermometer);
Serial.print("\n\r");
//================================================================================
if(!getPage(server,pageAdd)) Serial.print(F("Fail "));
else Serial.print(F("Pass "));
totalCount++;
Serial.println(totalCount,DEC);
}
loopCount++;
}
//===============================================================================
// dataino.it
//===============================================================================
byte getPage(IPAddress ipBuf,char *page)
{
int inChar;
char outBuf[128];
Serial.print(F("connecting..."));
if(client.connect(ipBuf,80))
{
Serial.println(F("connected"));
sprintf(outBuf,"GET %s HTTP/1.0\r\n\r\n",page);
client.write(outBuf);
Serial.println(F("outBuf"));
}
else
{
Serial.println(F("failed"));
return 0;
}
// connectLoop controls the hardware fail timeout
int connectLoop = 0;
//Unsigned long variables are extended size variables for number storage,
//and store 32 bits (4 bytes). Unlike standard longs unsigned longs
//won't store negative numbers, making their range from 0 to 4,294,967,295
long VAL[16];
while(client.connected())
{
while(client.available())
{
inChar = client.read();
if (inChar==char('#'))
{
inChar = client.read();
while (inChar!='#'){
int count_val =0 ;
unsigned long i = 1;
VAL[count_val]=0;
do {
VAL[count_val] = VAL[count_val] + (inChar-48) * i;
i=i*10;
inChar = client.read();
}
while ((inChar!=char(';')));
inChar = client.read();
//Serial.print(inChar);
if (inChar==char('*')){
Serial.print("Query=");
Serial.print(VAL[count_val]);
break;
}
count_val=count_val+1;
}
}
// set connectLoop to zero if a packet arrives
connectLoop = 0;
}
connectLoop++;
}
// if more than 10000 milliseconds since the last packet
if(connectLoop > 10000)
{
// then close the connection from this end.
Serial.println();
Serial.println(F("Timeout"));
client.stop();
}
// this is a delay for the connectLoop timing
delay(1);
Serial.println();
Serial.println(F("disconnecting."));
// close client end
client.stop();
return 1;
}
String ftoa(float number, uint8_t precision, uint8_t size) {
String s = "";
// Negative
if (number < 0.0) {
s = "-";
number = -number;
}
double rounding = 0.5;
for (uint8_t i = 0; i < precision; ++i) rounding /= 10.0;
number += rounding;
s += String(uint16_t(number)); // prints the integer part
if(precision > 0) {
s += "."; // prints the decimal point
uint32_t frac;
uint32_t mult = 1;
uint8_t padding = precision -1;
while(precision--) mult *= 10;
frac = (number - uint16_t(number)) * mult;
uint32_t frac1 = frac;
while(frac1 /= 10) padding--;
while(padding--) s += "0";
s += String(frac,DEC) ; // prints the fractional part
}
if (size>0) // checks size
if (s.length()>size) return("#");
else while(s.length()<size) s = " "+s;
return s;
}