/*
範例名稱:無線氣象台
版本:wifi-line notify
*/
#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BME280.h>
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <TridentTD_LineNotify.h>
// 修改成上述寄到登入郵箱的 Token號碼
#define LINE_TOKEN "申請LINE Notify產生的Token號碼"
#include <PubSubClient.h>
// 設定無線基地台SSID跟密碼
const char* ssid = "您的SSID";
const char* password = "SSID密碼";
// 將變數值更改為HomeAssistant IP地址,以便連接到MQTT Broker
const char* mqtt_server = "ip";
// 若MQTT Broker有設定認證請輸入自己設定的帳密
const char* mqtt_user = "mqtt username";
const char* mqtt_password = "mqtt password";
// 初始化espClient。如果您在家庭自動化系統中運行多個ESP,則應更改每個espClient變數名稱
WiFiClient espClient;
PubSubClient client(espClient);
Adafruit_BME280 bme; // I2C
// 輔助計時器變數
long now = millis();
long lastMeasure = 0;
//設定wifi初始化
void setup_start(){
WiFi.mode(WIFI_STA);
// 連接無線基地台
WiFi.begin(ssid, password);
Serial.print("\n\r \n\rWorking to connect");
// 等待連線,並從 Console顯示 IP
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.print("Connected to ");
Serial.println(ssid);
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
bool status;
Serial.println(F("BMP280 test"));
status = bme.begin(0x76);
if (!status) {
Serial.println("Could not find a valid BMP280 sensor, check wiring!");
while (1);
}
client.setServer(mqtt_server, 1883);
}
// 此功能將您的ESP8266重新連接到MQTT broker
// 如果要與ESP8266訂閱更多主題,請更改下面的功能
void reconnect() {
// 循環直到我們重新聯繫
while (!client.connected()) {
Serial.print("Attempting MQTT connection...");
String clientId = "ESP8266Client-";
clientId += String(random(0xffff), HEX);
if (client.connect(clientId.c_str(),mqtt_user,mqtt_password)) {
Serial.println("connected");
// 訂閱或重新訂閱主題
// 您可以訂閱更多主題 (可以控制更多燈泡等等)
client.subscribe("hotwater/control");
} else {
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 5 seconds");
// 重試前等待5秒鐘
delay(5000);
}
}
}
void setup() {
Serial.begin(115200);
Serial.setTimeout(2000);
// 等待序列初始化。
while(!Serial) { }
// 深度睡眠模式設定60秒,ESP8266自身喚醒,當GPIO 16(在Nodemcu板D0)連接到復位引腳時
setup_start();
sendlineval();
ESP.deepSleep(18e8);//每30分鐘傳送一次訊息
}
void loop() {
}
//發送訊息到Line聊天室
void sendlineval(){
if (!client.connected()) {
reconnect();
}
float Temp = bme.readTemperature();
Serial.print("T=");
Serial.print(Temp);
Serial.print(" *C");
float Humidity = bme.readHumidity();
Serial.print(" Humidity = ");
Serial.print(Humidity);
Serial.print(" %");
float Pressure = bme.readPressure() / 100.0F ;
Serial.print(" P=");
Serial.print(Pressure);
Serial.print(" hPa");
float Altitude = bme.readAltitude(1013.25);
Serial.print(" A= ");
Serial.print(Altitude); // this should be adjusted to your local forcase
Serial.println(" m");
/*這裏將四個值整合變成一個字串,再一起傳送給MQTT Server*/
String weatherStr = String(Temp) + "," + String(Humidity) + ","
+ String(Pressure) + "," + String(Altitude);
byte arrSize = weatherStr.length() + 1;
char message[arrSize];
weatherStr.toCharArray(message,arrSize);
Serial.println(message);
client.publish("mysql/weather", message);
String line_notify_result = "";
line_notify_result = "現在溫度:" + String(Temp) + " *C\n"
+ "濕度:" + String(Humidity) + " %\n"
+ "大氣壓力:" + String(Pressure) + " hPa\n"
+ "海拔:" + String(Altitude) + " m";
LINE.setToken(LINE_TOKEN);
// 先換行再顯示
LINE.notify("\n" + line_notify_result);
}