本文主要是介绍SHT30通过blinker获取温湿度,小爱同学查询温湿度,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
功能介绍:1、通过Blinker读取和统计温湿度历史数据;2、小爱同学查询温湿度;
硬件:SHT30传感器、ESP8266
程序介绍:第一部分为blinker界面配置(可复制到blinker APP内,直接配置界面);第二部分为SHT3x.h库的内容;第三部分为arduino编译的程序(如果没有库,复制命名为SHT3x.h然后放在和arduino程序同一个文件夹里),第三部分为blinker界面配置(可复制到blinker APP内,直接配置界面)
界面配置:
{¨config¨{¨headerColor¨¨transparent¨¨headerStyle¨¨dark¨¨background¨{¨img¨¨assets/img/headerbg.jpg¨¨isFull¨«}}¨dashboard¨|{¨type¨¨num¨¨t0¨¨温度¨¨ico¨¨fad fa-thermometer-three-quarters¨¨clr¨¨#389BEE¨¨min¨É¨max¨¤o¨uni¨¨°C¨¨bg¨É¨cols¨Í¨rows¨Ë¨key¨¨temp¨´x´É´y´Ñ¨speech¨|÷¨lstyle¨Ê}{ßAßBßC¨湿度¨ßE¨fad fa-humidity¨ßGßHßIÉßJ¢1cßK´%´ßMÉßNÍßOËßP¨humi¨´x´Í´y´ÑßR|÷ßSÊ}{ßA¨cha¨ßMɨsty¨¨line¨ßG¨#EA0909¨¨sty1¨ßY¨clr1¨¨#076EEF¨¨sty2¨ßY¨clr2¨ßHßNÑßOÍßPßQ´x´É´y´¤AßR|÷¨key2¨´´¨key0¨ßQßCßD¨t1¨ßT¨key1¨ßVßSÊ}÷¨actions¨|¦¨cmd¨¦¨switch¨‡¨text¨‡¨on¨¨打开?name¨¨off¨¨关闭?name¨—÷¨triggers¨|{¨source¨ßl¨source_zh¨¨开关状态¨¨state¨|ßnßp÷¨state_zh¨|¨打开¨¨关闭¨÷}÷}
库文件:<SHT3x.h>
/*Arduino library for Sensirion temperature and humidity sensors SHT30, SHT31 & SHT35.the heavy version.Check for /examples for examples of different use cases.The datasheet I followed is:https://www.sensirion.com/fileadmin/user_upload/customers/sensirion/Dokumente/2_Humidity_Sensors/Sensirion_Humidity_Sensors_SHT3x_Datasheet_digital.pdfFor more simple version check the SimpleSHT3x library.The constructor structure:SHT3x( int Address = 0x44, //I2C device address, 0x44 or 0x45ValueIfError Value = Zero, //What to return in case of errors. Zero or PrevValueuint8_t HardResetPin = 255, //Number of pin RESET connected to (input from 100 to 255 if not used)SHT3xSensor SensorType = SHT30, //Sensor type, SHT30, SHT31 or SHT35.SHT3xMode Mode=Single_HighRep_ClockStretch //Operation mode , look for "enum SHT3xMode"); Supports:Temperature data at Celsius, Kelvin and Fahrenheit scales.Relative humidity data.Absolute humidity data at Torr, mm Hg, Pa, bar, technical and standard atmosphere, psi scales.Data integrity (by CRC8 algorithm) (datasheet/section 4.12).Temperature, relative and absolute humidity tolerances (in dependence from measured values)Calibration (linear) of temperature and humidity data by factors or by reverse sensor values (2 points)Heater On/Off (integrated to SHT3x sensor) (datasheet/section 4.10)Different sensor actions modes (datasheet/section 4.3)Reset: soft (I2C) and hard (by corresponding pin) (datasheet/section 4.9)Do not supports:Action in periodic mode (datasheet/section 4.5)Interrupts (datasheet/section 3.5)Note 1: by default, the data from sensor updates not faster, than 2 times a second.For faster update use SetUpdateInterval(uint32_t UpdateIntervalMillisec); but do not exceed the datasheet values (10 measurments per second (100 ms)) because of sensor self-heating (datasheet/section 4.5, at the end of Table 9)Note 2: The sensor type affects the tolerance values only. Created by Risele for everyone's use (profit and non-profit).ALL THESE WOR_DSARE YOURS EXCEPTRISELEATTEMPT NOnamechangING THEREUSE THEM TOGETHERUSE THEM IN PEACE*/#ifndef SHT3x_h#define SHT3x_h//Arduino standart libraries#if defined(ARDUINO) && ARDUINO >= 100#include "Arduino.h"#else#include "WProgram.h"#endif //Arduino I2C/TWI library#include <Wire.h>//For calculating the tolerance of absolute humidity#include <math.h> class SHT3x{public:enum ValueIfError //Define, what to return in case of error: Zero or previous value{Zero,PrevValue};enum SHT3xMode{Single_HighRep_ClockStretch,Single_MediumRep_ClockStretch,Single_LowRep_ClockStretch,Single_HighRep_NoClockStretch,Single_MediumRep_NoClockStretch,Single_LowRep_NoClockStretch };enum SHT3xSensor{SHT30,SHT31,SHT35};enum TemperatureScale{Cel,Far,Kel};enum AbsHumidityScale{mmHg,Torr, //same as mm HgPa,Bar,At, //Techical atmosphereAtm, //Standart atmospheremH2O,psi,};struct CalibrationPoints{float First;float Second;};struct CalibrationFactors{CalibrationFactors():Factor(1.), Shift(0.){}float Factor;float Shift;};SHT3x( int Address = 0x44,ValueIfError Value = Zero,uint8_t HardResetPin = 255,SHT3xSensor SensorType = SHT30,SHT3xMode Mode=Single_HighRep_ClockStretch);void Begin();void UpdateData();float GetTemperature(TemperatureScale Degree = Cel);float GetRelHumidity();float GetAbsHumidity(AbsHumidityScale Scale = Torr);float GetTempTolerance(TemperatureScale Degree = Cel, SHT3xSensor SensorType = SHT30);float GetRelHumTolerance(SHT3xSensor SensorType = SHT30);float GetAbsHumTolerance(AbsHumidityScale Scale = Torr, SHT3xSensor SensorType = SHT30);uint8_t GetError();void SetMode(SHT3xMode Mode = Single_HighRep_ClockStretch);void SetTemperatureCalibrationFactors(CalibrationFactors TemperatureCalibration);void SetRelHumidityCalibrationFactors(CalibrationFactors RelHumidityCalibration);void SetTemperatureCalibrationPoints(CalibrationPoints SensorValues, CalibrationPoints Reference);void SetRelHumidityCalibrationPoints(CalibrationPoints SensorValues, CalibrationPoints Reference);void SoftReset();void HardReset();void HeaterOn();void HeaterOff();void SetAddress(uint8_t NewAddress);void SetUpdateInterval(uint32_t UpdateIntervalMillisec);void SetTimeout(uint32_t TimeoutMillisec);private:float _TemperatureCeil;float _RelHumidity;bool _OperationEnabled = false;uint8_t _HardResetPin;ValueIfError _ValueIfError;uint8_t _MeasLSB; //Data read command, Most Significant Byte uint8_t _MeasMSB; //Data read command, Most Significant Byte uint8_t _Address;SHT3xSensor _SensorType;uint32_t _UpdateIntervalMillisec = 500;uint32_t _LastUpdateMillisec = 0;uint32_t _TimeoutMillisec = 100;void SendCommand(uint8_t MSB, uint8_t LSB);bool CRC8(uint8_t MSB, uint8_t LSB, uint8_t CRC);float ReturnValueIfError(float Value);void ToReturnIfError(ValueIfError Value);CalibrationFactors _TemperatureCalibration;CalibrationFactors _RelHumidityCalibration;/* * Factors for poly for calculating absolute humidity (in Torr):* P = (RelativeHumidity /100%) * sum(_AbsHumPoly[i]*T^i)* where P is absolute humidity (Torr/mm Hg),* T is Temperature(Kelvins degree) / 1000,* ^ means power.* For more data, check the NIST chemistry webbook:* http://webbook.nist.gov/cgi/cbook.cgi?ID=C7732185&Units=SI&Mask=4&Type=ANTOINE&Plot=on#ANTOINE*/float _AbsHumPoly[6] ={-157.004, 3158.0474, -25482.532, 103180.197, -209805.497, 171539.883}; enum Errors{noError = 0,Timeout = 1,DataCorrupted = 2,WrongAddress = 3,//I2C errorsTooMuchData = 4,AddressNACK = 5,DataNACK = 6,OtherI2CError = 7,UnexpectedError = 8} _Error;void I2CError(uint8_t I2Canswer);};
#endif //SHT3x_h
arduino编译程序:
/*
*SHT30 通过I2C接口测量温湿度,ESP8266的D1与SHT30的SCL相连、D2与SDA相连
*/
#define BLINKER_WIFI
#define BLINKER_ESP_SMARTCONFIG //Smartconfig手机配网用这段,代码写入注释这一条
#define BLINKER_WITHOUT_SSL //非SSL加密通信接入,省堆栈
#define BLINKER_MIOT_SENSOR#include <Blinker.h>
#include <SHT3x.h>
SHT3x Sensor; //默认传感器类型为SHT30char auth[] = "Blinker APP key"; //blinker账户生产的密码,小爱同学必须选用阿里云
//代码配网用下面这两段
//char ssid[] = "WIFI ssid"; //WiFi账号
//char pswd[] = "WiFi pswd"; //WiFi密码//定义blinker软件内数据组件键名分别为:humi、temp
BlinkerNumber HUMI("humi");
BlinkerNumber TEMP("temp");float humi_read, temp_read;//小米小爱状态回调函数
void miotQuery(int32_t queryCode)
{BLINKER_LOG("MIOT Query codes: ", queryCode);int hVal = humi_read; //多次测试湿度须为整数型switch (queryCode){case BLINKER_CMD_QUERY_ALL_NUMBER :BLINKER_LOG("MIOT Query All");BlinkerMIOT.temp(temp_read);BlinkerMIOT.humi(hVal);BlinkerMIOT.print();break;default :BlinkerMIOT.temp(100);BlinkerMIOT.humi(100);BlinkerMIOT.print();break;}
}void heartbeat()
{TEMP.print(temp_read);HUMI.print(humi_read);
}void dataStorage()
{
Blinker.dataStorage("temp", temp_read);
Blinker.dataStorage("humi", humi_read);
}
void setup()
{//调试程序Serial.begin(115200);BLINKER_DEBUG.stream(Serial);Sensor.Begin(); //初始化温湿度传感器pinMode(LED_BUILTIN, OUTPUT);digitalWrite(LED_BUILTIN, LOW);Blinker.begin(auth); //手机配网用这段//Blinker.begin(auth, ssid, pswd); //代码配网用这段Blinker.attachHeartbeat(heartbeat);Blinker.attachDataStorage(dataStorage);//注册小爱同学状态回调函数BlinkerMIOT.attachQuery(miotQuery);
}void loop()
{Blinker.run();Sensor.UpdateData(); //更新传感器数据temp_read = Sensor.GetTemperature(); //读取温度humi_read = Sensor.GetRelHumidity();//调试窗口输出if (isnan(temp_read) || isnan(humi_read)){BLINKER_LOG("Failed to read from SHT sensor!");}else{BLINKER_LOG("Humidity: ", humi_read, " %");BLINKER_LOG("Temperature: ", temp_read, " °C");}Blinker.delay(1000);}
这篇关于SHT30通过blinker获取温湿度,小爱同学查询温湿度的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!