ESP8266-12F NodeMCU, MG90S steering gear, Arduino IDE - make a remote switch for home use (mobile phone remote control - only turn off the lights)

1. Effect demonstration

Because the servo arm is too short, I can only turn off the lights (I wrote this because I didn’t want to get out of bed to turn off the lights at night, so I will do this first because it meets the needs)

The effect is to open the specified webpage - turn the servo arm - turn off the light

ESP8266-12F NodeMCU, MG90S steering gear, Arduino IDE - make a remote switch for home use (mobile phone remote control - only turn off the lights)

2. Wiring

insert image description here

3. Code

#include <Arduino.h>
#include <ESP8266WiFi.h>
#include <ESP8266WiFiMulti.h>
#include <ESP8266WebServer.h>
#include <Servo.h>

// Servo
Servo servo;
// wifi
ESP8266WiFiMulti WiFiMulti;//建立ESP8266WiFiMulti对象,对象名称为WiFiMulti
ESP8266WebServer esp8266_server(80);//建立ESP8266WebServer对象,对象名称是“esp8266_server”,网络服务器响应http请求的端口号为80

// Wi-Fi 网络 SSID 和密码
const char *ssid = "这里填wifi名称";
const char *password = "这里填wifi密码";

const int ledPin = BUILTIN_LED;// ESP8266 内置 LED 引脚
const int servoPin = 12;    //定义舵机接口数字接口12,也就是舵机的橙色信号线

void setup() {
    
    
  
      Serial.begin(9600);// 串口波特率设置 //串口与舵机库可能会冲突,如果舵机未正常转动,先记住串口打印的ip地址,然后把这几处注释掉
  pinMode(ledPin, OUTPUT);// 设置 LED 引脚为输出模式
  servo.attach(servoPin);

  /*-------------连接 Wi-Fi------------*/
  // 开始执行 Wi-Fi 连接,由于连接需要一个过程,此处执行状态判断和等待
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    
    	//连接WiFi时,灯光闪烁
    digitalWrite(ledPin, LOW);
    delay(500);
    digitalWrite(ledPin, HIGH);
  }
     // Wi-Fi 连接后打印一些调试信息
     //串口与舵机库可能会冲突,如果舵机未正常转动,先记住串口打印的ip地址,然后把这几处注释掉
     Serial.println("WiFi 连接成功.");
     Serial.print("IP 地址为: ");
     Serial.println(WiFi.localIP());

  digitalWrite(ledPin, LOW);	//连接WiFi后,灯光常亮,等待启动网络服务器
  
  /*-------------启动网络服务功能------------*/
  esp8266_server.begin();//启动网络服务器
  esp8266_server.on("/", HTTP_GET, handleRoot); //on函数的作用就是提供页面服务,告诉MCU通过那个函数访问这个界面“/”页面,就是主页,通过handleRoot函数处理该页面
  esp8266_server.onNotFound(handleNotFound);//当请求页面不存在时,通过该函数处理

  digitalWrite(ledPin, HIGH);	启动网络服务器后,灯光熄灭
}
void loop() {
    
    
  esp8266_server.handleClient();//处理HTTP服务器访问
}
void handleRoot()
{
    
    
  esp8266_server.send(200, "text/html", "Closed");

  digitalWrite(ledPin, LOW);
  servo.write(30);	// 关灯
  delay(1000);
  servo.write(100);	// 舵机复位
  digitalWrite(ledPin, HIGH);
}
void handleNotFound()//当浏览器访问页面不存在时,通过该函数处理
{
    
     /*服务器响应状态码404(未找到浏览器需要的信息),text/plain,表示告诉浏览器接下来要返送信息内容的是一段纯文本信息,信息内容就是404 Not found*/
  esp8266_server.send(404, "text/plain", "404 Not found");
}

After downloading the code to the development board, open the serial monitor and check the ip address after connecting to WiFi
insert image description here

Visit the webpage http://your ip address to control the steering gear

The serial port may conflict with the servo library. If the servo does not rotate normally, first remember the ip address printed by the serial port, and then comment out several Serial


4. Some records

1.ESP8266-12F NodeMCU

First I bought the ESP8266-12F chip, but I didn’t know how to use it, and then I bought the ESP8266-12F NodeMCU development board

The development board is still easy to use, just write to turn off the lights, it feels a bit overkill, I hope it can play a greater value in the future

ESP8266-12F chip https://blog.csdn.net/qlexcel/article/details/121435024

ESP8266-12F NodeMcu https://www.cnblogs.com/liming19680104/p/10977755.html

Taichi Maker http://www.taichi-maker.com/homepage/reference-index/arduino-hardware-refrence/nodemcu/


2. MG90S steering gear

The basics of using the steering gear https://blog.csdn.net/pang9998/article/details/103200666

Never turn the steering gear by hand , my first MG90 is broken like this

Do pay attention to the external voltage not too high, my second MG996R is also broken

It is recommended to buy a steering gear tester, which is only a few dollars, but it can quickly rule out whether the steering gear is faulty


3. Development board power supply

I have been using the battery box-4 Nanfu-usb interface to supply power to the development board. Recently, I found that the battery box is out of power.

There is no way but to use other power supply methods

First, connect the Vin pin of the development board with the battery box-3 sections No. 5-DuPont line bus , and found that there is no response. Baidu + guessed that the voltage and current are not enough to start the development board.

Finally, power bank-20000mA is used for power supply. After a certain measurement on a certain day, it consumes one grid of electricity in one and a half days


4. The steering gear is powered separately

Originally, the development board was used to supply power to the MG996R steering gear. Later, Baidu found that the development board might be burned out, and the steering gear needs to be powered separately, so I bought a 4.5V battery box

But the effect is not very ideal. Originally, the steering gear rotates very stably. After the external power supply is connected, the steering gear seems to be in poor contact. It occasionally moves and sometimes does not move, and it will shake at a certain angle. Because the voltage and current of the battery box are unstable or something

I bought a step-down and voltage regulator chip cheaply, but I don’t know how to use it. I’d better buy the module directly in the future.

So in the end I bought MG90S and didn’t dare to connect external batteries anymore.

Servo separate power supply wiring https://blog.csdn.net/qq_36792878/article/details/115490209


5.Arduino IDE

Installation configuration https://zhuanlan.zhihu.com/p/475982456


6. Pay attention

Servo library and pwm output conflict

The steering gear library function conflicts with the DC motor

The serial port conflicts with the servo library

Guess you like

Origin blog.csdn.net/tfnmdmx/article/details/125558884