
ESP8266温湿度检测以及烟雾报警
原创大约 18 分钟
ESP8266温湿度检测以及烟雾报警
介绍
学校pcb设计的第三个设计,使用arduinoIDE为ESP8266编写代码并烧录,服务器使用RockyLinux系统,安装MySQL以及EMQX服务,
ESP 8266 运行的代码使用 arduino 作为 IDE,分为以下几个部分
ESP 8266 的设置和循环
// #define BLINKER_WIFI
// #define BLINKER_MIOT_SENSOR
#include <U8g2lib.h>
#include <Wire.h>
#include <ESP8266WiFi.h>
#include <PubSubClient.h>
#include <ArduinoJson.h>
#include <NTPClient.h>
#include <WiFiUdp.h>
#include <SimpleDHT.h>
#include <WiFiManager.h>
// #include <Blinker.h>
#define SCL 13
#define SDA 12
#define MQ2_ANALOG_PIN A0 // ESP8266 的 ADC
#define MQ2_DIGITAL_PIN 16 // ESP8266 的 GPIO16 (D0)
//dht11相关设置
int pinDHT11 = 14;
SimpleDHT11 dht11(pinDHT11);
U8G2_SSD1306_128X64_NONAME_F_SW_I2C u8g2(U8G2_R0, /*clock=*/SCL, /*data=*/SDA, /*reset=*/U8X8_PIN_NONE); // All Boards without Reset of the Display
#define SEND_BUFFER_DISPLAY_MS(ms)\
do {\
u8g2.sendBuffer(); \
delay(ms);\
}while(0);
// WiFi和MQTT参数
// const char* ssid = "Argo";
// const char* password = ".qwertyuiop";
// const char* auth = "fce1117f88d6";
const char* mqtt_server = "";
const char* mqtt_user = ""; // 更改为您提供的用户名
const char* mqtt_password = "";
const char* mqtt_topic = "";
const char* host = "api.seniverse.com"; // 将要连接的天气服务器地址
const int httpPort = 80; // 将要连接的天气服务器端口
// 心知天气HTTP请求所需信息
String reqUserKey = ""; // 私钥
String reqLocation = "duyun"; // 城市
String reqUnit = "c";
WiFiClient espClient;
WiFiUDP ntpUDP;
PubSubClient client(espClient);
NTPClient timeClient(ntpUDP, "ntp1.aliyun.com",60*60*8, 30*60*1000);
//使用中值滤波来消除偶然的1024值
int midAnalogValue[5];
int midDigitalValue[5];
//重连服务器一定次数后使用离线版本
int connectServerCount=0;
//防止每次离线版本都对mq2进行加热
int heatingMq2=0;
//离线运行一定次数后重连
int retryConnect;
//在线运行一定次数后显示当前的时间增加拓展性
int showTimeCtl;
//温度
int results_0_now_temperature_int;
//天气状态
int results_0_now_code_int;
String results_0_now_text_str;
// BlinkerNumber HUM("hum");
// BlinkerNumber TEMP("temp");
byte temperature = 0;
byte humidity = 0;
int bufTemp=0;
int bufHum=0;
void setup()
{
WiFi.mode(WIFI_STA); // 设置模式,esp默认为STA+AP
Serial.begin(115200);
initOLED();
WiFiManager wm; //定义manager对象
helpMessage();
bool res = wm.autoConnect("HelpMeToConnectWiFi","12345678"); //定义ap名称和密码
//WiFi.begin(ssid, password);
helpMessage();
int tryWifi=0;
while (!res) {
tryWifi++;
wifiText();
if(tryWifi>100){
retryConnect=30;
wifiConnectFailed();
offlineVersion();
tryWifi=0;
}
}
loadWifi();
wifiTextEnd();
loadText();
loadIcon();
pinMode(MQ2_DIGITAL_PIN, INPUT);
// 设置MQTT服务器和回调函数
client.setServer(mqtt_server, 1883);
timeClient.begin();
// BLINKER_DEBUG.stream(Serial);
// Blinker.begin(auth, ssid, password);
// Blinker.attachData(dataRead);
// BlinkerMIOT.attachQuery(miotQuery);
}
void loop()
{
if (!client.connected()) {
reconnect();
}
onlineVersion();
}
void reconnect() {
// 循环连接MQTT到一定次数后离线
int reconnectStatus=0;
while (!client.connected()) {
Serial.print("MQTT Server connection...");
if (client.connect("ESP8266Client", mqtt_user, mqtt_password)) {
Serial.println("connected");
} else {
reconnectStatus++;
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 2 seconds");
reconnecMqttServer();
}
if(reconnectStatus > 10){
retryConnect=30;
serverError();
offlineVersion();
reconnectStatus=0;
}
}
}
动画部分
// width: 118, height: 11
const unsigned char load1[] U8X8_PROGMEM = { 0x00,0xf8,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x01,0x00,0x00,0xfc,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x03,0x00,0x00,0x0e,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x07,0x00,0x00,0x06,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x06,0x00,0x00,0x06,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x06,0x00,0x00,0x06,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x06,0x00,0x00,0x06,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x06,0x00,0x00,0xfe,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x07,0x00,0x00,0xfc,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x03,0x00,0x00,0xf0,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 };
const unsigned char load2[] U8X8_PROGMEM = { 0x00,0xf8,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x01,0x00,0x00,0xfc,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x03,0x00,0x00,0xfe,0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x07,0x00,0x00,0xfe,0x03,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x06,0x00,0x00,0xfe,0x03,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x06,0x00,0x00,0xfe,0x03,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x06,0x00,0x00,0xfe,0x03,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x06,0x00,0x00,0xfe,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x07,0x00,0x00,0xfc,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x03,0x00,0x00,0xf0,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 };
const unsigned char load3[] U8X8_PROGMEM = { 0x00,0xf8,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x01,0x00,0x00,0xfc,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x03,0x00,0x00,0xfe,0xff,0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x07,0x00,0x00,0xfe,0xff,0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x06,0x00,0x00,0xfe,0xff,0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x06,0x00,0x00,0xfe,0xff,0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x06,0x00,0x00,0xfe,0xff,0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x06,0x00,0x00,0xfe,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x07,0x00,0x00,0xfc,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x03,0x00,0x00,0xf0,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 };
const unsigned char load4[] U8X8_PROGMEM = { 0x00,0xf8,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x01,0x00,0x00,0xfc,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x03,0x00,0x00,0xfe,0xff,0xff,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x07,0x00,0x00,0xfe,0xff,0xff,0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x06,0x00,0x00,0xfe,0xff,0xff,0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x06,0x00,0x00,0xfe,0xff,0xff,0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x06,0x00,0x00,0xfe,0xff,0xff,0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x06,0x00,0x00,0xfe,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x07,0x00,0x00,0xfc,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x03,0x00,0x00,0xf0,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 };
const unsigned char load5[] U8X8_PROGMEM = { 0x00,0xf8,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x01,0x00,0x00,0xfc,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x03,0x00,0x00,0xfe,0xff,0xff,0xff,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x07,0x00,0x00,0xfe,0xff,0xff,0xff,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x06,0x00,0x00,0xfe,0xff,0xff,0xff,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x06,0x00,0x00,0xfe,0xff,0xff,0xff,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x06,0x00,0x00,0xfe,0xff,0xff,0xff,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x06,0x00,0x00,0xfe,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x07,0x00,0x00,0xfc,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x03,0x00,0x00,0xf0,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 };
const unsigned char load6[] U8X8_PROGMEM = { 0x00,0xf8,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x01,0x00,0x00,0xfc,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x03,0x00,0x00,0xfe,0xff,0xff,0xff,0x7f,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x07,0x00,0x00,0xfe,0xff,0xff,0xff,0x7f,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x06,0x00,0x00,0xfe,0xff,0xff,0xff,0x7f,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x06,0x00,0x00,0xfe,0xff,0xff,0xff,0x7f,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x06,0x00,0x00,0xfe,0xff,0xff,0xff,0x7f,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x06,0x00,0x00,0xfe,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x07,0x00,0x00,0xfc,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x03,0x00,0x00,0xf0,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 };
const unsigned char load7[] U8X8_PROGMEM = { 0x00,0xf8,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x01,0x00,0x00,0xfc,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x03,0x00,0x00,0xfe,0xff,0xff,0xff,0xff,0x7f,0x00,0x00,0x00,0x00,0x00,0x00,0x07,0x00,0x00,0xfe,0xff,0xff,0xff,0xff,0x7f,0x00,0x00,0x00,0x00,0x00,0x00,0x06,0x00,0x00,0xfe,0xff,0xff,0xff,0xff,0x7f,0x00,0x00,0x00,0x00,0x00,0x00,0x06,0x00,0x00,0xfe,0xff,0xff,0xff,0xff,0x7f,0x00,0x00,0x00,0x00,0x00,0x00,0x06,0x00,0x00,0xfe,0xff,0xff,0xff,0xff,0x7f,0x00,0x00,0x00,0x00,0x00,0x00,0x06,0x00,0x00,0xfe,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x07,0x00,0x00,0xfc,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x03,0x00,0x00,0xf0,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 };
const unsigned char load8[] U8X8_PROGMEM = { 0x00,0xf8,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x01,0x00,0x00,0xfc,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x03,0x00,0x00,0xfe,0xff,0xff,0xff,0xff,0xff,0x3f,0x00,0x00,0x00,0x00,0x00,0x07,0x00,0x00,0xfe,0xff,0xff,0xff,0xff,0xff,0x3f,0x00,0x00,0x00,0x00,0x00,0x06,0x00,0x00,0xfe,0xff,0xff,0xff,0xff,0xff,0x3f,0x00,0x00,0x00,0x00,0x00,0x06,0x00,0x00,0xfe,0xff,0xff,0xff,0xff,0xff,0x3f,0x00,0x00,0x00,0x00,0x00,0x06,0x00,0x00,0xfe,0xff,0xff,0xff,0xff,0xff,0x3f,0x00,0x00,0x00,0x00,0x00,0x06,0x00,0x00,0xfe,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x07,0x00,0x00,0xfc,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x03,0x00,0x00,0xf0,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 };
const unsigned char load9[] U8X8_PROGMEM = { 0x00,0xf8,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x01,0x00,0x00,0xfc,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x03,0x00,0x00,0xfe,0xff,0xff,0xff,0xff,0xff,0xff,0x3f,0x00,0x00,0x00,0x00,0x07,0x00,0x00,0xfe,0xff,0xff,0xff,0xff,0xff,0xff,0x3f,0x00,0x00,0x00,0x00,0x06,0x00,0x00,0xfe,0xff,0xff,0xff,0xff,0xff,0xff,0x3f,0x00,0x00,0x00,0x00,0x06,0x00,0x00,0xfe,0xff,0xff,0xff,0xff,0xff,0xff,0x3f,0x00,0x00,0x00,0x00,0x06,0x00,0x00,0xfe,0xff,0xff,0xff,0xff,0xff,0xff,0x3f,0x00,0x00,0x00,0x00,0x06,0x00,0x00,0xfe,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x07,0x00,0x00,0xfc,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x03,0x00,0x00,0xf0,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 };
const unsigned char load10[] U8X8_PROGMEM = { 0x00,0xf8,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x01,0x00,0x00,0xfc,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x03,0x00,0x00,0xfe,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x1f,0x00,0x00,0x00,0x07,0x00,0x00,0xfe,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x1f,0x00,0x00,0x00,0x06,0x00,0x00,0xfe,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x1f,0x00,0x00,0x00,0x06,0x00,0x00,0xfe,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x1f,0x00,0x00,0x00,0x06,0x00,0x00,0xfe,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x1f,0x00,0x00,0x00,0x06,0x00,0x00,0xfe,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x07,0x00,0x00,0xfc,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x03,0x00,0x00,0xf0,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 };
const unsigned char load11[] U8X8_PROGMEM = { 0x00,0xf8,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x01,0x00,0x00,0xfc,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x03,0x00,0x00,0xfe,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x1f,0x00,0x00,0x07,0x00,0x00,0xfe,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x1f,0x00,0x00,0x06,0x00,0x00,0xfe,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x1f,0x00,0x00,0x06,0x00,0x00,0xfe,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x1f,0x00,0x00,0x06,0x00,0x00,0xfe,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x1f,0x00,0x00,0x06,0x00,0x00,0xfe,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x07,0x00,0x00,0xfc,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x03,0x00,0x00,0xf0,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 };
const unsigned char load12[] U8X8_PROGMEM = { 0x00,0xf8,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x01,0x00,0x00,0xfc,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x03,0x00,0x00,0xfe,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x0f,0x00,0x07,0x00,0x00,0xfe,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x0f,0x00,0x06,0x00,0x00,0xfe,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x0f,0x00,0x06,0x00,0x00,0xfe,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x0f,0x00,0x06,0x00,0x00,0xfe,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x0f,0x00,0x06,0x00,0x00,0xfe,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x07,0x00,0x00,0xfc,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x03,0x00,0x00,0xf0,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 };
const unsigned char load13[] U8X8_PROGMEM = { 0x00,0xf8,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x01,0x00,0x00,0xfc,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x03,0x00,0x00,0xfe,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x0f,0x07,0x00,0x00,0xfe,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x0f,0x06,0x00,0x00,0xfe,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x0f,0x06,0x00,0x00,0xfe,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x0f,0x06,0x00,0x00,0xfe,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x0f,0x06,0x00,0x00,0xfe,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x07,0x00,0x00,0xfc,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x03,0x00,0x00,0xf0,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 };
const unsigned char load14[] U8X8_PROGMEM = { 0x00,0xf8,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x01,0x00,0x00,0xfc,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x03,0x00,0x00,0xfe,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x07,0x00,0x00,0xfe,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x07,0x00,0x00,0xfe,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x07,0x00,0x00,0xfe,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x07,0x00,0x00,0xfe,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x07,0x00,0x00,0xfe,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x07,0x00,0x00,0xfc,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x03,0x00,0x00,0xf0,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 };
// width: 73, height: 54
const unsigned char wifi[] U8X8_PROGMEM = { 0x00,0x00,0x00,0xc0,0xff,0x0f,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xfe,0xff,0xff,0x07,0x00,0x00,0x00,0x00,0x00,0xe0,0xff,0xff,0xff,0x3f,0x00,0x00,0x00,0x00,0x00,0xfe,0xff,0xff,0xff,0xff,0x00,0x00,0x00,0x00,0x80,0xff,0xff,0xff,0xff,0xff,0x03,0x00,0x00,0x00,0xe0,0xff,0xff,0xff,0xff,0xff,0x0f,0x00,0x00,0x00,0xf8,0xff,0xff,0xff,0xff,0xff,0x3f,0x00,0x00,0x00,0xfc,0xff,0xff,0xff,0xff,0xff,0xff,0x00,0x00,0x00,0xfe,0xff,0xff,0xff,0xff,0xff,0xff,0x01,0x00,0x80,0xff,0xff,0x3f,0x00,0xf8,0xff,0xff,0x07,0x00,0xe0,0xff,0xff,0x01,0x00,0x00,0xff,0xff,0x0f,0x00,0xf0,0xff,0x3f,0x00,0x00,0x00,0xf8,0xff,0x1f,0x00,0xf8,0xff,0x07,0x00,0x00,0x00,0xc0,0xff,0x3f,0x00,0xfc,0xff,0x01,0x00,0x00,0x00,0x00,0xff,0x7f,0x00,0xfe,0xff,0x00,0x00,0x78,0x00,0x00,0xfe,0xff,0x00,0xfe,0x3f,0x00,0xf0,0xff,0x1f,0x00,0xf8,0xff,0x01,0xfe,0x1f,0x00,0xff,0xff,0xff,0x01,0xe0,0xff,0x01,0xfe,0x07,0xc0,0xff,0xff,0xff,0x07,0xc0,0xff,0x01,0xff,0x03,0xf0,0xff,0xff,0xff,0x1f,0x80,0xff,0x01,0xfe,0x01,0xfc,0xff,0xff,0xff,0x7f,0x00,0xff,0x01,0xfe,0x00,0xfe,0xff,0xff,0xff,0xff,0x00,0xfe,0x00,0x7c,0x80,0xff,0xff,0xff,0xff,0xff,0x03,0x78,0x00,0x00,0xc0,0xff,0xff,0xff,0xff,0xff,0x07,0x00,0x00,0x00,0xe0,0xff,0xff,0x01,0xff,0xff,0x0f,0x00,0x00,0x00,0xf0,0xff,0x1f,0x00,0xe0,0xff,0x1f,0x00,0x00,0x00,0xf8,0xff,0x03,0x00,0x80,0xff,0x3f,0x00,0x00,0x00,0xfc,0xff,0x00,0x00,0x00,0xfe,0x7f,0x00,0x00,0x00,0xfc,0x3f,0x00,0x00,0x00,0xf8,0x7f,0x00,0x00,0x00,0xfc,0x0f,0x00,0x7c,0x00,0xe0,0x7f,0x00,0x00,0x00,0xfc,0x07,0xc0,0xff,0x07,0xc0,0x7f,0x00,0x00,0x00,0xf8,0x03,0xf8,0xff,0x3f,0x80,0x3f,0x00,0x00,0x00,0xf0,0x01,0xfe,0xff,0xff,0x00,0x1f,0x00,0x00,0x00,0x60,0x00,0xff,0xff,0xff,0x01,0x0c,0x00,0x00,0x00,0x00,0x80,0xff,0xff,0xff,0x03,0x00,0x00,0x00,0x00,0x00,0xc0,0xff,0xff,0xff,0x0f,0x00,0x00,0x00,0x00,0x00,0xe0,0xff,0xff,0xff,0x1f,0x00,0x00,0x00,0x00,0x00,0xf0,0xff,0xff,0xff,0x1f,0x00,0x00,0x00,0x00,0x00,0xf0,0xff,0xc7,0xff,0x1f,0x00,0x00,0x00,0x00,0x00,0xf0,0x7f,0x00,0xfc,0x1f,0x00,0x00,0x00,0x00,0x00,0xf0,0x1f,0x00,0xf0,0x1f,0x00,0x00,0x00,0x00,0x00,0xf0,0x0f,0x00,0xe0,0x1f,0x00,0x00,0x00,0x00,0x00,0xe0,0x07,0x00,0xc0,0x0f,0x00,0x00,0x00,0x00,0x00,0x80,0x01,0x00,0x00,0x03,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x7c,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xfe,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xff,0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xff,0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x80,0xff,0x03,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x80,0xff,0x03,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x80,0xff,0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xff,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x7e,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 };
void loadGif()
{
int t = 1000;
u8g2.drawXBM(5, 26, 118, 11, load1);
SEND_BUFFER_DISPLAY_MS(t);
u8g2.drawXBM(5, 26, 118, 11, load2);
SEND_BUFFER_DISPLAY_MS(t);
u8g2.drawXBM(5, 26, 118, 11, load3);
SEND_BUFFER_DISPLAY_MS(t);
u8g2.drawXBM(5, 26, 118, 11, load4);
SEND_BUFFER_DISPLAY_MS(t);
u8g2.drawXBM(5, 26, 118, 11, load5);
SEND_BUFFER_DISPLAY_MS(t);
u8g2.drawXBM(5, 26, 118, 11, load5);
SEND_BUFFER_DISPLAY_MS(t);
u8g2.drawXBM(5, 26, 118, 11, load7);
SEND_BUFFER_DISPLAY_MS(t);
u8g2.drawXBM(5, 26, 118, 11, load8);
SEND_BUFFER_DISPLAY_MS(t);
u8g2.drawXBM(5, 26, 118, 11, load9);
SEND_BUFFER_DISPLAY_MS(t);
u8g2.drawXBM(5, 26, 118, 11, load10);
SEND_BUFFER_DISPLAY_MS(t);
u8g2.drawXBM(5, 26, 118, 11, load11);
SEND_BUFFER_DISPLAY_MS(t);
u8g2.drawXBM(5, 26, 118, 11, load12);
SEND_BUFFER_DISPLAY_MS(t);
u8g2.drawXBM(5, 26, 118, 11, load13);
SEND_BUFFER_DISPLAY_MS(t);
u8g2.drawXBM(5, 26, 118, 11, load14);
SEND_BUFFER_DISPLAY_MS(t);
}
void wifiGif(){
int t=1000;
u8g2.drawXBM(27, 10, 73, 54, wifi);
SEND_BUFFER_DISPLAY_MS(t);
}
在线模式
void onlineVersion(){
client.loop();
timeClient.update();
//获取初始值,方便后续用来进行中值滤波
for(int i=0;i<5;i++){
midAnalogValue[i] = analogRead(MQ2_ANALOG_PIN);
midDigitalValue[i] = digitalRead(MQ2_DIGITAL_PIN);
delay(300);
}
std::sort(midAnalogValue,midAnalogValue+5);
std::sort(midDigitalValue,midDigitalValue+5);
//dht11开始获取数据
Serial.println("=================================");
int err = SimpleDHTErrSuccess;
if ((err = dht11.read(&temperature, &humidity, NULL)) != SimpleDHTErrSuccess) {
Serial.print("Read DHT11 failed, err="); Serial.println(err);
return;
}
Serial.print("Sample OK: ");
Serial.print((int)temperature); Serial.print(" *C, ");
Serial.print((int)humidity); Serial.println(" RH");
bufTemp=(int)temperature;
bufHum=(int)humidity;
delay(250);
//打印出两秒内mq2所有的模拟值
Serial.print("All AnalogValue in 2 seconds:\n");
for(int i=0;i<5;i++){
Serial.println(midAnalogValue[i]);
}
int analogValue = midAnalogValue[3];
int digitalValue = midDigitalValue[3];
Serial.print("MQ-2 Analog Value: ");
Serial.println(analogValue);
Serial.print("MQ-2 Digital Value: ");
Serial.println(digitalValue);
showData(temperature,humidity,analogValue,digitalValue);
sendData(temperature,humidity,analogValue,digitalValue);
showTimeCtl++;
if(showTimeCtl>5){
getWeather();
showTimeAndWeather();
showTimeCtl=0;
}
}
离线模式
void offlineVersion(){
while(retryConnect){
if(heatingMq2==0){
loadText();
loadIcon();
}
heatingMq2=1;//mq-2加热过一次后不再进行加热
//获取初始值,方便后续用来进行中值滤波
for(int i=0;i<5;i++){
midAnalogValue[i] = analogRead(MQ2_ANALOG_PIN);
midDigitalValue[i] = digitalRead(MQ2_DIGITAL_PIN);
delay(300);
}
std::sort(midAnalogValue,midAnalogValue+5);
std::sort(midDigitalValue,midDigitalValue+5);
//dht11开始获取数据
Serial.println("=================================");
byte temperature = 0;
byte humidity = 0;
int err = SimpleDHTErrSuccess;
if ((err = dht11.read(&temperature, &humidity, NULL)) != SimpleDHTErrSuccess) {
Serial.print("Read DHT11 failed, err="); Serial.println(err);
return;
}
Serial.print("Sample OK: ");
Serial.print((int)temperature); Serial.print(" *C, ");
Serial.print((int)humidity); Serial.println(" H");
//打印出两秒内mq2所有的模拟值
Serial.print("All AnalogValue in 2 seconds:\n");
for(int i=0;i<5;i++){
Serial.println(midAnalogValue[i]);
}
int analogValue = midAnalogValue[3];
int digitalValue = midDigitalValue[3];
Serial.print("MQ-2 Analog Value: ");
Serial.println(analogValue);
Serial.print("MQ-2 Digital Value: ");
Serial.println(digitalValue);
showData(temperature,humidity,analogValue,digitalValue);
//sendData(temperature,humidity,analogValue,digitalValue);//离线版本不需要发送消息,避免浪费资源
retryConnect--;
}
}
显示屏模块需要显示的文字
void initOLED()
{
u8g2.begin();
u8g2.enableUTF8Print();
u8g2.setFont(u8g2_font_wqy16_t_gb2312);
}
void helpMessage(){
Serial.print("helpMeConnectWifi");
u8g2.firstPage();
do
{
u8g2.setCursor(0, 22); //指定显示位置
u8g2.print("MyNameIsArgo"); //使用print来显示字符串
u8g2.setCursor(0, 42);
u8g2.print("psd:123456");
u8g2.setCursor(0, 62);
u8g2.print("HelpMeGetWiFi");
} while (u8g2.nextPage());
delay(3000);
}
void reconnecMqttServer(){
Serial.print("MQTTConnectFailed");
u8g2.firstPage();
do
{
u8g2.setCursor(0, 22); //指定显示位置
u8g2.print("服务器连接失败"); //使用print来显示字符串
u8g2.setCursor(0, 42);
u8g2.print("正在重连");
u8g2.setCursor(0, 62);
u8g2.print("即将离线使用");
} while (u8g2.nextPage());
delay(2000);
}
void showTimeAndWeather(){
u8g2.firstPage();
do
{
u8g2.setCursor(0, 22);
u8g2.print("当前时间:");
u8g2.setCursor(70, 22);
u8g2.print(timeClient.getFormattedTime());
u8g2.setCursor(0, 42);
u8g2.print("今天温度:");
u8g2.setCursor(70, 42);
u8g2.print(results_0_now_temperature_int);
u8g2.setCursor(100, 42);
u8g2.print("℃");
u8g2.setCursor(0, 62);
u8g2.print("天气状况:");
u8g2.setCursor(70, 62);
u8g2.print(results_0_now_text_str);
} while (u8g2.nextPage());
delay(3000);
}
void wifiConnectFailed(){
Serial.print("wifiConnectFailed");
u8g2.firstPage();
do
{
u8g2.setCursor(0, 22); //指定显示位置
u8g2.print("WiFi连接失败"); //使用print来显示字符串
u8g2.setCursor(0, 42);
u8g2.print("暂时离线使用");
u8g2.setCursor(0, 62);
u8g2.print("一段时间后重连");
} while (u8g2.nextPage());
delay(5000);
}
void serverStatus(){ //连接不上mqtt服务器时调用提示,避免一直死循环连接
Serial.print("connect mqtt server failed");
u8g2.firstPage();
do
{
u8g2.setCursor(0, 40); //指定显示位置
u8g2.print("连接服务器失败"); //使用print来显示字符串
} while (u8g2.nextPage());
delay(6000);
}
void loadIcon(){
u8g2.firstPage();
do{
loadGif(); //加载动画
} while (u8g2.nextPage());
}
void loadWifi(){
u8g2.firstPage();
do{
wifiGif(); //加载动画
} while (u8g2.nextPage());
}
void wifiText(){
Serial.print("Connecting Wifi");
u8g2.firstPage();
do
{
u8g2.setCursor(5, 40); //指定显示位置
u8g2.print("Connecting Wifi..."); //使用print来显示字符串
} while (u8g2.nextPage());
}
void wifiTextEnd(){
Serial.print("Wifi Connected");
u8g2.firstPage();
do
{
u8g2.setCursor(10, 30); //指定显示位置
u8g2.print("Wifi Connected"); //使用print来显示字符串
u8g2.setCursor(5, 50);
u8g2.print(WiFi.localIP());
} while (u8g2.nextPage());
delay(2000);
}
void loadText(){
Serial.print("MQ-2 is initing, need about 20 seconds");
u8g2.firstPage();
do
{
u8g2.setCursor(5, 40); //指定显示位置
u8g2.print("Initing MQ-2...."); //使用print来显示字符串
} while (u8g2.nextPage());
delay(6000);
}
void serverError(){
u8g2.firstPage();
do
{
u8g2.setCursor(0, 22);
u8g2.print("连接服务器失败");
u8g2.setCursor(0, 42);
u8g2.print("暂时离线使用");
u8g2.setCursor(0, 62);
u8g2.print("一段时间后重连");
} while (u8g2.nextPage());
delay(5000);
}
void showData(byte temperature,byte humidity,int analogValue,int digitalValue){
u8g2.firstPage();
do
{
u8g2.setCursor(0, 20); //指定显示位置
u8g2.print("室内温度:"); //使用print来显示字符串
u8g2.setCursor(72, 20);
u8g2.print((int)temperature);
u8g2.setCursor(100, 20);
u8g2.print("℃");
u8g2.setCursor(0, 40); //指定显示位置
u8g2.print("室内湿度:"); //使用print来显示字符串
u8g2.setCursor(72, 40);
u8g2.print((int)humidity);
u8g2.setCursor(100, 40);
u8g2.print("RH");
u8g2.setCursor(0, 60); //指定显示位置
u8g2.print("烟雾浓度:"); //使用print来显示字符串
u8g2.setCursor(72, 60);
u8g2.print(analogValue);
u8g2.setCursor(100, 60);
u8g2.print("PPM");
} while (u8g2.nextPage());
}
将传感器数据转换成 Json 格式
void sendData(byte temperature,byte humidity,int analogValue,int digitalValue){
StaticJsonDocument<100> doc;
doc["时间"]=timeClient.getFormattedTime();
doc["温度"]=(int)temperature;
doc["湿度"]=(int)humidity;
doc["模拟值"] = analogValue;
// doc["digital_status"] = digitalValue ? "HIGH" : "LOW";
doc["数值"] = digitalValue;
if(analogValue>450 && digitalValue==0){
char status[10]="Dangerous";
doc["状态"]=status;
}else if((int)temperature>30){
char status[10]="Dangerous";
doc["状态"]=status;
}else{
char status[10]="Safe";
doc["状态"]=status;
}
char msg[100];
serializeJson(doc, msg);
client.publish(mqtt_topic, msg);
}
通过心知天气服务获取当天天气信息
void getWeather(){
String reqRes = "/v3/weather/now.json?key=" + reqUserKey +
+ "&location=" + reqLocation +
"&language=en&unit=" +reqUnit;
httpRequest(reqRes);
}
void httpRequest(String reqRes){
WiFiClient client;
// 建立http请求信息
String httpRequest = String("GET ") + reqRes + " HTTP/1.1\r\n" +
"Host: " + host + "\r\n" +
"Connection: close\r\n\r\n";
Serial.println("");
Serial.print("Connecting to "); Serial.print(host);
// 尝试连接服务器
if (client.connect(host, 80)){
Serial.println(" Success!");
// 向服务器发送http请求信息
client.print(httpRequest);
Serial.println("Sending request: ");
Serial.println(httpRequest);
// 获取并显示服务器响应状态行
String status_response = client.readStringUntil('\n');
Serial.print("status_response: ");
Serial.println(status_response);
// 使用find跳过HTTP响应头
if (client.find("\r\n\r\n")) {
Serial.println("Found Header End. Start Parsing.");
}
// 利用ArduinoJson库解析心知天气响应信息
parseInfo(client);
} else {
Serial.println(" connection failed!");
}
//断开客户端与服务器连接工作
client.stop();
}
void parseInfo(WiFiClient client){
const size_t capacity = JSON_ARRAY_SIZE(1) + JSON_OBJECT_SIZE(1) + 2*JSON_OBJECT_SIZE(3) + JSON_OBJECT_SIZE(6) + 230;
DynamicJsonDocument doc(capacity);
deserializeJson(doc, client);
JsonObject results_0 = doc["results"][0];
JsonObject results_0_now = results_0["now"];
const char* results_0_now_text = results_0_now["text"]; // "Sunny"
const char* results_0_now_code = results_0_now["code"]; // "0"
const char* results_0_now_temperature = results_0_now["temperature"]; // "32"
const char* results_0_last_update = results_0["last_update"]; // "2020-06-02T14:40:00+08:00"
// 通过串口监视器显示以上信息
results_0_now_text_str = results_0_now["text"].as<String>();
results_0_now_code_int = results_0_now["code"].as<int>();
results_0_now_temperature_int = results_0_now["temperature"].as<int>();
String results_0_last_update_str = results_0["last_update"].as<String>();
Serial.println(F("======Weahter Now======="));
Serial.print(F("Weather Now: "));
Serial.print(results_0_now_text_str);
Serial.print(F(" "));
Serial.println(results_0_now_code_int);
Serial.print(F("Temperature: "));
Serial.println(results_0_now_temperature_int);
Serial.print(F("Last Update: "));
Serial.println(results_0_last_update_str);
Serial.println(F("========================"));
}
负责接收数据并且保存到 MySQL 的服务器端代码
连接到 MySql 的代码
import random
import json
from paho.mqtt import client as mqtt_client
import connectMysql
broker = 'localhost'
port = 1883
topic = "device/attributes"
# generate client ID with pub prefix randomly
client_id = f'python-mqtt-{random.randint(0, 100)}'
def connect_mqtt() -> mqtt_client:
def on_connect(client, userdata, flags, rc):
if rc == 0:
print("Connected to MQTT Broker!")
else:
print("Failed to connect, return code %d\n", rc)
client = mqtt_client.Client(client_id)
client.on_connect = on_connect
client.connect(broker, port)
return client
def subscribe(client: mqtt_client):
def on_message(client,userdata, msg):
dict_data = json.loads(str(msg.payload.decode()))
print(dict_data)
connectMysql.insert(dict_data)
client.subscribe(topic)
client.on_message = on_message
def run():
client = connect_mqtt()
subscribe(client)
client.loop_forever()
连接到 Mqtt 服务器的代码
import random
import json
from paho.mqtt import client as mqtt_client
import connectMysql
broker = 'localhost'
port = 1883
topic = "device/attributes"
# generate client ID with pub prefix randomly
client_id = f'python-mqtt-{random.randint(0, 100)}'
def connect_mqtt() -> mqtt_client:
def on_connect(client, userdata, flags, rc):
if rc == 0:
print("Connected to MQTT Broker!")
else:
print("Failed to connect, return code %d\n", rc)
client = mqtt_client.Client(client_id)
client.on_connect = on_connect
client.connect(broker, port)
return client
def subscribe(client: mqtt_client):
def on_message(client,userdata, msg):
dict_data = json.loads(str(msg.payload.decode()))
print(dict_data)
connectMysql.insert(dict_data)
client.subscribe(topic)
client.on_message = on_message
def run():
client = connect_mqtt()
subscribe(client)
client.loop_forever()
Main 函数
import connectMqtt
import connectMysql
connectMqtt.run()