29| 0
|
[求助] Arduino uno+ESP01S+OLED屏幕不能同时使用问题 |
#include <ArduinoJson.h> // 引入 ArduinoJson 库 #include <SPI.h> #include <Wire.h> #include <SoftwareSerial.h> // 引入驱动OLED0.96所需的库 #include <Adafruit_GFX.h> #include <Adafruit_SSD1306.h> //SoftwareSerial espSerial(2, 3); // (RX, TX) #define LED_PIN 8 // LED 接口 #define SCREEN_WIDTH 128 // 设置OLED宽度,单位:像素 #define SCREEN_HEIGHT 64 // 设置OLED高度,单位:像素 Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1); // -----------------------光敏--------------------------------------------------- #define photosensitivePin A0 //定义模拟口A5 #define phTimeInterval 1000 //检测一次的时间间隔 unsigned long phTimes = 0; //记录设备运行时间 int photosenVal = 0; //光照度数值 int photoContent = 0; // ------------------------------------------------------------------------------ // -----------------------超声波--------------------------------------------------- #define Trig 10 //引脚Tring 连接 IO D10 #define Echo 9 //引脚Echo 连接 IO D9 float cm; //距离变量 float temp; //存储回波 // ------------------------------------------------------------------------------ // -----------------------浑浊度--------------------------------------------------- int sensorPin = A1; // 采集引脚 unsigned int sum_ad, ad1, voltage, kk, result; // ------------------------------------------------------------------------------ void setup() { pinMode(Trig, OUTPUT); pinMode(Echo, INPUT); // espSerial.begin(9600); // 初始化与 ESP01S 的串口通信 Serial.begin(9600); // 初始化串口通信 pinMode(photosensitivePin,INPUT);//设置光敏为输出模式 // 初始化Wire库 Wire.begin(); // 初始化OLED并设置其IIC地址为 0x3C display.begin(SSD1306_SWITCHCAPVCC, 0x3C); } void loop() { getPhData(); //WIFI_Send(); if(millis()%200 ==0){ words_display(); display.display(); } if(millis()%200 ==0){ ultrasonic(); } if(millis()%200 ==0){ turbidity(); } } /****************************************光敏电阻 part****************************************/ void getPhData() { if (millis() - phTimes >= phTimeInterval) { // 每隔一定时间检测一次 phTimes = millis(); photosenVal = analogRead(photosensitivePin); // 获取原始值 // photosenVal = constrain(photosenVal, 10, 1023); // 限制原始值范围 photoContent = 100 - ((photosenVal / 1023.0) * 100); // 计算百分比 // 调试信息 Serial.print("原始数据: "); Serial.print(photosenVal); Serial.print(" ,光照强度: "); Serial.print(photoContent); Serial.println(" %"); } } /****************************************超声波 part****************************************/ void ultrasonic() { //给Trig发送一个低高低的短时间脉冲 digitalWrite(Trig, LOW); //给Trig发送一个低电平(初始化) delayMicroseconds(2); //等待 2微妙 digitalWrite(Trig,HIGH); //给Trig发送一个高电平 delayMicroseconds(10); //等待 10微妙 digitalWrite(Trig, LOW); //给Trig发送一个低电平 temp = float(pulseIn(Echo, HIGH)); //存储回波等待时间, cm = (temp * 17 )/1000; //把回波时间换算成cm } /****************************************浑浊度 part****************************************/ void turbidity() { sum_ad = 0; // 求和清零 for (kk = 0; kk < 20; kk++) { // 20次取平均值 ad1 = analogRead(sensorPin); // 读取模拟值 delay(10); sum_ad += ad1; // 累加 } ad1 = sum_ad / 20; // 计算平均值 voltage = ad1 * (5.0 / 1023.0); // 转换为实际电压(最大值为5V) // 把电压转换为浑浊度 if (8.65 * voltage >= 3291) { result = 0; // 如果电压过大,浑浊度设为0 } else { result = 3291 - 8.65 * voltage; // 计算浑浊度 } display.print("turbidity :"); display.println(result); // 将混浊度转换为百分比 //float percentage = (result / 3291.0) * 100; } /****************************************OLED part****************************************/ void words_display(){ // 清除屏幕 display.clearDisplay(); // 设置字体颜色,白色可见 display.setTextColor(WHITE); //设置字体大小 display.setTextSize(1.5); // 光敏显示 display.setCursor(0, 0); display.print("Light Val :"); display.print(photoContent); display.print(" %"); // 超声波显示 display.setCursor(0, 20); display.print("Distance :"); display.print(cm); display.print(" CM"); display.setCursor(0, 35); display.print("turbidity :"); display.print(ad1); display.print(" NTU"); // 水温 display.setCursor(0, 50); display.print("Tem Val :"); display.print(12); display.print(" C"); } /****************************************WIFI part*******************************************/ // void WIFI_Send(){ // String jsonData = "{\"params\":{\"GM\":" + String(photoContent) + "},\"version\":\"1.0.0\"}"; // // 检查是否有从 ESP01S 发回的数据 // if (espSerial.available()) { // String feedback = espSerial.readString(); // 读取反馈数据 // Serial.println("收到 MQTT 消息: " + feedback); // 打印收到的消息 // // 如果收到包含 "thing.service.property.set" 的 MQTT 消息 // if (feedback.indexOf("thing.service.property.set") != -1) { // // 解析 JSON 数据 // StaticJsonDocument<512> doc; // 增加文档的内存大小 // DeserializationError error = deserializeJson(doc, feedback); // if (error) { // Serial.print("解析 JSON 失败, 错误: "); // Serial.println(error.f_str()); // return; // } // // 获取 PowerSwitch_1 的值 // JsonObject params = doc["params"]; // int powerSwitch = params["PowerSwitch_1"]; // // 控制 LED // if (powerSwitch == 1) { // digitalWrite(LED_PIN, HIGH); // 打开 LED // Serial.println("LED 开启"); // } else if (powerSwitch == 0) { // digitalWrite(LED_PIN, LOW); // 关闭 LED // Serial.println("LED 关闭"); // } // } // } // espSerial.println(jsonData); // //Serial.println("发送数据:" + jsonData); // delay(1000); // 延时,避免频繁发送 // } 为什么ESP01S用了OLED就没反应了?求助 |
© 2013-2025 Comsenz Inc. Powered by Discuz! X3.4 Licensed