编写esp8266的软件(自制wifi遥控小车ESP8266实践指南)
编写esp8266的软件(自制wifi遥控小车ESP8266实践指南)于是后来我还是用上了电池盒,用电池盒给L9110供电,正负极分别接VCC GND就可以了。一开始,我尝试了直接使用充电宝给NodeMCU开发板供电,然后L9110的VCC接NodeMCU的VIN,GND接NodeMCU的GND来供电。这样的话,简单便捷,不需要两套供电。但是不幸的是,动力电路走开发板的VIN确实会有问题:在电机运转一会儿后,开发板会重启。实际接线效果大致如图:固定电池盒和充电宝:4节串联的5号电池用于给L9110供电,而充电宝用于通过usb接口给NodeMCU开发板供电。
上次带大家利用ESP8266自制了wifi控制的LED点阵屏幕,大家觉得怎么样呢? 手把手教你用wifi控制显示屏!ESP8266实践指南(一)
今天我们来做点更有意思的~ wifi遥控小车!
一、所需材料- ESP8266 NodeMCU开发板:这次我们使用CP2102芯片的那款NodeMCU,优点是体积更小一点,更适合小车这种体积受限的场景。
- L9110电机控制模块
- 面包板:方便接线
- 两轮小车底盘套装,包含:塑料底盘,电机x2 轮子x2,万向轮,5号电池盒
- 公对公杜邦线若干
- 5号电池4节
- 手机移动电源一个
先按照说明书把小车底盘套装组装起来:
然后将ESP8266,L9110固定在面包板上,并按照线路图连接:
实际接线效果大致如图:
固定电池盒和充电宝:
4节串联的5号电池用于给L9110供电,而充电宝用于通过usb接口给NodeMCU开发板供电。
一开始,我尝试了直接使用充电宝给NodeMCU开发板供电,然后L9110的VCC接NodeMCU的VIN,GND接NodeMCU的GND来供电。这样的话,简单便捷,不需要两套供电。但是不幸的是,动力电路走开发板的VIN确实会有问题:在电机运转一会儿后,开发板会重启。
于是后来我还是用上了电池盒,用电池盒给L9110供电,正负极分别接VCC GND就可以了。
在连接好开发板,电机,电源之后,摆放好各个部件,用双面胶固定到位即可。有条件的话,电机的正负电源线建议使用电烙铁和焊锡固定好,防止震动导致松动。
三、上传代码有关ESP8266的开发环境配置在这里不在赘述,之前没有设置的朋友看这里~ 很简单哦~
十块钱入门物联网!ESP8266新手指南
将以下代码复制到Arduino IDE:
// Simple WiFi-controlled car on NodeMCU and L9110 motor drive board
// OrangeZero 零度橙子@头条
// source code modified from Alan Wang@hackster.io
#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
// WiFi settings
#define WIFI_MODE 1 // 1: AP模式,NodeMCU自身起一个wifi信号;2:SA模式,NodeMCU连上一个已有wifi。推荐使用AP模式
#define SSID_AP "NodeMCU_WiFi_Car" // for AP mode
#define PASSWORD_AP "12345678" // for AP mode
#define SSID_STA "your_wifi_ssid" // for STA mode
#define PASSWORD_STA "your_wifi_password" // for STA mode
// motor settings
#define RIGHT_MOTOR_PIN1 4 // pin 1 of right motor (D2)
#define RIGHT_MOTOR_PIN2 5 // pin 2 of right motor (D1)
#define LEFT_MOTOR_PIN1 12 // pin 1 of left motor (D6)
#define LEFT_MOTOR_PIN2 14 // pin 2 of left motor (D5)
#define MOTOR_SPEED 1200 // speed for motor
IPAddress local_ip(192 168 1 1); //IP for AP mode
IPAddress gateway(192 168 1 1); //IP for AP mode
IPAddress subnet(255 255 255 0); //IP for AP mode
ESP8266WebServer server(80);
int car_mode = 0; // set car drive mode (0 = stop)
// initialize
void setup() {
Serial.begin(9600);
Serial.println("NodeMCU Wifi Car");
pinMode(RIGHT_MOTOR_PIN1 OUTPUT);
pinMode(RIGHT_MOTOR_PIN2 OUTPUT);
pinMode(LEFT_MOTOR_PIN1 OUTPUT);
pinMode(LEFT_MOTOR_PIN2 OUTPUT);
car_control(); // stop the car
if (WIFI_MODE == 1) { // AP mode
WiFi.softAP(SSID_AP PASSWORD_AP);
WiFi.softAPConfig(local_ip gateway subnet);
} else { // STA mode
WiFi.begin(SSID_STA PASSWORD_STA);
Serial.print("Connecting to WiFi...");
while (WiFi.status() != WL_CONNECTED) {
delay(100);
Serial.print(".");
}
Serial.println("");
Serial.print("Connected! IP: ");
Serial.println(WiFi.localIP()); //the IP is needed for connection in STA mode
}
// setup web server to handle specific HTTP requests
server.on("/" HTTP_GET handle_OnConnect);
server.on("/forward" HTTP_GET handle_forward);
server.on("/backward" HTTP_GET handle_backward);
server.on("/left" HTTP_GET handle_left);
server.on("/right" HTTP_GET handle_right);
server.on("/stop" HTTP_GET handle_stop);
server.onNotFound(handle_NotFound);
//start server
server.begin();
Serial.println("NodeMCU web server started.");
}
// handle HTTP requests and control car
void loop() {
server.handleClient();
car_control();
}
// HTTP request: on connect
void handle_OnConnect() {
car_mode = 0;
Serial.println("Client connected");
server.send(200 "text/html" SendHTML());
}
// HTTP request: stop car
void handle_stop() {
car_mode = 0;
Serial.println("Stopped");
server.send(200 "text/html" SendHTML());
}
// HTTP request: go forward
void handle_forward() {
car_mode = 1;
Serial.println("Go forward...");
server.send(200 "text/html" SendHTML());
}
// HTTP request: go backward
void handle_backward() {
car_mode = 2;
Serial.println("Go backward...");
server.send(200 "text/html" SendHTML());
}
// HTTP request: turn left
void handle_left() {
car_mode = 3;
Serial.println("Turn left...");
server.send(200 "text/html" SendHTML());
}
// HTTP request: turn right
void handle_right() {
car_mode = 4;
Serial.println("Turn right...");
server.send(200 "text/html" SendHTML());
}
// HTTP request: other
void handle_NotFound() {
car_mode = 0;
Serial.println("Page error");
server.send(404 "text/plain" "Not found");
}
// control car movement
void car_control() {
switch (car_mode) {
case 0: // stop car
digitalWrite(RIGHT_MOTOR_PIN1 LOW);
digitalWrite(RIGHT_MOTOR_PIN2 LOW);
digitalWrite(LEFT_MOTOR_PIN1 LOW);
digitalWrite(LEFT_MOTOR_PIN2 LOW);
break;
case 1: // go forward
analogWrite(RIGHT_MOTOR_PIN1 MOTOR_SPEED);
digitalWrite(RIGHT_MOTOR_PIN2 LOW);
analogWrite(LEFT_MOTOR_PIN1 MOTOR_SPEED);
digitalWrite(LEFT_MOTOR_PIN2 LOW);
break;
case 2: // go backward
digitalWrite(RIGHT_MOTOR_PIN1 LOW);
analogWrite(RIGHT_MOTOR_PIN2 MOTOR_SPEED);
digitalWrite(LEFT_MOTOR_PIN1 LOW);
analogWrite(LEFT_MOTOR_PIN2 MOTOR_SPEED);
break;
case 3: // turn left
analogWrite(RIGHT_MOTOR_PIN1 MOTOR_SPEED);
digitalWrite(RIGHT_MOTOR_PIN2 LOW);
digitalWrite(LEFT_MOTOR_PIN1 LOW);
analogWrite(LEFT_MOTOR_PIN2 MOTOR_SPEED);
break;
case 4: // turn right
digitalWrite(RIGHT_MOTOR_PIN1 LOW);
analogWrite(RIGHT_MOTOR_PIN2 MOTOR_SPEED);
analogWrite(LEFT_MOTOR_PIN1 MOTOR_SPEED);
digitalWrite(LEFT_MOTOR_PIN2 LOW);
}
}
// output HTML web page for user
String SendHTML() {
String html = "<!DOCTYPE html>\n"
"<html>\n"
"<head>\n"
"<title>NodeMCU Wifi Car by OrangeZero</title>\n"
"<meta name=\"viewport\" content=\"width=device-width initial-scale=1\">\n"
"</head>\n"
"<body>\n"
"<div align=\"center\">\n"
"<h1>NodeMCU Wifi Car by OrangeZero</h1>\n"
"<br>\n"
"<form method=\"GET\">\n"
"<input type=\"button\" value=\"Go forward\" onclick=\"window.location.href='/forward'\">\n"
"<br><br>\n"
"<input type=\"button\" value=\"Go backward\" onclick=\"window.location.href='/backward'\">\n"
"<br><br>\n"
"<input type=\"button\" value=\"Turn left\" onclick=\"window.location.href='/left'\">\n"
"<br><br>\n"
"<input type=\"button\" value=\"Turn right\" onclick=\"window.location.href='/right'\">\n"
"<br><br>\n"
"<input type=\"button\" value=\"Car stop\" onclick=\"window.location.href='/stop'\">\n"
"</form>\n"
"</div>\n"
"<a href='https://profile.zjurl.cn/rogue/ugc/profile/?user_id=4182130004597211'>Copyright 2020 OrangeZero</a>"
"</body>\n"
"</html>\n";
return html;
}
点击上传按钮,稍等片刻,等待上传完成:
四、开动小车!到这里我们的小车就制作完成了!让我们来发动小车吧~
在打开移动电源的开关,连接好电池盒的正负极之后,稍等片刻,我们就可以搜索到wifi信号:
NodeMCU WiFi Car
手机连接wifi之后,在浏览器中打开:192.168.1.1
点击相关按钮就可以操纵小车前进,后退,转向,还有停止了~
大家有没有觉得并没有那么难,而且成就感满满的呢~
大家有什么还有什么想要做的物联网或者智能家居项目呢?在下面留言告诉我吧~
我是零度橙子,装机爱好者,科技达人,谷歌认证云计算架构师,大家可以关注我,了解有用有趣的科技知识~