1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193
| #include <Adafruit_MPU6050.h> #include <Adafruit_Sensor.h> #include <WiFi.h> #include <ESPAsyncWebServer.h> AsyncWebServer server(80);
const char index_html[] PROGMEM = R"rawliteral( <!DOCTYPE HTML> <html> <head> <meta charset="utf-8"> </head> <body> <h2>空调浇水控制</h2> <!-- 创建一个ID位dht的盒子用于显示获取到的数据 --> <div id="dht"> </div> <button onclick="set()"> 开启/关闭浇水 </button> <button onclick="autoset()"> 开启/关闭自动 </button> </body> <script> // 按下按钮会运行这个JS函数 function set() { var payload = "ESP32"; // 需要发送的内容 // 通过get请求给 /set var xhr = new XMLHttpRequest(); xhr.open("GET", "/set?value=" + payload, true); xhr.send(); } function autoset() { var payload = "ESP32"; // 需要发送的内容 // 通过get请求给 /set var xhr = new XMLHttpRequest(); xhr.open("GET", "/auto?value=" + payload, true); xhr.send(); } // 设置一个定时任务, 1000ms执行一次 setInterval(function () { var xhttp = new XMLHttpRequest(); xhttp.onreadystatechange = function () { if (this.readyState == 4 && this.status == 200) { // 此代码会搜索ID为dht的组件,然后使用返回内容替换组件内容 document.getElementById("dht").innerHTML = this.responseText; } }; // 使用GET的方式请求 /dht xhttp.open("GET", "/dht", true); xhttp.send(); }, 200) </script>)rawliteral";
Adafruit_MPU6050 mpu; #define mpu6050TimeInterval 100 unsigned long mpu6050Times = 0; float mpu6050Temp = 0; float xAcceleration , yAcceleration , zAcceleration ; float xAccele , yAccele , zAccele ; float xGyro = 0, yGyro = 0, zGyro = 0; float gravity = 9.8; int isdone=0; bool iswater=0,isauto=1;
String Merge_Data(void) { int Humidity = 50; int Temperature = 26; String dataBuffer = "<p>"; dataBuffer += "<h1>传感器数据 </h1>"; dataBuffer += "<b>温度: </b>"; dataBuffer += String(mpu6050Temp); dataBuffer += "<br />"; dataBuffer += "<b>当前浇水状态: </b>"; dataBuffer += String(iswater); dataBuffer += "<br />"; dataBuffer += "<b>当前自动状态: </b>"; dataBuffer += String(isauto); dataBuffer += "<br /></p>"; return dataBuffer; }
void Config_Callback(AsyncWebServerRequest *request) { iswater=!iswater; isauto=0; request->send(200, "text/plain", "OK"); }
void Auto_Callback(AsyncWebServerRequest *request) { isauto=!isauto; request->send(200, "text/plain", "OK"); } void setup() {
WiFi.begin("Gundan","lqy170309"); while(WiFi.status()!=WL_CONNECTED){ delay(500); Serial.println("正在连接..."); } Serial.println("连接成功!");
pinMode(1,OUTPUT);
server.on("/", HTTP_GET, [](AsyncWebServerRequest *request) { request->send_P(200, "text/html", index_html); }); server.on("/dht", HTTP_GET, [](AsyncWebServerRequest *request) { request->send_P(200, "text/plain", Merge_Data().c_str()); }); server.on("/set", HTTP_GET, Config_Callback); server.on("/auto", HTTP_GET, Auto_Callback); server.begin();
Serial.begin(115200); if (!mpu.begin()) { while (millis() - 1000) { Serial.println("Failed to find MPU6050 chip"); } } mpu.setAccelerometerRange(MPU6050_RANGE_16_G); mpu.setGyroRange(MPU6050_RANGE_250_DEG); mpu.setFilterBandwidth(MPU6050_BAND_21_HZ); Serial.println("Go online!"); }
void loop() { getMpu6050Data(); if(isauto==1){ if(mpu6050Temp>=30){ digitalWrite(1,HIGH); iswater=1; }else if(mpu6050Temp<30){ digitalWrite(1,LOW); iswater=0; } }else if(isauto==0){ digitalWrite(1,iswater); } }
void getMpu6050Data() { if (millis() - mpu6050Times >= mpu6050TimeInterval) { mpu6050Times = millis(); sensors_event_t a, g, temp; mpu.getEvent(&a, &g, &temp); mpu6050Temp = temp.temperature; xAcceleration = a.acceleration.x ; yAcceleration = a.acceleration.y ; zAcceleration = a.acceleration.z ; xAccele = xAcceleration / gravity; yAccele = yAcceleration / gravity; zAccele = zAcceleration / gravity; xGyro = g.gyro.x; yGyro = g.gyro.y; zGyro = g.gyro.z; Serial.print("Temp: "); Serial.print(mpu6050Temp); Serial.print(" , x-accele: "); Serial.print(xAccele); Serial.print(" , y-accele: "); Serial.print(yAccele); Serial.print(" , z-accele: "); Serial.print(zAccele); Serial.print(" , x-gyro:"); Serial.print(xGyro); Serial.print(" , y-gyro:"); Serial.print(yGyro); Serial.print(" , z-gyro:"); Serial.println(zGyro); } }
|