Arduino and json
Posted: Monday 13 April 2015 20:58
I have made (collected on the internet) the following code for an arduino uno with ethernetshield.
The arduino reads an analog input every 30 seconds and then sends the value (0 - 1024) to the serial output (hyperterminal) and to Domoticz through the ethernet shield.
In Domoticz i have created a virtual sensor that should change every 30 seconds (if the analog value is changed).
The serial output is working fine but the virtual sensor in Domoticz is updated only once. When i reset the arduino the virtual sensor in Domoticz is again updated (once).
It look like there is something wrong in my loop.
This my code:
#include <SPI.h>
#include <Ethernet.h>
byte mac[] = { 0x90, 0xA2, 0xDA, 0x0D, 0x7D, 0x54 };
char domoticz_server[] = "172.16.1.100";
EthernetClient domoticz_client;
int sensorValue = 0;
String deel1 = "GET /json.htm?type=command¶m=udevice&idx=7&nvalue=0&svalue=";
String deel2 = "";
String deel3 = " HTTP/1.1";
String dataInput = "";
void setup(void)
{
Serial.begin(9600);
Serial.println("zet hier iets leuks..");
// start the Ethernet connection and the server:
Serial.println("Trying to get an IP address using DHCP");
if (Ethernet.begin(mac) == 0)
{
Serial.println("Failed to configure Ethernet using DHCP");
while (true)
{
;
}
}
Serial.println();
// start listening for clients
Serial.print("server is at ");
Serial.println(Ethernet.localIP());
}
void loop(void)
{
sensorValue = analogRead(A0);
deel2 = String(sensorValue);
dataInput = String(deel1 + deel2 + deel3);
Serial.println(dataInput);
Serial.println();
domoticz_client.connect(domoticz_server, 8080);
domoticz_client.println(dataInput);
//domoticz_client.println("Host: 172.16.1.101");
//domoticz_client.println("User-Agent: Mozilla/5.0");
domoticz_client.println("Connection: close");
domoticz_client.println();
delay(30000);
}
The arduino reads an analog input every 30 seconds and then sends the value (0 - 1024) to the serial output (hyperterminal) and to Domoticz through the ethernet shield.
In Domoticz i have created a virtual sensor that should change every 30 seconds (if the analog value is changed).
The serial output is working fine but the virtual sensor in Domoticz is updated only once. When i reset the arduino the virtual sensor in Domoticz is again updated (once).
It look like there is something wrong in my loop.
This my code:
#include <SPI.h>
#include <Ethernet.h>
byte mac[] = { 0x90, 0xA2, 0xDA, 0x0D, 0x7D, 0x54 };
char domoticz_server[] = "172.16.1.100";
EthernetClient domoticz_client;
int sensorValue = 0;
String deel1 = "GET /json.htm?type=command¶m=udevice&idx=7&nvalue=0&svalue=";
String deel2 = "";
String deel3 = " HTTP/1.1";
String dataInput = "";
void setup(void)
{
Serial.begin(9600);
Serial.println("zet hier iets leuks..");
// start the Ethernet connection and the server:
Serial.println("Trying to get an IP address using DHCP");
if (Ethernet.begin(mac) == 0)
{
Serial.println("Failed to configure Ethernet using DHCP");
while (true)
{
;
}
}
Serial.println();
// start listening for clients
Serial.print("server is at ");
Serial.println(Ethernet.localIP());
}
void loop(void)
{
sensorValue = analogRead(A0);
deel2 = String(sensorValue);
dataInput = String(deel1 + deel2 + deel3);
Serial.println(dataInput);
Serial.println();
domoticz_client.connect(domoticz_server, 8080);
domoticz_client.println(dataInput);
//domoticz_client.println("Host: 172.16.1.101");
//domoticz_client.println("User-Agent: Mozilla/5.0");
domoticz_client.println("Connection: close");
domoticz_client.println();
delay(30000);
}