Page 1 of 1

Arduino and json

Posted: Monday 13 April 2015 20:58
by bitjeverkeerd
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&param=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);
}

Re: Arduino and json

Posted: Wednesday 02 December 2015 11:26
by Teatech
I have problem just like yours. Is this been soved?

Re: Arduino and json

Posted: Wednesday 02 December 2015 13:22
by pj-r
Add domoticz_client.stop() before domoticz_client.connect(....

https://www.arduino.cc/en/Tutorial/WebClientRepeating

Re: Arduino and json

Posted: Friday 04 December 2015 23:35
by Teatech
Now works as it should. I just didin't find that one.. thanks!

Re: Arduino and json

Posted: Saturday 10 August 2019 9:31
by mrohner
@Teatech,

and how did you do it now? Pls. post the solution i.e.the arduino code. Thanks