|
小白我借助例程试着编译个小程序,用ESP8266向HA的Mqtt发送温湿度数据,数据倒是送到HA里面了,但是因为自己一知半解,程序输出串口监测没问题,但不能向mqtt成功发送浮点值,只能把值定义为整数(int)才能正确显示,但我想精确到小数点后面一位数,反复试了一晚上,C++基础太差搞不定啊!只能请教请教论坛的大佬们,不胜感激!用的传感器是SHT31。
#include <Arduino.h>
#include <ESP8266WiFi.h>
#include <ubSubClient.h>
#include <Wire.h>
#include "Adafruit_SHT31.h"
bool enableHeater = false;
uint8_t loopCnt = 0;
Adafruit_SHT31 sht31 = Adafruit_SHT31();
// Update these with values suitable for your network.
const char* ssid = "RickyHome2.4GHz";
const char* password = "12345678";
const char* mqtt_server = "172.18.8.6";
#define MSG_BUFFER_SIZE (50)
char msg[MSG_BUFFER_SIZE];
//C++11引入了Raw字符串,不用以上的转义斜杠,换行也不用斜杠,只要用R"()"包起来,方便写代码了
char temperature[] = R"({
"device_class": "temperature",
"name": "outdoor_Temperature",
"state_topic": "homeassistant/sensor/outdoor/state",
"unit8_of_measurement": "°C","value_template": "{{ value_json.temperature}}"})";
char Humidity[] = R"({
"device_class": "humidity",
"name": "outdoor_Humidity",
"state_topic": "homeassistant/sensor/outdoor/state",
"unit8_of_measurement": "%",
"value_template": "{{ value_json.humidity}}"})";
char Text[] = R"({
"device_class": "TEXT",
"name": "outdoor_TEXT",
"state_topic": "homeassistant/sensor/outdoor/state",
"value_template": "{{ value_json.humidity}}"})";
WiFiClient espClient;
PubSubClient client(espClient);
void setup_wifi() {
delay(10);
// We start by connecting to a WiFi network
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
randomSeed(micros());
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}
void reconnect() {
// Loop until we're reconnected
while (!client.connected()) {
Serial.print("Attempting MQTT connection...");
if(client.connect("homeassistant/sensor/", "mqtt", "12345678"))//clientID, userName, userPassword
{
client.publish("homeassistant/sensor/outdoor_H/config", Humidity);
client.publish("homeassistant/sensor/outdoor_T/config", temperature);
client.publish("homeassistant/sensor/outdoor_TEXT/config", "text");
} else {
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 5 seconds");
// Wait 5 seconds before retrying
delay(20000);
}
}
}
void sht31_read() {
float temperature = sht31.readTemperature();
float humidity = sht31.readHumidity();
Serial.print("H:");
Serial.print(humidity);
Serial.println(" %RH");
Serial.print("C:");
Serial.print(temperature);
Serial.println("°C");
Serial.print((int)temperature);
Serial.print("°C");
Serial.print((int)humidity);
Serial.println("%RH");
char msg[100];
sprintf(msg,"{\"temperature\":%d, \"humidity\":%d}",(int)temperature,(int)humidity);
client.publish("homeassistant/sensor/outdoor/state", msg);
// sht31 sampling rate is 1HZ.
return ;
}
void setup() {
Serial.begin(9600);
setup_wifi();
client.setServer(mqtt_server, 1883);
if (!client.connected()) {
reconnect();
}
{
//SHT31
while (!Serial)
delay(500); // will pause Zero, Leonardo, etc until serial console opens
Serial.println("SHT31 Sensor");
if (! sht31.begin(0x44)) { // Set to 0x45 for alternate i2c addr
Serial.println("Couldn't find SHT31");
while (1) delay(1); }
Serial.print("Heater Enabled State: ");
if (sht31.isHeaterEnabled())
Serial.println("ENABLED");
else
Serial.println("DISABLED");}
}
unsigned long lastMsg = 0;
int value = 1;
void loop() {
if (!client.connected()) {reconnect();}
client.loop();
unsigned long now = millis();
if (now - lastMsg > 60000) {
lastMsg = now;
++value;
sht31_read();
}
}
|
-
|