|
各位大佬,请教下18b20上传温度到点灯科技当usb线插在板子,app能获取到温度,拔掉usb在刷新app读到-127。请问这是什么原因
#define BLINKER_WIFI
#define BLINKER_MIOT_MULTI_OUTLET //设置为小爱多个插座的模式
#define BLINKER_ALIGENIE_MULTI_OUTLET
#define BLINKER_MIOT_SENSOR
#define BLINKER_PRINT Serial
#include <OneWire.h>
#include <DallasTemperature.h>
#include <Blinker.h>
#include <ESP8266WiFi.h>
#include <DHT.h>
#define DHTTYPE DHT11
OneWire ds18b20(13);
DallasTemperature sensors(&ds18b20);
char auth[] = "**********";
//char ssid[] = "CMCC-happyking";
//char pswd[] = "12349876";
//多路继电器控制////////////
int gpio[] = {LED_BUILTIN, D1, D2, D3, D5}; //1=D5 2=D6 3=D7 4=D8
//#define DHTPIN D7
//float h;
float humi_read = 0, temp_read = 0;
//////////////////////////
bool WIFI_Status = true;
void smartConfig()
{
WiFi.mode(WIFI_STA);
Serial.println("\r\nWait for Smartconfig...");
WiFi.beginSmartConfig();//等待手机端发出的用户名与密码
while (1)
{
Serial.print(".");
digitalWrite(LED_BUILTIN, HIGH);
delay(1000);
digitalWrite(LED_BUILTIN, LOW);
delay(1000);
if (WiFi.smartConfigDone())//退出等待
{
Serial.println("SmartConfig Success");
Serial.printf("SSID:%s\r\n", WiFi.SSID().c_str());
Serial.printf("PSW:%s\r\n", WiFi.psk().c_str());
break;
}
}
}
void WIFI_Set()//
{
//Serial.println("\r\n正在连接");
int count = 0;
while(WiFi.status()!=WL_CONNECTED)
{
if(WIFI_Status)
{
Serial.print(".");
digitalWrite(LED_BUILTIN, HIGH);
delay(500);
digitalWrite(LED_BUILTIN, LOW);
delay(500);
count++;
if(count>=5)//5s
{
WIFI_Status = false;
Serial.println("WiFi连接失败,请用手机进行配网");
}
}
else
{
smartConfig(); //微信智能配网
}
}
/* Serial.println("连接成功");
Serial.print("IP:");
Serial.println(WiFi.localIP());*/
}
//DHT dht(DHTPIN, DHTTYPE);
//float sensors;
// 新建组件对象
BlinkerButton Button1("k1"); //按钮1
BlinkerButton Button2("k2"); //按钮2
BlinkerButton Button3("k3"); //按钮3
BlinkerButton Button4("k4"); //按钮4
BlinkerNumber HUMI("humi");
BlinkerNumber TEMP("temp");
///////////////LED蓝色小灯闪烁5次开始////////////
void led()
{
for (uint8_t o_num = 0; o_num < 5; o_num++)
{
delay(80);
digitalWrite(gpio[0], LOW);
delay(80);
digitalWrite(gpio[0], HIGH);
delay(80);
}
}
///////////////LED蓝色小灯闪烁5次结束////////////
////////////////反馈按键状态开始/////////////////
void Set_Butt(int num) //on反馈
{
if (num == 1) {
Button1.color("#228B22"); //设置app按键是绿色
Button1.text(String(num)+"海灯已打开");
Button1.print("on");
}
else if (num == 2) {
Button2.color("#228B22"); //设置app按键是绿色
Button2.text(String(num)+"藻灯已打开");
Button2.print("on");
}
else if (num == 3) {
Button3.color("#228B22"); //设置app按键是绿色
Button3.text(String(num)+"造浪已打开");
Button3.print("on");
}
else if (num == 4) {
Button4.color("#228B22"); //设置app按键是绿色
Button4.text(String(num)+"备用已打开");
Button4.print("on");
}
}
void Reset_Butt(int num) //off反馈
{
if (num == 1) {
Button1.color("#778899"); //设置app按键是灰色
Button1.text(String(num)+"海灯已关闭");
Button1.print("off");
}
else if (num == 2) {
Button2.color("#778899"); //设置app按键是灰色
Button2.text(String(num)+"藻灯已关闭");
Button2.print("off");
}
else if (num == 3) {
Button3.color("#778899"); //设置app按键是灰色
Button3.text(String(num)+"造浪已关闭");
Button3.print("off");
}
else if (num == 4) {
Button4.color("#778899"); //设置app按键是灰色
Button4.text(String(num)+"备用已关闭");
Button4.print("off");
}
}
////////////////反馈按键状态结束/////////////////
/////////////控制代码开始///////////////
void control(int num, const String state)
{
BLINKER_LOG("按键"+String(num)+"状态 : ", state); //串口打印
Blinker.delay(150); //延时150ms不能太少标准按键成功率
if (state == BLINKER_CMD_ON) { //如果state是on状态
BLINKER_LOG(String(num)+"设备已打开"); //串口打印
digitalWrite(gpio[num], HIGH);
Set_Butt(num);
led();
}
else if (state == BLINKER_CMD_OFF) { //如果state是off状态
BLINKER_LOG(String(num)+"设备已关闭"); //串口打印
digitalWrite(gpio[num],LOW);
Reset_Butt(num);
led();
}
}
/////////////控制代码结束///////////////
////////////////按键控制函数开始////////////////////////
void button1_callback(const String & state)
{
control(1, state);
}
/////////////////////////////////////////////////////
void button2_callback(const String & state)
{
control(2, state);
}
void button3_callback(const String & state)
{
control(3, state);
}
void button4_callback(const String & state)
{
control(4, state);
}
////////////////按键控制函数结束//////////////////////////////
bool oState[5] = { false };
void aligeniePowerState(const String & state, uint8_t num)
{
BLINKER_LOG("need set power state: ", state);
if (state == BLINKER_CMD_ON) {
BlinkerAliGenie.powerState("on", num);
BlinkerAliGenie.print();
BLINKER_LOG(String(num)+"设备已打开"); //串口打印
digitalWrite(gpio[num],HIGH);
Set_Butt(num);
oState[num] = true;
led();
}
else if (state == BLINKER_CMD_OFF) {
BlinkerAliGenie.powerState("off", num);
BlinkerAliGenie.print();
BLINKER_LOG(String(num)+"设备已关闭"); //串口打印
digitalWrite(gpio[num], LOW);
Reset_Butt(num);
oState[num] = false;
led();
if (num == 0)
{
for (uint8_t o_num = 0; o_num < 5; o_num++)
{
oState[o_num] = false;
if(o_num > 0)
{
digitalWrite(gpio[o_num], LOW);
led();
BLINKER_LOG(String(o_num)+"设备已关闭");
Reset_Butt(o_num);
}
}
BLINKER_LOG("所有设备已关闭");
}
}
}
//小爱接口函数开始///////
//bool oState[5] = { false };
void miotPowerState(const String & state, uint8_t num)
{
BLINKER_LOG("need set outlet: ", num, ", power state: ", state);
if (state == BLINKER_CMD_ON) {
BlinkerMIOT.powerState("on", num);
BlinkerMIOT.print();
BLINKER_LOG(String(num)+"设备已打开"); //串口打印
digitalWrite(gpio[num],HIGH);
Set_Butt(num);
oState[num] = true;
led();
}
else if (state == BLINKER_CMD_OFF) {
BlinkerMIOT.powerState("off", num);
BlinkerMIOT.print();
BLINKER_LOG(String(num)+"设备已关闭"); //串口打印
digitalWrite(gpio[num], LOW);
Reset_Butt(num);
oState[num] = false;
led();
if (num == 0)
{
for (uint8_t o_num = 0; o_num < 5; o_num++)
{
oState[o_num] = false;
if(o_num > 0)
{
digitalWrite(gpio[o_num], LOW);
led();
BLINKER_LOG(String(o_num)+"设备已关闭");
Reset_Butt(o_num);
}
}
BLINKER_LOG("所有设备已关闭");
}
}
}
//小爱接口函数结束///////
void miotQuery(int32_t queryCode, uint8_t num)
{
BLINKER_LOG("AliGenie Query outlet: ", num,", codes: ", queryCode);
// int hVal = dht.readHumidity(); //多次测试湿度必须为整数型
switch (queryCode)
{
case BLINKER_CMD_QUERY_ALL_NUMBER :
BLINKER_LOG("MIOT Query All");
BlinkerMIOT.powerState(oState[num] ? "on" : "off", num);
//BlinkerMIOT.temp(temp_read);
//BlinkerMIOT.humi(hVal);
BlinkerMIOT.print();
break;
case BLINKER_CMD_QUERY_POWERSTATE_NUMBER :
BLINKER_LOG("MIOT Query Power State");
BlinkerMIOT.powerState(oState[num] ? "on" : "off", num);
BlinkerMIOT.print();
break;
default :
BlinkerMIOT.powerState(oState[num] ? "on" : "off", num);
//BlinkerMIOT.temp(100);
//BlinkerMIOT.humi(100);
BlinkerMIOT.print();
break;
}
}
//*******app定时向设备发送心跳包, 设备收到心跳包后会返回设备当前状态30s~60s一次
void heartbeat()
{
sensors.requestTemperatures();
float h = sensors.getTempCByIndex(0);
float t = sensors.getTempCByIndex(0);
humi_read = h;
temp_read = t;
BLINKER_LOG("心跳包状态同步!");
HUMI.print(humi_read);
TEMP.print(temp_read);
for (uint8_t o_num = 0; o_num < 5; o_num++)
{
if (digitalRead(gpio[o_num])==HIGH)
{
Set_Butt(o_num); //返回开关状态给APP ON
}
else
{
Reset_Butt(o_num); //返回开关状态给APP OFF
}
}
}
/////////心跳包函数结束//////////////////////////////////////////////
void dataRead(const String & data)
{
BLINKER_LOG("Blinker readString: ", data);
Blinker.vibrate();
uint32_t BlinkerTime = millis();
Blinker.print("millis", BlinkerTime);
}
void dataStorage()
{
sensors.requestTemperatures();
float h = sensors.getTempCByIndex(0);
float t = sensors.getTempCByIndex(0);
humi_read = h;
temp_read = t;
Blinker.dataStorage("temp", temp_read);
Blinker.dataStorage("humi", humi_read);
}
void setup()
{
Serial.begin(115200);
sensors.begin();
BLINKER_DEBUG.stream(Serial);
WIFI_Set();
Blinker.begin(auth,WiFi.SSID().c_str(),WiFi.psk().c_str());
//////////////注册控制端口开始////////////////
for (uint8_t o_num = 0; o_num < 5; o_num++)
{
pinMode(gpio[o_num], OUTPUT);
digitalWrite(gpio[o_num], LOW);
}
//////////////注册控制端口结束////////////////
// Blinker.begin(auth, ssid, pswd); //连网参数
Blinker.attachData(dataRead);
Button1.attach(button1_callback);
Button2.attach(button2_callback);
Button3.attach(button3_callback);
Button4.attach(button4_callback);
BlinkerMIOT.attachPowerState(miotPowerState); //小爱语音操作注册函数
BlinkerMIOT.attachQuery(miotQuery); //小爱同学 数据反馈
Blinker.attachHeartbeat(heartbeat); //app定时向设备发送心跳包
Blinker.attachDataStorage(dataStorage);
BlinkerAliGenie.attachPowerState(aligeniePowerState);
led();
// dht.begin();
}
void loop()
{
Blinker.run();
sensors.requestTemperatures();
float h = sensors.getTempCByIndex(0);
float t = sensors.getTempCByIndex(0);
if (isnan(h)||isnan(t) )
{
BLINKER_LOG("Failed to read from DHT sensor!");
humi_read = h;
temp_read = t;
}else
{
BLINKER_LOG("Humidity: ", h, " ℃");
BLINKER_LOG("Temperature: ", t, " *C");
humi_read = h;
temp_read = t;
}
Blinker.delay(2000);
}
|
|