Управление светодиодом с использованием Lolin NodeMCU и Elbear ace-uno. Пошаговое руководство

от автора

Результат работы

Результат работы

Здравствуйте, в этой статье мы рассмотрим, как создать простой веб-сервер на основе Lolin NodeMCU с использованием микроконтроллера ESP8266. Этот проект позволит вам управлять светодиодом и отправлять команды через веб-интерфейс.

Основные компоненты проекта

  1. Lolin NodeMCU — это плата на основе ESP8266, которая поддерживает Wi-Fi и проста в использовании.

  2. Светодиод — для управления.

  3. Библиотеки — для работы с Wi-Fi и веб-сервером необходимо подключить библиотеки ESP8266WiFi.h и ESP8266WebServer.h.

Подключение:

  1. Lolin NodeMCU RX – TX Elbear ace-uno

  2. Lolin NodeMCU TX – RX Elbear ace-uno

Настройка сети

Первым шагом является настройка сети. В нашем коде мы задаем параметры Wi-Fi, такие как SSID и пароль, а также статический IP-адрес для устройства. Это позволит избежать проблем с изменением IP-адреса при перезагрузке устройства.

const char* ssid = "имя"; const char* password = "пароль"; IPAddress local_IP(192, 168, 1, 3);  // Установите желаемый статический IP IPAddress gateway(192, 168, 1, 1);      // Шлюз (обычно IP вашего маршрутизатора) IPAddress subnet(255, 255, 255, 0);     // Подсеть

Настройка веб-сервера

После настройки сети мы создаем веб-сервер, который будет слушать запросы на порту 80. Мы регистрируем маршруты для обработки команд, отправленных через веб-интерфейс.

ESP8266WebServer server(80); void setup() {     Serial.begin(9600); // Настройка последовательного порта для связи     WiFi.mode(WIFI_AP_STA); // Установка режима AP и станции     WiFi.config(local_IP, gateway, subnet); // Настройка статического IP     WiFi.begin(ssid, password); // Подключение к Wi-Fi сети     // Проверка подключения к Wi-Fi     while (WiFi.status() != WL_CONNECTED) {         delay(1000);     }     // Создание точки доступа     WiFi.softAP("ESP8266_AP", "password"); // Создание точки доступа с SSID и паролем     // Регистрация маршрутов     server.on("/", handleRoot);     server.on("/sendCommand", handleSendCommand);     server.on("/sendSerialInput", handleSendSerialInput);     server.begin(); }

Обработка команд

Веб-интерфейс позволяет пользователю отправлять команды для управления устройством. Например, команды `LED_ON` и `LED_OFF` включают и выключают встроенный светодиод. Каждая команда обрабатывается в функции `processCommand`, где также ведется журнал последних команд.

void processCommand(String command) {     Serial.println("Processing command: " + command); // Отладочное сообщение     Serial.print(command); // Отправка команды на другое устройство     if (command == "LED_ON") {         digitalWrite(LED_BUILTIN, HIGH); // Включение светодиода         Serial.println("LED is ON"); // Сообщение о подтверждении         storeCommand("LED ON");     } else if (command == "LED_OFF") {         digitalWrite(LED_BUILTIN, LOW); // Выключение светодиода         Serial.println("LED is OFF"); // Сообщение о подтверждении         storeCommand("LED OFF");     } else {         Serial.println("Unknown command received: " + command); // Сообщение о неизвестной команде     } }

Веб-интерфейс

В функции `handleRoot` мы создаем HTML-страницу, на которой пользователи могут вводить команды и видеть последние действия. Это делает управление устройством интуитивно понятным и доступным.

void handleRoot() {     String html = "<html><body><h1>Отправка команды на Elbear ACE-UNO</h1>";     html += "<form action=\"/sendCommand\" method=\"POST\">";     html += "<input type=\"text\" name=\"command\" placeholder=\"Введите команду\">";     html += "<input type=\"submit\" value=\"Отправить команду\">";     html += "</form>";     // Отображение последних 5 команд     html += "<h2>Последние команды:</h2><ul>";     for (int i = 0; i < 5; i++) {         if (lastCommands[i] != "") {             html += "<li>" + lastCommands[i] + "</li>";         }     }     html += "</ul>";     // Отправка HTML-кода обратно клиенту     server.send(200, "text/html", html); }

Заключение

Полный код Lolin NodeMCU

#include <ESP8266WiFi.h> #include <ESP8266WebServer.h>  const char* ssid = "имя"; const char* password = "пароль";  // Static IP and network settings IPAddress local_IP(192, 168, 1, 3);  // Set your desired static IP IPAddress gateway(192, 168, 1, 1);      // Gateway (usually the IP of your router) IPAddress subnet(255, 255, 255, 0);     // Subnet  ESP8266WebServer server(80); String lastCommands[5]; // Array to store the last 5 commands int commandIndex = 0;   // Index for adding new commands String serialOutput;     // String to store serial output  void setup() {   Serial.begin(9600);   // Set up the serial port for communication with Elbear ACE-UNO    // Set up Wi-Fi in client mode   WiFi.mode(WIFI_AP_STA); // Set to AP and station mode   WiFi.config(local_IP, gateway, subnet); // Set static IP   WiFi.begin(ssid, password); // Connect to Wi-Fi network    // Check for Wi-Fi connection   while (WiFi.status() != WL_CONNECTED) {     delay(1000);   }    // Set up access point   WiFi.softAP("ESP8266_AP", "password"); // Create access point with SSID and password    // Register routes   server.on("/", handleRoot);   server.on("/sendCommand", handleSendCommand); // Route to send custom command   server.on("/sendSerialInput", handleSendSerialInput); // Route to send serial input   server.begin(); }  void loop() { server.handleClient(); }  void processCommand(String command) {   Serial.println("Processing command: " + command); // Debug message for command processing    // Send command to the other device via Serial   Serial.print(command); // Send the command to the other device   Serial.println(); // Add newline to separate commands    if (command == "LED_ON") {     digitalWrite(LED_BUILTIN, HIGH); // Turn on LED     Serial.println("LED is ON"); // Confirmation message     storeCommand("LED ON");   } else if (command == "LED_OFF") {     digitalWrite(LED_BUILTIN, LOW); // Turn off LED     Serial.println("LED is OFF"); // Confirmation message     storeCommand("LED OFF");   } else {     Serial.println("Unknown command received: " + command); // Unknown command message   } }  void handleRoot() {   String html = "<html><body><h1>Send Custom Command to Elbear ACE-UNO</h1>";      // Form to send custom command   html += "<form action=\"/sendCommand\" method=\"POST\">";   html += "<input type=\"text\" name=\"command\" placeholder=\"Enter command\">";   html += "<input type=\"submit\" value=\"Send Command\">";   html += "</form>";    // Input for serialInput   html += "<h2>Serial Input:</h2>";   html += "<form action=\"/sendSerialInput\" method=\"POST\">";   html += "<input type=\"text\" name=\"serialInput\" placeholder=\"Enter serial command\">";   html += "<input type=\"submit\" value=\"Send Serial Command\">";   html += "</form>";    // Display the last 5 commands   html += "<h2>Last Commands:</h2><ul>";   for (int i = 0; i < 5; i++) {     if (lastCommands[i] != "") { // Check if there is a command       html += "<li>" + lastCommands[i] + "</li>";     }   }   html += "</ul>";    // Display serial output   html += "<h2>Serial Output:</h2><div>" + serialOutput + "</div>";    // Display connected devices   html += "<h2>Connected Devices:</h2><ul>";   int numDevices = WiFi.softAPgetStationNum(); // Get the number of connected devices   html += "<li>Number of connected devices: " + String(numDevices) + "</li>";      // Get information about connected devices   struct station_info *stationList = wifi_softap_get_station_info();   while (stationList != NULL) {     html += "<li>Device MAC: " + String(macToString(stationList->bssid)) + "</li>";     stationList = STAILQ_NEXT(stationList, next);   }   html += "</ul></body></html>";      server.send(200, "text/html", html); }  void handleSendCommand() {   if (server.hasArg("command")) {     String command = server.arg("command"); // Get the command from the form     command.trim(); // Remove extra spaces     Serial.println("Command from web: " + command); // Debug message     serialOutput += "Command from web: " + command + "<br>"; // Add to serial output for display     processCommand(command); // Process the command     server.send(200, "text/html", "<html><body><h1>Command sent: " + command + "</h1><a href=\"/\">Go back</a></body></html>");   } else {     server.send(400, "text/plain", "No command provided");   } }  void handleSendSerialInput() {   if (server.hasArg("serialInput")) {     String serialInput = server.arg("serialInput"); // Get the serial input from the form     serialInput.trim(); // Remove extra spaces     Serial.println("Serial Input from web: " + serialInput); // Send serial input to Serial     processCommand(serialInput); // Process the command     server.send(200, "text/html", "<html><body><h1>Serial command sent: " + serialInput + "</h1><a href=\"/\">Go back</a></body></html>");   } else {     server.send(400, "text/plain", "No serial command provided");   } }  void storeCommand(String command) {   lastCommands[commandIndex] = command; // Store command in array   commandIndex = (commandIndex + 1) % 5; // Increment index, reset at 5 }  // Function to convert MAC address to string String macToString(uint8_t *mac) {   String macStr = "";   for (int i = 0; i < 6; i++) {     macStr += String(mac[i], HEX);     if (i < 5) macStr += ":";   }   return macStr; } 

Полный код Elbear Ace-uno

void setup() {   pinMode(LED_BUILTIN, OUTPUT); // Настройка пина как выход   Serial.begin(9600); // Настройка последовательного порта   Serial.println("Ready to receive commands..."); // Сообщение о готовности }  void loop() {   if (Serial.available()) {     String command = Serial.readStringUntil('\n'); // Чтение команды     command.trim(); // Удаление лишних пробелов     Serial.println("Received command: " + command); // Отладочное сообщение     if (command == "LED_ON") {       digitalWrite(LED_BUILTIN, HIGH); // Включение светодиода       Serial.println("LED is ON"); // Подтверждение включения     } else if (command == "LED_OFF") {       digitalWrite(LED_BUILTIN, LOW); // Выключение светодиода       Serial.println("LED is OFF"); // Подтверждение выключения     } else {       Serial.println("Unknown command"); // Сообщение о неизвестной команде     }   } } 

Если интересно, подписывайтесь на мой ТГ


ссылка на оригинал статьи https://habr.com/ru/articles/875960/