AdSense

2015年11月3日火曜日

ミニ四駆をワイヤレス(Wi-Fi)で動かす③ モーターと無線を結合して確認

前回、前々回と、単体のモジュールは動かせたので、いよいよ組み合わせて
ミニ四駆をWi-Fiでリモートコントロールします。

前々回のWi-fiモジュール(ESP-WROOM-02)を動かした記事
http://mementomorisince2013.blogspot.jp/2015/11/esp-wroom-02.html

前回のDCモーター(DRV8832)を動かした記事
http://mementomorisince2013.blogspot.jp/2015/11/drv8832arduino.html


使ったもの
・ESP-WROOM-02

・DRV8832使用DCモータードライブキット
・ミニ四駆


とりあえず、前回、前々回の組み合わせだけで、
ミニ四駆のモーターを開始、停止、逆回転できました。

ESP-WROOM-02のIO13とIO14を、DRV8832のIN1とIN2に接続しています。
写真、動画では、IN1とIN2それぞれがHIGHになるとLEDを点灯させました。





動画はこちら



ソースコードはこちら


#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#include <ESP8266mDNS.h>
const char *ssid = "yyy";
const char *password = "xxx";
ESP8266WebServer server(80);
#define command_start 0
#define command_stop 1
#define command_back 2
char state = command_stop;
String form ="<html><head><meta name=viewport content=width=100></head>"
"<form action=start><input type= submit value=start target=tif></form>"
"<form action=back><input type=submit value=back target=tif></form>"
"<form action=stop><input type=submit value=stop target=tif></form>"
"<iframe src=javascript:false style=display:none name=tif id=tif></iframe>"
"</html>";
const int IN1 = 13;
const int IN2 = 14;
void setup() {
delay(1000);
Serial.begin(115200);
Serial.println();
Serial.print("Configuring access point...");
pinMode(IN1, OUTPUT);
pinMode(IN2, OUTPUT);
digitalWrite(IN1, 0);
digitalWrite(IN2, 0);
delay(40);
WiFi.begin(ssid, password);
Serial.println("");
// Wait for connection
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.print("Connected to ");
Serial.println(ssid);
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
if (MDNS.begin("esp8266")) {
Serial.println("MDNS responder started");
}
server.on("/", handleRoot);
server.onNotFound(handleNotFound);
server.on("/stop", handle_stop);
server.on("/start", handle_start);
server.on("/back", handle_back);
server.begin();
Serial.println("HTTP server started");
}
void loop() {
server.handleClient();
}
void handleRoot() {
server.send(200, "text/html", form);
}
void handleNotFound(){
String message = "File Not Found\n\n";
message += "URI: ";
message += server.uri();
message += "\nMethod: ";
message += (server.method() == HTTP_GET)?"GET":"POST";
message += "\nArguments: ";
message += server.args();
message += "\n";
for (uint8_t i=0; i<server.args(); i++){
message += " " + server.argName(i) + ": " + server.arg(i) + "\n";
}
server.send(404, "text/plain", message);
}
void handle_stop() {
Serial.print("stop");
if((state == command_start)||(state == command_back)) {
stop_motor();
state = command_stop;
}
server.send(200, "text/html", form);
}
void handle_start() {
Serial.print("start");
if(state == command_back){
stop_motor();
delay(10);
start_motor();
}else if(state == command_stop){
start_motor();
}
state = command_start;
server.send(200, "text/html", form);
}
void handle_back() {
Serial.print("back");
if(state == command_start){
stop_motor();
delay(10);
reverse_motor();
}else if(state == command_stop){
reverse_motor();
}
state = command_back;
server.send(200, "text/html", form);
}
void start_motor(){
delay(10);
digitalWrite(IN1, 0);
digitalWrite(IN2, 1);
delay(10);
}
void reverse_motor(){
delay(10);
digitalWrite(IN1, 1);
digitalWrite(IN2, 0);
delay(10);
}
void stop_motor(){
delay(10);
digitalWrite(IN1, 0);
digitalWrite(IN2, 0);
delay(10);
}
view raw gistfile1.txt hosted with ❤ by GitHub
さて、今回はモーター、WifiそれぞれにUSBシリアルとArduinoから5V電源供給していますが、制御がワイヤレスでも、電源が有線では意味無いので、次は電池駆動を考えます。

続く。

0 件のコメント:

コメントを投稿