MQTT RC car with Arduino and Raspberry Pi

Introduction

Why I decided to write this article

・ I wanted to write a memorandum ・ I wanted to use Qiita

Please note that this is the first article posted for me and may be difficult for people to read. Also, I'm making it quite appropriately, so I'll try to make it again soon.

Environment used

・ Windows10 ・ Ubuntu 18.04 ・ AWS

arduino program sketch and writing on windows10 Connect to Raspberry Pi with ssh on ubuntu and operate Raspberry Pi Use AWS as an MQTT broker

About MQTT

MQTT is omitted in this article There are many other sites that explain MQTT in an easy-to-understand manner, so please refer to that.

What was used

・ Arduino ・ Raspberry Pi 3 Model B ・ Usb cable (used for serial communication between Aruino and Raspi) ・ Mobile battery, usb cable (used to supply power to Raspberry Pi) ・ [Battery Box](https://www.amazon.co.jp/uxcell-%E9%9B%BB%E6%B1%A0%E3%83%9C%E3%83%83%E3%82%AF % E3% 82% B9-% E9% 9B% BB% E6% B1% A0% E3% 83% 9B% E3% 83% AB% E3% 83% 80% E3% 83% BC-1-5V% E5% 8D%98%E4%B8%89%E9%9B%BB%E6%B1%A0-%E5%8D%983%E9%9B%BB%E6%B1%A0%E3%82%B1%E3%83 % BC% E3% 82% B9 / dp / B01HUX8YDY / ref = sr_1_6? __mk_ja_JP =% E3% 82% AB% E3% 82% BF% E3% 82% AB% E3% 83% 8A & dchild = 1 & keywords =% E9% 9B % BB% E6% B1% A0% E3% 83% 9C% E3% 83% 83% E3% 82% AF% E3% 82% B9 & qid = 1603765741 & sr = 8-6) (Purchased at Amazon) ・ Motor driver (Purchased at Switch Science) ・ [Tamiya Fun Work Series Double Gear Box](https://www.amazon.co.jp/%E3%82%BF%E3%83%9F%E3%83%A4-%E6%A5%BD%E3 % 81% 97% E3% 81% 84% E5% B7% A5% E4% BD% 9C% E3% 82% B7% E3% 83% AA% E3% 83% BC% E3% 82% BA-No-168 -% E3% 83% 80% E3% 83% 96% E3% 83% AB% E3% 82% AE% E3% 83% A4% E3% 83% 9C% E3% 83% 83% E3% 82% AF% E3% 82% B9-% E5% B7% A6% E5% 8F% B3% E7% 8B% AC% E7% AB% 8B4% E9% 80% 9F% E3% 82% BF% E3% 82% A4% E3 % 83% 97 / dp / B001Q13BIU / ref = sr_1_1? __mk_ja_JP =% E3% 82% AB% E3% 82% BF% E3% 82% AB% E3% 83% 8A & dchild = 1 & keywords =% E3% 82% BF% E3 % 83% 9F% E3% 83% A4 +% E3% 83% 80% E3% 83% 96% E3% 83% AB% E3% 82% AE% E3% 83% A4% E3% 83% 9C% E3% 83 % 83% E3% 82% AF% E3% 82% B9 & qid = 1603764617 & sr = 8-1) (Purchased at Amazon) ・ [Tamiya Fun Work Series Narrow Tire Set 58mm](https://www.amazon.co.jp/%E3%82%BF%E3%83%9F%E3%83%A4-%E6%A5%BD% E3% 81% 97% E3% 81% 84% E5% B7% A5% E4% BD% 9C% E3% 82% B7% E3% 83% AA% E3% 83% BC% E3% 82% BA-No- 145-% E3% 83% 8A% E3% 83% AD% E3% 83% BC% E3% 82% BF% E3% 82% A4% E3% 83% A4% E3% 82% BB% E3% 83% 83 % E3% 83% 88-58mm% E5% BE% 84 / dp / B001VZJDZ6 / ref = pd_all_pref_10? Pd_rd_w = LumaR & pf_rd_p = 4f969a54-b46f-456c-b77f-72ed974b654e & pf_rd_r = ZNSFZS63R0 = B001VZJDZ6) (Purchased at Amazon)

Hardware preparation

-Cut the cardboard delivered by Amazon to an appropriate size, place the motor and battery box, and fix the motor driver on the motor with double-sided tape. Furthermore, arduino, Raspberry Pi, and mobile battery are fixed on it with masking tape.

・ I am making it with the idea that I should move for the time being. (I will remake it cleanly soon)

Software preparation

MQTT parameters

topic
mqtt_RC
message motion
w Advance
a Turn left
d Turn right
s Stop
b Backward
f End

Arduino program

The arduino controls the motor according to the signal sent from the Raspberry Pi via serial communication.

mqtt_RC_arduino.ino




const int MA1 = 10;
const int MA2 = 11;
const int MB1 = 12;
const int MB2 = 13;

int com = 0;

void setup() {

  Serial.begin(9600);
  
  pinMode(MA1,OUTPUT);
  pinMode(MA2,OUTPUT);
  pinMode(MB1,OUTPUT);  
  pinMode(MB2,OUTPUT);  

}

void loop() {
    while(true){

      if(Serial.available() > 0){
        com = Serial.read();
        Serial.println(com);
      }
      if(com == 'w'){
        motor_forward();
        delay(1000);
      }  
      else if(com =='d'){
        motor_righit();
        delay(1000);
      }
      else if(com =='a'){
        motor_left();
        delay(1000);
      }
      else if(com =='s'){
        motor_stop();
        delay(1000);
      }
      else if(com =='b'){
        motor_back();
        delay(1000);
        }
      else if(com =='f'){
        break;
      }
  }
}

void motor_forward(){
      digitalWrite(MA1,HIGH);
      digitalWrite(MA2,LOW);
      digitalWrite(MB1,HIGH);
      digitalWrite(MB2,LOW);
      Serial.println("forward");
}

void motor_back(){
      digitalWrite(MA1,LOW);
      digitalWrite(MA2,HIGH);
      digitalWrite(MB1,LOW);
      digitalWrite(MB2,HIGH);   
      Serial.println("back");   
}

void motor_stop(){
      digitalWrite(MA1,LOW);
      digitalWrite(MA2,LOW);
      digitalWrite(MB1,LOW);
      digitalWrite(MB2,LOW);  
      Serial.println("stop");
}

void motor_righit(){
      digitalWrite(MA1,HIGH);
      digitalWrite(MA2,LOW);
      digitalWrite(MB1,LOW);
      digitalWrite(MB2,LOW);  
      Serial.println("righit"); 
}

void motor_left(){
      digitalWrite(MA1,LOW);
      digitalWrite(MA2,LOW);
      digitalWrite(MB1,HIGH);
      digitalWrite(MB2,LOW);
      Serial.println("left");
}

Raspberry Pi program

Raspberry Pi receives the message sent from publiser by mqtt communication and sends it to arduino by serial communication. Enter the Raspberry Pi port that connects to the arduino via serial communication and the mqtt broker address by yourself.

mqtt_RC_raspi.py


import  sys
from time import sleep
import paho.mqtt.client as mqtt
import serial
# ------------------------------------------------------------------
sys.stderr.write("***Start communication***\n")


ser = serial.Serial('Enter the port of arduino to connect',9600)

host = 'Enter the broker's address'
port = 1883
topic = 'mqtt_RC'


def on_connect(client, userdata, flags, respons_code):
    print('status {0}'.format(respons_code))

    client.subscribe(topic)

def on_message(client, userdata, msg):
    st = str(msg.payload,'utf-8')
    print(st)

    if st == "w":
        print("go_straight")
        ser.write(b'w')

    elif st == "s":
        print("stop")
        ser.write(b's')

    elif st == "d":
        ser.write(b'd')
        print("turn_righit")

    elif st == "a":
        ser.write(b'a')
        print("turn_left")

    elif st == "b":
        ser.write(b'b')
        print("go_back")

    elif st =="f":
        print("fin")
        client.disconnect()

if __name__ == '__main__':

    client = mqtt.Client(protocol=mqtt.MQTTv311)
    client.on_connect = on_connect
    client.on_message = on_message

    client.connect(host, port=port, keepalive=60)

    client.loop_forever()

sys.stderr.write("***End***\n")

About MQTT publisher

Since there is an application that can publish and subsclibe by mqtt communication on the smartphone, you can control the sending of messages by publishing using it. This time, I created a program to publish on a personal computer and operated the personal computer as a controller.

result

I was able to run the program to publish on the terminal of the laptop computer and actually operate it as a radio control. I didn't feel any lag.

Recommended Posts

MQTT RC car with Arduino and Raspberry Pi
MQTT on Raspberry Pi and Mac
Pet monitoring with Rekognition and Raspberry pi
Create a car meter with raspberry pi
GPGPU with Raspberry Pi
DigitalSignage with Raspberry Pi
Get temperature and humidity with DHT11 and Raspberry Pi
I tweeted the illuminance of the room with Raspberry Pi, Arduino and optical sensor
Record temperature and humidity with systemd on Raspberry Pi
Machine learning with Raspberry Pi 4 and Coral USB Accelerator
Easy IoT to start with Raspberry Pi and MESH
Using Akizuki Denshi's 4WD car FT-MC-004 with Raspberry Pi
Detect mask wearing status with OpenCV and Raspberry Pi
Measure temperature and humidity with Raspberry Pi3 and visualize with Ambient
Ubuntu 20.04 on raspberry pi 4 with OpenCV and use with python
Getting Started with Yocto Project with Raspberry Pi 4 and WSL2
Troubleshoot with installing OpenCV on Raspberry Pi and capturing
Mutter plants with Raspberry Pi
Easy introduction to home hack with Raspberry Pi and discord.py
Python beginner opens and closes interlocking camera with Raspberry Pi
Create an LCD (16x2) game with Raspberry Pi and Python
Production of temperature control system with Raspberry Pi and ESP32 (1)
Measure and compare temperature with Raspberry Pi and automatically generate graph
Make an autonomous driving robot car with Raspberry Pi3 B + and ultrasonic distance sensor HC-SR04
[Raspberry Pi] Stepping motor control with Raspberry Pi
Robot running with Arduino and python
Use vl53l0x with Raspberry Pi (python)
Servo motor control with Raspberry Pi
Serial communication with Raspberry Pi + PySerial
OS setup with Raspberry Pi Imager
Try L Chika with raspberry pi
VPN server construction with Raspberry Pi
Try moving 3 servos with Raspberry Pi
Power on / off Raspberry pi on Arduino
Using a webcam with Raspberry Pi
Make a thermometer with Raspberry Pi and make it viewable with a browser Part 4
Make a Kanji display compass with Raspberry Pi and Sense Hat
Graph display of household power consumption with 3GPI and Raspberry Pi
Measure SIM signal strength with Raspberry Pi
[Raspberry Pi] Add a thermometer and a hygrometer
Hello World with Raspberry Pi + Minecraft Pi Edition
Get BITCOIN LTP information with Raspberry PI
Make a wireless LAN Ethernet converter and simple router with Raspberry Pi
Try fishing for smelt with Raspberry Pi
Get GrovePi + sensor value with Raspberry Pi and store it in kintone
Improved motion sensor made with Raspberry Pi
Try Object detection with Raspberry Pi 4 + Coral
RabbitMQ message notification app in Python with Growl ~ with Raspberry Pi and Julius ~
Power SG-90 servo motor with raspberry pi
Working with sensors on Mathematica on Raspberry Pi
Use PIR motion sensor with raspberry Pi
Make a wash-drying timer with a Raspberry Pi
Infer Custom Vision model with Raspberry Pi
Operate an oscilloscope with a Raspberry Pi
Inkbird IBS-TH1 value logged with Raspberry Pi
Production of temperature control system with Raspberry Pi and ESP32 (2) Production of transmission device
Working with GPS on Raspberry Pi 3 Python
Make a thermometer with Raspberry Pi and make it visible on the browser Part 3
Source compile Apache2.4 + PHP7.4 with Raspberry Pi and build a Web server --2 PHP introduction
Cross-compiling Raspberry Pi and building a remote debugging development environment with VS Code
Source compile Apache2.4 + PHP7.4 with Raspberry Pi and build a Web server ―― 1. Apache introduction