How to send more than one sensor info via JSON

Topics (not sure which fora)
when not sure where to post, post here and mods will move it to right forum.

Moderators: leecollings, remb0

Post Reply
Jumper3126
Posts: 105
Joined: Thursday 31 December 2015 15:26
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

How to send more than one sensor info via JSON

Post by Jumper3126 »

I have been succesfull in getting my particle core to send the data of a single temperature sensor to a dummy device in domoticz using the code below:

Code: Select all

            if (client1.connect(server, 8080)){
                    client1.print("GET /json.htm?type=command&param=udevice&idx=50&nvalue=0&svalue=");
                    client1.print(String(temp));
                    client1.println(" HTTP/1.0");
                    client1.println("Host: home.dtdns.net");
                    client1.println("Content-Length: 0");
                    client1.println();
                    ledStatus(2, 300);
                    delay(1000);        
                    client1.flush();
                    client1.stop();
                }
                else {}
Now I would like to send the data of other sensors as well to different Domoticz devices.
What would be the best way to do this? I was able to copy past the whole code, including the

Code: Select all

if (client1.connect(server, 8080)){
, but perhaps I just only need to repeat

Code: Select all

GET /json............... HTTP/1.0
.

Regards, Rutger
User avatar
bbqkees
Posts: 407
Joined: Sunday 17 August 2014 21:01
Target OS: Linux
Domoticz version: 4.1x
Location: The Netherlands
Contact:

Re: How to send more than one sensor info via JSON

Post by bbqkees »

When sending data to Domoticz via 'JSON URL's' you are actually performing a HTTP GET request to your Domoticz server.

You have to complete the whole HTTP GET request per sensor value before starting a new one.

I do it like the following with my Arduino's;

Every sensortype has it's own request function like this:

Code: Select all

// /json.htm?type=command&param=udevice&idx=IDX&nvalue=0&svalue=TEMP
void httpRequesttemp() {
  // if there's a successful connection:
  if (client.connect(server, port)) {
    Serial.println("client connected sending temp value");
    //Serial.println(dstemp);
    client.print( "GET /json.htm?type=command&param=udevice&idx=");
    client.print(IDXtemp);
    client.print("&nvalue=0&svalue=");
    client.print(dstemp);
    client.println( " HTTP/1.1");
    client.println( "Host: 192.168.x.xxx");
    client.println( "Connection: close");
    client.println();
    client.println();
    client.stop();
    //delay(requestdelay);
  } 
  else {
    Serial.println("client could not connect (DS value)");
    client.stop();
  }
}
You need to put that AFTER the 'void loop' part.
(So outside of the 'void loop' brackets)

Then inside the loop you add a check to see if the specific sensor interval has passed.
If so (and the value seems OK), send the specific request.
Other sensors can have different intervals. For most temperature sensors I use an interval of 1 minute.
Repeat the entire part below in the 'void loop' for every sensor.
If the interval is the same you can just add the request function for the specific sensor in the same if>interval function.

Code: Select all

 // loop for DS18B20 sensor
  if(millis() - lastTimetemp > intervaltemp) {
    lastTimetemp = millis(); 

    //DS readout starter
    sensors.requestTemperatures();

    dstemp = sensors.getTempC(thermometer);
    Serial.println(dstemp);

    // Only send data to Domoticz if the data is valid
    // If the data read fails, the reported temp will be -127C.
    // A bit cold to register that in Domoticz!
    // So everything below -40C will be ignored.
    if(dstemp >=-40){
    httpRequesttemp();
    }
    else {
    Serial.println("dstemp not valid! No HTTP request made.");  
    }
  }
As soon as you are using multiple sensor types or doing other stuff on the Arduino it is important to avoid using delay().
Delay() will 'pause' the processor and in the mean time it does nothing, this will give timing issues in the long run.

If you are using DHT sensors you might skip the Adafruit library and use another one because the Adafruit library is very slow and big.
And if you use a DHT or other humidity sensor you need to calculate wet/dry/normal etc too.

Depending which ethernet controller you are using you might need some tweaking with timing and maybe an additional client.println(); here and there.
Bosch / Nefit / Buderus / Junkers / Worcester / Sieger EMS bus Wi-Fi MQTT Gateway and interface boards: https://bbqkees-electronics.nl/
User avatar
bbqkees
Posts: 407
Joined: Sunday 17 August 2014 21:01
Target OS: Linux
Domoticz version: 4.1x
Location: The Netherlands
Contact:

Re: How to send more than one sensor info via JSON

Post by bbqkees »

Also,

If you have more than 1 sensor of the same sensor type rewrite the function to accept parameters below.
Now you can reuse the same function for all sensors.

f.i.:

Code: Select all

// define sensor 1
float sensor1 = 0;
int sensor1IDX = 210;
// define sensor 2
float sensor2 = 0;
int sensor2IDX = 212;

Code: Select all

httprequesttemp(sensor1, sensor1IDX);
httprequesttemp(sensor2, sensor2IDX);

Code: Select all

// /json.htm?type=command&param=udevice&idx=IDX&nvalue=0&svalue=TEMP
void httpRequesttemp(float value, int id) {
  // if there's a successful connection:
  if (client.connect(server, port)) {
    Serial.println("client connected sending temp value");
    //Serial.println(value);
    client.print( "GET /json.htm?type=command&param=udevice&idx=");
    client.print(id);
    client.print("&nvalue=0&svalue=");
    client.print(value);
    client.println( " HTTP/1.1");
    client.println( "Host: 192.168.x.xxx");
    client.println( "Connection: close");
    client.println();
    client.println();
    client.stop();
    //delay(requestdelay);
  } 
  else {
    Serial.println("client could not connect (DS value)");
    client.stop();
  }
}
Bosch / Nefit / Buderus / Junkers / Worcester / Sieger EMS bus Wi-Fi MQTT Gateway and interface boards: https://bbqkees-electronics.nl/
Jumper3126
Posts: 105
Joined: Thursday 31 December 2015 15:26
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: How to send more than one sensor info via JSON

Post by Jumper3126 »

Hi bbqkees,
Thanks for your explanation. I'm using the particle core, which is nice small and cheap controller with wifi. Although the reception can sometimes be a bit tricky.
I have placed the whole communication part outside the loop:

Code: Select all

void DomoticzTempUpdate(String DomData, String IDX){
    Serial.println();
    Serial.println("Updating to Domoticz... ");
    Serial.println("Date string: " + DomData + "to device: " + IDX);

    if (client.connect(server, 8080)){     // Connecting and sending data to Domoticz
        Serial.println("Domoticz connection succesful");
        client.print("GET /json.htm?type=command&param=udevice&idx=");
        client.print(IDX);
        client.print("&nvalue=0&svalue=");
        client.print(DomData);
        client.println(" HTTP/1.0");
        client.println("Host: home.dtdns.net");
        client.println("Content-Length: 0");
        client.println();
        ledStatus(3, 250);   
        Serial.println("Thingspeak update sent.");
        delay(200);
    }
    else{ // Failed to connect to Thingspeak
        Serial.println("Unable to connect to Domoticz.");
        ledStatus(4, 100);   
    }

    if(!client.connected()) {client.stop();}
    client.flush();
    client.stop();
}
Which I call on for each temp sensor with:

Code: Select all

            DomoticzFloat=((round(TempInside*10))/10); // round float to 1 decimal
            DomoticzTempUpdate(String (DomoticzFloat), "4"); // change float to string and send
User avatar
bbqkees
Posts: 407
Joined: Sunday 17 August 2014 21:01
Target OS: Linux
Domoticz version: 4.1x
Location: The Netherlands
Contact:

Re: How to send more than one sensor info via JSON

Post by bbqkees »

Jumper3126 wrote:Hi bbqkees,
Thanks for your explanation. I'm using the particle core, which is nice small and cheap controller with wifi. Although the reception can sometimes be a bit tricky.
Ok I was under the assumption you were using an Arduino like me.

But good to hear it works.
Bosch / Nefit / Buderus / Junkers / Worcester / Sieger EMS bus Wi-Fi MQTT Gateway and interface boards: https://bbqkees-electronics.nl/
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest