当前位置: 代码迷 >> 综合 >> nodemcu web公告栏
  详细解决方案

nodemcu web公告栏

热度:68   发布时间:2023-09-22 19:49:12.0

材料准备:nodemcu + oled 12864 + dht11 + 导线若干(万能的淘宝下约30元!)
开发工具: Arduino ide
功能

  1. 局域网下查看传感器温湿度
  2. web端可输入oled屏幕显示内容
  3. oled屏幕显示温湿度、esp的IP和想显示的英文内容

效果图
上电初,稍稍等待wifi的连接
nodemcu web公告栏初始界面
nodemcu web公告栏web端
电脑或手机与esp连接同一wifi。在浏览器内输入oled屏幕的ip
nodemcu web公告栏输入如“I love you”,按submit提交
nodemcu web公告栏更加详细的代码和资料见:
https://github.com/callmebg/nodemcu_examples

#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
#include <Adafruit_ssd1306syp.h>
#include <CN_SSD1306.h>
#include <string.h>
#include <SimpleDHT.h>#define pinDHT11 4 //dht11 的2号脚与esp的4号角相连,板上标注为D2
#define SDA_PIN 14 //oled 上面的sda与esp的14号角相连,板上标注为D5
#define SCL_PIN 12 //oled 上面的scl与esp的12号角相连,板上标注为D6
#define STASSID "TP-LINK_465C3A" //wifi的名字
#define STAPSK "123456789" //wifi密码SimpleDHT11 dht11(pinDHT11);    //一个dht11的对象
ESP8266WebServer server(80);    //一个web对象
Adafruit_ssd1306syp display(SDA_PIN,SCL_PIN); //一个显示屏对象unsigned char localIp[5] = "....";  //esp的ip
String msg = "test";                //显示文字内容
byte temperature = 0;               //温度 ℃
byte humidity = 0;                  //湿度 %
int err = SimpleDHTErrSuccess;      //dht11 错误信息void change(uint32_t ip)  //翻译ip
{
    unsigned char* t = (unsigned char*)&ip;for(unsigned char i = 0; i < 4; i++){
    localIp[i] = t[i]; }
}
void homepage() 
{
    String content = "<html><head><title>hhh 我写出来了</title><meta charset='UTF-8'></head>";content += "<body><form action='/' method='GET'>项目地址:https://github.com/callmebg/nodemcu_examples<br>";content += "当前温度:" + String(temperature)+ "℃<br>";content += "当前湿度:" + String(humidity) + "%<br>";content += "输入想要显示的内容:<input type='text' name='words' placeholder='" + msg +"'><br>";content += "<input type='submit' name='SUBMIT' value='Submit'></form></body></html>";if (server.hasArg("words")) {
    Serial.println("words");msg = server.arg("words");}server.send(200, "text/html", content);Serial.println("用户访问了主页");
}
void ifnotfound() 
{
    server.send(404, "text/html", "<html><head><title>见鬼了</title><meta charset='UTF-8'></head></html>嘤嘤嘤!你访问的页面被外星人劫持了哦");Serial.println("用户访问了一个不存在的页面");
}void setup()
{
    Serial.begin(115200);Serial.println();//显示屏初始化display.initialize();display.setTextColor(WHITE); display.println("wait a minute...");display.update();//wifi配置WiFi.mode(WIFI_STA);WiFi.begin(STASSID, STAPSK);Serial.print("Connecting");while (WiFi.status() != WL_CONNECTED){
    delay(500);Serial.print(".");}Serial.println();Serial.print("Connected, IP address: ");Serial.print(WiFi.localIP());change(WiFi.localIP());//初始化WebServerserver.on("/", homepage); //主页server.onNotFound(ifnotfound);  //404server.begin();Serial.println("HTTP server started");}
void loop()
{
    unsigned char i;display.setCursor(0,0);display.setTextSize(1);//监听客户请求并处理server.handleClient();//读取温湿度if ((err = dht11.read(&temperature, &humidity, NULL)) != SimpleDHTErrSuccess) {
    Serial.print("Read DHT11 failed, err="); Serial.println(err);delay(1000);return;}Serial.print("Sample OK: ");Serial.print((int)temperature); Serial.print(" *C, "); Serial.print((int)humidity); Serial.println(" H");//画面display.clear();display.print("DHT11: ");display.print(temperature);display.print("*C ");display.print((int)humidity);display.println("%");display.print("IP: ");display.print(localIp[0]);display.print(".");display.print(localIp[1]);display.print(".");display.print(localIp[2]);display.print(".");display.println(localIp[3]);display.setTextSize(2);display.println(msg);display.update();delay(500);//2Hz的频率
}