Announcement

Collapse
No announcement yet.

Custom API help? - Adding a pushbutton

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

  • Custom API help? - Adding a pushbutton

    I'm starting off basic...I want to have a pushbutton toggle an output off and on, then i'll be elborating with more pushbuttons and outputs, then link it to HS. This is what I have so far:

    Code:
    /*************************************************************
     *Arduino to Homeseer 3 Plugin API written by Enigma Theatre.*
     * V1.0.0.140                                                *
     *                                                           *
     *************************************************************/
    int FromHS[50];                                              
    boolean IsConnected = false;                                 
    //************************************************************
    
    
    //**************Declare your variables here******************* 
    //Inputs
    int FogPin = 10;
    
    //Outputs
    int DrivFogLight = 16;
    int PassFogLight = 5; 
    
    //Variables
    int stateFog = HIGH;
    int readingFog;
    int previousFog = LOW;
    
    long time = 0;
    long debounce = 200;
    //****************************************************************
    
    void HSSetup() {
      
      //************************
      //Add YOUR SETUP HERE;
      //************************
    //Define Input Pins
      pinMode(FogPin, INPUT);
    
    //Define Output Pins
      pinMode(DrivFogLight, OUTPUT);
      pinMode(PassFogLight, OUTPUT);
    
    }
    
    void HSloop() {
    
        //************************
        //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.
         */
    
        /*Execute regardless of connection status*/
    
    //Monitoring the Fog Light Pin
    readingFog = digitalRead(FogPin);
    // if we just pressed the button (i.e. the input went from LOW to HIGH),
    // and we've waited long enough since the last press to ignore any noise...
    if (readingFog == HIGH && previousFog == LOW && millis() - time > debounce) {
      // ... invert the output
      if (stateFog == HIGH)
        stateFog = LOW;
      else
        stateFog = HIGH;
        // ... and remember when the last button press was
       time = millis();
    }
    digitalWrite(DrivFogLight, stateFog);
    digitalWrite(PassFogLight, stateFog);
    previousFog = readingFog;
    
     if (IsConnected == true) {
       /*Execute ONLY when HomeSeer is connected*/
       
    
      }
    
    }
    
    //************Do not change anything after Here*****************
    Now I can get this code to run just fine on it's on, but when I put it into the HS API I get a compiling error due to the use of "time". Any idea as the time is used for debounce with this switch but also must be used in the HS section too.

    C:\Users\conradan\AppData\Local\Temp\arduino_build_565717/arduino.ar(time.c.o): In function `time':

    C:\Users\conradan\AppData\Local\Arduino15\packages\esp8266\h ardware\esp8266\2.3.0-rc2\cores\esp8266/time.c:93: multiple definition of `time'

    C:\Users\conradan\AppData\Local\Temp\arduino_build_565717\sk etch\APIBoard3.ino.cpp.o.bss.time+0x0): first defined here

    c:/users/conradan/appdata/local/arduino15/packages/esp8266/tools/xtensa-lx106-elf-gcc/1.20.0-26-gb404fb9-2/bin/../lib/gcc/xtensa-lx106-elf/4.8.2/../../../../xtensa-lx106-elf/bin/ld.exe: Warning: size of symbol `time' changed from 4 in C:\Users\conradan\AppData\Local\Temp\arduino_build_565717\sk etch\APIBoard3.ino.cpp.o to 29 in C:\Users\conradan\AppData\Local\Temp\arduino_build_565717/arduino.ar(time.c.o)

    c:/users/conradan/appdata/local/arduino15/packages/esp8266/tools/xtensa-lx106-elf-gcc/1.20.0-26-gb404fb9-2/bin/../lib/gcc/xtensa-lx106-elf/4.8.2/../../../../xtensa-lx106-elf/bin/ld.exe: Warning: type of symbol `time' changed from 1 to 2 in C:\Users\conradan\AppData\Local\Temp\arduino_build_565717/arduino.ar(time.c.o)

    collect2.exe: error: ld returned 1 exit status

  • #2
    Name your variable something other than time. Maybe PreviousMillis.

    Also, and this may just be me being anal, I would change your if statement to be more clear of your intentions.

    Now
    Code:
    if (readingFog == HIGH && previousFog == LOW && millis() - time > debounce) {
    My change
    Code:
    if ((readingFog == HIGH) && (previousFog == LOW) && ((millis() - PreviousMillis) > debounce)) {

    Comment


    • #3
      Are you still stuck or are you ok?

      Comment


      • #4
        I was away all weekend, but I was working on this offline and found some other sample code. So stripped everything down and have one MO pushbutton and a relay connected to an output. I can get it to work with this code, no problem.

        Code:
        /*
         Debounce
        
         Each time the input pin goes from LOW to HIGH (e.g. because of a push-button
         press), the output pin is toggled from LOW to HIGH or HIGH to LOW.  There's
         a minimum delay between toggles to debounce the circuit (i.e. to ignore
         noise).
        
         The circuit:
         * LED attached from pin 13 to ground
         * pushbutton attached from pin 2 to +5V
         * 10K resistor attached from pin 2 to ground
        
         * Note: On most Arduino boards, there is already an LED on the board
         connected to pin 13, so you don't need any extra components for this example.
        
        
         created 21 November 2006
         by David A. Mellis
         modified 30 Aug 2011
         by Limor Fried
         modified 28 Dec 2012
         by Mike Walters
         modified 30 Aug 2016
         by Arturo Guadalupi
        
        
         This example code is in the public domain.
        
         http://www.arduino.cc/en/Tutorial/Debounce
         */
        
        // constants won't change. They're used here to
        // set pin numbers:
        const int FogButtonPin = 10;    // the number of the pushbutton pin
        const int DrivFogLightPin = 16;      // the number of the LED pin
        
        // Variables will change:
        int FogState = HIGH;         // the current state of the output pin
        int readingFog;             // the current reading from the input pin
        int lastreadingFog = LOW;   // the previous reading from the input pin
        
        // the following variables are unsigned long's because the time, measured in miliseconds,
        // will quickly become a bigger number than can be stored in an int.
        unsigned long lastDebounceTime = 0;  // the last time the output pin was toggled
        unsigned long debounceDelay = 50;    // the debounce time; increase if the output flickers
        
        void setup() {
          pinMode(FogButtonPin, INPUT);
          pinMode(DrivFogLightPin, OUTPUT);
        
          // set initial LED state
          digitalWrite(DrivFogLightPin, FogState);
        }
        
        void loop() {
          // read the state of the switch into a local variable:
          int reading = digitalRead(FogButtonPin);
        
          // check to see if you just pressed the button
          // (i.e. the input went from LOW to HIGH),  and you've waited
          // long enough since the last press to ignore any noise:
        
          // If the switch changed, due to noise or pressing:
          if (reading != lastreadingFog) {
            // reset the debouncing timer
            lastDebounceTime = millis();
          }
        
          if ((millis() - lastDebounceTime) > debounceDelay) {
            // whatever the reading is at, it's been there for longer
            // than the debounce delay, so take it as the actual current state:
        
            // if the button state has changed:
            if (reading != readingFog) {
              readingFog = reading;
        
              // only toggle the LED if the new button state is HIGH
              if (readingFog == HIGH) {
                FogState = !FogState;
              }
            }
          }
        
          // set the LED:
          digitalWrite(DrivFogLightPin, FogState);
        
          // save the reading.  Next time through the loop,
          // it'll be the lastreadingFog:
          lastreadingFog = reading;
        }
        It works great. Toggles the relay on and off. No probs. Now I snip and move those pieces into the HS API and nothing happens.

        Code:
        /*************************************************************
         *Arduino to Homeseer 3 Plugin API written by Enigma Theatre.*
         * V1.0.0.140                                                *
         *                                                           *
         *************************************************************/
        int FromHS[50];                                              
        boolean IsConnected = false;                                 
        //************************************************************
        
        
        //**************Declare your variables here******************* 
        //Inputs
        const int FogButtonPin = 10;
        
        //Outputs
        const int DrivFogLightPin = 16;
        
        //Variables
        int FogState = HIGH;         // the current state of the output pin
        int readingFog;             // the current reading from the input pin
        int lastreadingFog = LOW;   // the previous reading from the input pin
        
        // the following variables are unsigned long's because the time, measured in miliseconds,
        // will quickly become a bigger number than can be stored in an int.
        unsigned long lastDebounceTime = 0;  // the last time the output pin was toggled
        unsigned long debounceDelay = 50;    // the debounce time; increase if the output flickers
        //****************************************************************
        
        void HSSetup() {
          
          //************************
          //Add YOUR SETUP HERE;
          //************************
        //Define Input Pins
          pinMode(FogButtonPin, INPUT);
        
        //Define Output Pins
          pinMode(DrivFogLightPin, OUTPUT);
        
        //Set Initial Light State
          digitalWrite(DrivFogLightPin, FogState);
        }
        
        void HSloop() {
        
            //************************
            //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.
             */
        
            /*Execute regardless of connection status*/
        
          // read the state of the switch into a local variable:
          int reading = digitalRead(FogButtonPin);
        
          // check to see if you just pressed the button
          // (i.e. the input went from LOW to HIGH),  and you've waited
          // long enough since the last press to ignore any noise:
        
          // If the switch changed, due to noise or pressing:
          if (reading != lastreadingFog) {
            // reset the debouncing timer
            lastDebounceTime = millis();
          }
        
          if ((millis() - lastDebounceTime) > debounceDelay) {
            // whatever the reading is at, it's been there for longer
            // than the debounce delay, so take it as the actual current state:
        
            // if the button state has changed:
            if (reading != readingFog) {
              readingFog = reading;
        
              // only toggle the LED if the new button state is HIGH
              if (readingFog == HIGH) {
                FogState = !FogState;
              }
            }
          }
        
          // set the LED:
          digitalWrite(DrivFogLightPin, FogState);
        
          // save the reading.  Next time through the loop,
          // it'll be the lastreadingFog:
          lastreadingFog = reading;
        
         if (IsConnected == true) {
           /*Execute ONLY when HomeSeer is connected*/
           
        
          }
        
        }
        
        //************Do not change anything after Here*****************
        I also tried your suggestion on changing the variable and it didn't work either...not sure what's up...I feel like this is a small hurdle and once I'm over it, it should open up a lot of options.

        Comment


        • #5
          Can you post the full API sketch file you are uploading to the board.

          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


          • #6
            During initial development, I will often insert Serial.println("Testing debug line") statements to let me see the logic path as well as examine contents of variables. Then on the IDE you can use the serial monitor to see the debug lines. Once the code is operating properly, I will either comment the lines or remove them.

            Comment


            • #7
              For what it is worth, here is code that I use in one of my configurations that has 6 buttons on it. They are momentary buttons. It doesn't include any debounce logic but it does work.

              Code:
              // Button Variables
              int ButtonCnt = 6;        // can support from 0 to 6 buttons. Set this to the # you have
              int ButtonIndex = 0;
              int ButtonPin[]= {4, 2, 12, 13, 16, 5};
              int ButtonDevice[]= {7, 8, 9, 10, 11, 12};
              int ButtonStatus[] = {HIGH, HIGH, HIGH, HIGH, HIGH, HIGH};
              int ButtonReadStatus = HIGH;
              
              void HSSetup() {
                
                //************************
                //Add YOUR SETUP HERE;
                //************************
              
                for (ButtonIndex  = 0; ButtonIndex < ButtonCnt; ButtonIndex++) {
                   if (ButtonPin[ButtonIndex]) pinMode(ButtonPin[ButtonIndex],  INPUT_PULLUP);  
                }
              }
              
              void HSloop() {
                  //************************
                  //Add YOUR CODE HERE;
                  //************************
              
              // ************  Start of 'Buttons' *************
              // ***** Up to six buttons may be processed *****
                 if (ButtonIndex) {
                   for (ButtonIndex = 0; ButtonIndex < ButtonCnt; ButtonIndex++) {
                      if (ButtonPin[ButtonIndex]) {
                         ButtonReadStatus = HIGH;
                         ButtonReadStatus = digitalRead(ButtonPin[ButtonIndex]);
                         if (ButtonReadStatus != ButtonStatus[ButtonIndex]) {       
                            if (ButtonReadStatus == LOW) SendToHS(ButtonDevice[ButtonIndex],100);
                            if (ButtonReadStatus == HIGH) SendToHS(ButtonDevice[ButtonIndex],0); 
                            ButtonStatus[ButtonIndex] = ButtonReadStatus;  
                         }      
                     }
                   }
                 }
              // ****** End of 'Buttons' **************
              }

              Comment


              • #8
                This is the whole code. Doesn't throw any compile errors when uploading to the board. I am running this outside of my home wifi network so I can't see it connect or anything yet...just trying to get the pushbutton stuff to work and then I can get within my wifi network.

                I'll try the serial print to see where things go awry. Or I'll try and use your code and see what's up too. I know it has to be something simple I'm missing.

                I omitted the rest of the code before as it had IP, wifi, password etc...but I just changed them to random stuff for stray prying eyes.

                Code:
                /*************************************************************
                 *Arduino to Homeseer 3 Plugin API written by Enigma Theatre.*
                 * V1.0.0.140                                                *
                 *                                                           *
                 *************************************************************/
                int FromHS[50];                                              
                boolean IsConnected = false;                                 
                //************************************************************
                
                
                //**************Declare your variables here******************* 
                //Inputs
                const int FogButtonPin = 10;
                
                //Outputs
                const int DrivFogLightPin = 16;
                
                //Variables
                int FogState = HIGH;         // the current state of the output pin
                int readingFog;             // the current reading from the input pin
                int lastreadingFog = LOW;   // the previous reading from the input pin
                
                // the following variables are unsigned long's because the time, measured in miliseconds,
                // will quickly become a bigger number than can be stored in an int.
                unsigned long lastDebounceTime = 0;  // the last time the output pin was toggled
                unsigned long debounceDelay = 50;    // the debounce time; increase if the output flickers
                //****************************************************************
                
                void HSSetup() {
                  
                  //************************
                  //Add YOUR SETUP HERE;
                  //************************
                //Define Input Pins
                  pinMode(FogButtonPin, INPUT);
                
                //Define Output Pins
                  pinMode(DrivFogLightPin, OUTPUT);
                
                //Set Initial Light State
                  digitalWrite(DrivFogLightPin, FogState);
                }
                
                void HSloop() {
                
                    //************************
                    //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.
                     */
                
                    /*Execute regardless of connection status*/
                
                  // read the state of the switch into a local variable:
                  int reading = digitalRead(FogButtonPin);
                
                  // check to see if you just pressed the button
                  // (i.e. the input went from LOW to HIGH),  and you've waited
                  // long enough since the last press to ignore any noise:
                
                  // If the switch changed, due to noise or pressing:
                  if (reading != lastreadingFog) {
                    // reset the debouncing timer
                    lastDebounceTime = millis();
                  }
                
                  if ((millis() - lastDebounceTime) > debounceDelay) {
                    // whatever the reading is at, it's been there for longer
                    // than the debounce delay, so take it as the actual current state:
                
                    // if the button state has changed:
                    if (reading != readingFog) {
                      readingFog = reading;
                
                      // only toggle the LED if the new button state is HIGH
                      if (readingFog == HIGH) {
                        FogState = !FogState;
                      }
                    }
                  }
                
                  // set the LED:
                  digitalWrite(DrivFogLightPin, FogState);
                
                  // save the reading.  Next time through the loop,
                  // it'll be the lastreadingFog:
                  lastreadingFog = reading;
                
                 if (IsConnected == true) {
                   /*Execute ONLY when HomeSeer is connected*/
                   
                
                  }
                
                }
                
                //************Do not change anything after Here*****************
                 
                #define ISIP 1
                #define BoardType 3
                const byte BoardAdd = 3;
                
                #include <EEPROM.h>
                
                #if BoardType == 3
                #include <ESP8266WiFi.h>
                #include <WiFiUdp.h>
                char ssid[] = "The Internet";
                char pass[] = "123456";
                #else
                #include <SPI.h>
                #include <Ethernet.h>
                #include <EthernetUdp.h>
                #endif
                
                
                #if ISIP == 1
                
                
                byte mac[] = {0x00, 0xAA, 0xBB, 0xCC, 0xDE, 0x03};
                IPAddress ip(192,168,1,140);     //IP entered In HS config.
                const unsigned int localPort = 8910;      //port entered In HS config.
                IPAddress HomeseerIP(192,168,1,190); //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);
                char packetBuffer[UDP_TX_PACKET_MAX_SIZE];              
                const unsigned int ServerPort = 8888; 
                #endif
                
                                                        
                                           
                
                #if BoardType == 3
                WiFiUDP Udp;
                WiFiUDP SendPort;
                #else
                EthernetUDP Udp;
                #endif
                
                #if BoardType == 3
                void resetFunc() {
                  ESP.restart();
                }
                #else
                void(* resetFunc) (void) = 0;
                #endif
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                char* Version = "API1.0.0.140";
                
                byte Byte1, Byte2, Byte3;
                unsigned int Byte4, Byte5;
                
                
                void setup() {
                
                #if BoardType == 3
                 // WiFi.persistent(false);
                  EEPROM.begin(256);
                  EEpromVersion = EEPROM.read(250);
                #endif
                
                #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;
                  }
                #if BoardType == 3
                  //Serial.begin(115200);
                  WiFi.begin(ssid, pass);
                  WiFi.config(ip, gateway, subnet);
                
                  while (WiFi.status() != WL_CONNECTED) {
                    delay(500);
                  }
                #else
                  Ethernet.begin(mac, ip, gateway, gateway, subnet);
                #endif
                  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;
                HSSetup();
                }
                
                
                void loop(){
                  HSloop();
                #if ISIP == 1
                  UDPCheck();
                #endif
                }
                
                
                void SendConnect()
                {
                #if ISIP == 0
                  Serial.print("Connect ");
                  Serial.println(BoardAdd);
                #else
                    Udp.beginPacket(HomeseerIP,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 UDPCheck(){
                  int packetSize = Udp.parsePacket();
                  if(packetSize)
                  {
                    
                 #if BoardType == 3
                    IPAddress remote = Udp.remoteIP();
                    Byte1 = Udp.parseInt();
                    Udp.read();
                    Byte2 = Udp.read();
                    Udp.read();
                    Byte3 = Udp.parseInt();
                    Udp.read();
                    Byte4 = Udp.parseInt();
                    Udp.read();
                    Byte5 = Udp.parseInt();
                    DataEvent();
                    #else
                
                   ServerIP = Udp.remoteIP();
                    Byte1 = Udp.parseInt();
                    Udp.read();
                    Byte2 = Udp.read();
                    Byte3 = Udp.parseInt();;
                    Byte4 = Udp.parseInt();
                    Byte5 = Udp.parseInt();;
                
                    DataEvent();
                #endif
                  }
                }
                
                #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) {
                
                #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
                
                    switch (Byte2) {
                    case 'c':
                      IsConnected = true;
                      break;
                
                    case 'C':   
                #if ISIP == 1
                      Udp.beginPacket(HomeseerIP, ServerPort);
                      Udp.print("Version ");
                      Udp.print(BoardAdd);
                      Udp.print(" ");
                      Udp.print(Version);
                      Udp.println(" HS3");
                      Udp.endPacket();
                
                      Udp.beginPacket(HomeseerIP, 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(HomeseerIP, 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(HomeseerIP, 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, int Data){
                if (IsConnected == true) {
                #if ISIP == 1
                  Udp.beginPacket(HomeseerIP, 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(HomeseerIP, 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, double Data){
                if (IsConnected == true) {
                #if ISIP == 1
                  Udp.beginPacket(HomeseerIP, 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
                }
                }

                Comment


                • #9
                  Originally posted by Conrad_Turbo View Post
                  This is the whole code. Doesn't throw any compile errors when uploading to the board. I am running this outside of my home wifi network so I can't see it connect or anything yet...just trying to get the pushbutton stuff to work and then I can get within my wifi network.

                  I'll try the serial print to see where things go awry. Or I'll try and use your code and see what's up too. I know it has to be something simple I'm missing.

                  I omitted the rest of the code before as it had IP, wifi, password etc...but I just changed them to random stuff for stray prying eyes.

                  Code:
                  /*************************************************************
                   *Arduino to Homeseer 3 Plugin API written by Enigma Theatre.*
                   * V1.0.0.140                                                *
                   *                                                           *
                   *************************************************************/
                  int FromHS[50];                                              
                  boolean IsConnected = false;                                 
                  //************************************************************
                  
                  
                  //**************Declare your variables here******************* 
                  //Inputs
                  const int FogButtonPin = 10;
                  
                  //Outputs
                  const int DrivFogLightPin = 16;
                  
                  //Variables
                  int FogState = HIGH;         // the current state of the output pin
                  int readingFog;             // the current reading from the input pin
                  int lastreadingFog = LOW;   // the previous reading from the input pin
                  
                  // the following variables are unsigned long's because the time, measured in miliseconds,
                  // will quickly become a bigger number than can be stored in an int.
                  unsigned long lastDebounceTime = 0;  // the last time the output pin was toggled
                  unsigned long debounceDelay = 50;    // the debounce time; increase if the output flickers
                  //****************************************************************
                  
                  void HSSetup() {
                    
                    //************************
                    //Add YOUR SETUP HERE;
                    //************************
                  //Define Input Pins
                    pinMode(FogButtonPin, INPUT);
                  
                  //Define Output Pins
                    pinMode(DrivFogLightPin, OUTPUT);
                  
                  //Set Initial Light State
                    digitalWrite(DrivFogLightPin, FogState);
                  }
                  
                  void HSloop() {
                  
                      //************************
                      //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.
                       */
                  
                      /*Execute regardless of connection status*/
                  
                    // read the state of the switch into a local variable:
                    int reading = digitalRead(FogButtonPin);
                  
                    // check to see if you just pressed the button
                    // (i.e. the input went from LOW to HIGH),  and you've waited
                    // long enough since the last press to ignore any noise:
                  
                    // If the switch changed, due to noise or pressing:
                    if (reading != lastreadingFog) {
                      // reset the debouncing timer
                      lastDebounceTime = millis();
                    }
                  
                    if ((millis() - lastDebounceTime) > debounceDelay) {
                      // whatever the reading is at, it's been there for longer
                      // than the debounce delay, so take it as the actual current state:
                  
                      // if the button state has changed:
                      if (reading != readingFog) {
                        readingFog = reading;
                  
                        // only toggle the LED if the new button state is HIGH
                        if (readingFog == HIGH) {
                          FogState = !FogState;
                        }
                      }
                    }
                  
                    // set the LED:
                    digitalWrite(DrivFogLightPin, FogState);
                  
                    // save the reading.  Next time through the loop,
                    // it'll be the lastreadingFog:
                    lastreadingFog = reading;
                  
                   if (IsConnected == true) {
                     /*Execute ONLY when HomeSeer is connected*/
                     
                  
                    }
                  
                  }
                  
                  //************Do not change anything after Here*****************
                   
                  #define ISIP 1
                  #define BoardType 3
                  const byte BoardAdd = 3;
                  
                  #include <EEPROM.h>
                  
                  #if BoardType == 3
                  #include <ESP8266WiFi.h>
                  #include <WiFiUdp.h>
                  char ssid[] = "The Internet";
                  char pass[] = "123456";
                  #else
                  #include <SPI.h>
                  #include <Ethernet.h>
                  #include <EthernetUdp.h>
                  #endif
                  
                  
                  #if ISIP == 1
                  
                  
                  byte mac[] = {0x00, 0xAA, 0xBB, 0xCC, 0xDE, 0x03};
                  IPAddress ip(192,168,1,140);     //IP entered In HS config.
                  const unsigned int localPort = 8910;      //port entered In HS config.
                  IPAddress HomeseerIP(192,168,1,190); //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);
                  char packetBuffer[UDP_TX_PACKET_MAX_SIZE];              
                  const unsigned int ServerPort = 8888; 
                  #endif
                  
                                                          
                                             
                  
                  #if BoardType == 3
                  WiFiUDP Udp;
                  WiFiUDP SendPort;
                  #else
                  EthernetUDP Udp;
                  #endif
                  
                  #if BoardType == 3
                  void resetFunc() {
                    ESP.restart();
                  }
                  #else
                  void(* resetFunc) (void) = 0;
                  #endif
                  
                  
                  
                  
                  
                  
                  
                  
                  
                  
                  
                  
                  
                  
                  
                  
                  
                  
                  
                  
                  
                  
                  
                  
                  
                  
                  
                  
                  
                  
                  
                  
                  
                  
                  
                  
                  char* Version = "API1.0.0.140";
                  
                  byte Byte1, Byte2, Byte3;
                  unsigned int Byte4, Byte5;
                  
                  
                  void setup() {
                  
                  #if BoardType == 3
                   // WiFi.persistent(false);
                    EEPROM.begin(256);
                    EEpromVersion = EEPROM.read(250);
                  #endif
                  
                  #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;
                    }
                  #if BoardType == 3
                    //Serial.begin(115200);
                    WiFi.begin(ssid, pass);
                    WiFi.config(ip, gateway, subnet);
                  
                    while (WiFi.status() != WL_CONNECTED) {
                      delay(500);
                    }
                  #else
                    Ethernet.begin(mac, ip, gateway, gateway, subnet);
                  #endif
                    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;
                  HSSetup();
                  }
                  
                  
                  void loop(){
                    HSloop();
                  #if ISIP == 1
                    UDPCheck();
                  #endif
                  }
                  
                  
                  void SendConnect()
                  {
                  #if ISIP == 0
                    Serial.print("Connect ");
                    Serial.println(BoardAdd);
                  #else
                      Udp.beginPacket(HomeseerIP,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 UDPCheck(){
                    int packetSize = Udp.parsePacket();
                    if(packetSize)
                    {
                      
                   #if BoardType == 3
                      IPAddress remote = Udp.remoteIP();
                      Byte1 = Udp.parseInt();
                      Udp.read();
                      Byte2 = Udp.read();
                      Udp.read();
                      Byte3 = Udp.parseInt();
                      Udp.read();
                      Byte4 = Udp.parseInt();
                      Udp.read();
                      Byte5 = Udp.parseInt();
                      DataEvent();
                      #else
                  
                     ServerIP = Udp.remoteIP();
                      Byte1 = Udp.parseInt();
                      Udp.read();
                      Byte2 = Udp.read();
                      Byte3 = Udp.parseInt();;
                      Byte4 = Udp.parseInt();
                      Byte5 = Udp.parseInt();;
                  
                      DataEvent();
                  #endif
                    }
                  }
                  
                  #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) {
                  
                  #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
                  
                      switch (Byte2) {
                      case 'c':
                        IsConnected = true;
                        break;
                  
                      case 'C':   
                  #if ISIP == 1
                        Udp.beginPacket(HomeseerIP, ServerPort);
                        Udp.print("Version ");
                        Udp.print(BoardAdd);
                        Udp.print(" ");
                        Udp.print(Version);
                        Udp.println(" HS3");
                        Udp.endPacket();
                  
                        Udp.beginPacket(HomeseerIP, 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(HomeseerIP, 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(HomeseerIP, 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, int Data){
                  if (IsConnected == true) {
                  #if ISIP == 1
                    Udp.beginPacket(HomeseerIP, 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(HomeseerIP, 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, double Data){
                  if (IsConnected == true) {
                  #if ISIP == 1
                    Udp.beginPacket(HomeseerIP, 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
                  }
                  }
                  Try changing

                  pinMode(FogButtonPin, INPUT);

                  to

                  pinMode(FogButtonPin, INPUT_PULLUP);

                  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


                  • #10
                    Originally posted by enigmatheatre View Post
                    Try changing

                    pinMode(FogButtonPin, INPUT);

                    to

                    pinMode(FogButtonPin, INPUT_PULLUP);

                    Greig.
                    Tried that too Greig and nothing...I also changed it on the debounce sample code and it still worked with that when it was either INPUT or INPUT_PULLUP. Hmph.

                    I also tossed in the HSSetup()

                    Serial.begin(115200);

                    and also did it in the debounce code and I could read from the serial monitor the code tossed in Serial.println("Testing debug line"); in the debounce code. I can see it come up in the serial monitor for the debounce code however for the API code nothing comes up in the serial monitor. I'm not getting why the code is not functioning the same as a non API code...

                    Comment


                    • #11
                      Originally posted by Conrad_Turbo View Post
                      Tried that too Greig and nothing...I also changed it on the debounce sample code and it still worked with that when it was either INPUT or INPUT_PULLUP. Hmph.

                      I also tossed in the HSSetup()

                      Serial.begin(115200);

                      and also did it in the debounce code and I could read from the serial monitor the code tossed in Serial.println("Testing debug line"); in the debounce code. I can see it come up in the serial monitor for the debounce code however for the API code nothing comes up in the serial monitor. I'm not getting why the code is not functioning the same as a non API code...
                      Are you running these tests on a single board or do you have two boards? If multiple, are they the same type of board?

                      Comment


                      • #12
                        Originally posted by logbuilder View Post
                        Are you running these tests on a single board or do you have two boards? If multiple, are they the same type of board?
                        Exact same NodeMCU board. Just loading the debounce sketch to it, it works, then load the HS API with the "same" sketch and it doesn't work...I have no idea.

                        Comment

                        Working...
                        X