一、硬件
ESP32
白色LED
DHT11温湿度传感器
有源蜂鸣器
USB转串口(只用到VCC,GND)
面包板
二、软件
Arduino IDE版ESP32开发板
Blinker,apk
三、电路连接
const int LED=18; LED控制管脚
const int BUZ=2; 有源蜂鸣器VCC管脚
#define DHTPIN 14 温湿度传感器输出管教
0.96寸oled
scl D22
sda D21
四、代码
#include <SdFat.h>#include <DHT.h>
#define BLINKER_WIFI
#include <Blinker.h>
#include <Adafruit_SSD1306.h>
#include <Adafruit_GFX.h>#include <wire.h>
#include <SPI.h>#define DHTPIN 14
#define DHTTYPE DHT11DHT Dht(DHTPIN, DHTTYPE);#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels#define OLED_RESET -1 // Reset pin # (or -1 if sharing Arduino reset pin)
#define SCREEN_ADDR 0x3C ///< See datasheet for Address; 0x3D for 128x64, 0x3C for 128x32
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);#define LOGO_HEIGHT 16
#define LOGO_WIDTH 16static const unsigned char PROGMEM font[][32] =
{
0x04,0x40,0x04,0x40,0x04,0x40,0x04,0x44,0x04,0x48,0x7C,0x50,0x04,0x60,0x04,0x40,
0x04,0x40,0x04,0x40,0x04,0x40,0x04,0x42,0x1C,0x42,0xE4,0x42,0x44,0x3E,0x04,0x00,/*"北",0*/0x02,0x00,0x01,0x00,0xFF,0xFE,0x00,0x00,0x00,0x00,0x1F,0xF0,0x10,0x10,0x10,0x10,
0x10,0x10,0x1F,0xF0,0x01,0x00,0x11,0x10,0x11,0x08,0x21,0x04,0x45,0x04,0x02,0x00,/*"京",1*/0x08,0x80,0x08,0x80,0x08,0x84,0x10,0x88,0x10,0x90,0x30,0xA0,0x30,0xC0,0x50,0x80,
0x91,0x80,0x12,0x80,0x14,0x80,0x10,0x82,0x10,0x82,0x10,0x82,0x10,0x7E,0x10,0x00,/*"化",2*/0x00,0x00,0x00,0x00,0x7F,0xFC,0x01,0x00,0x01,0x00,0x01,0x00,0x01,0x00,0x01,0x00,
0x01,0x00,0x01,0x00,0x01,0x00,0x01,0x00,0x01,0x00,0xFF,0xFE,0x00,0x00,0x00,0x00,/*"工",3*/0x01,0x00,0x01,0x00,0x01,0x00,0x01,0x00,0x01,0x00,0xFF,0xFE,0x01,0x00,0x01,0x00,
0x02,0x80,0x02,0x80,0x04,0x40,0x04,0x40,0x08,0x20,0x10,0x10,0x20,0x08,0xC0,0x06,/*"大",4*/0x22,0x08,0x11,0x08,0x11,0x10,0x00,0x20,0x7F,0xFE,0x40,0x02,0x80,0x04,0x1F,0xE0,
0x00,0x40,0x01,0x80,0xFF,0xFE,0x01,0x00,0x01,0x00,0x01,0x00,0x05,0x00,0x02,0x00,/*"学",5*/};char auth[] = "2a54b7664c77"; //手机Blinker.apk中ESP32密钥
char ssid[] = "701"; //室内WiFi名称
char pswd[] = "1234567890"; //WiFi密码int r=0;int g=0;
int led=0;
int buz=0;const int LED=18;
const int BUZ=2;char state1[]="on";
char state2[]="off";// 新建组件对象
BlinkerButton Button("开关");BlinkerText LEDstate("状态");BlinkerNumber Wen("温度");
BlinkerNumber Shi("湿度");float wen=0;
float shi=0;//----------------------------------
//把温度湿度传送给组件
void heartbeat()
{Wen.print(wen);Shi.print(shi);}
//---------------------------------// 按下按键即会执行该函数
void button1_callback(const String & state)
{BLINKER_LOG("get button state: ", state);
if(g==1){digitalWrite(BUZ,LOW); }
}void setup() {// put your setup code here, to run once:// 初始化串口Serial.begin(115200);Blinker.begin(auth, ssid, pswd);Wire.begin();display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDR);display.display();// Clear the bufferdisplay.clearDisplay();// put your main code here, to run repeatedly:if(!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDR)) {Serial.println(F("SSD1306 allocation failed"));for(;;); // Don't proceed, loop forever}BLINKER_DEBUG.stream(Serial);BLINKER_DEBUG.debugAll();// 初始化有LED的IOpinMode(LED, OUTPUT);digitalWrite(LED, LOW);pinMode(BUZ, OUTPUT);digitalWrite(BUZ, LOW);// 初始化blinkerButton.attach(button1_callback);Blinker.attachHeartbeat(heartbeat);Dht.begin();}void loop() {delay(100);Blinker.run();display.clearDisplay();display.setTextSize(1); //设置字体大小display.setTextColor(SSD1306_WHITE);//开像素点发光// testdrawbitmap(cn_allArray[0],16,16);for(int i = 0;i < 6;i++){display.drawBitmap(i*16, 0, font[i], LOGO_WIDTH,LOGO_HEIGHT, SSD1306_WHITE);delay(1);}display.setCursor(0,0); display.println();display.println();display.println();wen=Dht.readTemperature();shi=Dht.readHumidity();if(wen>=26){digitalWrite(LED,HIGH);digitalWrite(BUZ,LOW);g=0;}if(wen<26&&g==0){digitalWrite(LED,LOW);digitalWrite(BUZ,HIGH);g=1;}led=digitalRead(LED);buz=digitalRead(BUZ);display.setTextSize(2); //设置字体大小display.print("Tem:");display.print(wen,1);display.print("\xA7");display.println("C");display.print("Hum:");display.print(shi,1);display.println("%");display.display();delay(200);}
五、视频(有解说)
ESP32WiFi(Blinker)-室内舒适度检测装置