<span>esp8266</span>
is primarily used for IoT development and can work with various sensors to meet our needs. In this article, let’s explore the world of<span>esp8266</span>
together!
ESP8266 Development Routes
Route One: DIY Approach
You need to write the relevant sensor code yourself and solve various dependency issues. You need to purchase a public network server and solve internal network penetration issues. It is time-consuming and labor-intensive with low work efficiency.
Route Two: User-Friendly Approach
With just a smartphone, we can connect our devices using platforms like <span>blinker</span>
. The official code library is quite rich, and there are many developers. There is no need to purchase a public network server, and it is convenient to integrate with various voice assistants on domestic mobile platforms, such as Xiao Ai and Tmall Genie.
Preparation
🌽 Hardware Preparation
-
esp8266
(a few dollars each, including shipping,required
) -
Dupont wires ( required
) -
Sensors, motors, servos, etc. (purchase according to your actual situation)
🥑 Software Preparation
-
Arduino IDE -
Blinker APP (for operating IoT devices)
Environment Configuration
Run the development tool Arduino IDE
, click File
– Preferences
– Board Manager URL
and fill in the following domain: https://arduino.me/packages/esp8266.json (you can add or reduce according to your actual situation later).
Then click Tools
– Select Other Boards
and search according to your board situation, such as my board is 8266
.
After selecting the corresponding board, automatically download or manually download the corresponding library (it is recommended to download manually; if the network is not good, use a proxy)
Load the Blinker Library
Since we are developing based on Blinker
, we need to download its library. Download link (https://diandeng.tech/dev) After downloading, use the Arduino IDE Menu
>Project
>Load Library
>Add .ZIP Library
to import into the library, as shown:
At this point, the development environment configuration is complete.
Hello World
🍧01 Add Device in App, Get Secret Key
Open the App, click the “+
” icon in the upper right corner, then select Add Device, choose Arduino
> WiFi
access and copy the requested Secret Key
🍭02 Compile and Upload Example Program
Open Arduino IDE, go to File
>Examples
>Blinker
>Blinker_Hello/Hello_WiFi
to open the example. In the program, find the following variables and fill in the Secret Key (auth) you obtained and the WiFi hotspot name (ssid), password (pswd)
char auth[] = "abcdefghijkl"; // The Secret Key obtained in the app in the previous step
char ssid[] = "Your WiFi Hotspot Name"; // Your WiFi hotspot name
char pswd[] = "Your WiFi Password"; // Your WiFi password
Once configured, compile and upload first.
Then you can see the device online in the mobile APP.Of course, the name and icon can be changed.
🚒03 DIY Interface
On the device list page, click the device icon to enter the device control panel. The first time entering the device control panel, a wizard page will pop up. Click Load Example on the wizard page to load the example components.
Practical Project: Remote Power On/Off with ESP8266
First, let’s take a look at the final effect.
🛳 Preparation
-
ESP8266 development board -
Dupont wires -
Voltmeter (optional) -
Power bank (optional)
🚂 Usage Method
Compile the following code into the firmware. Connect one end of the two wires to the GND
(negative) of the development board and the other end to the mainboard’s power port.
#define BLINKER_WIFI
#define BLINKER_MIOT_OUTLET
#include <Blinker.h>
char auth[] = "5fdb51bc1674";
char ssid[] = "PDCN";
char pswd[] = "1234567890";
int GPIO = 0; // Define pin as gpio0
// Create button object with the same name as the button in your DIY page, which is kali
BlinkerButton Button1("kali");
int counter = 0;
// DIY button code. If you only want to use Xiao Ai, you can delete this directly.
void button1_callback(const String & state)
{
// Power on event
BLINKER_LOG("get button state: ", state);
if (state == BLINKER_CMD_BUTTON_TAP) { // Respond to short press
if (digitalRead(LED_BUILTIN) == HIGH) { // IF high level state
digitalWrite(LED_BUILTIN, LOW); // Light off
digitalWrite(GPIO, HIGH); // Turn off high level
BLINKER_LOG("Button tap!");
delay(300); // Define 3s
digitalWrite(LED_BUILTIN, HIGH); // Restart high level
digitalWrite(GPIO, LOW); // High level input light on
Button1.color("#FFC800");
Button1.text("Running");
Button1.print();
}
}
// Power off event
else if (state == BLINKER_CMD_BUTTON_PRESSUP) { // Respond to long press, initialize state as power off
if (digitalRead(LED_BUILTIN) == LOW) { // If the computer is on (computer started via APP)
digitalWrite(LED_BUILTIN, LOW); // Relay connected
BLINKER_LOG("Button pressed!");
Blinker.delay(5000);
digitalWrite(LED_BUILTIN, HIGH); // eps-01s light off
digitalWrite(GPIO, LOW); // After 5 seconds, the relay disconnects, equivalent to pressing the computer power button for 5 seconds
Button1.color("#CCCCCC");
Button1.text("Initializing");
Button1.print();
}
}
}
// Button event ends
// If an unbound component is triggered, the content inside will be executed
void dataRead(const String & data)
{
BLINKER_LOG("Blinker readString: ", data);
counter++;
Number1.print(counter);
}
// Introduce Xiao Ai
void miotPowerState(const String & state)
{
BLINKER_LOG("need set power state: ", state);
if (state == BLINKER_CMD_ON) {
digitalWrite(LED_BUILTIN, LOW);
BlinkerMIOT.powerState("off");
delay(500);
digitalWrite(LED_BUILTIN, HIGH);
BlinkerMIOT.powerState("on");
BlinkerMIOT.print();
}
else if (state == BLINKER_CMD_OFF) {
digitalWrite(LED_BUILTIN, LOW);
BlinkerMIOT.powerState("off");
BlinkerMIOT.print();
}
}
void setup()
{
// Initialize serial port
Serial.begin(115200);
BLINKER_DEBUG.stream(Serial);
BLINKER_DEBUG.debugAll();
// Initialize IO with LED
pinMode(LED_BUILTIN, OUTPUT);
digitalWrite(LED_BUILTIN, HIGH);
// Initialize blinker
Blinker.begin(auth, ssid, pswd);
Blinker.attachData(dataRead);
Button1.attach(button1_callback); // Initialize button parameters
BlinkerMIOT.attachPowerState(miotPowerState);// Initialize Xiao Ai parameters
}
void loop() {
Blinker.run();
}
🕹 Core Code Explanation
digitalWrite(LED_BUILTIN, LOW);
BlinkerMIOT.powerState("off");
delay(500);
digitalWrite(LED_BUILTIN, HIGH);
BlinkerMIOT.powerState("on");
When the computer is powered on, the two wires need to be disconnected. Therefore, I added a time parameter delay(500);
to power the device first, and then disconnect it after 500ms.
🍯 Challenges Faced
Because the computer will also lose power after shutting down, providing power to the esp8266 is a big issue. Therefore, you need to use a voltmeter to measure which pin has a voltage above 3V when the computer is off. Then use the motherboard to power the esp8266. However, my motherboard is twenty years old, and after a series of operations, I found that only one pin had a voltage of 0.8V. In the absence of a solution, I directly powered the esp8266 using the USB port of the optical modem. That should solve the problem!
🎃 Integrate with Xiao Ai
-
1. Open the Mi Home App. Go to
My
>Other Platform Devices
>Click Add
>DianDeng Technology
> Bind the account, bind theblinker
account -
2. After successful binding, the blinker devices that support Xiao Ai control will appear in My>Other Platform Devices>DianDeng Technology device list
-
3. Now you can use Xiao Ai to control this device
For more exciting articles, please follow us