Explaining the title. This is my car number 4, and the 11th version of the software. For what it's worth, I am working on SD card #1 which is 16GB.
2024- August. It wasn't working after I changed the subscriber, so I have rebuilt the card using card 11.
Installed Raspios Lite 2024-07-04 using the Raspberry Pi Imager on kubuntu and setting a username and password, SSH on, and wifi configuration.
It booted on Ethernet and I used fing to find the IP address and SSH logged in.
sudo apt-get install firmware-zd1211.
Sudo apt update
Sudo Apt upgrade
sudo apt install python3-pip
Install ad-hoc networking.
sudo apt-get install dnsmasq hostapd
Plug in Zyxel dongle and reboot. Ifconfig shows current IP's.
sudo nmtui. This works to set both the Ethernet and Wifi IP addresses.
Setting up MQTT
https://pimylifeup.com/raspberry-pi-mosquitto-mqtt-server/
https://bytebeam.io/blog/getting-started-with-mqtt-on-raspberry-pi-using-python/ - Probably better as my Subscriber is written in Python.
sudo apt install mosquitto mosquitto-clients
sudo pip3 install paho-mqtt --break-system-packages was necessary.
So far so good. Get the subscriber stuff into the right place.
Under this new OS, the format is /home/peterm/pi/mqtt...
The old e format didn't have the /peterm/ in it. How do I change the configuration?
-------------------
Been thinking about a 4 button controller on a Raspberry Pi that I saw, and how I could replace the current system that I have. One post suggested MQTT to talk between Pi's and gave a good reference to it at:
www ref 1
Here is a good explanation:
www ref 2
and look at others of his to see how to use buttons with a microcontroller.
www.ref 3
MQTT Client on Android:
www.ref 4
Think about it and try it?
13/9/20 Trying that R-Pi stuff on card#1
Necessary to install pip: sudo apt install python3-pip
Note to use pip it is instruction pip3
-------------------
Starting state: Using SD card 1 which previously contained Motion software.
It is obviously set up with networking for IP ...9 as that works.
Configured for ....8 on bmth-wireless network
Eventually I will need to AP software and firmware for ZD1211 card.
Have added ZD1211 firmware.
--------------------
Downloaded paho-mqtt as recommended.
Have copied app.py to file subscriber.py in /home/mtqq
I need to look at documentation to see how I have to modify this file.
And also to set up Mosquitto.
www ref 5
You can configure the mosquitto,You will find the mosquitto conf file in etc/mosquitto
-------------------
14/9/20 Some links to setting up Mosquitto
1.www ref 6
2.www ref 7
3. www ref 8
---------------
from the adafruit link [3]:
sudo apt-get install mosquitto mosquitto-clients python-mosquitto
I think that I have done the first two, but not the python one.
Hah- Apt Cannot find python-mosquitto!
After that there are some good examples, but Adafruit bits are needed, so I am going to leave this one aside for now. It shows how to set up security.
---------------
[2] is about setting up a client/Publisher on the R-Pi and I want to set up a subscriber.
---------------
[1] gives a good example of how you can test a setup within a R-Pi.
---------------
16/9/20
According to this, paho-Mqtt is only a python client library, and I still need a broker.www ref 9
Mosquitto is the broker.
like this for examples using mosquitto:
www ref 10
All examples that I have found for a subscriber call for a 'broker address' and port.
www ref 11
examples I have found for setting up mosquitto.conf seem too sparse and don't give me what I want. such as Broker name.
I may have been looking at this all wrong and need to treat each function that I want to perform as a seperate client.
16/9/20
I have a file called subscriber.py and when I called it by
'python3 subscriber.py'
it got over the initial lines of code and stopped at
pi@raspberrypi:~/mqtt $ python3 subscriber.py
Traceback (most recent call last):
File "subscriber.py", line 21, in <module>
import RPi.GPIO as GPIO
ModuleNotFoundError: No module named 'RPi'
Because this is a new build I think that I forgot to add this.
pip3 install RPi.GPIO
Now no errors displayed. Will look at how to do the loop to action the response to messages.
17/9/20
Added some lines to try and set up communications
# Create client --?? Is this the same as User?
client = mqtt.Client(client_id="rc-car-11")
# Connect to broker
client.connect(broker_address,1883,60)
# Subscriber to topic
client.subscribe("rc-car-pwm")
# Run loop
client.loop_forever()
got this response
traceback (most recent call last):
File "subscriber.py", line 20, in <module>
client = mqtt.Client(client_id="rc-car-11")
NameError: name 'mqtt' is not defined
changed this line to just mqtt and not mqttClient
import paho.mqtt.client as mqtt
And no errors.
Testing MQTT-Dash from: www ref 12
set it up with
192.168.1.8 port 1883
name = rc-car-11
topic = rc-car-pwm
and a button that gave 'forward' on one press, and 'stop' on the next.
to see if it was being received by Mosquitto on the Pi I had to add the following:
sudo apt install mosquitto-clients
and then start this with the command:
mosquitto_sub -h 192.168.1.8 -v -t "rc-car-pwm/#"
and it showed the messages being received.
Need to look at MQTT-Dash to see if I could set up a number of buttons doing what I wanted each to do. Left/Right will be the difficulty.
Just found www ref 13
Have started to replace flask stuff with While loop.
Think I need a Def statement as in this
www ref 14
at the beginning of the while loop?
Looked up Def Keyword. It defines a function see 'Byte of Pi' page 100.
19/8/20
Used this a lot today to set up subscriber file without While loop.
4. www ref 15
Now to test.
Subscriber.py just falls out, no error messages, and no response.
See the end of this:
www ref 16
23/9/20
trying it out, expect errors - - - yep, debugging.
Note from the error message question on a forum that I only need to connect once, not on every loop. I had commented this out.
At the end of the day, added an else command to the on_message function, which called the motorleft LED, and that worked. So something wrong with the other if and elif commands.
Took out brackets around 'message ==forward' test. still didn't work.
changed from "forward" to forward to 'forward' . No change, still does not work.
Added line in on_message routine to print (msg) and this is what I got:
pi@raspberrypi:~/mqtt $ python3 subscriber.py
<paho.mqtt.client.MQTTMessage object at 0xb6131070>
which is why this routine does not work.
Added this line to program:
print("Message received-> " + msg.topic + " " + str(msg.pyload)) # Print a received message
and this is what I got:
Message received-> rc-car-pwm b'forward'
Message received-> rc-car-pwm b'motorleft'
Message received-> rc-car-pwm b'stop'
change if statement to msg.payload
No, Didn't work.
-- - - - - -
Got it working:
def on_message(client, userdata, message):
print("Received message '" + str(message.payload) + "' on topic '" + message.topic + "' with QoS " + str(message.qos))
if message.payload == b'forward':
up_15()
elif message.payload ==b'stop':
on()
else:
motorleft()
I am going to try the method 5 mentioned in www ref 18
and call my unit file subscriber.service
Doesn't seem to work and I can't find any error messages.
pi@raspberrypi:/lib/systemd/system $ sudo service subscriber status
● subscriber.service - My MQTT Subscriber Service
Loaded: loaded (/lib/systemd/system/subscriber.service; enabled; vendor preset: enabled)
Active: failed (Result: exit-code) since Mon 2020-10-26 09:35:35 GMT; 29min ago
Process: 492 ExecStart=/usr/bin/python3 /home/pi/mqtt/subscriber.py (code=exited, status=1/FAILURE)
Main PID: 492 (code=exited, status=1/FAILURE)
Oct 26 09:35:32 raspberrypi systemd[1]: Started My MQTT Subscriber Service.
Oct 26 09:35:35 raspberrypi python3[492]: Traceback (most recent call last):
Oct 26 09:35:35 raspberrypi python3[492]: File "/home/pi/mqtt/subscriber.py", line 29, in <module>
Oct 26 09:35:35 raspberrypi python3[492]: import RPi.GPIO as GPIO
Oct 26 09:35:35 raspberrypi python3[492]: ModuleNotFoundError: No module named 'RPi'
Oct 26 09:35:35 raspberrypi systemd[1]: subscriber.service: Main process exited, code=exited, status=1/
Oct 26 09:35:35 raspberrypi systemd[1]: subscriber.service: Failed with result 'exit-code'.
------------------------ ------------------- -------
2/11/20
No comments:
Post a Comment