Saturday, 10 October 2020

Arduino Uno, Wemos D1 Mini, WiFi and MQTT

 I have recently been given an Arduino Uno, and I already have the Wemos D1 which has Wifi. 



What I want to do as an end product is have the UNO with a number of push button switches connected, using MQTT to communicate through the Wemos Wifi to my RC car that is using MQTT. 

I think that the first step is to make the wemos into a Wifi dongle for the Arduino. 

Here are some videos to help:

https://www.youtube.com/watch?v=WnRk8w7SyTo 

This looks like a good intro to getting started with Wemos D1, but no Wifi. 

https://www.youtube.com/watch?v=q2k3CzT5qE0   -This one just a repeat, ignore it. 

https://www.youtube.com/watch?v=3bFs_MZVFxw    -Wemos wifi web server to control a relay.

Setting static IP address. More there than you think. 

https://iotbyhvm.ooo/esp8266-static-ip-address-using-arduino-example/

And another: https://www.geekstips.com/esp8266-arduino-tutorial-iot-code-example/

also: https://create.arduino.cc/projecthub/imjeffparedes/add-wifi-to-arduino-uno-663b9e



MQTT


https://www.arduino.cc/reference/en/libraries/mqtt-client/

https://www.digikey.co.uk/en/maker/blogs/2018/how-to-use-basic-mqtt-on-arduino

https://github.com/256dpi/arduino-mqtt

https://pubsubclient.knolleary.net/   *** Just a library

22/10/20

https://www.youtube.com/watch?v=NtyyfcYQxa4      and

https://github.com/martin-ger/uMQTTBroker

Observing operation via a terminal on the R-Pi:

mosquitto_sub -h 192.168.4.1 -v -t "rc-car-pwm/#"  

FORGET THE ARDUINO

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.    [See references at end]   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/


MQTT-DASH                Wemos program

UserName=rc-car-11
+Password
Client_ID=rc-car-11c   client_Name=rc-car-11
Topic=rc-car-pwm
                                       
                                        forward,backwards etc

It's confusing.

More hints: 
https://iotdesignpro.com/projects/how-to-connect-esp8266-with-mqtt
-- testing this. 

http://bytesofgigabytes.com/mqtt/esp8266-as-mqtt-publisher-and-subscriber/

https://vimalb.github.io/IoT-ESP8266-Starter/Lesson_04/lesson.html

TESTING SCREEN

Going to try this: 
https://blog.hau.me/2018/12/26/i2c-oled-display-on-a-wemos-d1-mini/
from first test file address is 0x27
Second test file didn't do anything.
third test file (the adafruit one) caused the display to flash, so that's progress of a sort. 
This is with D1 for the clock. 
D5 for the clock didn't do anything. 

Found this: Uses 3v3 and not 5v
http://www.esp8266learning.com/wemos-mini-i2c-lcd-example.php

After  trying a number of test programs I discovered that the screen brightness was turned up full and thus no characters could be seen. 
Now have it displaying with the Emile Renaud program - the first. 
Wemos-MQTT-Test doesn't
I2C-Test-2 doesn't
I2C-Test-3 flashes screen, doesn't display words
I2C-test-4 works once I change the order of the setCursor values
    lcd.setCursor(0,2);   is position 0, line 2


As the program seems to work, but pushing the buttons doesn't do anything, I'll test the voltage to the switches and see if they need pullups. Only the stop button registers any voltage. All the others need pullups. Perhaps because all of them are at 0v is the reason why nothing can be transmitted. 

Found an IC that is 1 central pin for 12 15K resistors on other pins. Will use it for pullups for input pins. 

Interesting sites:

https://chewett.co.uk/blog/1066/pin-numbering-for-wemos-d1-mini-esp8266/

https://community.wia.io/d/49-wemos-d1-mini-esp8266-smart-iot-button-with-messagebird-sms-and-voice-messages      *** this is quite a complex example with short and long presses of a button. 

https://docs.wemos.cc/en/latest/d1/d1_mini.html

The random Nerd has a good explanation and example:
https://randomnerdtutorials.com/esp8266-nodemcu-digital-inputs-outputs-arduino/

This one tells me about INPUT_PULLUP, enabling internal pullup resistors
https://vimalb.github.io/IoT-ESP8266-Starter/Lesson_09/lesson.html


25/11/20 Lots of errors in the modified program. I tried to set the message to be transmitted (Topic) according to which button was pushed, but it doesn't like this. I think that I am going to have to specify an array of messages and then use the button press to set the key. 

That looks better, but still having trouble with the connect. 

Added Esp8266MQTTClient library by Tuan PM
EspMQTTClient by Patrick LaPointe is already installed.
adding Esp_WiFiManager by  Khoi Huang

Still have this error:
/home/peterm/wemos-Arduino/mqtt-Wemos-RC11-c/mqtt-Wemos-RC11-c.ino: 
In function 'void loop()':  mqtt-Wemos-RC11-c:62:18: error: expected primary-expression before '.' token      if (!WiFiClient.connected() && WiFi.status() == 3)
                  ^


Tomorrow, look at this:
https://gist.github.com/boverby/d391b689ce787f1713d4a409fb43a0a4

It compiles with a change of a deprecated label. 
Now set my Wifi bits in, and then add the display 

serial set to 115200
Connecting to rc-car
..........
WiFi connected
IP address: 
192.168.4.7
Attempting MQTT connection...connected
subscribed to : rc-car-11/esp-bc:dd:c2:24:b0:f4/in
Publish topic: rc-car-11/esp-bc:dd:c2:24:b0:f4/out
Publish message: {"micros":20001072,"counter":1,"client":esp-bc:dd:c2:24:b0:f4}
Publish topic: rc-car-11/esp-bc:dd:c2:24:b0:f4/out
Publish message: {"micros":40002085,"counter":2,"client":esp-bc:dd:c2:24:b0:f4}
--------
Put in LCD stuff, and moved void (setup) to beginning, but no display, though it all compiles OK. 
Put in display commands at appropriate place and it's OK.  At some places it didn't work...
Note that it is using the library ESP_WiFiManager.

Going to change it to version f, and add the pins setup.

29/11/20 
Connected to Raspberry Pi with Termius,
mosquitto_sub -h 192.168.4.1 -v -t "rc-car-11/#"  

And this is the result: 


This gives me a starting place for adding the switches. 

Note that the Rc-car is 'rc-car-pwm', but I have used on the wemos program 'rc-car-11'. This will have to change.  

Maybe that's why nothing worked on the previous revision of the wemos program- Stupid.

5/12/2020 Ok, I have the wemos sending the state 'forward' to the Raspberry Pi correctly now. 
I have now added the switch sampling software to this and got it to compile without errors. I need to reduce the 5000 msec delay and test it. Oh, first add debug messages to the display. 
Sort of works... Display flashes all sorts of random characters filling screen unti it has a message to display.   I don't think the flow of routines is quite correct. 
The serial monitor shows lots of stuff. 

It's a watchdog timer thing. 
https://github.com/esp8266/Arduino/issues/2866
or
https://forum.arduino.cc/index.php?topic=442570.0

Need to add a Yield() statement, now trying that to see where.

Adjusted timout back to 2sec from 0.5 sec.
iFi connected
IP address: 
192.168.4.7
Attempting MQTT connection...connected
subscribed to : rc-car-pwm/Wemos-ESP8266/in

--------------- CUT HERE FOR EXCEPTION DECODER ---------------

Exception (3):
epc1=0x40100961 epc2=0x00000000 epc3=0x00000000 excvaddr=0x40000150 depc=0x00000000

>>>stack>>>

ctx: cont
sp: 3ffffd80 end: 3fffffc0 offset: 0190
3fffff10:  00000001 3ffee4c4 00000001 40201720  
3fffff20:  00000000 00000c00 3ffee4c4 3ffee698  
3fffff30:  3ffee460 3ffe84cc 00000020 40100c07  
3fffff40:  3ffee460 3ffee4c4 3fffff8c 40206fde  
3fffff50:  40206fac 3ffee4c4 3fffff8c 402042e6  
3fffff60:  3ffee460 3ffee4c4 3fffff8c 402042fc  
3fffff70:  3ffee460 00000000 3fffff8c 402015e6  
3fffff80:  00000000 00000000 00000001 40100154  
3fffff90:  3fffdad0 00000000 3ffee658 40100175  
3fffffa0:  3fffdad0 00000000 3ffee658 40204adc  
3fffffb0:  feefeffe feefeffe 3ffe8514 40100ec1  
<<<stack<<<

--------------- CUT HERE FOR EXCEPTION DECODER ---------------

 ets Jan  8 2013,rst cause:2, boot mode:(3,6)

load 0x4010f000, len 3584, room 16 
tail 0
chksum 0xb0
csum 0xb0
v2843a5ac
~ld

Connecting to rc-car
......

Got it working by changing the code  to a do-while loop. 

Have wired it up in the case. 

Slow  (D0)      green
Right  (D3)     Orange
Stop (D4)        Red
Forward (D5)  Blue
Reverse (D6)   Yellow
Left (D7)         White
Ground            Black


Here are some pictures of the finished product. The case was an old ADSL router.




and inside:



I moved the Wemos and turned it around so that the USB connection lined up with the hole in the case just to the right of it, so that it can be powered by a USB powerbank, or updated.  The pushbuttons were cannibalized out of an old digital radio. The bits  of pcb they are attached to made it easy to hot glue them in and also to solder to.  I never connected up the battery pack. 


24/1/2021  I keep having the problem that the 'SLOW' button connected to D0 causes flickering on the display and keeps on occuring despite other buttons being pressed.  
Investigation finds that it is not a hardware problem, and here is a possible cause that needs invetigation. It looks like I can only have 5 inputs and my sixth is invalid!

https://community.blynk.cc/t/esp8266-gpio-pins-info-restrictions-and-features/22872


26/1/2021  Trying to find out the state of the subscriber on the R-Pi and why it doesn't work.

Learnt the command ps aux | grep subscriber
and the analysis of the output:
D    uninterruptible sleep (usually IO)
R    running or runnable (on run queue)       *** state desired. ***
S    interruptible sleep (waiting for an event to complete)
T    stopped, either by a job control signal or because it is being traced
W    paging (not valid since the 2.6.xx kernel)
X    dead (should never be seen)
Z    defunct ("zombie") process, terminated but not reaped by its parent
15/2/2021  If I remember correctly I got it working by taking out the timestamp from the Logging. I am just logging statements now. 





References: 
13/11/2023
https://www.ebay.co.uk/itm/295128076779










No comments: