Announcement

Collapse
No announcement yet.

How to pass to devices the information of sensors coming from arduino serial port?

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

  • #16

    Yes, this is the goal :-)
    I use for my watt meter a nodemcu. I simply count the time every 2 pulse and calculate the power.


    Now I have to check my sketch because I don't remember well but the "state" variable is to check if the state of the pin is changed in order to "listen" the pulse.
    I added an other variable to check if arduino was restarted (arduino riavviato = arduino restarted)... why?
    Simple, arduino count every pulse (one pulse is one Wh). Every pulse, arduino send to HS the total pulse. If I restart arduino, or if arduino reset/restart I lose the count of pulse (power consumption). So, if "arduino_riavviato" var is 1 (arduino restarted), arduino read the "old" number of pulses from HS, in this way, arduino can sum other pulse to the previous... (It is hard for me to explain... :-) )

    Well thought!
    I do not need something so elaborate.
    For me the important thing is to continuously measure the instantaneous power, to have an event that allows me to warn of excessive consumption ... or / and a recurring event that informs me of the power consumption
    If you disconnect the HS3, or the server, for some reason you will also lose energy readings ... For me the energy consumed, in relation to power is an additional gift: a "good to have", but "not a "must to have"


    If you want, I can share with you my sketch.

    If you can share your sketch it will be great; thanks!


    (…) Sorry, I don't understand... If is a pulse, why you want to check as analogue signal? (…)


    The analogue inputs are digital inputs with the added functionality of analogue.

    sensor = digitalRead(14); // reads a digital signal on pin A0 …

    The idea came from here:

    http://www.smartofthehome.com/2013/0...eters-arduino/


    but as I said I'm thinking ..



    Comment


    • #17
      Originally posted by ANTOLIVEIRA View Post
      [The analogue inputs are digital inputs with the added functionality of analogue.

      sensor = digitalRead(14); // reads a digital signal on pin A0 …
      The idea came from here:


      http://www.smartofthehome.com/2013/0...eters-arduino/[/SIZE]
      Ok, I know that analog inputs can be used as digital input! But if I have digital pin free I prefer use that.
      In the link, is used a analogue pin because is used a photo-transistor and led. I think he want read the led light reflection on wheel of meter, so use analogue to determinate the threshold.
      In my case, I have a watt meter that return a pulse (+5V) for every Watt, so I use a digital pin...

      Here my sketch:

      Code:
      /************************************************************
        Arduino to Homeseer 3 Plugin API written by Enigma Theatre.
         V1.0.0.118
         6/12/2016 Utilizzato nuovamente il GETPULSEIN
                   aggiunto controllo sicurezza di 3 secondi prima
                   di inviare valore se power > 3000
         2/1/2017 inseriti ingressi analogici con invio dati solo se
                  valore è differente dal precedente
      
       *******Change the values below only**************************/
      
      // Pin pulse
      const int getpulsepin = 2;
      
      // variable
      int power = 0;
      unsigned long startime = 0;
      unsigned long duration = 0;
      int state = 0;
      int pinval = 0;
      unsigned long tempoPrecedente = millis();
      unsigned long tempoPrecedente1 = millis();
      unsigned long tempoPrecedente2 = millis();
      unsigned long tempoPrecedente3 = millis();
      unsigned long tempoPrecedente4 = millis();
      unsigned long tempoAllerta = millis();
      float duration3 = 0;
      boolean allerta = false;
      
      int consumo_generale = 0;
      int sens_allagamento_lavastoviglie = 0;
      int sens_vasi_bagnati = 0;
      int sens_luminosita_rep_giorno = 0;
      float sens_batteria_UPS = 0;
      int consumo_generale_old = 0;
      int sens_allagamento_lavastoviglie_old = 0;
      int sens_vasi_bagnati_old = 0;
      int sens_luminosita_rep_giorno_old = 0;
      float sens_batteria_UPS_old = 0;
      
      
      
      
      
      //************Do not change anything in Here*****************
      
      #define ISIP 0
      #if ISIP == 1
      #include <EEPROM.h>
      #include <SPI.h>
      #endif
      
      
      const byte BoardAdd = 4;
      
      #include <Ethernet.h>
      #include <EthernetUdp.h>
      
      #if ISIP == 1
      byte mac[] = {0x00, 0xAA, 0xBB, 0xCC, 0xDE, 0x04};
      IPAddress ip(***********);  //IP entered In HS config.
      const unsigned int localPort = 8903;      //port entered In HS config.
      IPAddress HomeseerIP(**********); //Homeseer IP address
      IPAddress ServerIP(EEPROM.read(2), EEPROM.read(3), EEPROM.read(4), EEPROM.read(5));
      IPAddress gateway(192, 168, 178, 1);
      IPAddress subnet(255, 255, 255, 0);
      byte EEpromVersion = EEPROM.read(250);
      #endif
      
      int FromHS[10];                                          // *
      boolean IsConnected = false;                             // *
      #if ISIP == 1                                            // *
      char packetBuffer[UDP_TX_PACKET_MAX_SIZE];               // *
      EthernetUDP Udp;                                         // *
      const unsigned int ServerPort = 8888;                    // *
      #endif                                                   // *
      void(* resetFunc) (void) = 0;                            // *
      //***********************************************************
      
      
      void setup() {
        HSSetup();
        //************************
        //Add YOUR SETUP HERE;
      
        pinMode(getpulsepin, INPUT_PULLUP); // pin del sensore + pullUP
      
        //************************
      
      
      
      
      }
      
      
      
      
      void loop() {
      #if ISIP == 1
        IsUDP();
      #endif
      
        //************************
        //Add YOUR CODE HERE;
      
        pinval = digitalRead(getpulsepin);   // legge il pin del sensore
      
        if ((state == 1) & (pinval == 0)) { // punto di transizione
          duration = millis() - startime;       // calcola la duration della pulsazione
          startime = millis();            // setta il nuovo tempo di partenza
          power = 3600.0 / duration * 1000; // calcola consumo
          duration3 = duration / 1000.0;  //duration in seconds
      
          if (power <= 3000) {
            SendToHS(1, power);
            SendToHS(2, duration3);
            allerta = false;
          } else {
            // se power è > 3000
            if (allerta) {
              //era > 3000 anche prima
              if ((millis() - tempoAllerta) > 3000) {
                // sono passati più di 3 secondi
                SendToHS(1, power);
                SendToHS(2, duration3);
              }
            } else {
              // è la prima volta che è > 3000
              allerta = true;
              tempoAllerta = millis();
            }
          }
      
        }
        state = pinval;         // imposta lo stato uguale alla lettura del pin
      
      
      
      
        if (millis() - tempoPrecedente > 5000) {
          tempoPrecedente = millis();
          consumo_generale = analogRead(A0) * (100 / 1023.0 * 6);
          if (consumo_generale != consumo_generale_old) {
            SendToHS(3, consumo_generale);
            consumo_generale_old = consumo_generale;
          }
        }
      
        if (millis() - tempoPrecedente1 > 5100) {
          tempoPrecedente1 = millis();
          sens_batteria_UPS = (analogRead(A1) / 56.39);
          if (sens_batteria_UPS != sens_batteria_UPS_old) {
            SendToHS(4, sens_batteria_UPS);
            sens_batteria_UPS_old = sens_batteria_UPS;
          }
        }
      
        if (millis() - tempoPrecedente2 > 5200) {
          tempoPrecedente2 = millis();
          sens_luminosita_rep_giorno = (analogRead(A2) * 100 / 1023 * 4);
          if (sens_luminosita_rep_giorno != sens_luminosita_rep_giorno_old) {
            SendToHS(5, sens_luminosita_rep_giorno);
            sens_luminosita_rep_giorno_old = sens_luminosita_rep_giorno;
          }
        }
      
        if (millis() - tempoPrecedente3 > 5300) {
          tempoPrecedente3 = millis();
          sens_allagamento_lavastoviglie = (analogRead(A3) * 100 / 1023);
          if (sens_allagamento_lavastoviglie != sens_allagamento_lavastoviglie_old) {
            SendToHS(6, sens_allagamento_lavastoviglie);
            sens_allagamento_lavastoviglie_old = sens_allagamento_lavastoviglie;
          }
        }
      
        if (millis() - tempoPrecedente4 > 5400) {
          tempoPrecedente4 = millis();
          sens_vasi_bagnati = (analogRead(A4) * 100 / 1023);
          if (sens_vasi_bagnati != sens_vasi_bagnati_old) {
            SendToHS(7, sens_vasi_bagnati);
            sens_vasi_bagnati_old = sens_vasi_bagnati;
          }
        }
      
      
        //************************
        /* 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 (IsConnected == true) {
          /*Execute ONLY when HomeSeer is connected*/
      
        }
      }
      char* Version = "API1.0.0.118";
      
      byte Byte1, Byte2, Byte3;
      int Byte4, Byte5;
      
      
      void HSSetup() {
      
      #if ISIP == 1
        if (EEpromVersion != 22) {
          ServerIP = HomeseerIP;
          EEPROM.write(2, ServerIP[0]);
          EEPROM.write(3, ServerIP[1]);
          EEPROM.write(4, ServerIP[2]);
          EEPROM.write(5, ServerIP[3]);
          EEPROM.write(250, 22); //Store the version where the eeprom data layout was last changed
          EEpromVersion = 22;
        }
        Ethernet.begin(mac, ip);
        Udp.begin(localPort);
        Udp.setTimeout(0);
        delay(1000);
        SendConnect();
      #else
        Serial.begin(115200);
        Serial.flush();
        Serial.setTimeout(0);
        delay(1000);
        Serial.print("Connect ");
        Serial.println(BoardAdd);
      #endif
      
        IsConnected = false;
      
      }
      
      void SendConnect()
      {
      #if ISIP == 0
        Serial.print("Connect ");
        Serial.println(BoardAdd);
      #else
        Udp.beginPacket(ServerIP, ServerPort); //First send a connect packet to the dynamic IP stored in eeprom
        Udp.print("Connect ");
        Udp.print(BoardAdd);
        Udp.endPacket();
        if (ServerIP != HomeseerIP) {
          Udp.beginPacket(HomeseerIP, ServerPort); //Then if the stored value doesn't match the pre-specified one, send a connect packet there also
          Udp.print("Connect ");
          Udp.print(BoardAdd);
          Udp.endPacket();
        }
      
      #endif
      }
      
      
      #if ISIP == 1
      void IsUDP() {
        int packetSize = Udp.parsePacket();
        if (packetSize)
        {
          IPAddress remote = Udp.remoteIP();
          Byte1 = Udp.parseInt();
          Udp.read();
          Byte2 = Udp.read();
          Byte3 = Udp.parseInt();
          Byte4 = Udp.parseInt();
          Byte5 = Udp.parseInt();
          DataEvent();
        }
      }
      
      #else
      void serialEvent() {
        while (Serial.available() > 0) {
          delay(17);
      
      
          Byte1 = Serial.parseInt();
          Serial.read();
          Byte2 = Serial.read();
          Byte3 = Serial.parseInt();
          Byte4 = Serial.parseInt();
          Byte5 = Serial.parseInt();
          DataEvent();
        }
      }
      #endif
      
      
      /*
      
        Used Data Input Cases
        D Disconnect
        r reset
        K Keepalive
        O PinMode Output Set
        d Input debounce time set
        C Connect request
        c Connection established - report current status
      */
      void DataEvent() {
      
      
      
        if (Byte1 == BoardAdd) {
          switch (Byte2) {
      
            case 'c':
              IsConnected = true;
      #if ISIP == 1
              if (Udp.remoteIP() != ServerIP) {
                ServerIP = Udp.remoteIP();
                EEPROM.write(2, ServerIP[0]);
                EEPROM.write(3, ServerIP[1]);
                EEPROM.write(4, ServerIP[2]);
                EEPROM.write(5, ServerIP[3]);
              }
      #endif
      
              break;
      
            case 'C':
      #if ISIP == 1
              Udp.beginPacket(Udp.remoteIP(), ServerPort);
              Udp.print("Version ");
              Udp.print(BoardAdd);
              Udp.print(" ");
              Udp.print(Version);
              Udp.println(" HS3");
              Udp.endPacket();
      
              Udp.beginPacket(Udp.remoteIP(), ServerPort);
              delay(100);
              Udp.print("Connected ");
              Udp.println(BoardAdd);
              Udp.endPacket();
      #else
              Serial.print("Version ");
              Serial.print(BoardAdd);
              Serial.print(" ");
              Serial.print(Version);
              Serial.println(" HS3");
              delay(100);
              Serial.print("Connected ");
              Serial.println(BoardAdd);
      #endif
              delay(100);
              IsConnected = false;
              break;
      
            case 'K':
              delay(200);
      #if ISIP == 1
              Udp.beginPacket(Udp.remoteIP(), ServerPort);
              Udp.print("Alive ");
              Udp.println(BoardAdd);
              Udp.endPacket();
              if (Udp.remoteIP() != ServerIP) {
                ServerIP = Udp.remoteIP();
                EEPROM.write(2, ServerIP[0]);
                EEPROM.write(3, ServerIP[1]);
                EEPROM.write(4, ServerIP[2]);
                EEPROM.write(5, ServerIP[3]);
              }
      #else
              Serial.print("Alive ");
              Serial.println(BoardAdd);
      #endif
              break;
      
            case 'r':
              delay(200);
              resetFunc();  //call reset
              break;
      
            case 'O':
              FromHS[Byte3] = Byte4;
              break;
      
            case 'D':
              IsConnected = false;
              break;
          }
        }
      }
      
      void SendToHS(byte Device, long Data) {
        if (IsConnected == true) {
      #if ISIP == 1
          Udp.beginPacket(Udp.remoteIP(), ServerPort);
          Udp.print(BoardAdd);
          Udp.print(" API ");
          Udp.print(Device);
          Udp.print(" ");
          Udp.print(Data);
          Udp.endPacket();
      #else
          Serial.print(BoardAdd);
          Serial.print(" API ");
          Serial.print(Device);
          Serial.print(" ");
          Serial.println(Data);
      #endif
        }
      }
      
      void SendToHS(byte Device, int Data) {
        if (IsConnected == true) {
      #if ISIP == 1
          Udp.beginPacket(Udp.remoteIP(), ServerPort);
          Udp.print(BoardAdd);
          Udp.print(" API ");
          Udp.print(Device);
          Udp.print(" ");
          Udp.print(Data);
          Udp.endPacket();
      #else
          Serial.print(BoardAdd);
          Serial.print(" API ");
          Serial.print(Device);
          Serial.print(" ");
          Serial.println(Data);
      #endif
        }
      }
      
      void SendToHS(byte Device, float Data) {
        if (IsConnected == true) {
      #if ISIP == 1
          Udp.beginPacket(Udp.remoteIP(), ServerPort);
          Udp.print(BoardAdd);
          Udp.print(" API ");
          Udp.print(Device);
          Udp.print(" ");
          Udp.print(Data);
          Udp.endPacket();
      #else
          Serial.print(BoardAdd);
          Serial.print(" API ");
          Serial.print(Device);
          Serial.print(" ");
          Serial.println(Data);
      #endif
        }
      }
      This is the sketch (board4 connected via USB)I used before to pass to nodemcu. As you can see I read some analogue inputs (flood checker, ambient light...) and pass the data to homeseer only every x seconds and only if the new data is different to previous.

      Comment


      • #18
        Ok!

        I will have to connect an oscilloscope to see the signal and decide on the approach. I also ordered two nodemcu to test

        Thanks again for sharing your sketch it will be very useful

        I was thinking of decentralizing my system to have an arduino per division connected by ethernet to the HS (node zero) and in this way acquire temperature, humidity, luminosity, detection of movement and command heating and blinds.However, before I proceed I need to test functionality and reliability.

        I think the plugin will allow a maximum of 9 arduinos... I am particularly concerned about any connection losses between the Arduinos and the plugin,..

        I already have work for the next weeks ...

        Comment

        Working...
        X