Hardware preparation: esp8266;
SG90 servo;
A microusb data cable (can transmit data);
The above can be purchased on Taobao.
Software preparation: Arduino IDE
Dian Deng Technology (mobile version) https://apps.apple.com/cn/app/Dian Deng-blinker/id1498805902
Servo wiring: brown is the ground wire GND
Red is the power cord
Yellow is the signal line
GED is connected to the G of the microcontroller
The power cord is connected to the 3V of the microcontroller
The signal line is connected to D4 of the microcontroller
Open Arduino and click on File Preferences
Will http://arduino.esp8266.com/stable/package_esp8266com_index.json
Copy to the add-on development manager URL
Click OK
Click Project-Load Library-Manage Library
Search for the microcontroller esp8266 we need to use and install the latest version
Click Tools-Development version and select ESP8266 Board (3.0.1)-Generic ESP8266 Module
The code is written by combining some open source projects and adding modifications by yourself. The code link and specific content are as follows. You can download and use it yourself.
Codehttps://share.weiyun.com/ySWJgAi0
#define BLINKER_WIFI #define BLINKER_MIOT_LIGHT #include <Blinker.h> #include <Servo.h> Servo myservo; //Define servo char auth[] = "bc6196889229"; //Light up Key char ssid[] = "GG"; //wifi name char pswd[] = "7777777105"; //wifi password //Create a new component object BlinkerButton Button1("test"); int counter = 0; void miotPowerState(const String & amp; state)//Power operation { BLINKER_LOG("need set power state: ", state); if (state == BLINKER_CMD_ON) { myservo.write(135);//After receiving the "on" command, the servo rotates 110 degrees BlinkerMIOT.powerState("on"); BlinkerMIOT.print();//Feedback status delay(500);//delay 0.5 seconds myservo.write(90);//Reset the servo to zero and return to the vertical state } else if (state == BLINKER_CMD_OFF) { myservo.write(45); //The steering gear deflects 70° BlinkerMIOT.powerState("off"); BlinkerMIOT.print(); delay(500); myservo.write(90); } } // This function will be executed when the button is pressed void button1_callback(const String & amp; state) { BLINKER_LOG("get button state: ", state); if (state=="on") { myservo.write(150);//After receiving the "on" command, the servo rotates 150 delay(500);//delay myservo.write(90);//Reset the servo to zero vertically } else if(state=="press"||state=="tap") { myservo.write(30);//Long press the switch button and the servo rotates 30 delay(500);//delay myservo.write(90);//Reset the servo to zero vertically } } // If an unbound component is triggered, its content will be executed void dataRead(const String & data) { BLINKER_LOG("Blinker readString: ", data); counter + + ; } void setup() { //Initialize serial port Serial.begin(115200); BLINKER_DEBUG.stream(Serial); //Initialize the servo myservo.attach(2); //The IO port of the servo, the D4 port of nodemcu myservo.write(90);//The servo returns to zero and vertical when powered on BlinkerMIOT.attachPowerState(miotPowerState); //Initialize blinker Blinker.begin(auth, ssid, pswd); Blinker.attachData(dataRead); Button1.attach(button1_callback); } void loop() { Blinker.run(); } Autumn: http://arduino.esp8266.com/stable/package_esp8266com_index.json
Download Dian Deng Technology APP
1. Select the independent device
2. Copy the generated key. Use in the code.
3. Return to the device and click the three dots in the upper right corner
4. Select the interface configuration
5. Copy the following content into the box and click Update Configuration
{¨config¨{¨headerColor¨¨transparent¨¨headerStyle¨¨dark¨¨background¨{¨img¨¨assets/img/headerbg.jpg¨¨isFull¨?}}¨dashboard¨|{¨type¨¨btn¨ ¨ico¨¨fad fa-lightbulb-on¨¨mode¨ê¨t0¨¨turn on the light¨¨t1¨¨text2¨¨bg¨é¨cols¨í¨rows¨í¨key¨¨test¨′x′ ?′y′ì¨speech¨|÷¨cus¨¨on¨¨lstyle¨?¨clr¨¨#595959¨}{?A?B?C¨fad fa-lightbulb¨?Eé?F¨Turn off the lights¨? H?I?Jé?Kí?Lí?M?N′x′?′y′D?O|÷?RS?T}{?A¨deb¨?Eé?Jé?KLì?M ¨debug¨′x′é′y′¤D}÷¨actions¨||¨cmd¨|¨switch¨?¨text¨Q¨open?name¨¨off¨¨off?name¨-÷¨triggers ¨ | .png” src=”//i2.wp.com/img-blog.csdnimg.cn/97b9aaa9b3a9414f82b30a2c1a56e10e.png”>
Open Arduino and import the shared code
Replace the copied key into the first line of quotes
Replace the second and third lines with the name and password of your mobile hotspot
Then click compile
The following situation indicates that the compilation is successful and the code has no errors
Finally, burn the program into the microcontroller
Note: When burning the program to the microcontroller, the servo cannot be connected to the microcontroller, otherwise the programming will not be successful.
Note: Select the 2.4GHz frequency band instead of the 5GHz frequency band for the mobile hotspot AP band, otherwise the ESP8266 will not be able to connect to the mobile hotspot network.
The method demonstrated in this article is more suitable for students who just want to learn about the Internet of Things. If you want to learn more about it, it is recommended to start with the use of C language and Arduino, as well as the mechanism and pin usage of IoT microcontrollers such as ESP8266 and ESP32. Nowadays, there are many mature electronic modules on the market for us to use, which greatly reduces our learning costs. We can also use MiSiQi or Arduino graphical programming to complete our projects, further reducing the tedious time of entering codes.
Finally
I only regard the Internet of Things microcontroller as an interest and will not study it in depth. I only use this blog to record my learning, and also provide my own experience for students who want to understand the Internet of Things. I hope every electronics enthusiast can walk on the road of love.