Custom Arduino Sensor/Script

Must be cleaned all topics go to the right subforum
Post Reply
devtone
Posts: 4
Joined: Wednesday 30 November 2016 0:38
Target OS: -
Domoticz version:
Contact:

Custom Arduino Sensor/Script

Post by devtone » Wednesday 30 November 2016 0:48

I have a Arduino with 4 sensors, LAN connected.

It runs a HTTP server that responds with:

Code: Select all

<current>
<light value="630"/>
<co2 value="238"/>
<temp value="25.70"/>
<presure value="103239"/>
</current>
I've created a HTTP poller running the following LUA script:

Code: Select all

s = request['content'];
local light = domoticz_applyXPath(s,'//current/light/@value')
local co2 = domoticz_applyXPath(s,'//current/co2/@value')
local temp = domoticz_applyXPath(s,'//current/temp/@value')
local presure = domoticz_applyXPath(s,'//current/pressure/@value')
domoticz_updateDevice(2988507,'',light .. ";" .. co2 .. ";0;" .. temp .. ";0;" .. presure .. ";0")
HTTP poller works as i see the data in the Domoticz log:

Code: Select all

2016-11-30 00:03:45.346 CLuaHandler (updateDevice from LUA) : idx=2988507 nvalue= svalue=624;241;0;25.70;0;103246;0 invalue=0 signallevel=12 batterylevel=255
Extra info on the data:

624 = lightsensor
241 = CO2
25.70 = temp
103246 = presure
Last edited by devtone on Wednesday 30 November 2016 1:53, edited 2 times in total.

devtone
Posts: 4
Joined: Wednesday 30 November 2016 0:38
Target OS: -
Domoticz version:
Contact:

Re: Custom Arduino Sensor/Script

Post by devtone » Wednesday 30 November 2016 0:54

Bonuscode (Arduino):

Code: Select all

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

Adafruit_BMP085 bmp;
int SensorCO2Value;
int SensorLightValue;
const int SensorCO2=1;
const int SensorLight=0;
byte mac[] = {
  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED
};
IPAddress ip(192, 168, 68, 222);
EthernetServer server(80);

void setup()
{
  Serial.begin(9600);
  while (!Serial) {
    ;
  }
  Ethernet.begin(mac, ip);
  server.begin();
  Serial.print("server is at ");
  Serial.println(Ethernet.localIP());
  bmp.begin();  
}
void loop()
{
  EthernetClient client = server.available();
  if (client) {
    Serial.println("Probed");
    boolean currentLineIsBlank = true;
    while (client.connected()) {
      if (client.available()) {
        char c = client.read();
        Serial.write(c);
        if (c == '\n' && currentLineIsBlank) {
          client.println("HTTP/1.1 200 OK");
          client.println("Content-Type: text/html");
          client.println("Connection: close");  // the connection will be closed after completion of the response
          client.println();
          client.println("<current>");
            int sensorReading0 = analogRead(0);
            client.print("<light value=\"");
            client.print(sensorReading0);
            client.println("\"/>");
            int sensorReading1 = analogRead(1);
            client.print("<co2 value=\"");
            client.print(sensorReading1);
            client.println("\"/>");
            client.print("<temp value=\"");
            client.print(bmp.readTemperature());
            client.println("\"/>");
            client.print("<presure value=\"");
            client.print(bmp.readPressure());
            client.println("\"/>");    
          client.println("</current>");
          break;
        }
        if (c == '\n') {
          currentLineIsBlank = true;
        } else if (c != '\r') {
          currentLineIsBlank = false;
        }
      }
    }
    delay(1);
    client.stop();
    Serial.println("disconnected");
  }
}

devtone
Posts: 4
Joined: Wednesday 30 November 2016 0:38
Target OS: -
Domoticz version:
Contact:

Re: Custom Arduino Sensor/Script

Post by devtone » Wednesday 30 November 2016 1:27

Got some data with:

Code: Select all

s = request['content'];

local light = domoticz_applyXPath(s,'//current/light/@value')
local co2 = domoticz_applyXPath(s,'//current/co2/@value')
local temp = domoticz_applyXPath(s,'//current/temp/@value')
local presure = domoticz_applyXPath(s,'//current/presure/@value')

domoticz_updateDevice(1,'',temp .. ";0")
domoticz_updateDevice(2,'',presure .. ";0")
domoticz_updateDevice(3,'',co2 .. ";0")
domoticz_updateDevice(4,'',light .. ";0")

Code: Select all

2016-11-30 00:53:59.519 CLuaHandler (updateDevice from LUA) : idx=1 nvalue= svalue=25.60;0 invalue=0 signallevel=12 batterylevel=255
2016-11-30 00:53:59.522 CLuaHandler (updateDevice from LUA) : idx=2 nvalue= svalue=1.0321;0 invalue=0 signallevel=12 batterylevel=255
2016-11-30 00:53:59.525 CLuaHandler (updateDevice from LUA) : idx=5 nvalue= svalue=244;0 invalue=0 signallevel=12 batterylevel=255
2016-11-30 00:53:59.545 CLuaHandler (updateDevice from LUA) : idx=4 nvalue= svalue=627;0 invalue=0 signallevel=12 batterylevel=255

devtone
Posts: 4
Joined: Wednesday 30 November 2016 0:38
Target OS: -
Domoticz version:
Contact:

Re: Custom Arduino Sensor/Script

Post by devtone » Wednesday 30 November 2016 2:53

Solved.

Can anyone explain the diff between nvalue and svalue? Can't find any info on it..

Post Reply

Who is online

Users browsing this forum: No registered users and 3 guests