Announcement

Collapse
No announcement yet.

API Board Sketch Won't Update HS Via SendToHS() Function

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • API Board Sketch Won't Update HS Via SendToHS() Function

    I've got the board connected to HS, API sketch is downloaded.

    All of my 'calculation'/business logic code within the API sketch is working as I have it spitting output to the console window and connecting to HS3.

    I have created one input in the Arduino plug, and it is HS device 1415 (I'm assuming the server side plug uses the device ID's such as the url variable in deviceutility).


    Two issues:

    1. In my 'custom' code, when I pass a value to SendToHS(1415,5) [this would be the byte/long flavor of the function], I look at plugin logging in HS and I get the following. I don't know why it is looking for a device #135 when I'm sending it #1415.

    Code:
    Data Received:1 API 135 2309
    New UDP Text Recieved = 1 API 135 2309
    Warning = Arduino, Board:1, API Input:135 does not exist. Please create the device first.
    Data Received:1 API 135 2309
    New UDP Text Recieved = 1 API 135 2309
    Warning = Arduino, Board:1, API Input:135 does not exist. Please create the device first.


    2. So I (temporarily) changed the canned SendToHs function at the bottom of the API sketch to hardcode 1415 instead of the passed argument device ID. Now in the logging I get this, which doesn't make sense as there is absolutely a HS device with #1415, it was created by the plugin and it's the correct device.

    Code:
    New UDP Text Recieved = 1 API 1415 7
    Data Received:1 API 1415 6
    Warning = Arduino, Board:1, API Input:1415 does not exist. Please create the device first.
    New UDP Text Recieved = 1 API 1415 6
    Data Received:1 API 1415 7
    Warning = Arduino, Board:1, API Input:1415 does not exist. Please create the device first.
    New UDP Text Recieved = 1 API 1415 7
    Data Received:1 API 1415 6


    Here is the complete sketch minus several things (IP,etc):
    Code:
    /************************************************************
     *Arduino to Homeseer 3 Plugin API written by Enigma Theatre.*
     * V1.0.0.118                                                 *
     *                                                           *
     *******Change the values below only*************************
     */
    
    //************Do not change anything in Here*****************
    
    #define ISIP 1
    #if ISIP == 1
    #include <EEPROM.h>
    #include <SPI.h>      
    #endif
    
    
    const byte BoardAdd = 1;
    
    #include <Ethernet.h>
    #include <EthernetUdp.h>
    
    #if ISIP == 1
    byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xEE};
    IPAddress ip(192,168,1,25);     //IP entered In HS config.
    const unsigned int localPort = 8900;      //port entered In HS config.
    IPAddress HomeseerIP(192,168,1,20); //Homeseer IP address
    IPAddress ServerIP(EEPROM.read(2),EEPROM.read(3),EEPROM.read(4),EEPROM.read(5));
    IPAddress gateway(192,168,1,1);
    IPAddress subnet(255,255,255,0);
    byte EEpromVersion = EEPROM.read(250);
    #endif
    
    int FromHS[10];                                          // *
    boolean IsConnected = false;                             // *
    #if ISIP == 1                                            // *
    char packetBuffer[UDP_TX_PACKET_MAX_SIZE];               // *
    EthernetUDP Udp;                                         // *
    const unsigned int ServerPort = 8888;                    // *
    #endif                                                   // *
    void(* resetFunc) (void) = 0;                            // *
    //***********************************************************
    
    
    int trigPin = 11;    //Trig - OUTPUT
    int echoPin = 12;    //Echo - INPUT
    long duration, cm, inches;
    unsigned long previousMilliseconds = 0;        // will store last time sensor was fired
    
    // constants won't change:
    const long intervalMilliseconds = 1000;           // interval at which to refresh the sensor(s) (milliseconds)
    
    void setup() {
      HSSetup();
      //************************
      //Add YOUR SETUP HERE;
      //************************
    
      //Serial Port begin
      Serial.begin (9600);
      //Define inputs and outputs
      pinMode(trigPin, OUTPUT);
      pinMode(echoPin, INPUT);
    }
    
    
    
    
    void loop() {
    #if ISIP == 1
      IsUDP();
    #endif
    
        //************************
        //Add YOUR CODE HERE;
        //************************
        /* To Send Data to Homeseer use SendToHS(Device,Value)
         Eg.. SendToHS(1,200); where 1 is the API device in homeseer and 200 is the value to send
         To Recieve data from Homeseer look up the FromHS array that is updated when the device value changes.
         Eg.. FromHS[5] would be the data from API Output device 5
         All code that is located just below this block will execute regardless of connection status!
         You can include SendToHS() calls, however when there isn't an active connection, it will just return and continue.
         If you only want code to execute when HomeSeer is connected, put it inside the if statement below.
         */
    
          unsigned long currentMilliseconds = millis();
    
          // Proximity/distance sensor
    
          // using milliseconds delay from https://www.arduino.cc/en/Tutorial/BlinkWithoutDelay
          if (currentMilliseconds - previousMilliseconds >= intervalMilliseconds) {
    
            // The sensor is triggered by a HIGH pulse of 10 or more microseconds.
            // Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
            digitalWrite(trigPin, LOW);
            delayMicroseconds(5);
            digitalWrite(trigPin, HIGH);
            delayMicroseconds(10);
            digitalWrite(trigPin, LOW);
    
            // Read the signal from the sensor: a HIGH pulse whose
            // duration is the time (in microseconds) from the sending
            // of the ping to the reception of its echo off of an object.
            pinMode(echoPin, INPUT);
            duration = pulseIn(echoPin, HIGH);
    
            // convert the time into a distance
            cm = (duration/2) / 29.1;
            inches = (duration/2) / 74;
    
    
    
            // save the last time you fired the sensor
            previousMilliseconds = currentMilliseconds;
    
            //Serial.print(inches);
            //Serial.print("in, ");
            SendToHS(1415,cm);
    
            Serial.print(cm);
            Serial.print("cm");
            Serial.println();
        } // milliseconds delay bracket
    
    /*Execute regardless of connection status*/
    
    
     if (IsConnected == true) {
       /*Execute ONLY when HomeSeer is connected*/
    
    
    
      }
    }
    
    
    
    
    
    
    
    
    char* Version = "API1.0.0.118";
    
    byte Byte1,Byte2,Byte3;
    int Byte4,Byte5;
    
    
    void HSSetup() {
    
    #if ISIP == 1
        if (EEpromVersion!=22) {
        ServerIP=HomeseerIP;
        EEPROM.write(2,ServerIP[0]);
        EEPROM.write(3,ServerIP[1]);
        EEPROM.write(4,ServerIP[2]);
        EEPROM.write(5,ServerIP[3]);
        EEPROM.write(250,22); //Store the version where the eeprom data layout was last changed
        EEpromVersion=22;
      }
    Ethernet.begin(mac,ip);
      Udp.begin(localPort);
      Udp.setTimeout(0);
      delay(1000);
    SendConnect();
    #else
      Serial.begin(115200);
      Serial.flush();
      Serial.setTimeout(0);
      delay(1000);
      Serial.print("Connect ");
      Serial.println(BoardAdd);
    #endif
    
      IsConnected = false;
    
    }
    
    void SendConnect()
    {
    #if ISIP == 0
      Serial.print("Connect ");
      Serial.println(BoardAdd);
    #else
        Udp.beginPacket(ServerIP,ServerPort);  //First send a connect packet to the dynamic IP stored in eeprom
        Udp.print("Connect ");
        Udp.print(BoardAdd);
        Udp.endPacket();
        if (ServerIP!=HomeseerIP) {
          Udp.beginPacket(HomeseerIP,ServerPort);  //Then if the stored value doesn't match the pre-specified one, send a connect packet there also
          Udp.print("Connect ");
          Udp.print(BoardAdd);
          Udp.endPacket();
        }
    
    #endif
    }
    
    
    #if ISIP == 1
    void IsUDP(){
      int packetSize = Udp.parsePacket();
      if(packetSize)
      {
        IPAddress remote = Udp.remoteIP();
        Byte1 =Udp.parseInt();
        Udp.read();
        Byte2 =Udp.read();
        Byte3 =Udp.parseInt();
        Byte4 =Udp.parseInt();
        Byte5 =Udp.parseInt();
        DataEvent();
      }
    }
    
    #else
    void serialEvent() {
      while (Serial.available() > 0) {
        delay(17);
    
    
        Byte1 = Serial.parseInt();
        Serial.read();  
        Byte2 = Serial.read();
        Byte3 = Serial.parseInt();
        Byte4 = Serial.parseInt();
        Byte5 = Serial.parseInt();
        DataEvent();
      }
    }
    #endif
    
    
    /*
    
    Used Data Input Cases
    D Disconnect
    r reset
    K Keepalive
    O PinMode Output Set
    d Input debounce time set
    C Connect request
    c Connection established - report current status
    */
    void DataEvent() {
    
    
    
      if (Byte1 == BoardAdd) {
        switch (Byte2) {
    
        case 'c':
          IsConnected = true;
    #if ISIP == 1
          if (Udp.remoteIP() != ServerIP) {
            ServerIP=Udp.remoteIP();
            EEPROM.write(2,ServerIP[0]);
            EEPROM.write(3,ServerIP[1]);
            EEPROM.write(4,ServerIP[2]);
            EEPROM.write(5,ServerIP[3]);
          }    
    #endif
    
          break;
    
        case 'C':  
    #if ISIP == 1
          Udp.beginPacket(Udp.remoteIP(), ServerPort);
          Udp.print("Version ");
          Udp.print(BoardAdd);
          Udp.print(" ");
          Udp.print(Version);
          Udp.println(" HS3");
          Udp.endPacket();
    
          Udp.beginPacket(Udp.remoteIP(), ServerPort);
          delay(100);
          Udp.print("Connected ");
          Udp.println(BoardAdd);
          Udp.endPacket();
    #else
          Serial.print("Version ");
          Serial.print(BoardAdd);
          Serial.print(" ");
          Serial.print(Version);
          Serial.println(" HS3");
          delay(100);
          Serial.print("Connected ");
          Serial.println(BoardAdd);
    #endif
          delay(100);
          IsConnected = false;
          break;
    
        case 'K':
          delay(200);
    #if ISIP == 1
          Udp.beginPacket(Udp.remoteIP(), ServerPort);
          Udp.print("Alive ");
          Udp.println(BoardAdd);
          Udp.endPacket();
          if (Udp.remoteIP() != ServerIP) {
            ServerIP=Udp.remoteIP();
            EEPROM.write(2,ServerIP[0]);
            EEPROM.write(3,ServerIP[1]);
            EEPROM.write(4,ServerIP[2]);
            EEPROM.write(5,ServerIP[3]);
          }    
    #else    
          Serial.print("Alive ");
          Serial.println(BoardAdd);
    #endif
          break;
    
          case 'r':
          delay(200);
          resetFunc();  //call reset
          break;
    
        case 'O':
          FromHS[Byte3] = Byte4;
          break;
    
        case 'D':
          IsConnected = false;
          break;  
        }
      }
    }
    
    void SendToHS(byte Device, long Data){
    if (IsConnected == true) {
    #if ISIP == 1
      Udp.beginPacket(Udp.remoteIP(), ServerPort);
      Udp.print(BoardAdd);
      Udp.print(" API ");
      //Udp.print(Device);
      Udp.print(1415); //hardcoded just to test
      Udp.print(" ");
      Udp.print(Data);
      Udp.endPacket();
    #else
      Serial.print(BoardAdd);
      Serial.print(" API ");
      Serial.print(Device);
      Serial.print(" ");
      Serial.println(Data);
    #endif
    }
    }
    
    void SendToHS(byte Device, int Data){
    if (IsConnected == true) {
    #if ISIP == 1
      Udp.beginPacket(Udp.remoteIP(), ServerPort);
      Udp.print(BoardAdd);
      Udp.print(" API ");
      Udp.print(Device);
      Udp.print(" ");
      Udp.print(Data);
      Udp.endPacket();
    #else
      Serial.print(BoardAdd);
      Serial.print(" API ");
      Serial.print(Device);
      Serial.print(" ");
      Serial.println(Data);
    #endif
    }
    }
    
    void SendToHS(byte Device, float Data){
    if (IsConnected == true) {
    #if ISIP == 1
      Udp.beginPacket(Udp.remoteIP(), ServerPort);
      Udp.print(BoardAdd);
      Udp.print(" API ");
      Udp.print(Device);
      Udp.print(" ");
      Udp.print(Data);
      Udp.endPacket();
    #else
      Serial.print(BoardAdd);
      Serial.print(" API ");
      Serial.print(Device);
      Serial.print(" ");
      Serial.println(Data);
    #endif
    }
    }

  • #2
    Debug/log file. Certain things erased for privacy.

    Code:
           Debug log For Homeseer 3 Arduino Plugin By Enigmatheatre (Greig Dempster)
    
                       Debugging Started at = 11/9/2018 ,12:02:25.041
    ****************************************************************************************
    Arduino Plugin Version = 1.0.0.131
    
    *************************************Homeseer*******************************************
    Homeseer Version = 3.0.0.435
    Homeseer IP = 192.168.1.5
    11/9/2018 ,12:02:25.072 Data Received:1 API 1415 6
    11/9/2018 ,12:02:25.087 Timer 1 Reset
    11/9/2018 ,12:02:25.103 Arduino, Board:1, API Input:1415 does not exist. Please create the device first.
    11/9/2018 ,12:02:25.103 New UDP Text Recieved = 1 API 1415 6
    OS Version = Microsoft Windows Server 2012 R2 Standard (6.2.9200.0)
    
    *************************************Plugins********************************************
    Plug-In List, Arduino Plugin: is currently enabled.
    Plug-In List, weatherXML: is currently enabled.
    Plug-In List, Z-Wave: is currently enabled.
    
    **************************************Setup*********************************************
    11/9/2018 ,12:02:26.041 Data Received:1 API 1415 6
    11/9/2018 ,12:02:26.072 Timer 1 Reset
    11/9/2018 ,12:02:26.088 New UDP Text Recieved = 1 API 1415 6
    11/9/2018 ,12:02:26.103 Arduino, Board:1, API Input:1415 does not exist. Please create the device first.
    11/9/2018 ,12:02:27.009 Data Received:1 API 1415 6
    11/9/2018 ,12:02:27.009 Timer 1 Reset
    11/9/2018 ,12:02:27.025 New UDP Text Recieved = 1 API 1415 6
    11/9/2018 ,12:02:27.041 Arduino, Board:1, API Input:1415 does not exist. Please create the device first.
    Ref = 1374    | Address = Arduino, Board:1        autoconnect = 1, boardno = 1, boardtype = 1, comport = 1001, debounce = 30, index = 1, ipaddress = 192.168.1.38, macaddress = DE:AD:BE:EF:FE:EE, port = 8900, type = Board,
    Ref = 1384    | Address = Arduino, Board:2        autoconnect = 1, boardno = 2, boardtype = 0, comport = 1001, debounce = 30, index = 2, ipaddress = 192.168.1.37, macaddress = DE:AD:BE:EF:FE:EF, port = 8900, type = Board,
    Ref = 1415    | Address = Arduino, Board:1, API Input:1        boardno = 1, index = 1, type = APIInput, unit = , update = 0,
    Board: 1,     Board index: 1374, Comport = 1001
    Board: 2,     Board index: 1384, Comport = 1001
    
    
    ***************************************LOG**********************************************
    11/9/2018 ,12:02:27.994 Data Received:1 API 1415 6
    11/9/2018 ,12:02:27.994 Timer 1 Reset
    11/9/2018 ,12:02:28.009 Arduino, Board:1, API Input:1415 does not exist. Please create the device first.
    11/9/2018 ,12:02:28.009 New UDP Text Recieved = 1 API 1415 6
    11/9/2018 ,12:02:28.978 Data Received:1 API 1415 7
    11/9/2018 ,12:02:28.978 PED Get Comport from Device Ref 1384 returning = 1001
    11/9/2018 ,12:02:28.994 Timer 1 Reset
    11/9/2018 ,12:02:28.994 PED Get IPAddress from Device Ref 1384 returning = 192.168.1.37
    11/9/2018 ,12:02:29.009 New UDP Text Recieved = 1 API 1415 7
    11/9/2018 ,12:02:29.025 Arduino, Board:1, API Input:1415 does not exist. Please create the device first.
    11/9/2018 ,12:02:29.025 PED Get Port from Device Ref 1384 returning = 8900
    11/9/2018 ,12:02:29.088 Sent to IP:192.168.1.37 Port No:8900 = 2 K           ,Keep Alive Command
    11/9/2018 ,12:02:29.088 Keeping Board: 2 Comport Alive.
    11/9/2018 ,12:02:29.963 Data Received:1 API 1415 7
    11/9/2018 ,12:02:29.978 Timer 1 Reset
    11/9/2018 ,12:02:29.994 Arduino, Board:1, API Input:1415 does not exist. Please create the device first.
    11/9/2018 ,12:02:29.994 New UDP Text Recieved = 1 API 1415 7
    11/9/2018 ,12:02:30.463 postBackProc ShowLog=checked&id=ShowLog
    11/9/2018 ,12:02:31.072 Data Received:1 API 1415 6
    11/9/2018 ,12:02:31.072 Timer 1 Reset
    11/9/2018 ,12:02:31.088 New UDP Text Recieved = 1 API 1415 6
    -snip-

    Comment


    • #3
      The sendtohs() function uses the plugin device number not the device ref. So I guess as this is the first device you have setup it would be 1.

      You can view the numbers on the plugin page and i thik the device address also has it in it.

      Greig.

      Sent from my SM-G950F using Tapatalk


      Zwave = Z-Stick, 3xHSM100� 7xACT ZDM230, 1xEverspring SM103, 2xACT HomePro ZRP210.
      X10 = CM12U, 2xAM12, 1xAW10, 1 x TM13U, 1xMS13, 2xHR10, 2xSS13
      Other Hardware = ADI Ocelot + secu16, Global Cache GC100, RFXtrx433, 3 x Foscams.
      Plugings = RFXcom, ActiveBackup, Applied Digital Ocelot, BLDeviceMatrix, BLGarbage, BLLAN, Current Cost, Global Cache GC100,HSTouch Android, HSTouch Server, HSTouch Server Unlimited, NetCAM, PowerTrigger, SageWebcamXP, SqueezeBox, X10 CM11A/CM12U.
      Scripts =
      Various

      Comment


      • #4
        Aaaaaaand that was it. Duh.

        Thank you Greig!!!!!!

        Also, do you have an onboarding process for adding new sensor types?

        For example, I could easily clean up this code for the ultrasonic distance sensor and send to you.

        I also am going to code for the newer DHT-22 sensor.

        To be super smart, what would be sharp would be to have an online library of sensor types (on a third party website) that you download into the Board config.

        So you create a pin first in the Plugin for that board, which opens a textarea formfield on the Plugin config Page. Then you copy/paste a code snipped from the library site for that sensor.

        Allows for a gazillion device types without bloating your canned board code / core/basic device types. But doesn’t immediately force a non-developer into the full API sketch.

        Just a thought.

        PS you could almost run the code library as a wiki

        Comment


        • #5
          FKSK

          I was thinking along those lines after looking at Tasmota & ESPeasy, add different device types without going to an API.....Currently I've been able to add these unusual devices by using those distributions with MQTT. Would be nice if they were available in the Arduino Plugin however it would probably need a substantial change in the way the plugin works....that is Greigs domain :-)

          Cheers..Pete
          HS 2.2.0.11

          Comment

          Working...
          X