基于esp8266+串口屏的桌面摆件

先给看看成品的样子,本意是出于装饰品,顺便学习8266
在这里插入图片描述
后来给做了个外壳在这里插入图片描述


#include <ArduinoJson.h>
#include <ESP8266WiFi.h>
#include <EEPROM.h>
#include <SoftwareSerial.h>
#include "F:\Arduino\libraries\hz.c"//根据路径创建文字库
#include <ESP8266HTTPClient.h>
#define LED 2
SoftwareSerial swSer(D7, D8, false, 256);//软串口
String payload = "";              //获取数据储存变量
String com, tem1, tem2, humi, temnow, sta, state1, state2, time0, time1, time2, english;
String webadd = "http://flash.weather.com.cn/wmaps/xml/hunan.xml";    //天气接口地址
String city_id = "101180101";//河南郑州
String time_url = "http://quan.suning.com/getSysTime.do";   //时间接口地址
String english_url = "http://open.iciba.com/dsapi/";   //英语接口地址
String daytime = "";
struct CONFIG {  //结构体存放账号密码
  char ssid[32];
  char password[32];
};
void smartConfig()
{
  WiFi.mode(WIFI_STA);
  Serial.println("\r\nWait for Smartconfig");
  WiFi.beginSmartConfig();
  while (1)
  {
    Serial.print(".");
    digitalWrite(LED, 0);
    sao_sao();
    delay(500);
    digitalWrite(LED, 1);
    delay(500);
    if (WiFi.smartConfigDone())
    {
       EEPROM.begin(512);
      CONFIG buf;
      Serial.println("SmartConfig Success");
      Serial.printf("SSID:%s\r\n", WiFi.SSID().c_str());
      Serial.printf("PSW:%s\r\n", WiFi.psk().c_str());
      strcpy(buf.ssid,  WiFi.SSID().c_str());
      strcpy(buf.password, WiFi.psk().c_str());
      EEPROM.put<CONFIG>(0, buf);
      EEPROM.commit();
      Serial.println(buf.ssid);
      Serial.println(buf.password);
      break;
    }
  }
}
void setup()
{
  Serial.begin(115200);
   swSer.begin(115200);
  sao_sao();
  pinMode(LED, OUTPUT);
  digitalWrite(LED, 0);
  setup_wifi();
  open_show();
}
void sao_sao()
{
 swSer.print("CLS(0);");
 swSer.print("BPIC(1,45,29,17);");
 swSer.print("DS16(36,8,'");swSer.print(hz[2]);swSer.print("',13,0);");
 swSer.print("\r\n");
}
void setup_wifi() {
  sao_sao();
 EEPROM.begin(512);
  CONFIG buf;
  EEPROM.get<CONFIG>(0, buf);
  Serial.println(buf.ssid);
  Serial.println(buf.password);
  EEPROM.commit();
  WiFi.begin(buf.ssid, buf.password);
  long lastMsg = millis();
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
    long now = millis();
    if (now - lastMsg > 10000) {
      smartConfig();  //微信智能配网
      break;
    }
  }
}
void open_show()
{
 swSer.print("CLS(0);");
  delay(200);
 swSer.print("CLS(0);"); //CLS(0);
 swSer.print("DS12(10,20,'");swSer.print(hz[0]);swSer.print("',4,0);"); //DS12(10,20,'晴天',4,0);
 swSer.print("CBOX(3,3,90,120,10,5);");
 swSer.print("CBOX(2,125,217,173,5,5);");
 swSer.print("PL(3,53,90,53,5);");
 swSer.print("CIR(150,60,50,1);");
 swSer.print("PIC(125,40,13);");
 swSer.print("\r\n");
  delay(200);
}
/*****************************************http数据获取*******************************************/
void http() {
  HTTPClient http;
  http.begin(webadd);
  int httpCode = http.GET();
  if (httpCode > 0) {
    payload = http.getString();
    //Serial.println(payload);
    int a = payload.indexOf(city_id);//自己搜索链接可知指定城市的id郑州市的id为‘101180101’
    //indexof();在字符串中中查找字符位置,返回值为给定字符的第一个位置,查找失败返回-1
    com = payload.substring(a - 280, a + 12);
    //字符串解析函数:substring(?,...),返回值为指定位置的字符串值
    //Serial.println(com);
    //<city cityX="376" cityY="184" cityname="郑州市" centername="郑州市" fontColor="FFFF00" pyName="" state1="0" state2="1" stateDetailed="晴转多云" tem1="37" tem2="24" temNow="33" windState="南风3-4级" windDir="西南风" windPower="2级" humidity="39%" time="11:00" url="101180101"/>
  }
  http.end();
}
/*********************************获取时间**************************************/
void get_time()
{
  HTTPClient http;
  http.begin(time_url);
  int httpCode = http.GET();
  if (httpCode > 0) {
    payload = http.getString();
    //Serial.println(payload);
    /************************************json 数据处理********************************************/
    DynamicJsonBuffer jsonBuffer;
    String  input =   payload;
    JsonObject& root = jsonBuffer.parseObject(input);
    String  daytime =  root[String("sysTime2")];
    time0 = daytime;
    time1 = daytime.substring(0, 10);//2018-06-17
    time2 =  daytime.substring(11, 19);//2018-06-17
  }
  http.end();
}
/*************************获取每日英语*******************/
void get_english()
{
  HTTPClient http;
  http.begin(english_url);
  int httpCode = http.GET();
  if (httpCode > 0) {
    payload = http.getString();
    //Serial.println(payload);
    /*******json 数据处理******/
    DynamicJsonBuffer jsonBuffer;
    String  input =   payload;
    JsonObject& root = jsonBuffer.parseObject(input);
    String  content =  root[String("content")];
    if(content.length()>100) english = content.substring(0, 100); //修复每日英语数据过长无法显示的问题。
    else english = content;
    //Serial.println(english);
  }
  http.end();
}
/**************************************天气判断**************************************************/
void tianqi()
{
  switch (state1.toInt()) {
    case 0:swSer.print("PIC(43,11,1);");swSer.print("DS12(10,20,'");swSer.print(tq[0]);swSer.println("',4,0);"); break; //晴天
    case 1:swSer.print("PIC(43,11,2);");swSer.print("DS12(10,20,'");swSer.print(tq[1]);swSer.println("',4,0);"); break;//多云
    case 2:swSer.print("PIC(43,11,3);");swSer.print("DS12(10,20,'");swSer.print(tq[2]);swSer.println("',4,0);"); break;//阴天
    case 3:swSer.print("PIC(43,11,4);");swSer.print("DS12(10,20,'");swSer.print(tq[4]);swSer.println("',4,0);"); break;//阵雨
    case 4:swSer.print("PIC(43,11,4);");swSer.print("DS12(10,20,'");swSer.print(tq[5]);swSer.println("',4,0);"); break;//雷阵雨
    case 7:swSer.print("PIC(43,11,4);");swSer.print("DS12(10,20,'");swSer.print(tq[3]);swSer.println("',4,0);"); break;//小雨
    case 8:swSer.print("PIC(43,11,4);");swSer.print("DS12(10,20,'");swSer.print(tq[6]);swSer.println("',4,0);"); break;//中雨
    case 9:swSer.print("PIC(43,11,4);");swSer.print("DS12(10,20,'");swSer.print(tq[7]);swSer.println("',4,0);"); break;//大雨
    case 5:swSer.print("PIC(43,11,5);");swSer.print("DS12(10,20,'");swSer.print(tq[8]);swSer.println("',4,0);"); break;//雪天
    case 6:swSer.print("PIC(43,11,5);");swSer.print("DS12(10,20,'");swSer.print(tq[8]);swSer.println("',4,0);"); break;//雪天
  }
  delay(200);
}
/*************************************截取字符数据处理*******************************************/
void data() {
  int i = 0;
  i = com.indexOf("tem1");
  tem1 = com.substring(i + 6, i + 9);
  i = 0;
  i = com.indexOf("tem2");  //tem2="24"
  tem2 = com.substring(i + 6, i + 9);
  i = 0;
  i = com.indexOf("temNow");
  temnow = com.substring(i + 8, i + 11);
  i = 0;
  i = com.indexOf("humidity"); // humidity="88%" time="22:00"
  humi = com.substring(i + 10, i + 13); //humidity="27%"
  i = 0;
  i = com.indexOf("state1");
  state1 = com.substring(i + 8, i + 9);
  i = 0;
  i = com.indexOf("state2");
  state2 = com.substring(i + 7, i + 11);
  i = 0;
  com = "";
}
/*********************************************屏幕显示**************************************************/
void show()
{
 swSer.print("DS12(7,60,'");swSer.print(hz[6]);swSer.print(humi);swSer.print("',13);");
 swSer.print("DS12(7,75,'");swSer.print(hz[3]);swSer.print(tem1);swSer.print("c',3);"); //DS12(7,65,'最高温度:',1);
 swSer.print("DS12(7,90,'");swSer.print(hz[4]);swSer.print(temnow);swSer.print("c',3);"); //DS12(7,85,'当前温度:',3);
 swSer.print("DS12(7,105,'");swSer.print(hz[5]);swSer.print(tem2);swSer.print("c',3);"); //DS12(7,105,'最低温度:',13);
 swSer.print("DS12(100,109,'");swSer.print(time0);swSer.println("',1);"); //BS12(8,140,216,4,'日期时间',1);
  delay(200);
 swSer.print("BS12(8,130,210,2,'");swSer.print("  ");swSer.println("',13);");//局部刷新--原创000
  //经过我的研究发现英文中不能出现“'”这个符号,例如:that's就不行,所以将其替换为“.”
  english.replace("'", ".");
 swSer.print("BS12(8,130,210,1,'");swSer.print(english);swSer.println("',13);");
  delay(300);
}
void loop()
{
  digitalWrite(LED, 1);
 swSer.println("PIC(141,53,10);");
  get_time();
  get_english();
  http();
 swSer.println("PIC(135,48,11);");
  data();
 swSer.println("PIC(132,45,12);");
  show();
  tianqi();
  digitalWrite(LED, 0);
 swSer.println("PIC(125,40,13);");
  delay(1000);
}
1. 硬件准备: - ESP8266开发板 - DHT11温湿度传感器 - 蜂鸣器 - 杜邦线 - 面包板 2. 软件准备: - Arduino IDE - ESP8266库 - DHT库 3. 接线: 将DHT11的VCC接到ESP8266的3.3V,将DHT11的GND接到ESP8266的GND,将DHT11的DATA接到ESP8266的D1口,将蜂鸣器的正极接到ESP8266的D2口,将蜂鸣器的负极接到ESP8266的GND。 4. 代码编写: ``` #include <ESP8266WiFi.h> #include <DHT.h> #define DHTPIN D1 // DHT11 DATA引脚连接ESP8266的D1口 #define DHTTYPE DHT11 // DHT11传感器类型 #define buzzerPin D2 // 蜂鸣器连接ESP8266的D2口 const char* ssid = "你的WiFi名称"; // WiFi名称 const char* password = "你的WiFi密码"; // WiFi密码 DHT dht(DHTPIN, DHTTYPE); void setup() { Serial.begin(9600); pinMode(buzzerPin, OUTPUT); digitalWrite(buzzerPin, HIGH); delay(1000); digitalWrite(buzzerPin, LOW); delay(1000); WiFi.begin(ssid, password); while (WiFi.status() != WL_CONNECTED) { delay(1000); Serial.println("Connecting to WiFi..."); } Serial.println("Connected to WiFi"); } void loop() { delay(2000); float h = dht.readHumidity(); // 读取湿度 float t = dht.readTemperature(); // 读取温度 if (isnan(h) || isnan(t)) { Serial.println("Failed to read from DHT sensor!"); return; } Serial.print("Humidity: "); Serial.print(h); Serial.print("% Temperature: "); Serial.print(t); Serial.println("°C"); if (t > 28) { // 温度超过28°C时触发报警 digitalWrite(buzzerPin, HIGH); delay(1000); digitalWrite(buzzerPin, LOW); delay(1000); } } ``` 5. 上传代码: 将ESP8266通过USB连接到计算机,打开Arduino IDE,选择正确的开发板和端口,将代码上传到ESP8266中。 6. 测试: 将DHT11放置在需要检测的环境中,开启串口监视器,可以看到ESP8266每隔2秒钟会读取一次温湿度数据并打印到串口中,当温度超过28°C时,蜂鸣器会响起一次,表示触发了报警。
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值