Node-RED controlling Robot via MQTT

I've had an idea to run MQTT clients on cheap ESP8266/ESP32/ devices with simple programs to accept messages via MQTT to control motors and publish sensor info from things ir line followers back to a laptop/tablet

So I'm starting off with a Pi robot that I have (as a POC) and here is first working run at it.

Simple Node-RED flow (the one at bottom) that keeps motors running but sends stop when distance is < 10cm

You can also see the ir obstacle sensors changing state as well

I'd love to see the flow.

Looks like the start of a brilliant project.

I might do something similar for the students on my "IoT after-school club".

BTW - I notice you have your robot on a seperate tab in your flow. One way seperate different tasks is to name them using the 'node-red' command to start node-red.

For instance, say you a want to use node red for your robot and you want to develop a flow to control a skelleton head for halloween and you want a flow to handle measuring temperatures in a greenhouse. You could put them all in the same flow and have a lot of tabs or you could seperate the tasks into different flows.

To do this, create an empty flow by naming the file to use:

  • for the robot, say node-red pi2go.json and put the robot code there.
  • for the halloween job, start node-red with node-red halloween.json
  • for the greenhouse you could use node-red greenhouse.json

Now you have a clean flow for each task. It's a great way to keep from having a massive set of tabs in one flow. In your .node-red folder you will see the different json files for each flow you created.

Just passing along a tip I got awhile ago from @dceejay

Just been thinking about your project...

You could add a Raspberry Pi-ZERO-W and a Pi Camera, then your robot could "see" and "show" where it's going. i.e.send images/video back to Node-Red and your laptop.

The ESP8266 has Pulse Width Modulation (PWM) capability so with the addition of an H-bridge you could control speed and direction of both motors.

[{"id":"620f988d.f18b48","type":"mqtt in","z":"34b05225.5f8aee","name":"","topic":"robot/ultrasonic","qos":"2","broker":"c6e3938e.73397","x":120,"y":600,"wires":[["4b732d29.b9c104","eec6bd01.4e8cb"]]},{"id":"4b732d29.b9c104","type":"switch","z":"34b05225.5f8aee","name":"dist < 10","property":"payload","propertyType":"msg","rules":[{"t":"lt","v":"10","vt":"num"},{"t":"else"}],"checkall":"true","repair":false,"outputs":2,"x":320,"y":600,"wires":[["8346ddf8.af68c"],["8ef5f5ac.978238"]]},{"id":"4c855c07.fa7964","type":"mqtt out","z":"34b05225.5f8aee","name":"","topic":"","qos":"","retain":"","broker":"c6e3938e.73397","x":950,"y":580,"wires":[]},{"id":"8346ddf8.af68c","type":"change","z":"34b05225.5f8aee","name":"Yes:Stop Motor","rules":[{"t":"set","p":"payload","pt":"msg","to":"stop","tot":"str"},{"t":"set","p":"topic","pt":"msg","to":"robot/stop","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":520,"y":520,"wires":[["4c855c07.fa7964","50a7e425.cd944c"]]},{"id":"8ef5f5ac.978238","type":"change","z":"34b05225.5f8aee","name":"No:Forward 20","rules":[{"t":"set","p":"payload","pt":"msg","to":"20","tot":"num"},{"t":"set","p":"topic","pt":"msg","to":"robot/forward","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":520,"y":660,"wires":[["4c855c07.fa7964","50a7e425.cd944c"]]},{"id":"50a7e425.cd944c","type":"debug","z":"34b05225.5f8aee","name":"Status","active":true,"tosidebar":false,"console":false,"tostatus":true,"complete":"payload","x":690,"y":580,"wires":[]},{"id":"eec6bd01.4e8cb","type":"debug","z":"34b05225.5f8aee","name":"Distance","active":true,"tosidebar":false,"console":false,"tostatus":true,"complete":"payload","x":320,"y":540,"wires":[]},{"id":"c6e3938e.73397","type":"mqtt-broker","z":"","name":"Win8","broker":"192.168.0.23","port":"1883","clientid":"","usetls":false,"compatmode":true,"keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","willTopic":"","willQos":"0","willPayload":""}]

The flow is just a simple demo showing how it can interact with the python prog running on the Pi

This sort of setup is not really needed on a Pi as it has enough power and memory and access and many libraries but I'm using it to find out what I need

I'm hoping I can fit at least an ultrasonic sensor and 2 line followers on an ESP8266 based bot

Going to start off using continuous servos as I have a pair lying around and can plug them direct into controller without needing H-Bridge

I'm sorry but I don't understand your post.

I thought each tab in the editor IS a different flow?

How would I switch between flows using your method?

each tab is a flow, but what if you wanted to have your robot, but you also want to develope a set of flows for a greenhouse. You could have tabs 1 to 10 the robot and tabs 11-20 for the greenhouse but it gets messy (in my mind).

by starting NR with node-red robot.json you could isolate the tabs for the robot n one json file and when you want to work on the greenhouse stuff, stop NR and start it with node-red greenhouse.json and now you only have the greenhouse tabs.

This is like the noob version of the new PROJECTS option and lacks version control etc, but it is an easy way to test something without messing up your existing flow.

Got you now - I just couldn't see how to switch between flows :slight_smile:

Doesn't appeal to me as a easy method of working - but I can see it might be useful sometimes

Hi Simon,
I managed to connect an SRF-05 sensor to a Wemos D1 Mini (ESP8266) a couple of months ago.
There are drivers already built into ESP Easy firmware, so control is dead easy.
As the SRF-05 operates from 5V you need to level shift the Echo Output (with a couple of resistors) so the signal matches 3V3, otherwise you could damage the ESP8266.

This is something I found confusing at first. Each tab contains a Flow and the complete set of tabs (flows) is saved in a flows file. I think the use of the word flow to refer to a tab is a mistake, as the whole set of tabs is conceptually a single flow to my way of thinking as the messages flow round the complete set of tabs, particularly with the advent of the link node.

@zenofmud was using the word flow in the same way I think of it, I believe, meaning a complete set of tabs in a flow file. To avoid confusion I try not to use the word flow, but refer to flows file or tabs as appropriate.

1 Like

and indeed a single tab can contain several "flows" (independently starting on left and going to right etc) - so yes the term is madly overloaded... as is node... :-)... The main point to remember is that when you hit deploy all (enabled) tabs are now live and running.

You can use The new feature to manage projects
https://nodered.org/docs/user-guide/projects/

1 Like

We recently took the Halloween theme and ran with it ... Node Red on Pi Zero W connected to three PIR sensors and eight Halloween animatronics characters via an 8-way relay board. Lots of 8 year olds and a couple of ‘older kids’ were entertained :slight_smile:

image

Finally got round to cobbling up a WEMOS D1 mini Lite running a simple #MQTT PubSubClient Sketch connected to the same flow I used to test with the python prog running on a Pi robot.

Going to try assembling it all together and add in line follower capability