Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Format of MQTT Schedule plugin commands?
#1
Hi folks,

I've had some downtime to work on my moisture monitors for my SIP system, and decided to use MQTT as a way for the monitors, which deep sleep most of the time, to send messages to SIP.  I loaded up the MQTT plugins in SIP, messed around with stuff a bit to get Mosquitto working properly (apt and apt-get on Raspberry Pi don't install the latest version of Mosquitto, see https://www.switchdoc.com/2018/02/tutori...pberry-pi/ for details), and now can see messages via the MQTT Zone Broadcaster.

I then tried to send commands to the MQTT Schedule plugin via mosquitto_pub, just to play around with it.  I found that "mosquitto_pub -d -h localhost -p 1883 -t sip/schedule -m {\"S01\":\"1\"}" would turn on Station 1 (S01).  However, "mosquitto_pub -d -h localhost -p 1883 -t sip/schedule -m {\"S01\":\"0\"}" would not turn off Station 1. I had to manually "Stop All Stations" in the SIP web interface for that to happen.  

I didn't see any documentation about the format of the JSON messages that should be sent to the MQTT Schedule plugin, so I was just sort of screwing around to get anything to happen.  Is there some documentation somewhere that I'm missing?  How should I be controlling the zones via MQTT?

Thanks!
Ryan
Reply
#2
You might find the SIP HTTP GET commands reference helpful:
https://github.com/Dan-in-CA/SIP/wiki/SI...mmands.pdf
<p><br></p>
Reply
#3
I think the mqtt_slave plugin has the functionality you need.
It is designed for controlling a remote Pi from a master Pi, both running SIP, but the code for turning stations on and off can be used for what you are trying to do.

The relevant code is at line 68 through 81 in the file mqtt_slave.py.

See gv_reference.txt in the SIP folder (line 65) for info on the format of the run schedule list. That controls when stations are turned on or off. If you set the start time for a station to gv.now (the current time) the station will be turned on. If you set the stop time to gv.now the station will be turned off. Setting the duration to infinity (gv.rs[i][1] = float('inf') the station will stay on until stop time is set to gv_now.
<p><br></p>
Reply
#4
Hi Dan,

Thanks for the information! To make sure I understand, I can send over MQTT commands in the same format as the API commands?

I'm digging into the mqtt_slave plugin as well, but I don't think I can run the slave plugin on the same Pi as the master, right?

Take care,
Ryan
Reply
#5
As far as MQTT, you can send any type of command as the payload.

You are right about not running the slave plugin on the master. You could use it as a starting point to make a plugin for your particular purpose.

Do you want the moisture sensor reading to stop SIP from running a program like a rain sensor or do you want the moisture sensors both start and stop irrigation?
<p><br></p>
Reply
#6
Yeah, I was playing with MQTT in general a bit just to be more familiar with it. I was specifically trying to figure out what commands the MQTT Schedule plugin for SIP responded to, and figured out that sending {\"S01\":\"1\"}" to the Schedule plugin would start Station 1, but {\"S01\":\"0\"}" would not stop it. It seems that {\"S01\":\"1\"}" starts the station in an odd state where the run time is 0:00 minutes : seconds, but the station never stops.

Typing this out and thinking about this - do I need to have the SIP instance I'm trying to control with the MQTT Schedule plugin in "manual" mode? That would make sense based on the MQTT Slave plugin documentation.

With regards to your question about what I want the moisture sensor to do; I'm still figuring that out a bit, but my rough idea was that the sensor (running on an ESP8265 or ESP32 for the built in WiFi and familiar Arduino IDE) would sample the soil's moisture level at a set time, and then based on the moisture level, tell SIP to water the area the sensor is in for a preset amount of time. I'd eventually want to make this more complex, but I could fairly easily put a lookup table into the ESP to implement something like "If the moisture level is 20%, then water the zone for 15 minutes. If the moisture level is 40%, then water the zone for 10 minutes." - and so on.

So, based on that, I just need the sensor to send a MQTT message / command string to the SIP instance to turn on a zone for a set amount of time. The sensor does most of the "hard work" in terms of figuring out how long to irrigate the zone it is in.

Thanks again for your help and feedback!
Ryan
Reply
#7
I was just looking at the code for the MQTT_schedule plugin.

It looks like it can do what you want.

I didn't write that plugin but with a little testing I think I can write some needed documentation for it. Without testing it looks like you can send either a Python list (array) containing just a run time for each station (eg [30, 0, 0, 0, 0, 0, 0, 0] to run station 1 for 30 seconds) or a dictionary like you have used with the station name included.
<p><br></p>
Reply
#8
The MQTT_schedule plugin works like the SIP Run Once feature.
You can set a run time for one or more stations and they will run for the designated time but you can't send start and stop commands with that plugin.
<p><br></p>
Reply
#9
Thanks, I appreciate you digging into the plugin even though you didn't create it.  I'll fool around with this some more on my test Pi and update this thread with what I learn.
Reply
#10
Here is some Python code that works with the MQTT_schedule plugin:
Code:
#!/usr/bin/python
# -*- coding: utf-8 -*-

from __future__  import print_function
import paho.mqtt.publish as publish

def pub():
    """
    Send an MQTT mesage to SIP.
    """
    host='192.168.1.XXX' # URL of system runing mosquitto
    topic = 'SIP/schedule'
   
#     payload = '[0, 5, 0, 5, 0, 0, 0, 0]' # payload as list (array)
#     payload = '{"S01":0, "S02":5, "S03":0, "S04":5, "S05":0, "S06":0, "S07":0, "So8":0}' #  payload as Python dictionary (JavaScript object)
    payload = '{"S08":5}'  #  payload as Python dictionary (JavaScript object)

    try:
        publish.single(topic, payload, hostname=host) #  , port=1883, retain=False, qos=0)
    except Exception as err:
            print("Couldn't publish: ", err)
           
pub()


You will need to have the python Paho module installed in order for it to work and be sure to change the host to the URL of the Pi you are running mosqitto on.

The payload examples should give you something to work with.
<p><br></p>
Reply


Forum Jump:


Users browsing this thread: 16 Guest(s)