#include <Arduino.h>
#include <ESP8266WiFi.h>
#include <ESP8266WiFiMulti.h>
#include <ESP8266HTTPClient.h>
#include <SimpleDHT.h>
ESP8266WiFiMulti WiFiMulti;
WiFiClient wifiClient;
byte temperature = 0;
byte humidity = 0;
int ledPin = 2;//板载led ==2 到D3==2
int pinDHT11 = 2; //IO2
SimpleDHT11 dht11(pinDHT11);
//请求地址
#define HTTPIP "test.sangsang.xyz"
#define HTTPPORT 8767
#define WIFINAME "MERCURY_8E92"
#define WIFIPW "Hmzz502502"
void dht(){
int err = SimpleDHTErrSuccess;
if ((err = dht11.read(&temperature, &humidity, NULL)) != SimpleDHTErrSuccess) {
Serial.print("Read DHT11 failed, err="); Serial.println(err);delay(1000);
return;
}
}
void setup() {
//打开串口
Serial.begin(115200);
Serial.println();
Serial.println("wifi ok????");
WiFiMulti.addAP(WIFINAME,WIFIPW);
while(WiFiMulti.run()!=WL_CONNECTED)
{
delay(500);
Serial.print(">> ?..?");
}
Serial.print("wifi ok");
pinMode(ledPin, OUTPUT);
digitalWrite(ledPin, LOW);
// pinMode(pin0, OUTPUT);
//digitalWrite(pin0, LOW);
//pinMode(pin2, OUTPUT);
//digitalWrite(pin2, LOW);
}
void loop() {
dht();
HTTPClient httpClient;
Serial.println("go to http");
//连接指定的网页ip和端口,以及路径
String h = (String)humidity;
String c = (String)temperature;
//Serial.print("c:")
Serial.println(c);
//Serial.print("h:");
Serial.println(h);
httpClient.begin(wifiClient,HTTPIP,HTTPPORT,"/dht/upd?id=10&h="+h+"&c="+c);
int Code = httpClient.GET();
String htmls = "";
Serial.println(httpClient.getString());
//串口回复
Serial.println("hello,sl!");
//设置等待时间30s
delay(30000);
}
ESP01s通过HTTP上传环境温湿度(需自备后台接口)
最新推荐文章于 2024-01-27 20:24:44 发布