send json request to Domoticz

Hi

i'm a beginner, so maybe there is some strange things in this code, (don't blame me :wink: )

I try to use an arduino to push sensor values to a raspberry pi, to change some dummy sensor with a json request.

When i send one request, it works, but when i try to send multiple json with a "GET", the serial monitor return me the "connection failed" in the end of the sketch, like the program never try to execute the GET2 part…
i actually try to use this with two ultrasonic sensor, but i want to add many other devices on the arduino to create chip and modulars sensors.

The code :

#include <SPI.h>
#include <Ethernet.h>

byte mac[] = { 
  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};

boolean received = false;
boolean get2 = false;  // switch between the two GET
EthernetClient client;

char server[] = "192.168.0.150"; 
unsigned long lastConnectionTime = 0;
boolean lastConnected = false;
const unsigned long postingInterval = 10000;  // delay between updates, in milliseconds de base 5000
// declaration capteur
int trig = 6;
int echo = 7;
long lecture_echo;
long cm;
int trig2 = 8;
int echo2 = 9;
long lecture_echo2;
long cm2;
// fin declaration capteur
void setup() {
  // capteur
   pinMode(trig, OUTPUT);
  digitalWrite(trig, LOW);
  pinMode(echo, INPUT);
  pinMode(trig2, OUTPUT);
  digitalWrite(trig2, LOW);
  pinMode(echo2, INPUT);
  //
  randomSeed(analogRead(0));
  Serial.begin(9600);
  delay(1000);
  Ethernet.begin(mac);
}

void loop() {
  //capteur
   digitalWrite(trig, HIGH);
  delayMicroseconds(10);
  digitalWrite(trig, LOW);
  lecture_echo = pulseIn(echo, HIGH);
  cm = lecture_echo / 58;
  Serial.print("Distancem : ");
  Serial.println(cm);
  delay(2000); 
  digitalWrite(trig2, HIGH);
  delayMicroseconds(10);
  digitalWrite(trig2, LOW);
  lecture_echo2 = pulseIn(echo2, HIGH);
  cm2 = lecture_echo2 / 58;
  Serial.print("Distancem2 : ");
  Serial.println(cm2);
  delay(2000);
  // fin capteur
  while (client.available()) {
    char c = client.read();
    Serial.print(c);
    received = true;
  }
 if(received)
 {
        client.stop();
        received = false;
 }

  if (!client.connected() && lastConnected) {
    Serial.println();
    Serial.println("disconnecting.");
    client.stop();
      }

  if(!client.connected() && (millis() - lastConnectionTime > postingInterval)) {
    int i = random(10000);
    String reading = "1:" + String(i);
    httpRequest(reading);
  }
  lastConnected = client.connected();
}

// first GET
void httpRequest(String val) {
  if (client.connect(server, 8080) && !get2) {  // rajout
    Serial.println("connecting...");
    Serial.println("Sending " + val);
    client.print("GET /json.htm?type=command&param=udevice&idx=33&nvalue=0&svalue=");
    client.print(cm);
    client.println(" HTTP/1.1");
    client.println("Host: *HOST*");
    client.println("Connection: keep-open");
    client.println();
    lastConnectionTime = millis();
    get2 = true;
      } 
  // second GET
  else if (client.connect(server, 8080) && get2) {
    Serial.println("connecting...");
    Serial.println("Sending " + val);
    client.print("GET /json.htm?type=command&param=udevice&idx=32&nvalue=0&svalue=");
    client.print(cm2);
    client.println(" HTTP/1.1");
    client.println("Host: *HOST*");
    client.println("Connection: keep-open");
    client.println();
    lastConnectionTime = millis();
    get2 = false;
      } 
  // end second get
  
  else {
    Serial.println("connection failed");
    Serial.println("disconnecting.");
    client.stop();
    get2 = false; // debug
  }
}

and the serial return :

Distancem : 141
Distancem2 : 0
connecting...
Sending 1:8943
Distancem : 142
Distancem2 : 0
HTTP/1.0 200 OK
Content-Length: 53
Content-Type: text/html;charset=UTF-8
Cache-Control: no-cache
Pragma: no-cache

{
"status" : "OK",
"title" : "Update Device"
}

disconnecting.
Distancem : 140
Distancem2 : 0
connection failed
disconnecting.
Distancem : 140
Distancem2 : 0

(the result 0 of the "distancem2" was normal, not plugged when testing)

So if someone has a solution, I'll take it :wink:

Thanks

yoann

no one ? :confused: