Announcement

Collapse
No announcement yet.

API pulse measure

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

    API pulse measure

    Hi,
    I'd like to use arduino to send to HS data about electricity power.
    I have a system that send a pulse (+5V) every watt.
    Using this sketch on serial monitor all is ok:

    Code:
    // Pin impulso
    const int getpulsepin = 2;
    
    // variabili
    float assorbimento = 0.0;
    unsigned long startime = 0;
    unsigned long durata = 0;
    int stato = 0;
    int pinval = 0;
    
    void setup() {
      Serial.begin(9600);  
      pinMode(getpulsepin, INPUT); // pin dell'anemometro
    }
    
    
    void loop ()  
    { 
        pinval = digitalRead(getpulsepin);   // read sensor pin
     
        if ((stato == 0) & (pinval == 1)) { // transition point
        durata = millis() - startime;       // duration pulse
        startime = millis();            // reset millis startime 
        assorbimento = 3600.0/durata*1000;  // Wh
        }
     
        stato = pinval;         // 
        Serial.print("assorbimento KW/h = ");
          Serial.println(assorbimento);  
    }

    Created a API board in HS, selected device 1 as IMPUT and loaded the arduino API sketch:

    Code:
    //For serial set to 0 and for Ethernet set to 1
    #define ISIP 0
    
    //Do NOT modify these
    #if ISIP == 1
    #include <EEPROM.h>
    #include <SPI.h>       
    #include <Ethernet.h>
    #include <EthernetUdp.h> 
    #endif
    /************************************************************
     *Arduino to Homeseer 3 Plugin API writen by Enigma Theatre.*
     * V1.0.0.36                                                *
     *                                                          *
     *******Change the values below only*************************
     */
     
     // Pin impulso
    const int getpulsepin = 2;
    
    // variabili
    float assorbimento = 0.0;
    unsigned long startime = 0;
    unsigned long durata = 0;
    int stato = 0;
    int pinval = 0;
    
    
    //Address of the board.
    const byte BoardAdd = 1;
    
    #if ISIP == 1
    // Enter a MAC address and IP address for your board below.
    byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};
    
    // The IP address will be dependent on your local network.
    IPAddress ip(192,168,0,100);     //IP entered in HS config.
    const unsigned int localPort = 9000;      //port entered in HS config.
    IPAddress HomeseerIP(192,168,0,123); //Homeseer IP address
    IPAddress ServerIP(EEPROM.read(2),EEPROM.read(3),EEPROM.read(4),EEPROM.read(5));
    byte EEpromVersion = EEPROM.read(250);
    #endif
    
    
    //************Do not change anything in Here*****************
    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;                            // *
    //***********************************************************
    
    
    void setup() {
      HSSetup();
      //************************
      //Add YOUR SETUP HERE;
      //************************
    
      pinMode(getpulsepin, INPUT); // pin dell'anemometro
    
    
    }
    
    
    
    
    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.
         */
    
        pinval = digitalRead(getpulsepin);   // legge il pin del sensore
     
        if ((stato == 0) & (pinval == 1)) { // punto di transizione
        durata = millis() - startime;       // calcola la durata della pulsazione
        startime = millis();            // setta il nuovo tempo di partenza 
        assorbimento = 3600.0/durata*1000;  // calcola consumo
        }
        stato = pinval;         // imposta lo stato uguale alla lettura del pin
        SendToHS(1,assorbimento);  
        
        
    
    
    /*Execute regardless of connection status*/
    
    
     if (IsConnected == true) {
       /*Execute ONLY when HomeSeer is connected*/
    
      }
    }
    
    
    
    
    const char* Version = "API1.0.0.36";
    
    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(" ");
      Udp.print(Data);
      Udp.endPacket();
    #else
      Serial.print(BoardAdd);
      Serial.print(" API ");
      Serial.print(Device);
      Serial.print(" ");
      Serial.println(Data);
    #endif
    }
    }
    I'm having the error:
    PHP Code:
    lug-12 11:50:27         Arduino Plugin    Exception in RecievedTextRiferimento a un oggetto non impostato su un'istanza di oggetto.
    lug-12 11:50:27         Arduino Plugin    Exception in RecievedText: Riferimento a un oggetto non impostato su un'
    istanza di oggetto.
    lug-12 11:50:27         Arduino Plugin    Exception in RecievedTextRiferimento a un oggetto non impostato su un'istanza di oggetto.
    lug-12 11:50:27         Arduino Plugin    Exception in RecievedText: Riferimento a un oggetto non impostato su un'
    istanza di oggetto.
    lug-12 11:50:27         Arduino Plugin    Exception in RecievedTextRiferimento a un oggetto non impostato su un'istanza di oggetto.
    lug-12 11:50:27         Arduino Plugin    Exception in RecievedText: Riferimento a un oggetto non impostato su un'
    istanza di oggetto.
    lug-12 11:50:27         Arduino Plugin    Board 1Device Ref 4API Input 1Value 0String 0Wh
    lug
    -12 11:50:27         Arduino Plugin    Board 1Device Ref 4API Input 1Value 0String 0Wh
    lug
    -12 11:50:27         Arduino Plugin    Exception in RecievedTextRiferimento a un oggetto non impostato su un'istanza di oggetto.
    lug-12 11:50:27         Arduino Plugin    Board 1, Device Ref = 4, API Input 1, Value = 0, String = 0Wh
    lug-12 11:50:27         Arduino Plugin    Board 1, Device Ref = 4, API Input 1, Value = 0, String = 0Wh 

    Were the translation of "Exception in RecievedText: Riferimento a un oggetto non impostato su un'istanza di oggetto." is "Exception in RecievedText: Object reference not set to an instance of an object."

    Where I'm wrong?

    This is my last step and then i can switch all my automation on arduino (I/O, PWM, etc. work well).

    Thanks

    #2
    I think this is an old bug in the API setup. Please back up your system then run the beta that is in the updater as it is fixed in there.

    Sent from my HTC One 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


      #3
      Thanks Greig! Ok, now work well, or quite well...
      Is not important for me, but is it possible pass to HS a decimal value?
      I'm trying using float, but no decimal in HS devices.
      Example, I count the time between 2 pulse, are 12435 millisecond. I'd like to see 12,435sec on HS device.
      Do you think that the italian setting for decimal number could be the problem? (i use 12,435 (comma) and not 12.435 (dot).

      Thanks

      Cris

      Comment


        #4
        Originally posted by khriss75 View Post
        Thanks Greig! Ok, now work well, or quite well...
        Is not important for me, but is it possible pass to HS a decimal value?
        I'm trying using float, but no decimal in HS devices.
        Example, I count the time between 2 pulse, are 12435 millisecond. I'd like to see 12,435sec on HS device.
        Do you think that the italian setting for decimal number could be the problem? (i use 12,435 (comma) and not 12.435 (dot).

        Thanks

        Cris
        Can you not change the device decimal place in the device config status graphics setup?

        Greig.
        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


          #5
          Originally posted by enigmatheatre View Post
          Can you not change the device decimal place in the device config status graphics setup?

          Greig.
          Could be that I didn't understood correctly your suggestion, but in the device config --> status graph --> add new range value if i insert "2" in "Dec Places" box, nothing change.

          Do you think that I have to pass two different data by API input?
          -a integer for device value
          -a float or string for device string

          A little other info:
          In "Arduino Plugin Config" tab, when i select board 2, appear a message "Building board n2 setup" and the screeen is quite gray color.
          The log:
          PHP Code:
          lug-12 18:49:07         Arduino Plugin    Updating Values on Board No2
          lug
          -12 18:49:04         Arduino Plugin    Updating pins on Board No2
          lug
          -12 18:49:04         Arduino Plugin    Connected to Board No:2 on COM5
          lug
          -12 18:49:04         Arduino Plugin    Board 2 Version API1.0.0.81 and Plugin Version 1.0.0.85 are compatible
          Is it normal?

          Thanks

          Comment

          Working...
          X