#define BLINKER_WIFI
#define soilPin A0 ////////////////////////A0插上土壤检测的数据线口
//存储中间值
int soilValue;
//定义土壤湿度
int soilMoisture;
#include <Blinker.h>
//blinker 提供的key
char auth[] = "f0191fbd";
//你的wifi名字
char ssid[] = "54M";
//你的wifi密码
char pswd[] = "1vgjuguihghg";
//发送到 blinker 的湿度数据key
BlinkerNumber HUMI("humi");
//发送到 blinker 的温度数据key
BlinkerNumber TEMP("temp");
BlinkerNumber SOIL("soil");//发送到 blinker 的土壤数据key
//引入 DHT 库
#include <DHT.h>
#define DHTPIN D8 ///////定义输入温度湿度的针脚为D8
#define DHTTYPE DHT11 // DHT 11
//#define DHTTYPE DHT22 // DHT 22 (AM2302), AM2321
//#define DHTTYPE DHT21 // DHT 21 (AM2301)
DHT dht(DHTPIN, DHTTYPE);
uint32_t read_time = 0;
float humi_read, temp_read, soil_read;
void dataRead(const String & data)
{
BLINKER_LOG("Blinker readString: ", data);
Blinker.vibrate();
uint32_t BlinkerTime = millis();
Blinker.print("millis", BlinkerTime);
}
void heartbeat()
{
HUMI.print(humi_read);
TEMP.print(temp_read);
SOIL.print(soil_read);
}
void dataStorage() {
//添加数据存储 以便于图标数据展示
Blinker.dataStorage("humi", humi_read);
//添加数据存储 以便于图标数据展示
Blinker.dataStorage("temp", temp_read);
Blinker.dataStorage("soil", soil_read);
}
void setup()
{
Serial.begin(115200);
BLINKER_DEBUG.stream(Serial);
pinMode(LED_BUILTIN, OUTPUT);
digitalWrite(LED_BUILTIN, LOW);
Blinker.begin(auth, ssid, pswd);
Blinker.attachData(dataRead);
Blinker.attachHeartbeat(heartbeat);
Blinker.attachDataStorage(dataStorage);
dht.begin();
}
void loop()
{
Blinker.run();
//读取土壤传感器上的电压值
soilValue = analogRead(soilPin);
//把电压值按照[0,1023]映射到[100,0]
soilMoisture = map(soilValue,0,1023,100,0);
//每隔一秒输出一次值
delay(2000);
if (read_time == 0 || (millis() - read_time) >= 2000)
{
read_time = millis();
//读取湿度
float h = dht.readHumidity();
//读取温度
float t = dht.readTemperature();
float s = soilMoisture;
if (isnan(h) || isnan(t)) {
BLINKER_LOG("Failed to read from DHT sensor!");
return;
}
float hic = dht.computeHeatIndex(t, h, false);
humi_read = h;
temp_read = t;
soil_read = s;
BLINKER_LOG("Humidity: ", h, " %");
BLINKER_LOG("Temperature: ", t, " *C");
BLINKER_LOG("Heat index: ", hic, " *C");
BLINKER_LOG("soil: ", s, "% ");
}
}
原文转载自:【pico论坛】https://bbs.picoxr.com/post/84691
原文链接:【VR游戏网】https://vr.lxybaike.com/blog/27979.html,转载请注明出处。
请先
!