diff --git a/wifi-mqtt/wifi-mqtt/wifi-mqtt.ino b/wifi-mqtt/wifi-mqtt/wifi-mqtt.ino index 91059fc..7250e3f 100644 --- a/wifi-mqtt/wifi-mqtt/wifi-mqtt.ino +++ b/wifi-mqtt/wifi-mqtt/wifi-mqtt.ino @@ -4,8 +4,15 @@ #include #include +#include "SSD1306Wire.h" -#define DHTPIN D4 //说明数据接口为8266开发板的D4口,也可以写为#define DHTPIN 2既8266芯片的IO口2 +const int I2C_ADDR = 0x3c; // oled屏幕的I2c地址 +#define SDA_PIN D4 // SDA引脚,默认gpio4(D2) +#define SCL_PIN D5 // SCL引脚,默认gpio5(D1) +SSD1306Wire oled(I2C_ADDR, SDA_PIN, SCL_PIN); + +//#define DHTPIN D4 //说明数据接口为8266开发板的D4口,也可以写为#define DHTPIN 2既8266芯片的IO口2 +#define DHTPIN D1 //说明数据接口为8266开发板的D4口,也可以写为#define DHTPIN 2既8266芯片的IO口2 #define DHTTYPE DHT11//说明使用的模块是DHT11 DHT dht(DHTPIN,DHTTYPE);//DHT11初始化 //定义浮点类型变量保存测量值 @@ -20,7 +27,7 @@ const char* host = "192.168.3.26:8873"; // MQTT代理 const char *mqtt_broker = "47.242.184.139"; -const char *topic = "/device/esp8266/001"; +const char *topic = "/device/esp8266/004"; const char *mqtt_username = "admin"; const char *mqtt_password = "publish452131wW452131wW$"; const int mqtt_port = 1883; @@ -33,7 +40,9 @@ PubSubClient client(espClient); ESP8266WebServer server(80); void setup() { - pinMode(D4, INPUT); + pinMode(D2, OUTPUT); + digitalWrite(D2,HIGH);//IO口2设置为高电平,点亮数字2口LED + //digitalWrite(D2,LOW);//IO口2设置为高电平,点亮数字2口LED Serial.begin(115200); delay(100); dht.begin(); @@ -59,7 +68,7 @@ void setup() { //mqtt init //连接到mqtt代理 client.setServer(mqtt_broker, mqtt_port); - //client.setCallback(callback); + client.setCallback(callback); while (!client.connected()) { String client_id = "esp8266-client-"; client_id += String(WiFi.macAddress()); @@ -75,6 +84,14 @@ void setup() { // 发布和订阅 //client.publish(topic, "hello emqx"); client.subscribe(topic); + + /* 2. oled屏幕初始化 */ + oled.init(); + oled.flipScreenVertically(); // 设置屏幕翻转 + oled.setContrast(255); // 设置屏幕亮度 + drawRect(); // 测试屏幕显示 + oled.clear(); + oled.display(); // 清除屏幕 } void loop() { //http @@ -126,8 +143,21 @@ void loop() { if (WiFi.status() == WL_CONNECTED) { if(client.connected()){ String sendStr = String(Temperature)+" "+String(Humidity); + String wendu = String("Tem:")+String(Temperature); + String shidu = String("Hum:")+String(Humidity); + Serial.println(sendStr); client.publish(topic,sendStr.c_str()); + + oled.setFont(ArialMT_Plain_16); // 设置字体 + oled.drawString(0, 0, wendu); // 将要显示的文字写入缓存 + oled.drawString(0, 20,shidu); // 将要显示的文字写入缓存 + oled.drawString(0, 40,sendStr); // 将要显示的文字写入缓存 + oled.display(); // 将缓存里的文字在屏幕上显示 + digitalWrite(LED_BUILTIN, LOW); // Turn the LED on + delay(300); + digitalWrite(LED_BUILTIN, HIGH); // Turn the LED off + delay(500); }else{ Serial.println(client.state()); String client_id = "esp8266-client-"; @@ -135,6 +165,7 @@ void loop() { Serial.printf("The client %s connects to the public mqtt broker\n", client_id.c_str()); if(client.connect(client_id.c_str(), mqtt_username, mqtt_password)) { Serial.println("Public emqx mqtt broker connected"); + client.subscribe(topic); }else{ Serial.print("failed with state "); Serial.print(client.state()); @@ -209,3 +240,10 @@ void callback(char *topic, byte *payload, unsigned int length) { Serial.println("-----------callback------------"); } +void drawRect(void) { + for (int16_t i = 0; i < oled.getHeight() / 2; i += 2) { + oled.drawRect(i, i, oled.getWidth() - 2 * i, oled.getHeight() - 2 * i); + oled.display(); + delay(50); + } +}