From 0f6b59f52dffb2009579fcf6e8d5aee10fbbf3c8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=85=95=E7=82=8E?= <29385962@qq.com> Date: Fri, 14 Jan 2022 13:12:26 +0000 Subject: [PATCH] =?UTF-8?q?update=20esp32=5Fair=5Fpump/esp32=5Fair=5Fpump?= =?UTF-8?q?=5Fmain.h.=20=E4=BF=AE=E6=AD=A3bar=E6=95=B0=E6=8D=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- esp32_air_pump/esp32_air_pump_main.h | 115 ++++++++++++++------------- 1 file changed, 61 insertions(+), 54 deletions(-) diff --git a/esp32_air_pump/esp32_air_pump_main.h b/esp32_air_pump/esp32_air_pump_main.h index f2de0c1..9f5e4d1 100644 --- a/esp32_air_pump/esp32_air_pump_main.h +++ b/esp32_air_pump/esp32_air_pump_main.h @@ -20,9 +20,9 @@ bool touch_STATE[4] = {0, 0, 0, 0}; // 定义按键触发对象状态变量初 const double min_voltage = 6; //电池检测最低电压 double bat_voltage; +double pressure_val = 0; //气压数据 如果设置初始值为-1则不检测气压 传感器强制执行 int running_type = 0; //运行状态 0不运行 1篮球 2摩托车 3汽车 4自行车 5自定义 uint32_t running_time; -double pressure_val = -1; //气压数据 如果设置初始值为-1则不检测气压 传感器强制执行 int pressure_type[6] = {600000, 1305485, 3762277, 3839579, 5691781, 1305485}; //气压类型 0不运行 1篮球 2摩托车 3汽车 4自行车 float running_pressure_type; @@ -47,7 +47,7 @@ double return_voltage_value(int pin_no) tmp = tmp + analogRead(pin_no); } avg = tmp / 50; - ADCVoltage = ((avg * 3.3) / (4095)) + 0.225; + ADCVoltage = ((avg * 3.3) / (4095)) + 0.138; inputVoltage = ADCVoltage / (R2_VOLTAGE / (R1_VOLTAGE + R2_VOLTAGE)); // formula for calculating voltage in i.e. GND return inputVoltage; } @@ -123,10 +123,50 @@ long readADC() digitalWrite(clck, LOW); delayMicroseconds(1); } - return result; } +String TimeString(int TimeMillis) { + + String stringTime; + + stringTime += int(TimeMillis / 60 / 60); + stringTime += ":"; + stringTime += int(TimeMillis / 60 % 60); + stringTime += ":"; + stringTime += TimeMillis % 60; + + return stringTime; +} + +String ProcessUpdate() //页面更新 +{ + //自动生成一串用“,”隔开的字符串。 + //HTML脚本会按照“, ”分割,形成一个字符串数组。 + //并把这个数组填个表格的相应部分。 + String ReturnString; + if (touch_STATE[0]) { + ReturnString += TimeString((millis() - running_time) / 1000); + } else { + ReturnString += "0"; + } + ReturnString += ","; + ReturnString += bat_voltage; //电压值 + ReturnString += ","; + ReturnString += pressure_val; //气压值 + ReturnString += ","; + ReturnString += touch_STATE[0]; //运行状态 + ReturnString += ","; + ReturnString += running_type; //运行方式 + ReturnString += ","; + ReturnString += running_pressure_type; //运行气压自定义 + ReturnString += ","; + ReturnString += TimeString(millis() / 1000); //系统运行时间 + + Serial.println(ReturnString); + return ReturnString; +} + void setup() { delay(50); @@ -270,10 +310,6 @@ void loop() { ArduinoOTA.handle(); - ESP32Server.handleClient(); - delay(2); - TenthSecondsSinceStartTask(); - //触摸感应处理 touchAttach(0, T4); touchAttach(1, T3); @@ -346,15 +382,17 @@ void loop() if ( pressure_val != -1) { //传感器正常 long adcValue = readADC(); - pressure_val = adcValue / 100000 * 0.0689476; - pressure_type[5] = running_pressure_type*100000 / 0.0689476; + if(adcValue!=-1){ + pressure_val = adcValue/100000 * 0.0689476 - 0.23; + } + pressure_type[5] = (running_pressure_type + 0.23) * 100000 / 0.0689476; - if (adcValue > 600000 ) { - Serial.print("ADC reading: "); + if (adcValue > 520000 ) { + Serial.print("ADC reading:"); Serial.print(adcValue); Serial.print("\t"); Serial.print(pressure_val); - Serial.print("bar\t"); + Serial.print(" bar\t"); Serial.println(pressure_type[5]); } @@ -370,7 +408,7 @@ void loop() } } else { //无传感器强制启动,设定时间1分钟停机 if (touch_STATE[0]) { - if ( ((millis() - running_time) / 1000) > 60 ){ + if ( ((millis() - running_time) / 1000) > 60 ) { touch_STATE[0] = 0; delay(500); digitalWrite(RelayPin[0], HIGH); @@ -379,11 +417,15 @@ void loop() } } } + + TenthSecondsSinceStartTask(); } unsigned long LastMillis = 0; void TenthSecondsSinceStartTask() //100ms { + ESP32Server.handleClient(); + delay(2); unsigned long CurrentMillis = millis(); if (abs(int(CurrentMillis - LastMillis)) > 100) { @@ -391,6 +433,7 @@ void TenthSecondsSinceStartTask() //100ms TenthSecondsSinceStart++; OnTenthSecond(); } + yield(); } bool battery_low = 0; @@ -426,44 +469,7 @@ void OnTenthSecond() // 100ms 十分之一秒 } -String TimeString(int TimeMillis) { - String stringTime; - - stringTime += int(TimeMillis / 60 / 60); - stringTime += ":"; - stringTime += int(TimeMillis / 60 % 60); - stringTime += ":"; - stringTime += TimeMillis % 60; - - return stringTime; -} - -String ProcessUpdate() //页面更新 -{ - //自动生成一串用“,”隔开的字符串。 - //HTML脚本会按照“, ”分割,形成一个字符串数组。 - //并把这个数组填个表格的相应部分。 - String ReturnString; - if (touch_STATE[0]) { - ReturnString += TimeString((millis() - running_time) / 1000); - } else { - ReturnString += "0"; - } - ReturnString += ","; - ReturnString += bat_voltage; //电压值 - ReturnString += ","; - ReturnString += pressure_val; //气压值 - ReturnString += ","; - ReturnString += touch_STATE[0]; //运行状态 - ReturnString += ","; - ReturnString += running_type; //运行方式 - ReturnString += ","; - ReturnString += running_pressure_type; //运行气压自定义 - - //Serial.println(ReturnString); - return ReturnString; -} void PocessControl(int DeviceType, int DeviceIndex, int Operation, float Operation2) { @@ -475,16 +481,16 @@ void PocessControl(int DeviceType, int DeviceIndex, int Operation, float Operati if (Operation % 3 == 0) { //重新设置传感器 - pressure_val =0; + pressure_val = 0; pinMode(ndrdy, INPUT); digitalWrite(ndrdy, LOW); - + pinMode(clck, OUTPUT); digitalWrite(clck, LOW); - + while (digitalRead(ndrdy)) {} while (!digitalRead(ndrdy)) {} - + } else if (Operation % 3 == 1) { @@ -535,6 +541,7 @@ void handleNotFound() // 获取用户请求网址信息 String webAddress = ESP32Server.uri(); int AutheTimes = 0; +//Serial.println(pressure_val); if (!ESP32Server.authenticate(username, userpassword)) //校验用户是否登录 {