User Tools

Site Tools


add_temperature_sensor_in_domoticz

This is an old revision of the document!


Add temperature sensor in Domoticz

Introduction

In a previous post, I have shown how to record temperature from Lexibook ASM30 sensor with Arduino. Now, we will import the temperature in Domoticz.

Assembly

First, prepare the assembly from page Record temperature from Lexibook ASM30 sensor with Arduino. Then, just plug the arduino to the raspberry pi USB.

The Arduino sketch

I modified a little bit the previous sketch in order to read from the serial line and send the temperature only when it is requested by client.

#include <RFControl.h>
 
// Save current temperature and channel
int temperature = 0;
int channel = 0;
 
void setup() {
 
  // Initialize serial line
  Serial.begin(9600);
 
  // Start receiving RF signals
  RFControl::startReceiving(0);
}
 
// The function prints current data to the serial line
void printData() {
    Serial.print("{ ");
    Serial.print("\"temp\": ");
    Serial.print(temperature);
    Serial.print(", \"channel\": ");
    Serial.print(channel);
    Serial.print(" }\n");
}
 
void loop() {
 
  // Decode data when received
  if(RFControl::hasData()) {
    unsigned int *timings;
    unsigned int timings_size;
    unsigned int pulse_length_divider = RFControl::getPulseLengthDivider();
    RFControl::getRaw(&timings, &timings_size);
 
    int iTemperatureTmp = 0;
    int iChannelTmp = 0;
 
    for(int i=0; i < timings_size; i++) {
      unsigned long timing = timings[i] * pulse_length_divider;
 
      // Temperature is at bit 16*2 coded in decimal x 10 on 8 bits
      if( (i >= 16*2) && (i < (16*2 + 8*2)) ) {
        if(i == 16*2) iTemperatureTmp = 0;
 
        if(timing < 1000) {
          // bit separator, do nothing
        }
        else if(timing < 2000) {
          iTemperatureTmp = iTemperatureTmp<<1;
        }
        else if(timing < 5000) {
          iTemperatureTmp = iTemperatureTmp<<1;
          iTemperatureTmp++;
        }
      }
      // Sensor Channel is after byte 3, on 2 bits, decimal encoded
      else if((i >= 24*2) && i <= 26*2) {
        if(i == 24*2) channel = 0;
        if(timing < 1000) {
          // bit separator, do nothing
        }
        else if(timing < 2000) {
          iChannelTmp = iChannelTmp<<1;
        }
        else if(timing < 5000) {
          iChannelTmp = iChannelTmp<<1;
          iChannelTmp++;
        }
      }
      // Other data
      else {
        // Do nothing for now
      }
    }
 
    temperature = iTemperatureTmp;
    channel = iChannelTmp;
 
    RFControl::continueReceiving();
  }
 
  if(Serial.available() > 0) {
    // For now, send data whatever the received command is
    Serial.read();
 
    // Wait a little
    delay(500);
 
    printData();  
  }
 
}

Domoticz

The first thing to do is to add a dummy device with virtual sensor in Domoticz:

  • In settings/hardware, add “Dummy” type harware
  • In hardware list, click “Create virtual sensor” next to your dummy hardware.

Reference

add_temperature_sensor_in_domoticz.1459712274.txt.gz · Last modified: 2016/04/03 21:37 by sgripon