Sunday 13 September 2020

RC-Car-4-11 PWM car using MQTT

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.

                                                              -------------------

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()

-- - - - - - - - - - - 


26/10/20  The subscriber program won't start at bootup for some reason. It is in rc.local. 

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'.

Fixed by uninstalling RPi.GPIO and re-installing using sudo. 
------------------------   ------------------- ------- 
2/11/20 
 Tested today with a 3Com Router as an AP, and it responded much more quickly to commands. 

Now to configure R-Pi as an AP and test.  See previous blog about doing this. 
https://madsmaddad.blogspot.com/2020/02/raspberry-pi-ad-hoc-wireless.html


3/11/20
Have installed Hostapd and dnsmasq. 
It boots to the rc-car wifi. Now I need to find which files I have to change on mosquitto configuration to specify new IP address and also in MQTT-Dash on tablet. 

one is /home/pi/mqtt/subscriber.py   to specify new IP address for subscriber to access Mosquitto.

In MQTT-Dash, just hold down the button for the publisher connection, then select Edit and change the IP address, and it works, Easy-peasy. 

---------------------------------- -------------------
12/11/20
I have discovered how to configure software pwm on pin 12 (Backwards) so that I can move in reverse at less than full speed. A little problem with that at first, but it now works and is hardwired to 30%. 
It didn't like  "  back_pwm.ChangeDutyCycle(30)"
so I took this out of the initialisation and used it: "  back_pwm.start(30)  ". In the past I remember that there was some problem with the ChangeDutyCycle command. 

I did have the program listed above but have taken it out as it has changed quite a bit. If anyone wants it they can ask. 

--------------------------- --------- --------------- 

6/11/20      This should be a seperate Blog post. 

Have discovered this and will convert it to use the Wemos D-1 as the Publisher for a test. 
https://www.instructables.com/The-Universal-MQTT-Remote/

Have ordered 4x20 LCD screen.      had to change program from 16x2
It connects as follows:
gnd
Vcc           Vin. 5v. 
SDA         D2 or GPIO 4
SCL          D1 or GPIO 5


As the program stands it has 4 switches, so I have assigned them as follows:
Forward +15        14, D5 on Wemos, Orange
Stop                      2 , D4 on Wemos, Purple
Reverse                 12, D6 on Wemos, Blue
Left                        13, D7 on Wemos, Grey



14/11/20  connected up with new screen,  fing shows wifi on all devices
arduino IDE serial monitor shows wemos attempting to make mqtt connection multiple times, but of course my R-Pi software does not have the loop to also try that. 
 rebooted pi so see if that worked. 


Arduino Uno compile error:
WARNING: library LiquidCrystal_I2C claims to run on avr architecture(s) and may be incompatible with your current board which runs on esp8266 architecture(s).  This is apparently due to it not specifically specifying esp8166 in the config file.

Because of this I swapped SDA and SCL https://www.instructables.com/Interfacing-LCD-20X4-Display-to-Nodemcu/



Adding Ultrasonic sensor

Sensor has 4 pins

Gnd     Light Blue  to GPIO pin 9 
Echo    green, via 330 Ohm resistor to GPIO Pin 15
Trig      Blue  to GPIO Pin 13
VCC    Grey - to GPIO pin 2
Other resistor to GPIO Pin 25


2021- May 09 
Ben brought me a large RC racing car that he had picked up at the tip. It has a six cell battery, probabbly Nicad by the look of it, Made by Wintonic, a chinese company. It is not on their website which has NiMH and Li-Ion batteries. It has charged. 

The 27MHz chip is a PT 8A978BP and has the same pinouts as other ones that I have had, so will be easy to change for a Raspberry Pi.  I noticed that pin 12 is a Turbo  pin, so might try and make that work, though I don't have a switch to drive it on the controller. 

Got it running and gave up on it as it is so old, pre PWM. 








No comments: