Announcement

Collapse
No announcement yet.

NodeMCU API for beginners

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

    NodeMCU API for beginners

    All,

    Does anyone have available a simple NodeMCU API sketch performing a simple process? For example- blinking a light or something else very basic. I am a complete novice who has managed to get a light to blink using the IDE alone and figured out how to use the Homeseer with the Arduino Plugin but this API stuff is not clicking yet. So far all the examples/sketches I found seem too complicated with LCD and sensors I don't have yet.

    I have also looked (unsuccessfully) to see if a post exists as a library of successful sketches people have made for this plugin.

    Thanks!

    Russ

    #2
    This is an example of a NodeMCU API sketch that has 5 temperature sensors, flashing lights and LCD panel. The example contains a lot of screenshots of the setup as well as having the code commented. Hopefully this will help get you going. See https://forums.homeseer.com/forum/an...-panel-example

    Greig also has a few examples in the documentation for plugin

    Hopefully this will get you going.
    Billy

    Comment


      #3
      Thanks Billy, I ordered the recommended humidity sensors to see if I can get it to work.

      Currently in inventory for things I am trying to figure out are:

      MQ2
      HX711
      MAX31850K
      SHT30
      DHT22 (on order)

      I've had luck with one-wire DS18B20, infrared fluid switch, float switch, reed switch, fluid flow meter but all those used Grieg's existing functionality so it was fairly foolproof. Connecting the dots with API where we take Arduino code and allow HS to use it is where I am having a mental block.

      Comment


        #4
        Finally figured it all out, here is my library so far which includes HX711, DHT22, MQ2. This is only the top API portion of the sketch and simplified as much as I could make it.

        HX711

        Code:
        /************************************************************ *
        Arduino to HS4 Plugin API written by Enigma Theatre.
        V4.0.2.16 VALUE AND STRING
        * *
        ************************************************************ */
        int FromHS[50];
        boolean IsConnected = false;
        byte AlivePin = 255;
        //************************************************************
        //To Use the Alive pin change the 255 to the pin number above.
        
        
        
        //**************Declare your variables here*******************
        
        
        [HASHTAG="t3158"]include[/HASHTAG] "HX711.h"
        
        // HX711 circuit wiring DT d6 SCK d7 VCC 3v3 GRD
        const int LOADCELL_DOUT_PIN = 12;
        const int LOADCELL_SCK_PIN = 13;
        
        HX711 scale;
        
        //************************************************************ ****
        
        
        
        
        void HSSetup() {
        
        //************************
        //Add YOUR SETUP HERE;
        //************************
        
        Serial.begin(115200);
        Serial.println("HX711 Demo");
        Serial.println("Initializing the scale");
        
        scale.begin(LOADCELL_DOUT_PIN, LOADCELL_SCK_PIN);
        
        Serial.println("Before setting up the scale:");
        Serial.print("read: \t\t");
        Serial.println(scale.read()); // print a raw reading from the ADC
        
        Serial.print("read average: \t\t");
        Serial.println(scale.read_average(20)); // print the average of 20 readings from the ADC
        
        Serial.print("get value: \t\t");
        Serial.println(scale.get_value(5)); // print the average of 5 readings from the ADC minus the tare weight (not set yet)
        
        Serial.print("get units: \t\t");
        Serial.println(scale.get_units(5), 1); // print the average of 5 readings from the ADC minus tare weight (not set) divided
        // by the SCALE parameter (not set yet)
        
        scale.set_scale(42.648);
        //scale.set_scale(-471.497); // this value is obtained by calibrating the scale with known weights; see the README for details
        scale.tare(); // reset the scale to 0
        
        Serial.println("After setting up the scale:");
        
        Serial.print("read: \t\t");
        Serial.println(scale.read()); // print a raw reading from the ADC
        
        Serial.print("read average: \t\t");
        Serial.println(scale.read_average(20)); // print the average of 20 readings from the ADC
        
        Serial.print("get value: \t\t");
        Serial.println(scale.get_value(5)); // print the average of 5 readings from the ADC minus the tare weight, set with tare()
        
        Serial.print("get units: \t\t");
        Serial.println(scale.get_units(5), 1); // print the average of 5 readings from the ADC minus tare weight, divided
        // by the SCALE parameter set with set_scale
        
        Serial.println("Readings:");
        }
        
        
        
        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*/
        
        Serial.print("one reading:\t");
        Serial.print(scale.get_units(), 1);
        Serial.print("\t| average:\t");
        Serial.println(scale.get_units(10), 5);
        SendToHS(1,scale.get_units());
        }
        
        
        //Voids After here.
        DHT22

        Code:
        /************************************************************ *
        Arduino to HS4 Plugin API written by Enigma Theatre.
        V4.0.2.16
        * *
        ************************************************************ */
        int FromHS[50];
        boolean IsConnected = false;
        byte AlivePin = 255;
        //************************************************************
        //To Use the Alive pin change the 255 to the pin number above.
        
        
        
        //**************Declare your variables here*******************
        
        [HASHTAG="t3158"]include[/HASHTAG] "DHT.h"
        
        #define DHTPIN 0 // what digital pin the DHT22 is conected to
        #define DHTTYPE DHT22 // there are multiple kinds of DHT sensors
        
        DHT dht(DHTPIN, DHTTYPE);
        
        //************************************************************ ****
        
        
        
        
        void HSSetup() {
        
        //************************
        //Add YOUR SETUP HERE;
        //************************
        
        Serial.begin(115200);
        
        
        dht.begin();
        
        }
        
        
        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*/
        
        
        Serial.print("Humidity: ");
        Serial.print(dht.readHumidity());
        SendToHS(1,dht.readHumidity());
        Serial.print("%\t");
        Serial.print("Temperature: ");
        Serial.print(dht.readTemperature(true));
        SendToHS(2,dht.readTemperature(true));
        Serial.println("F");
        
        
        
        delay(5000);
        
        }
        
        
        
        //Voids After here.

        MQ2

        Code:
        /************************************************************ *
        Arduino to HS4 Plugin API written by Enigma Theatre.
        V4.0.2.16
        * *
        ************************************************************ */
        int FromHS[50];
        boolean IsConnected = false;
        byte AlivePin = 255;
        //************************************************************
        //To Use the Alive pin change the 255 to the pin number above.
        
        
        
        //**************Declare your variables here*******************
        
        int Gas_analog = A0; // used for ESP8266
        int Gas_digital = D1; // used for ESP8266
        
        //************************************************************ ****
        
        
        
        
        void HSSetup() {
        
        //************************
        //Add YOUR SETUP HERE;
        //************************
        
        Serial.begin(115200);
        pinMode(D5, OUTPUT);
        pinMode(Gas_digital, INPUT);
        }
        
        
        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.
        Gasoline fumes are between 1.4% (14,000 ppm) and 7.6% (76,000 ppm).
        */
        
        /*Execute regardless of connection status*/
        
        int gassensorAnalog = analogRead(Gas_analog);
        int gassensorDigital = digitalRead(Gas_digital);
        
        Serial.println(gassensorAnalog);
        SendToHS(1,gassensorAnalog);
        
        if (gassensorAnalog > 1000) {
        SendToHS(2,HIGH);
        digitalWrite (D5, HIGH) ; //Red Alert
        
        }
        else {
        SendToHS(2,LOW);
        digitalWrite (D5, LOW) ; //no tone
        }
        delay(5000);
        }
        
        
        //Voids After here.


        Comment


          #5
          Nice job, glad you were able to make progress!
          Billy

          Comment


            #6
            Just as a note, this code will work as test code but you need to remove the Delay() that you have in each section as this will stop the plugin sketch section from running when required and you will see connection errors. Look up Arduino Blink Without Delay.
            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


              #7
              Greig,

              I originally had the delay out but found the plugin would get stuck, not update the value or trigger any events. I added the delay and then it worked as expected.

              Billy,

              Thanks!

              Comment


                #8
                Originally posted by crossing@marker99.com View Post
                Greig,

                I originally had the delay out but found the plugin would get stuck, not update the value or trigger any events. I added the delay and then it worked as expected.

                Billy,

                Thanks!
                Yes, You need the delay just not using the delay function, with no delay the plugin will be flooded with information on every run off the loop but as I said using the delay function will cause the Arduino to stop at that section and will not respond to any commands from HS so you need to update to use something like:


                Code:
                /************************************************************ *
                Arduino to HS4 Plugin API written by Enigma Theatre.
                V4.0.2.16
                * *
                ************************************************************ */
                int FromHS[50];
                boolean IsConnected = false;
                byte AlivePin = 255;
                //************************************************************
                //To Use the Alive pin change the 255 to the pin number above.
                
                
                
                //**************Declare your variables here*******************
                
                int Gas_analog = A0; // used for ESP8266
                int Gas_digital = D1; // used for ESP8266
                
                //************************************************************ ****
                
                
                
                
                void HSSetup() {
                
                //************************
                //Add YOUR SETUP HERE;
                //************************
                
                Serial.begin(115200);
                pinMode(D5, OUTPUT);
                pinMode(Gas_digital, INPUT);
                }
                
                
                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.
                Gasoline fumes are between 1.4% (14,000 ppm) and 7.6% (76,000 ppm).
                */
                
                /*Execute regardless of connection status*/
                
                int gassensorAnalog = analogRead(Gas_analog);
                int gassensorDigital = digitalRead(Gas_digital);
                const long interval = 10000;
                
                unsigned long currentMillis = millis();
                
                if (currentMillis - previousMillis >= interval) {
                // save the last time you blinked the LED
                previousMillis = currentMillis;
                
                Serial.println(gassensorAnalog);
                SendToHS(1,gassensorAnalog);
                
                if (gassensorAnalog > 1000) {
                SendToHS(2,HIGH);
                digitalWrite (D5, HIGH) ; //Red Alert
                
                }
                else {
                SendToHS(2,LOW);
                digitalWrite (D5, LOW) ; //no tone
                }
                delay(5000);
                }
                
                }
                
                //Voids After here.
                The other way to do it is to store the last sent value in the Arduino then check if it has changed and update HS only if it has changed.
                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


                  #9
                  Greig,

                  Just saw your "Arduino Blink Without Delay" comment as you were posting your response. I will take a look and improve/update the code!

                  Russ

                  Comment


                    #10
                    Hi crossing@marker99.com ,

                    Do you have any other sketches that you got working in the API that you would share?

                    Thanks,
                    Tim

                    Comment


                      #11
                      Sure, these are only the top portions of the sketches above the "//Voids After here" line.

                      It took me a while to understand how this is all supposed to work but most everything you need to know is in the manual Greig provides like using Blink Without Delay and SendToHS() which is critical. Assume my code is not perfect.


                      Counter for Flow Meter

                      Code:
                      /************************************************************ *
                      Arduino to Homeseer 4 Plugin API written by Enigma Theatre.
                      V4.0.2.16
                      * *
                      ************************************************************ */
                      int FromHS[50];
                      boolean IsConnected = false;
                      byte AlivePin = 255;
                      //************************************************************
                      //To Use the Alive pin change the 255 to the pin number above.
                      
                      
                      
                      //**************Declare your variables here*******************
                      
                      const int input = 12; // This is where the input is fed.
                      int pulse = 0; // Variable for saving pulses count.
                      int var = 0;
                      
                      //************************************************************ ****
                      
                      
                      
                      
                      void HSSetup() {
                      
                      //************************
                      //Add YOUR SETUP HERE;
                      //************************
                      pinMode(input, INPUT);
                      
                      Serial.begin(115200);
                      }
                      
                      
                      
                      
                      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*/
                      if(digitalRead(input) > var)
                      {
                      var = 1;
                      pulse++;
                      
                      Serial.println(pulse);
                      SendToHS(1,pulse);
                      }
                      
                      if(digitalRead(input) == 0) {var = 0;}
                      
                      }
                      
                      //Voids After here.
                      Single DS18B20

                      Code:
                      /************************************************************ *
                      Arduino to Homeseer 4 Plugin API written by Enigma Theatre.
                      V4.0.2.16
                      * *
                      ************************************************************ */
                      int FromHS[50];
                      boolean IsConnected = false;
                      byte AlivePin = 255;
                      //************************************************************
                      //To Use the Alive pin change the 255 to the pin number above.
                      
                      
                      
                      //**************Declare your variables here*******************
                      [HASHTAG="t3158"]include[/HASHTAG] <OneWire.h>
                      [HASHTAG="t3158"]include[/HASHTAG] <DallasTemperature.h>
                      unsigned long previousMillis = 0;
                      const long interval = 5000;
                      
                      
                      // GPIO where the DS18B20 is connected to
                      const int oneWireBus = 4;
                      
                      // Setup a oneWire instance to communicate with any OneWire devices
                      OneWire oneWire(oneWireBus);
                      
                      // Pass our oneWire reference to Dallas Temperature sensor
                      DallasTemperature sensors(&oneWire);
                      
                      
                      //************************************************************ ****
                      
                      
                      
                      
                      void HSSetup() {
                      
                      //************************
                      //Add YOUR SETUP HERE;
                      //************************
                      // Start the Serial Monitor
                      Serial.begin(115200);
                      // Start the DS18B20 sensor
                      sensors.begin();
                      }
                      
                      
                      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*/
                      unsigned long currentMillis = millis();
                      
                      if (currentMillis - previousMillis >= interval) {
                      // save the last time you blinked the LED
                      previousMillis = currentMillis;
                      sensors.requestTemperatures();
                      float temperatureF = sensors.getTempFByIndex(0);
                      Serial.print(temperatureF);
                      Serial.println("ºF");
                      SendToHS(2,temperatureF);
                      }
                      }
                      
                      //Voids After here.
                      MQ2 Sensor and DHT11 combo

                      Code:
                      /************************************************************ *
                      Arduino to Homeseer 4 Plugin API written by Enigma Theatre.
                      V4.0.2.16
                      * *
                      ************************************************************ */
                      int FromHS[50];
                      boolean IsConnected = false;
                      byte AlivePin = 255;
                      //************************************************************
                      //To Use the Alive pin change the 255 to the pin number above.
                      
                      
                      
                      //**************Declare your variables here*******************
                      
                      int Gas_analog = A0; // used for ESP8266
                      int Gas_digital = D1; // used for ESP8266
                      
                      [HASHTAG="t3158"]include[/HASHTAG] "DHT.h"
                      
                      #define DHTPIN 0 // what digital pin the DHT22 is conected to
                      #define DHTTYPE DHT22 // there are multiple kinds of DHT sensors
                      
                      DHT dht(DHTPIN, DHTTYPE);
                      
                      // Generally, you should use "unsigned long" for variables that hold time
                      // The value will quickly become too large for an int to store
                      unsigned long previousMillis = 0; // will store last time LED was updated
                      
                      // constants won't change:
                      const long interval = 60000; // interval at which to blink (milliseconds)
                      
                      
                      
                      //************************************************************ ****
                      
                      
                      
                      
                      void HSSetup() {
                      
                      //************************
                      //Add YOUR SETUP HERE;
                      //************************
                      // put your setup code here, to run once:
                      Serial.begin(115200);
                      pinMode(D5, OUTPUT);
                      pinMode(Gas_digital, INPUT);
                      dht.begin();
                      }
                      
                      
                      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*/
                      // put your main code here, to run repeatedly:
                      unsigned long currentMillis = millis();
                      
                      if (currentMillis - previousMillis >= interval) {
                      // save the last time you blinked the LED
                      previousMillis = currentMillis;
                      
                      
                      int gassensorAnalog = analogRead(Gas_analog);
                      int gassensorDigital = digitalRead(Gas_digital);
                      
                      Serial.print(gassensorAnalog);
                      SendToHS(1,gassensorAnalog);
                      Serial.print("u\t");
                      Serial.print("Humidity: ");
                      Serial.print(dht.readHumidity());
                      SendToHS(2,dht.readHumidity());
                      Serial.print("%\t");
                      Serial.print("Temperature: ");
                      Serial.print(dht.readTemperature(true));
                      SendToHS(3,dht.readTemperature(true));
                      Serial.println("F");
                      
                      
                      
                      
                      }
                      digitalWrite(D5, FromHS[1]);
                      digitalWrite(D4, FromHS[0]);
                      }
                      
                      
                      
                      //Voids After here.

                      Comment


                        #12
                        Thank you for that!

                        Comment

                        Working...
                        X