Jump to content

Welcome to Smart Home Forum by FIBARO

Dear Guest,

 

as you can notice parts of Smart Home Forum by FIBARO is not available for you. You have to register in order to view all content and post in our community. Don't worry! Registration is a simple free process that requires minimal information for you to sign up. Become a part of of Smart Home Forum by FIBARO by creating an account.

 

As a member you can:

  •     Start new topics and reply to others
  •     Follow topics and users to get email updates
  •     Get your own profile page and make new friends
  •     Send personal messages
  •     ... and learn a lot about our system!

 

Regards,

Smart Home Forum by FIBARO Team


  • 0

Integration of cheap chines sensors with HC2


Guest ilyaext

Question

Guest ilyaext

I integrated cheap chines PIR Motion 433mhz sensors with HC2. The signal from sensors is received by Arduino module, which sends HTTP requests to HC2.

The system works fine few months for automatic turning on lights in rooms and on stairs.

If somebody is interested, I could share my code and description of the used equipment .

Link to comment
Share on other sites

2 answers to this question

Recommended Posts

  • 0
Guest ilyaext
  • Inquirer
  • Equipment:

    1. Kerui PIR sensors like 

    Please login or register to see this link.

     or 

    Please login or register to see this link.

    2. Arduino module with LAN or Wireless adapter. I use Arduino Yun.

    3. 433Mhz receiver for Arduino. I tested number of different receivers and the best coverage had 

    Please login or register to see this link.

     and 

    Please login or register to see this link.

     (without an additional antenna). I use one Arduino module on each floor because my first receivers didn't get a good coverage. But thees receivers allow to cover two floors with one Arduino module.

     

    Flow:

    Each sensor sends signal with his unique ID. Arduino sketch detects this ID and maps it to HTTP request to HC2. Unfortunatelly, I didn't find a way to the request to HC2 scene with parameter. Therefore, for do not create many scenes I created a VD with many buttons and the HTTP requests execute a button according to the sensor ID.

     

    Software:

    1. Arduino sketch. I used 

    Please login or register to see this link.

     source.

     

    #include <Bridge.h>
    #include <Console.h>
    #include <HttpClient.h>

    #define KERUI_BITS 24
    #define KERUI_MAX_SIGNAL_LENGTH 255
    #define KERUI_UPPER_THRESHOLD 100
    #define KERUI_LOWER_THRESHOLD 80

    #define KERUI_DURATION_BIT 500
    #define KERUI_DURATION_DELIMITER 5000

    #define KERUI_RESULT_SUCCESS 0
    #define KERUI_RESULT_TIMEOUT_LOW 1
    #define KERUI_RESULT_TIMEOUT_HIGH 2
    #define KERUI_RESULT_MANY_ERRORS 3
    #define KERUI_RESULT_DELIMETER_NOT_FOUND 4

    #define KERUI_MAX_ERROR_COUNT 50

    #define LED_PIN 13
    #define UNKNOWN_DEVICE "Unknown"

    void setup() {
      Bridge.begin();
      Console.begin();
    //  Serial.begin(9600);
      pinMode(LED_PIN, OUTPUT);

      digitalWrite(LED_PIN, HIGH);
      delay(2000);
      digitalWrite(LED_PIN, LOW);
    }

    const int rowSize = 2 * (KERUI_BITS + 1);
    int row[rowSize];
    byte bits[KERUI_BITS];
    unsigned long tLast = 0;
    long lastDeviceID = 0;

    void loop() {
      digitalWrite(LED_PIN, HIGH);
      int result = keruiReadRow(A0, 2, bits, row);
      digitalWrite(LED_PIN, LOW);
      switch (result) {
        case KERUI_RESULT_SUCCESS:
          sendResult();
          break;
        case KERUI_RESULT_TIMEOUT_LOW:
    //      Console.println("KERUI_RESULT_TIMEOUT_LOW");
          break;
        case KERUI_RESULT_TIMEOUT_HIGH:
    //      Console.println("KERUI_RESULT_TIMEOUT_HIGH");
          break;
        case KERUI_RESULT_MANY_ERRORS:
    //      Console.println("KERUI_RESULT_MANY_ERRORS");
          break;
        case KERUI_RESULT_DELIMETER_NOT_FOUND:
    //      Console.println("KERUI_RESULT_DELIMETER_NOT_FOUND");
          break;
      }
    }

    void sendResult() {
      long deviceID = 0;
      unsigned long tNow = millis();
      unsigned long intervalInSeconds = (tNow - tLast)/1000;
      for (int i = 0; i < KERUI_BITS; i++) {
        deviceID  = deviceID * 2;
        deviceID += bits;
      }
      if (lastDeviceID != deviceID || intervalInSeconds > 3) {
        String deviceIndex = getSensorIndexByDeviceID(deviceID);
        String message = "ID: " + String(deviceID, DEC) + ", Index: " + deviceIndex + ", delay from last: " + String(intervalInSeconds);
        if (deviceIndex != UNKNOWN_DEVICE) {
          String resp = send_GET_HTTP_Request(deviceIndex);
          message  = message + ", Response: " + resp;
        }
        tLast = tNow;
        lastDeviceID = deviceID;
        Console.println(message);
      }
    }

    String getSensorIndexByDeviceID(long deviceID) {
      String deviceIndex;
      switch (deviceID) {
        case <replace with your sensor ID>:
          deviceIndex = 2;
          break;
        case <replace with your sensor ID>:
          deviceIndex = 3;
          break;

       ...


        default:
          deviceIndex = UNKNOWN_DEVICE;
      }
      return deviceIndex;
    }

    String send_GET_HTTP_Request(String deviceIndex) {
      String response = "";
      HttpClient client;
      client.setHeader("Authorization: Basic <replace with your authorization>");
      String sRequest = "http://<your HC2 IP address>/api/callAction?deviceID=242&name=pressButton&arg1=" + deviceIndex + "&";
      int result = client.get(sRequest);
      String res = "";
      while (client.available()) {
        char c = client.read();
        res = res + c;
      }
      return res;
    }

     

    /**
      Returns true if data was read successfully otherwise false
      analogPin - pin number to read from. Usualy it is A0
      result - must be size of KERUI_BITS
      passes - number of required succesful passes
      row - can be null. If provided size should be 2*(KERUI_BITS+1). Each pair is LOW/HIGH time in microsecond, the first pair is the delimiter
    **/
    int keruiReadRow(int analogPin, int passes, byte result[], int row[]) {
      unsigned long time, time2;
      int counter, low, high;
      time = millis();
      while (analogRead(analogPin) < 1);

      int successes = 0;
      int bitNumber = -1;
      byte bitValue;
      int errorCount = 0;
      int noDelimiterCounter = 0;

      while (successes < passes) {
        // Read LOW signal
        time = micros();
        counter = 0;
        while (analogRead(analogPin) > KERUI_UPPER_THRESHOLD) {
          counter++;
          if (counter > KERUI_MAX_SIGNAL_LENGTH) return KERUI_RESULT_TIMEOUT_LOW;
        }
        time2 = micros();
        low = time2 - time;

        // Read HIGH signal
        counter = 0;
        while (analogRead(analogPin) < KERUI_LOWER_THRESHOLD) {
          counter++;
          if (counter > KERUI_MAX_SIGNAL_LENGTH) return KERUI_RESULT_TIMEOUT_HIGH;
        }
        high = micros() - time2;

        if (row) {
          row[2 * (bitNumber + 1)] = low;
          row[2 * (bitNumber + 1) + 1] = high;
        }

        if (high > KERUI_DURATION_DELIMITER) {
          bitNumber = 0;
          noDelimiterCounter = 0;
        }
        else {
          if (bitNumber < 0) {
            if (noDelimiterCounter++ > KERUI_BITS)
              return KERUI_RESULT_DELIMETER_NOT_FOUND;
          }
          else {
            if (high < KERUI_DURATION_BIT) {
              bitValue = 1;
            }
            else {
              bitValue = 0;
            }

            if (successes > 0 && bitValue != result[bitNumber]) {
              successes = 0;
              if (errorCount++ > KERUI_MAX_ERROR_COUNT) {
                return KERUI_RESULT_MANY_ERRORS;
              }
            }
            result[bitNumber] = bitValue;
            bitNumber++;

            if (bitNumber >= KERUI_BITS) {
              bitNumber = -1;
              successes++;
            }
          }
        }
      }

      return KERUI_RESULT_SUCCESS;
    }

     

    2. Virtual Device in HC2.

    It has number of buttons (one button per sensor). The LUA script is according to your needs. If somebody need a help with it, I could share my implementation.

     

    Hope it will be usable for you.

    Edited by ilyaext
    Link to comment
    Share on other sites

    Join the conversation

    You can post now and register later. If you have an account, sign in now to post with your account.

    Guest
    Answer this question...

    ×   Pasted as rich text.   Paste as plain text instead

      Only 75 emoji are allowed.

    ×   Your link has been automatically embedded.   Display as a link instead

    ×   Your previous content has been restored.   Clear editor

    ×   You cannot paste images directly. Upload or insert images from URL.

    ×
    ×
    • Create New...