ESP8266 制作一个网页控制程序

    ​手上的这块MCU为ESP8266,在arduino中进行开发需要安装对应的支持包,这里up选择nodemcu 1.0 ESP-12E版本。

    ​

一、核心硬件清单

首先确认需要准备的硬件,避免接线时遗漏:

  • ESP8266 开发板(如 NodeMCU、D1 mini,引脚定义需与代码一致)

  • SSD1306 128×32 OLED 显示屏(I2C 接口)

  • 舵机(SG90 等常见型号,支持 500-2500μs 脉冲控制)

  • 按键(1 个,用于触发舵机往复转动)

  • LED(1 个,需串联 220Ω 限流电阻,防止烧毁)

  • 杜邦线(公对母 / 公对公若干)

  • 电源(ESP8266 建议 5V/1A 供电,舵机若电流较大需单独 5V 供电)

 

代码:

#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <Servo.h>
#include <WiFiManager.h>
#include <ESP8266WebServer.h>

#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 32 // OLED display height, in pixels

// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
#define OLED_RESET     -1 // Reset pin # (or -1 if sharing Arduino reset pin)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);

Servo myservo;  // create servo object to control a servo

const int buttonPin = D5;    // the number of the pushbutton pin
int buttonState = 0;         // variable for reading the pushbutton status

const int togglePin = D0;    // the number of the toggle pin
bool toggleState = false;    // variable for storing the toggle state

const int ledPin = D7;       // LED 连接的引脚

ESP8266WebServer server(80);

void setup() {
  Serial.begin(115200);

  // Initialize the OLED display
  if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
    Serial.println(F("SSD1306 allocation failed"));
    for (;;); // Don't proceed, loop forever
  }
  display.clearDisplay();
  display.setTextSize(1);      // Normal 1:1 pixel scale
  display.setTextColor(SSD1306_WHITE); // Draw white text
  display.setCursor(0, 0);     // Start at top-left corner
  display.println("Initializing...");
  display.display();

  // WiFi 配置
  WiFiManager wifiManager;
  if (!wifiManager.autoConnect("ESP8266_AP")) {
    Serial.println("Failed to connect and hit timeout");
    // 可以在这里添加更多错误处理逻辑
    delay(3000);
    // 重置并尝试重新连接
    ESP.reset();
    delay(5000);
  }

  Serial.println("Connected to WiFi");
  display.clearDisplay();
  display.setCursor(0, 0);
  display.println("Connected to WiFi");
  display.print("IP: ");
  display.println(WiFi.localIP());
  display.display();

  // Attach the servo on pin D6 to the servo object
  myservo.attach(D6, 500, 2500); // Ad
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值