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.
・ 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
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.
・ 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)
-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)
topic |
---|
mqtt_RC |
message | motion |
---|---|
w | Advance |
a | Turn left |
d | Turn right |
s | Stop |
b | Backward |
f | End |
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 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")
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.
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