Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
how to set up a slave controller?
#1
Hi All,


I have two SIP controllers and I am trying to set one up as a master controller and the other as its slave. I cannot get the slave controller to run in slave mode. 


Question: Besides correctly setting the parameters in the relevant plugins pages, do I need to modify any python scripts in order to let the master control the slave? If no script needs to be modified, could someone please point out the errors in my parameters setup or in the assumptions I make below?


My setup:
Two Raspberry Pi Zero WH’s, one for the master, controlling 16 channels, and the other for the slave also controlling 16 channels. Both rPi’s run on "Raspbian GNU/Linux 10 (buster)"
SIP software version: 4.1.20
I use 32 LED’s to represent the 32 valves to facilitate troubleshooting.
I use four shift registers, each controlling 8 channels by following the wiring instructions here:
https://github.com/Dan-in-CA/sip/wiki/Relay-interface


Here is a picture showing the master on the right controlling blue LED’s; the slave on the left  controls green LED’s.  The two controllers are assembled on the same breadboard but there is no electrical connection between the two controller circuits.
--Fig A: two controllers with labels
[Image: no9iuNBs-8T0Dnpcxn3icQsnjKVBvROyOGLUUDUf...BMjxPtJGwB]


I verify the hardware works as expected by setting up identical programs on the two controllers and run both first in auto mode to verify.  Both sets of LED’s switch on and off according to a simple program schedule described further down.


Fig B: Blue LED turns on indicating master circuit works properly in auto mode
[Image: gZTsho9dTejXY0J3R_2dZfv6hw_8meD_bdLE8MZl...wkQZCULlsA]


Fig C: Green LED turns on indicating slave circuit works properly in auto mode
[Image: i-zY1M41rcUDAutw6T_hx3pMB9QIirB8a30X9bAl...axMzi7AaOg]


The program schedule runs in sequential mode, turning on each LED for 2 seconds, lasting 16*2 = 32 seconds.  Then repeat every minute from the first minute of day to the last minute of day.


--Figure D: Both controllers run the same program schedule
[Image: jxoXnoS8OlEQcYth-Qu9HUkqZ3D1WS1KpMTjeWJP...MJLoLB-eq0]



To set up the master-slave communication, I follow the instructions in the MQTT slave docs, found here:
http://192.168.0.114/static/docs/plugins/slave-docs.html
where 192.168.0.114 is my slave ip address.
(This link is accessible by pressing the HELP button under the page MQTT Slave Plugin.)


The two figures below show parameters of the Base MQTT plugins


--Figure E: 
Top: base mqtt of the master
Bottom:  base mqtt of the slave
[Image: a8MHyqVNZS-lyb0E_Q57ivyYCngxCiHeipS3J36R...1YrWyaX481]


The broker mqtt runs on the master (localhost in Figure E; and 192.168.0.135 in Figure F).  In other words, the broker runs at 192.168.0.135, which is the master’s ip address.


In addition to the above base_mqtt, an mqtt_zone plugin is also installed at the master:


--Figure G: mqtt_zone plugin at the master controller
[Image: 8pnyABuKBCpnW21XNyACto0XHg7Jn5n2WpOklYjg...Zu2Jk_rdpk]


Likewise, in addition to its base_mqtt, the slave controller has a slave_mqtt plugin.  I understand these two additional plugins are complementary, allowing the master to publish, and the slave to subscribe, to the same “Zone topic” -- “sip/zones” in my case. 



--Figure H: mqtt_slave plugin at the slave controller
[Image: S9YFjuBzHez6yar7Dn_tp6sUSWzWXyAsodXyUWr7...rZ8Uzjmz_W]



The master and the slave each control 16 channels.  I suppose the function of the parameter “First station number” is to map Channel 17 on the master’s program schedule to Channel 1 on the slave’s schedule.  


So when Channel 17 on the master is activated on the web UI display, the green LED should light up at Channel 1 on the slave controller.  But I am not observing this mapping, leading me to take my own hypothesis with a palm of salt.


The total number of channels on the master’s schedule is 32. 
The total number of channels on the slave is 16. 


--Figure I: 
Left: Master controls 16 channels, but its program schedule contains 32 channels, with the latter 16 (channels 17-32) acting as placeholders, to be mapped to the slave’s 16 channels. Each channel turns on for 2 seconds. In this figure, channel 12 is just about to expire. 
Right: the slave controller is set to manual mode,  a necessary setting for the slave controller
[Image: QbjNBvBBAGy1JCeYNHd3ln8tUfYFihsRo--loX-c...QPWxTj-Lhx]


To verify I have properly installed the paho-mqtt package, I run the following script in the ssh terminal of the master ip address. This script is derived from post #10 in this forum thread (https://nosack.com/sipforum/showthread.php?tid=174). It turns on channels 3 and 5 for 2 and 3 seconds respectively.  I observed the green LED turn on correctly so paho-mqtt must have been properly installed.
#########
#!/usr/bin/python
# -*- coding: utf-8 -*-
from __future__  import print_function
import paho.mqtt.publish as publish


def pub():
    """
    Send an MQTT message to SIP.
    """
    host='192.168.0.135' # URL of system running mosquitto
    topic = 'slave01/schedule'
    payload = '{"S01":2, "S05":3}'  #  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()
########


Of course, the mqtt_schedule plugin must be installed beforehand on the slave controller in order for this paho-mqtt verification to work. Since I run the script from the master’s ssh terminal, and the LED correspondingly lights up in the slave unit, I know the paho-mqtt must have been properly installed in both controllers.


--Figure J: mqtt_schedule parameters at the slave controller with Scheduling topic matching the publication topic in the script above.  
[Image: MnMCB3NhaebQ0An-89r9AwuCXhCeBMMh3mFiwoVM...VScK1zoLU6]



We finally reached the bottom of the “slave-docs.html”:
Test your setup:
“With your master and at least one slave set up and running, You can switch the master to manual and you can test individual stations.”


So:
I switched the master to manual.
I leave the slave in the manual mode. -- Shouldn’t I?
I turn on Channel 1 on the master schedule for two seconds. The green LED lights up at Channel 1 of the master, as to be expected.
I turn on Channel 17 on the master schedule for two seconds. No blue LED lights up at any channels of the slave.  I would expect Channel 1 of the slave to light up.


--Figure K: 
Top: the master log showed two channels turned on manually
Bottom:  the slave log did not register any channel activity
[Image: FE3BRuLYIhW8L1qx44jITHt4w1OMWe7H3XwtBhTm...RQs2B3cfzg]
If you reach here, thanks for your patience.


Any insight is greatly appreciated.
Reply
#2
Hi Paul,

It looks like you have things set up properly.
I will try to do some testing here. There have been quite a few changes to the Python code after the slave plugin was developed mostly relating to making SIP compatible with both Python 2 and 3.
What version of Python are running SIP under?

Dan
<p><br></p>
Reply
#3
Paul,

You may need to use a different user name and password in the MQTT base plugin on each Pi.

Did you change the name of each SIP installation under Options > System >system name ?
They should be different from sip and different from each other.. After the names are changed the new names will appear at the top of the UI pages.

Still setting up a couple of Pis here.

Dan
<p><br></p>
Reply
#4
Got a couple of Pi's setup and the plugins installed and configured.
Running under Python 3 i had to install paho using "pip3 install paho-mqtt".

Seems to be working as expected. Looks like the software updates didn't cause problems.

Dan
<p><br></p>
Reply
#5
(2021 Jul 21, 06:40 PM)dan Wrote: Hi Paul,

It looks like you have things set up properly.
I will try to do some testing here. There have been quite a few changes to the Python code after the slave plugin was developed mostly relating to making SIP compatible with both Python 2 and 3.
What version of Python are running SIP under?

Dan

Hi Dan,

Thanks for the help.

SIP was running under python3. 

I followed the instructions here to set up python3 for SIP:
https://github.com/Dan-in-CA/SIP/wiki/Installation
  1. sudo cp SIP/sip3.service /etc/systemd/system/
  2. sudo apt install python3-rpi.gpio
  3. sudo systemctl enable sip3.service
  4. sudo reboot
As a check, I run “systemctl status sip3” in the terminals:

--Figure L: sip3 status showing SIP running under python3
[Image: E2JbcotmhhvKaFRY66NndKLdCeH525ovisits1UM...jVlGkASirE]
Here are the three ip addresses for clarification:
192.168.0.135: master ip, mqtt broker ip
192.168.0.114: slave ip
192.168.0.113: iMac ip, from which I ssh into the pi’s.

(2021 Jul 21, 08:49 PM)dan Wrote: Paul,

You may need to use a different user name and password in the MQTT base plugin on each Pi.

Did you change the name of each SIP installation under Options > System >system name ?
They should be different from sip and different from each other.. After the names are changed the new names will appear at the top of the UI pages.

Still setting up a couple of Pis here.

Dan

Dan,

I just changed their usernames and passwords:
-- Figure M: using distinct usernames and passwords for the two controllers
[Image: hvVhgmDvuGbPfF_2QuIbFlP6P9d2M7MarKyL0WOv...LqAkimqIiJ]
Result: slave controller is still not responsive

You ask: "Did you change the name of each SIP installation under Options > System >system name ?"

The system names were previously set as “Master” and “slave01”.  See Figure M above. So no modification here.

(2021 Jul 21, 10:08 PM)dan Wrote: Got a couple of Pi's setup and the plugins installed and configured.
Running under Python 3 i had to install paho using "pip3 install paho-mqtt".

Seems to be working as expected. Looks like the software updates didn't cause problems.

Dan

Hi Dan,

Got a couple of Pi's setup and the plugins installed and configured.
Running under Python 3 i had to install paho using "pip3 install paho-mqtt".


Yes, I also did install paho-mqtt under python3, so running the installation again returns “requirement already satisfied” -- so no additional installation below:

-------
$ pip3 install paho-mqtt
Looking in indexes: https://pypi.org/simple, https://www.piwheels.org/simple
Requirement already satisfied: paho-mqtt in ./.local/lib/python3.7/site-packages (1.5.1)
------

Besides SIP, I also installed comitup to automate the wifi-provisioning of the pi.  
https://davesteele.github.io/comitup/


I don’t know if comitup is interfering with SIP running in slave mode.  I will remove comitup and see its impact on SIP.  


Oh now I remember, I didn’t install raspbian from the official site. Instead, the comitup image has a built-in raspbian os.  


I will install the official raspbian and report on any changes.

Paul
Reply
#6
Paul,

A method I frequently use to diagnose problems is to start SIP manually:
SSH into the Pi and cd into the SIP directory and stop sip with
Code:
systemctl stop sip3

then start sip from the command line:
Code:
python3 sip.py
When sip starts several lines of information will be printed including any error messages. Pay close attention to the first couple of lines which is where any plugin errors usually appear.

Another useful tool is a MQTT monitoring program. I have used mqtt-spy which is a java based app. Unfortunately I recently upgraded the version of Java on my main computer and now mqtt-spy and a couple of other java based programs refuse to load.

With a tool like mqtt-spy or MQTT Explorer you can subscribe to the topic sip is using and see what is being sent.

Dan
<p><br></p>
Reply
#7
(2021 Jul 22, 02:05 PM)dan Wrote: Paul,

A method I frequently use to diagnose problems is to start SIP manually:
SSH into the Pi and cd into the SIP directory and stop sip with
Code:
systemctl stop sip3

then start sip from the command line:
Code:
python3 sip.py
When sip starts several lines of information will be printed including any error messages. Pay close attention to the first couple of lines which is where any plugin errors usually appear.

Another useful tool is a MQTT monitoring program. I have used mqtt-spy which is a java based app. Unfortunately I recently upgraded the version of Java on my main computer and now mqtt-spy and a couple of other java based programs refuse to load.

With a tool like mqtt-spy or MQTT Explorer you can subscribe to the topic sip is using and see what is being sent.

Dan

Hi Dan,


Thanks for these suggestions.


I can now get the slave mode to work properly.  In my case, the slave mode works only when I install paho-mqtt via super-user privilege, using sudo prefix:
$ sudo pip3 install paho-mqtt
$ sudo reboot


To check for consistency, I also sudo uninstalled and was able to reproduce the error in slave mode again.  I did one more test cycle of sudo install/uninstall and got consistent behavior.  I repeated the same test on a second sd card and got the same result.  Could you please try and see if you see the same behavior?


$ sudo pip3 uninstall paho-mqtt
$ y # to confirm removal of package
$ sudo reboot # for change to take effect


I did this test on my slave controller only.  The master controller must have been previously sudo installed with paho-mqtt.
If you observe the same behavior, the following might give a clue as to what happens.  I don’t have an explanation.
I followed your tips above about running sip.py manually to help diagnoses.  SIP reports an OSError error 
“OSError: No socket could be created -- (('::', 80, 0, 0): [Errno 13] Permission denied)”
So I ran a sudo prefix (sudo python3 sip.py) and the OSError above went away


pi@raspberrypi:~/SIP $ python3 sip.py
Messge from Signaling plugin!: Just an example!
MQTT plugin couldn't initalize client: [Errno 111] Connection refused
plugins loaded:
  mobile_app
  mqtt
  mqtt_schedule
  mqtt_slave
  plugin_manager
  signaling_examples
  system_update
Starting timing loop
zones changed
[0, 0, 0, 0, 0, 0, 0, 0]

A new day has started.
http://:::80/
Traceback (most recent call last):
  File "sip.py", line 314, in <module>
    app.run()
  File "sip.py", line 232, in run
    return web.httpserver.runsimple(func, (ip, port))
  File "/home/pi/SIP/web/httpserver.py", line 178, in runsimple
    server.start()
  File "/home/pi/SIP/cheroot/server.py", line 1820, in start
    self.prepare()
  File "/home/pi/SIP/cheroot/server.py", line 1779, in prepare
    raise socket.error(msg)
OSError: No socket could be created -- (('::', 80, 0, 0): [Errno 13] Permission denied)
pi@raspberrypi:~/SIP $ sudo python3 sip.py
Messge from Signaling plugin!: Just an exampl!
MQTT publish UP
plugins loaded:
  mobile_app
  mqtt
  mqtt_slave
  plugin_manager
  signaling_examples
  system_update
Starting timing loop

zones changed
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
A new day has started.
zones changed
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]


In summary, I need paho-mqtt to be sudo installed for slave mode to work.  But I don’t know why.

Paul
Reply
#8
Paul,

Sorry, I should have included "sudo" in the instructions I provided. It is a Linux security feature.
I usually log into my development Pi as root which allows me to save files and do other actions without using sudo and I sometimes forget to include it in instructions. If you don't use sudo you will often not be able to install software or run programs but will get a "permission denied" error message.

I'm glad to hear that your slave setup is working.

Dan
<p><br></p>
Reply
#9
(2021 Jul 23, 02:06 PM)dan Wrote: Paul,

Sorry, I should have included "sudo" in the instructions I provided. It is a Linux security feature.
I usually log into my development Pi as root which allows me to save files and do other actions without using sudo and I sometimes forget to include it in instructions. If you don't use sudo you will often not be able to install software or run programs but will get a "permission denied" error message.

I'm glad to hear that your slave setup is working.

Dan

Dan,

That makes sense then.

By the way, I came across this thread when trying to fix the OSError above about socket creation permission.

https://nosack.com/sipforum/showthread.p...be+created

Would sudo-install be a better a solution than changing the port ("htp": 80,) in SIP/data/sd.json?  

I did change the port and the permission error went away, but it created another problem when I press the "clear log" button under the Log menu:

  [Image: z7mqlIAdQuQ5QFPkBpcolYTh_ucz11Yr_PcdzSSJ...Q46_V8AJ5i]
It didn't clear the log and I have to press the back button to get back the SIP screen.

And because I was running "python sip.py" manually in the background I saw this error message, a permission error accessing log.json :

      p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Menlo; color: #000000} span.s1 {font-variant-ligatures: no-common-ligatures}

PermissionError: [Errno 13] Permission denied: './data/log.json'

I saw similar types of permission error when I used other the web UI functions but I can't remember which ones now.

So maybe the best practice overall is to ssh as root user into the pi.

Paul
Reply
#10
Running programs as a non-root user is a (very) good practice. But a non privileged user is by default not allowed to start a socket on the lower ports like 80 or 443. Another good practice is to run a non privileged program on a higher port (e.g. 8080) and use a rev proxy (HAproxy, Squid, Apache2, NGinx etc.) for (SSL/TLS) offloading and adding a little more security.

--Gerard
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)