Skip to content
  • MySensors
  • OpenHardware.io
  • Categories
  • Recent
  • Tags
  • Popular
Skins
  • Light
  • Brite
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Brand Logo
  1. Home
  2. Troubleshooting
  3. Openhab - How to respond to Arduino request?

Openhab - How to respond to Arduino request?

Scheduled Pinned Locked Moved Troubleshooting
14 Posts 2 Posters 5.8k Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Daniel LindbergD Offline
    Daniel LindbergD Offline
    Daniel Lindberg
    wrote on last edited by
    #1

    In the EnergyPulseMeter example that comes with MySensors, we have this line

    gw.begin(incomingMessage);
    

    The incomingMessage method is then defined as:

    void incomingMessage(const MyMessage &message) {
      if (message.type==V_VAR1) {  
        pulseCount = oldPulseCount = message.getLong();
        Serial.print("Received last pulse count from gw:");
        Serial.println(pulseCount);
        pcReceived = true;
      }
    }
    
    

    We also have

    // Fetch last known pulse count value from gw
    gw.request(CHILD_ID, V_VAR1);
      
    

    So if my understanding is correct, the last call is a request to the GW, and whenever we get a response, the incomingMessage method will handle it.

    My question then - How can I respond to this request from my Openhab rules?

    1 Reply Last reply
    0
    • T Offline
      T Offline
      TimO
      Hero Member
      wrote on last edited by
      #2

      Your understanding is correct. :)

      How does the request looks like in the serial monitor? Do you already have an corresponding item that stores the pulse count?

      1 Reply Last reply
      0
      • Daniel LindbergD Offline
        Daniel LindbergD Offline
        Daniel Lindberg
        wrote on last edited by
        #3

        This is the output from the serial monitor when starting the sensor:

        sensor started, id=105, parent=0, distance=1
        send: 105-105-0-0 s=255,c=3,t=11,pt=0,l=12,sg=0,st=ok:Energy Meter
        send: 105-105-0-0 s=255,c=3,t=12,pt=0,l=3,sg=0,st=fail:1.0
        send: 105-105-0-0 s=1,c=0,t=13,pt=0,l=0,sg=0,st=ok:
        send: 105-105-0-0 s=1,c=2,t=24,pt=0,l=0,sg=0,st=ok:
        send: 105-105-0-0 s=1,c=2,t=24,pt=0,l=0,sg=0,st=ok:
        

        I'm not storing any pulse count yet, still trying to figure out how everything works together.

        My rules, still based on your code, where I've just added another if for now to log the request

        if(msgType == 1 ){
                        if (subType == V_TEMP){
                            postUpdate(sensorToItemsMap.get( nodeId + ";" + childId + ";"), msg)
                            println ("Temp item: " + sensorToItemsMap.get( nodeId + ";" + childId + ";") + " temp: " + msg )
                            }
                        if (subType == V_HUM){
                            postUpdate(sensorToItemsMap.get( nodeId + ";" + childId + ";"), msg)
                            println ("Hum item: " + sensorToItemsMap.get( nodeId + ";" + childId + ";") + " hum: " + msg )
                            }
                        }    
                    // Message Request
                    if(msgType == 2){
                    	logInfo("TestCheck", "Request, SubType: " + subType)
                     
                    }
        
        1 Reply Last reply
        0
        • Daniel LindbergD Offline
          Daniel LindbergD Offline
          Daniel Lindberg
          wrote on last edited by Daniel Lindberg
          #4

          Eventually this is what worked for me:

          if(msgType == 2){
              if (subType == V_VAR1){
                  sendCommand(Arduino,"105;1;1;0;24;0\n")
              }
          }
          

          Just an example of course. Here I'm saying that the last pulse count is always zero. But this will return data to my sensor at least.

          1 Reply Last reply
          0
          • T Offline
            T Offline
            TimO
            Hero Member
            wrote on last edited by
            #5

            @Daniel-Lindberg : I suppose you are already storing the pulse count in an item?
            If this is the case you simple replace "0" with "pulseCountItem.state".

            For example:

            if(msgType == 2){
                if (subType == V_VAR1){
                    sendCommand(Arduino,"105;1;1;0;24;" + pulseCountItem.state + "\n")
                }
            }
            

            Where pulseCountItem is the name of the item you specified in the item definition file.

            1 Reply Last reply
            0
            • Daniel LindbergD Offline
              Daniel LindbergD Offline
              Daniel Lindberg
              wrote on last edited by Daniel Lindberg
              #6

              Actually no, not yet. I have been to obsessed with getting a response back to my sensor :-)
              Next step would be to store the value, and send the last one back as per your example.

              One thing I still don't understand. Do I need a separate item for storing the pulse count?
              Perhaps I actually need three items, since in my sensor code (basically copy/paste from mysensors example) I have. Do I generally need one openhab item per message ?

              MyMessage wattMsg(CHILD_ID,V_WATT);
              MyMessage kwhMsg(CHILD_ID,V_KWH);
              MyMessage pcMsg(CHILD_ID,V_VAR1);
              
              1 Reply Last reply
              0
              • T Offline
                T Offline
                TimO
                Hero Member
                wrote on last edited by
                #7

                I use one Item for every node- and child ID combination. My mapping table in the rules file is getting large ...

                Are you planning to use more than one power meter? Because in that case you also want to map to the corresponding item that stores the pulse count.

                Daniel LindbergD 1 Reply Last reply
                0
                • T TimO

                  I use one Item for every node- and child ID combination. My mapping table in the rules file is getting large ...

                  Are you planning to use more than one power meter? Because in that case you also want to map to the corresponding item that stores the pulse count.

                  Daniel LindbergD Offline
                  Daniel LindbergD Offline
                  Daniel Lindberg
                  wrote on last edited by
                  #8

                  @TimO No, Just one power meter. My current items hashmap is

                  // Mappings
                  var HashMap<String, String> sensorToItemsMap = newLinkedHashMap(
                      "101;0;"            -> "livingHum01",
                      "livingHum01"       -> "101;0;",
                      "101;1;"            -> "livingTemp01",
                      "livingTemp01"      -> "101;1;",
                      "105;1;"            -> "cellarPow01",
                      "cellarPow01"       -> "105;1;"
                  )
                  

                  Not sure how to separate the numbers for watts, kwhs and pulse counts since they all share the same CHILD_ID (in the example files). Should I change that to be three separate child IDs ?

                  MyMessage wattMsg(CHILD_ID_1,V_WATT);
                  MyMessage kwhMsg(CHILD_ID_2,V_KWH);
                  MyMessage pcMsg(CHILD_ID_3,V_VAR1);
                  

                  It feels like using the same CHILD_ID for all three messages would cause postUpdate to write watts, kwhs and pc to the same item, which wouldn't make sense right?

                  1 Reply Last reply
                  0
                  • T Offline
                    T Offline
                    TimO
                    Hero Member
                    wrote on last edited by
                    #9

                    Using three separate child IDs is the easiest way I think, because of the way the rule is working.

                    1 Reply Last reply
                    0
                    • Daniel LindbergD Offline
                      Daniel LindbergD Offline
                      Daniel Lindberg
                      wrote on last edited by
                      #10

                      @TimO said:

                      @Daniel-Lindberg : I suppose you are already storing the pulse count in an item?
                      If this is the case you simple replace "0" with "pulseCountItem.state".

                      For example:

                      if(msgType == 2){
                          if (subType == V_VAR1){
                              sendCommand(Arduino,"105;1;1;0;24;" + pulseCountItem.state + "\n")
                          }
                      }
                      

                      Where pulseCountItem is the name of the item you specified in the item definition file.

                      @TimO , I tried this command earlier today, but I'm seeing the following statement in the log:

                      given new state is NULL, couldn't post update for 'pulseCountItem'
                      

                      So it seems like there is no state for the item.
                      Also tried using

                      pulseCountItem.previousState(now).state
                      

                      but that gives

                      Error during the execution of rule 'Arduino sends to Openhab': Could not invoke method: org.openhab.core.persistence.extensions.PersistenceExtensions.previousState(org.openhab.core.items.Item,boolean,java.lang.String) on instance: null
                      

                      Any ideas on how to proceed?

                      1 Reply Last reply
                      0
                      • T Offline
                        T Offline
                        TimO
                        Hero Member
                        wrote on last edited by
                        #11

                        @Daniel-Lindberg Did you declare an item "pulseCountItem" in the items file? Did you create a rule for the pulse counts that are received by OH (and afterwards stored in pulseCountItem)?

                        1 Reply Last reply
                        0
                        • Daniel LindbergD Offline
                          Daniel LindbergD Offline
                          Daniel Lindberg
                          wrote on last edited by
                          #12

                          @TimO I did, and I can see my stored values in the "my openhab" console.

                          I cross-posted my issue on open-habs community, and the concensus there seems to be that my openhab does not support restore/retrieve queries.

                          https://community.openhab.org/t/unable-to-restore-state-for-item/3172/2

                          1 Reply Last reply
                          0
                          • T Offline
                            T Offline
                            TimO
                            Hero Member
                            wrote on last edited by
                            #13

                            Well that's reasonable according to persistance and my.openhab.

                            What should work is: item.state

                            I'm using a rule like:

                                if(shutterAutomatic.state == ON)
                                	sendCommand(Shutter_All, "UP")
                            

                            I'm using persistance with MySQL, but I would expect item.state to work without persistance.
                            Have you tried to simply print out item.state?

                            1 Reply Last reply
                            0
                            • Daniel LindbergD Offline
                              Daniel LindbergD Offline
                              Daniel Lindberg
                              wrote on last edited by
                              #14

                              I did, but after a simulated "power outage" item.state would simply return 0, so my sensor would reset and start accumulating KWH from 0 again.

                              I updated use rrd4j rather than my.openhab and used

                              pulseCountItem.previousState(false, "rrd4j").state // get the most recent update, even if it is the same as the current state
                              

                              It's now working :-)

                              1 Reply Last reply
                              0
                              Reply
                              • Reply as topic
                              Log in to reply
                              • Oldest to Newest
                              • Newest to Oldest
                              • Most Votes


                              14

                              Online

                              11.7k

                              Users

                              11.2k

                              Topics

                              113.0k

                              Posts


                              Copyright 2019 TBD   |   Forum Guidelines   |   Privacy Policy   |   Terms of Service
                              • Login

                              • Don't have an account? Register

                              • Login or register to search.
                              • First post
                                Last post
                              0
                              • MySensors
                              • OpenHardware.io
                              • Categories
                              • Recent
                              • Tags
                              • Popular