element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • Members
    Members
    • Benefits of Membership
    • Achievement Levels
    • Members Area
    • Personal Blogs
    • Feedback and Support
    • What's New on element14
  • Learn
    Learn
    • Learning Center
    • eBooks
    • STEM Academy
    • Webinars, Training and Events
    • More
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • More
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • More
  • Products
    Products
    • Arduino
    • Dev Tools
    • Manufacturers
    • Raspberry Pi
    • RoadTests & Reviews
    • Avnet Boards Community
    • More
  • Store
    Store
    • Visit Your Store
    • Choose Another Store
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
Pi IoT
  • Challenges & Projects
  • Design Challenges
  • Pi IoT
  • More
  • Cancel
Pi IoT
Blog [Pi IoT] -  Remote Horse Feeder System #6 : OpenHAB, Pi Face and MQTT
  • Blog
  • Forum
  • Documents
  • Events
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
Blog Post Actions
  • Subscribe by email
  • More
  • Cancel
  • Share
  • Subscribe by email
  • More
  • Cancel
Group Actions
  • Group RSS
  • More
  • Cancel
Engagement
  • Author Author: jomoenginer
  • Date Created: 29 Aug 2016 8:35 AM Date Created
  • Views 267 views
  • Likes 4 likes
  • Comments 5 comments
Related
Recommended

[Pi IoT] -  Remote Horse Feeder System #6 : OpenHAB, Pi Face and MQTT

jomoenginer
jomoenginer
29 Aug 2016

I just realized that the cut off time for submission is 11:59 PM GMT which is a bit sooner than I expected, so here be what I have thus far.

After getting the connections completed with the Pi Rack, I moved to working on the Automation application of the Feeder System I have been working on. This has included implementing Mosquitto, Paho, and MQTT for communication between OpenHAB and the feeder system.  With this, I can adjust the timer and setting locally on the Feeder System as well as change the Timer, Run the Feeder Manually, view the Pi CAM remotely and be notified when there is motion in the stall.

 

MQTT Phao and Mosquitto

 

Thus far in the config, I have 3 Topics that get updated at various intervals and events.

feeder/timer - Used to notify topic Subscribers that an update to the timer has been performed. This can be accomplished locally via the Pi Face Display and Control interface or remotely from open HAB.

feeder/manual - Used to Trigger the feeder system to run in Manual mode thus bypassing any timer settings

feeder/motion - Used to notify that there is moved in the Stall via a PIR sensor connected to the Pi Face Digital 2.

 

feeder/timer

Within OpenHAB, to handle the user input for setting the Feeder Timer remotely, the following config was implemented.

The site was very helpful in getting the Timer section completed:

https://github.com/openhab/openhab/wiki/AlarmClock

 

The Initial interface for the OpenHAB config displays the Feeder Timer, an option to set the Timer, Stall CAM (Both Video and Still cam options), The iLumi BLE Lights and EnOcean Energy Harvesting Switches.

 

 

 

From the Stall Timer, the user can Set the Timer by setting the Set_Timer switch to on.  The will grab the Timer that is site from the OpenHAB interface and send it to the feeder/timer topic which is picked up by the Feeder system. Also, the user can run the Feeder manually from the interface.  And, if there is motion, the Stall Motion indicator will be lit.

From the interface, the user selects the Time (Hour/Min) and the day to run the timer and once selected, click the Set Timer Switch.

 

Sitemap

sitemap home label="Smart Stall"
{
        Frame label="Stall Timer" {
                Frame label="Feeder Timer" {

                        Text label="Timer [%s]" item=timerMessage icon="clock" {
                                Frame label="Run Mode"  {
                                        Switch item=Set_Timer label="Set Timer"
                                        Switch item=Manual_Run label="Manual Run"
                                        Switch item=Motion_Alert label="Stall Motion"
                                        Text item=Motion_Detect
                                }
                                Frame label="Time"  {
                                        Setpoint item=Set_Hour minValue=0 maxValue=23 step=1
                                        Setpoint item=Set_Minute minValue=0 maxValue=55 step=5
                                }
                                Frame label="Days" {
                                        Switch item=timerMonday
                                        Switch item=timerTuesday
                                        Switch item=timerWednesday
                                        Switch item=timerThursday
                                        Switch item=timerFriday
                                        Switch item=timerSaturday
                                        Switch item=timerSunday
                                }
                        }

                }

 

Items

 

Group iLumi
Group Feeder
Group DaysOfWeek
String Feeder_Timer "Get Timer [%s]"  <clock> (Feeder, iLumi) {mqtt="<[jiot2:feeder/timer:state:default]"}
Switch Set_Timer  <switch> (Feeder, iLumi)
String  Send_Timer "[%s]" (Feeder, iLumi) { mqtt=">[jiot2:feeder/timer:command:*:default]" }
Switch Manual_Run  <switch> (Feeder, iLumi)
String  Run_Manual "[%s]" (Feeder, iLumi) { mqtt=">[jiot2:feeder/manual:command:*:default]" }
Number Set_Hour   "Hour [%d]"  <clock> (Feeder, iLumi)
Number Set_Minute   "Minute [%d]"  <clock> (Feeder, iLumi)
Dimmer Set_Day   "Day [%s %%]" (Feeder, iLumi)
String New_Day   "Day [%s]" (Feeder, iLumi)
String New_Hour   "Hour [%d]" (Feeder, iLumi)
String timerMessage "%s"
Switch Motion_Alert  <siren> (Feeder, iLumi)
String  Motion_Detect "Switch Motion[%s]"  (Feeder, iLumi) { mqtt="<[jiot2:feeder/motion:state:default]" }

Switch timerMonday      "Monday"    <switch>  (DaysOfWeek)
Switch timerTuesday     "Tuesday"    <switch>  (DaysOfWeek)
Switch timerWednesday   "Wednesday"    <switch>  (DaysOfWeek)
Switch timerThursday    "Thursday"    <switch>  (DaysOfWeek)
Switch timerFriday      "Friday"    <switch>  (DaysOfWeek)
Switch timerSaturday    "Saturday"    <switch>  (DaysOfWeek)
Switch timerSunday      "Sunday"    <switch>  (DaysOfWeek)

 

Timer Rules

var String timerToMQTT = ""

rule "Initialization"
when
        System started
then
        postUpdate(Set_Hour, 6)
        postUpdate(Set_Minute, 15)
        postUpdate(timerMonday, ON)
        postUpdate(timerTuesday, OFF)
        postUpdate(timerWednesday, OFF)
        postUpdate(timerThursday, OFF)
        postUpdate(timerFriday, OFF)
        postUpdate(timerSaturday, OFF)
        postUpdate(timerSunday, OFF)
        postUpdata(Manual_Run, OFF)
        postUpdata(Motion_Alert, OFF)
end
er rules

 

rule "Set Timer"
when
        Item Set_Hour changed or
        Item Set_Minute changed
then
        logInfo("Set Timer", "Set Timer")
        var String msg = ""
        var String day = ""
        var String ampm = ""
        var hour = Set_Hour.state as DecimalType
        var minute = Set_Minute.state as DecimalType

        if (timerMonday.state == ON) { day = "Mon" }
        if (timerTuesday.state == ON) { day = "Tue" }
        if (timerWednesday.state == ON) { day = "Wed" }
        if (timerThursday.state == ON) { day = "Thu" }
        if (timerFriday.state == ON) { day = "Fri" }
        if (timerSaturday.state == ON) { day = "Sat" }
        if (timerSunday.state == ON) { day = "Sun" }

        if (hour < 10) { msg = "0"  }
        msg = msg + Set_Hour.state.format("%d") + ":"
        if (hour >= 12) {ampm = "PM"}
        if (hour < 12) {ampm = "AM"}

        if (minute < 10) { msg + msg = "0"  }
        msg = msg + Set_Minute.state.format("%d")

        msg = day + "  " + msg + " " + ampm

        postUpdate(timerMessage, msg)

        timerToMQTT = msg
end

rule "Set Feed Timer"
when
        Item Set_Timer changed from OFF to ON
then
        var String feed_timer

        //feed_timer = "Mon  07:30 AM"

        sendCommand(Send_Timer, timerToMQTT)
end

 

rule "Manual Feeder Run"
when
    Item Manual_Run changed from OFF to ON
then
    var String set_manual = ""

    set_manual = "Run"

    sendCommand(Run_Manual, set_manual)
end

rule "Manual Feeder Stop"
when
    Item Manual_Run changed from ON to OFF
then
    var String set_manual = ""

    set_manual = "Stop"

    sendCommand(Run_Manual, set_manual)
end

rule "Motion Detected"
when
    Item Motion_Detect changed
then
    var String motion_message = ""
    logInfo("Motion Detected", "In Da Motion")

    motion_message = Motion_Detect.state.toString

    logInfo("Motion Detected", motion_message)
    if (motion_message == "Motion Detected") {
        sendCommand(Motion_Alert, ON)
    }
    else {
        sendCommand(Motion_Alert, OFF)
    }

end

 

OpenHAB.cfg

mqtt:jiot2.url=tcp://192.168.2.130:1883

# Optional. Client id (max 23 chars) to use when connecting to the broker.
# If not provided a default one is generated.
#mqtt:<broker>.clientId=<clientId>
mqtt:jiot2.clientId=FeedMQTT
mqtt-eventbus:broker=jiot2
mqtt-eventbus:commandPublishTopic=feeder/timer/${item}/command
mqtt-eventbus:commandSubscribeTopic=feeder/timer/${item}/state
mqtt-eventbus:commandPublishTopic=feeder/manual/${item}/command
mqtt-eventbus:commandSubscribeTopic=feeder/manual/${item}/state
mqtt-eventbus:commandSubscribeTopic=feeder/motion/${item}/state

 

Feeder System Paho config

def send_mqtt(message_dict):
    mqttc = mqtt.Client('python_publisher')
    mqttc.connect('192.168.2.130', 1883)
    #message_json2str = json.dumps(message_dict)
    hourTemp = str(timer_dict['hour']).rjust(2, '0')
    minTemp = str(timer_dict['min']).rjust(2, '0')
    message_json2str = timer_dict['day'] + " " + hourTemp + ":" + minTemp + " " + timer_dict['ampm']
    mqttc.publish('feeder/timer', message_json2str, retain=True)
    t.sleep(2)
    mqttc.disconnect()

# The callback for when the client receives a CONNACK response from the server
def on_connect(client, userdata, rc):
    print("Connected with result code "+str(rc))
    # Subscribing in on_connect() means that we lose the connect and
    # reconnect then subscriptions will be renewed
    client.subscribe([("feeder/timer", 0), ("feeder/manual", 0)])
    #client.subscribe("feeder/timer")
    #client.subscribe("feeder/manual")


# The callback forwhen a PUBLISH message is received from the server.
def on_message(client, userdata, msg):
    hourTemp = ""
    minTemo = ""
    man_run = ""
    current_timer = ""
    #msg_read =""
    msg_read = msg.payload.decode('utf-8')
    #print(msg.topic+" "+str(msg.payload))
    print(msg.topic+" "+msg_read)
    if (msg.topic == "feeder/manual"):
        man_run = msg_read
        #man_run = str(msg.payload)
        print("feeder/manual = %s" % man_run)
        if ('Run' in man_run):
            runManual()
            #client.disconnect()

    if (msg.topic == "feeder/timer"):
        timer_data = msg_read
        if os.path.isfile('timer.json'):
            with open('timer.json') as datafile:

           current_timer = json.load(datafile)
        print("New time message %s " % timer_data)
        timer_data = timer_data.split()
        print("Current Timer %s " % current_timer)
        if current_timer != timer_data:
            print("New Timer")
            with open('timer.json', 'w') as outfile:
                json.dump(timer_data, outfile)
            timer_dict['day'] = timer_data[0].strip()
            hour_min = timer_data[1].split(":")
            timer_dict['hour'] = hour_min[0]
            timer_dict['min'] = hour_min[1]
            timer_dict['ampm'] = timer_data[2]
            saveTimerData(timer_dict)
            showTimer()
        #client.disconnect()
        #hourTemp = str(timer_dict['hour']).rjust(2, '0')
        #minTemp = str(timer_dict['min']).rjust(2, '0')]
        #message_json2str = timer_dict['day'] + " " + hourTemp + ":" + minTemp + " " + timer_dict['ampm']

    client.disconnect()

def getMQTTTimer():
    mqttc = mqtt.Client()
    mqttc.on_connect = on_connect
    mqttc.on_message = on_message
    mqttc.connect('192.168.2.130', 1883, 60)
    '''
    if (mqttc.connected is not False):
        mqttc.connect('192.168.2.130', 1883, 60)
        #mqttc.loop_start()
    '''

    mqttc.loop_forever(timeout=1.0, max_packets=2, retry_first_connection=False)
    mqttc.disconnect(

 

Motion Sensor to Pi Face Digital 2

 

The sensor I used for the Motion Sensing is the Parallax PI Sensor rev B.  This is connected to the Pi Face Digital 2 Input 3, 5V and Ground. (NOTE: Since the Digital 2 Inputs are by default pulled up to 5V, I ended up having to put a 10K resistor between GND and Pin 3 to pull the pin low when the PIR sensor was on.)

 

PiFace Digital IO Config:

import pifacedigitalio as pdio


def detectMotion():
    MOTION = 0
    NO_MOTION = 1
    pri1 = NO_MOTION
    pdio.init()
    t.sleep(1)

    while True:
        pir1 = pdio.digital_read(3,1)
        if pir1 is MOTION:
            print ("Motion Detected!")
            send_message(topic_motion, feeder_broker, "Motion Detected")
        t.sleep(1)

 

This was started as a Process in Python so it would run parallel in the background to the main app:

    p = Process(target=getMQTTTimer)
    m = Process(target=detectMotion)
    p.start()
    p.join()
    m.daemon = False
    m.start()

 

 

With all of that, this is what it looks like in the raw, nothing in case at this point.

You don't have permission to edit metadata of this video.
Edit media
x
Upload Preview

 

This has been an awesome adventure and I appreciate the opportunity to use the tools given to create a project; I just wish I could have completed it in the time allotted.  I'll keep working on this and hopefully get something that is complete be years end.

 

Thanks,

 

Jon

Anonymous

Top Comments

  • DAB
    DAB over 5 years ago +2

    HI Jon,

     

    Nice update.

    A lot of projects get overtaken by events and no matter how well you plan, things happen.

     

    That said, it looks like you are getting close so I hope to see you post the completed system…

  • rhe123
    rhe123 over 5 years ago +2

    I hope you'll finished it anyway, it's an interesting project jomoenginer! You already made some nice progress

  • jomoenginer
    jomoenginer over 5 years ago in reply to rhe123 +2

    Robin,

     

    Yep, the plan is to complete the whole system and I am still working on it.  I like what you did in your project with the lighting so I may have to borrow some of that for future features.

     

    Cheers…

  • fvan
    fvan over 5 years ago in reply to jomoenginer

    That's what's so good about these challenges: a mass of new and typically useful content is created, things are figured out and solved in different ways, helping us all achieve better projects in the future

     

    Keep going it at, and you'll have an awesome feeder system!

    • Cancel
    • Vote Up 0 Vote Down
    • Reply
    • More
    • Cancel
  • jomoenginer
    jomoenginer over 5 years ago in reply to rhe123

    Robin,

     

    Yep, the plan is to complete the whole system and I am still working on it.  I like what you did in your project with the lighting so I may have to borrow some of that for future features.

     

    Cheers,

     

    Jon

    • Cancel
    • Vote Up +2 Vote Down
    • Reply
    • More
    • Cancel
  • jomoenginer
    jomoenginer over 5 years ago in reply to DAB

    Thanks DAB,

     

    Yeah, certainly some unexpected events kept me from allocating the time needed to complete the project on time.  Just last Friday the router I was using to connect all of the IoT devices died on me so I had to resort to using an Ethernet switch and no WiFi.  This eliminated the Phillips Hue Hub I had but the direction I was looking to go was to have a solution that did not require an active internet connection so that worked out.

    I'm still hammering away at it and will complete it soon.

     

    Cheers,

     

    Jon

    • Cancel
    • Vote Up 0 Vote Down
    • Reply
    • More
    • Cancel
  • rhe123
    rhe123 over 5 years ago

    I hope you'll finished it anyway, it's an interesting project jomoenginer! You already made some nice progress

    • Cancel
    • Vote Up +2 Vote Down
    • Reply
    • More
    • Cancel
  • DAB
    DAB over 5 years ago

    HI Jon,

     

    Nice update.

    A lot of projects get overtaken by events and no matter how well you plan, things happen.

     

    That said, it looks like you are getting close so I hope to see you post the completed system.

     

    I am sure others would like to see it in operation.

     

    DAB

    • Cancel
    • Vote Up +2 Vote Down
    • Reply
    • More
    • Cancel
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2022 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

ICP 备案号 10220084.

Follow element14

  • Facebook
  • Twitter
  • linkedin
  • YouTube