Hello, below is the sketch i use to read data from 2 one wire sensord. but the readings are getting fault.
When i use a test program without the network stuff the readings are correct.
what could be wrong.
#include <OneWire.h>
#include <DallasTemperature.h>
#include <SPI.h>
#include <Ethernet.h>
//#include "EmonLib.h"
// Data wire is plugged into pin 2 on the Arduino
#define ONE_WIRE_BUS 53
// Setup a oneWire instance to communicate with any OneWire devices
// (not just Maxim/Dallas temperature ICs)
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
//Setup network settings
// assign a MAC address for the ethernet controller.
// fill in your address here:
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
// fill in an available IP address on your network here, if DHCP fails
IPAddress ip(192, 168,1, 237);
IPAddress myDns(192, 168, 1, 1);
// initialize the library instance:
class EthernetClient client;
unsigned long lastConnectionTime = 0; // last time you connected to the server, in milliseconds
boolean lastConnected = false; // state of the connection last time through the main loop
const unsigned long postingInterval = 60L * 100L; // delay between updates, in milliseconds
// EmonCMS server ip-adres
char EmonServer[] = "192.168.1.102";
char apikey[] ="913f557706d069745bfe8dac70040261";
const int wdTimer = 6; // seconds to wait before sending information
int second=0; // used to count number of seconds elapsed
//Setup EmonCMS payload
typedef struct
{
String InputName;
int InputValue;
int Pinnummer;
} EmoncmsInput;
EmoncmsInput emoncms[6] = {
{"ElektraWatt", 0,1},
{"ElektraVolt",0,A1},
{"ElektraStroom",0,A2},
{"WaterLiter",0,A3},
{"TemperatuurBuiten",0,13},
{"TemperatuurBinnen",0,13}
};
void setup() {
// start serial port:
Serial.begin(9600);
delay(1000); //Wait for Ethernet shield to be powerd on
Serial.println("Starup board");
sensors.begin();
// start the Ethernet connection using DHCP:
if (Ethernet.begin(mac) == 0) {
Serial.println("Failed to configure Ethernet using DHCP");
// no point in carrying on, so do nothing forevermore:
// try to congifure using IP address instead of DHCP:
Ethernet.begin(mac,ip);
}
// print the Ethernet board/shield's IP address:
Serial.print("Ethernet board startuped, your ip adres is ");
Serial.println(Ethernet.localIP());
Serial.println("Setup Temperatuur sensors...");
// Start up the library
}
void WriteData()
{
second++;
if (second >wdTimer){
second = 0;
emoncms[4].InputValue = round(sensors.getTempCByIndex(0)*10);
emoncms[5].InputValue = round(sensors.getTempCByIndex(1)*10);
SendData(ConstructPayload());
}
}
void loop() {
// put your main code here, to run repeatedly:
if (client.available()) {
char c = client.read();
Serial.print(c);
}
// if there's no net connection, but there was one last time
// through the loop, then stop the client:
if (!client.connected() && lastConnected) {
Serial.println();
Serial.println("disconnecting.");
client.stop();
}
if (!client.connected() && ((millis() - lastConnectionTime) > postingInterval)) {
WriteData();
}
sensors.requestTemperatures();
Serial.println(sensors.getTempCByIndex(0));
lastConnected = client.connected();
}
//**************************************
//Construct the string which should be send out by the http request
//**************************************
String ConstructPayload ()
{
String payload;
payload = "GET /emoncms/input/post.json?json={";
for (int i = 0; i < 5; i++) {
payload = payload + emoncms[i].InputName;
payload = payload + ":";
payload = payload + emoncms[i].InputValue;
payload = payload + ",";
}
payload = payload + emoncms[5].InputName;
payload = payload + ":";
payload = payload + emoncms[5].InputValue;
payload = payload + "}&apikey=";
payload = payload + apikey;
Serial.println(payload);
return payload;
}
void SendData (String HTTPstring)
{
// Serial.println("send data..");
if (client.connect(EmonServer, 80)) {
Serial.println("connecting...");
// send the HTTP PUT request:
client.println(HTTPstring);
client.println("Host: 192.168.1.102");
client.println("Connection: close");
client.println();
// note the time that the connection was made:
lastConnectionTime = millis();
}
else {
// if you couldn't make a connection:
Serial.println("connection failed");
Serial.println("disconnecting.");
client.stop();
}
}