[{"id":"3affa9c0.1c6896","type":"mqtt in","z":"34bd759c.1796da","name":"","topic":"alex9ufo/#","qos":"0","broker":"41a78e1b.8ebeb","x":100,"y":54,"wires":[["551ad94f.d99f58"]]},{"id":"4d0c7e60.1edac","type":"function","z":"34bd759c.1796da","name":"Convert to float","func":"var str = msg.payload;\nmsg.payload = parseFloat (str);\nreturn msg;","outputs":1,"noerr":0,"x":680,"y":114,"wires":[["5055c086.b7b71"]]},{"id":"5055c086.b7b71","type":"switch","z":"34bd759c.1796da","name":"Check Temp > 29°C","property":"payload","propertyType":"msg","rules":[{"t":"gt","v":"29","vt":"str"}],"checkall":"true","repair":false,"outputs":1,"x":880,"y":114,"wires":[["844df3cf.80a0b"]]},{"id":"95dd3ea1.e2b11","type":"template","z":"34bd759c.1796da","name":"Message","field":"payload","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"警告!\n\\冰箱的溫度會下降!\\\n不可能解凍。","output":"str","x":700,"y":234,"wires":[["13f92463.30b53c"]]},{"id":"13f92463.30b53c","type":"template","z":"34bd759c.1796da","name":"Subject","field":"topic","fieldType":"msg","format":"handlebars","syntax":"plain","template":"!!警告冰箱溫度警告!","output":"str","x":900,"y":234,"wires":[["69e427a6.77f4d8"]]},{"id":"844df3cf.80a0b","type":"trigger","z":"34bd759c.1796da","op1":"","op2":"","op1type":"pay","op2type":"payl","duration":"5","extend":false,"units":"min","reset":"","bytopic":"all","name":"Trigger mail","x":1090,"y":114,"wires":[["95dd3ea1.e2b11"]]},{"id":"8025f79b.06ecf8","type":"ui_chart","z":"34bd759c.1796da","name":"DS19B20溫度紀錄","group":"5590022e.fa566c","order":0,"width":"0","height":"0","label":"溫度紀錄","chartType":"line","legend":"true","xformat":"HH:mm:ss","interpolate":"linear","nodata":"","dot":true,"ymin":"0","ymax":"100","removeOlder":"120","removeOlderPoints":"","removeOlderUnit":"60","cutout":0,"useOneColor":false,"colors":["#1f77b4","#aec7e8","#ff7f0e","#2ca02c","#98df8a","#d62728","#ff9896","#9467bd","#c5b0d5"],"useOldStyle":false,"x":690,"y":34,"wires":[[],[]]},{"id":"3dda0770.b80798","type":"ui_gauge","z":"34bd759c.1796da","name":"DS18B20現在溫度","group":"5590022e.fa566c","order":0,"width":"0","height":"0","gtype":"gage","title":"現在溫度","label":"°C","format":"{{value}}","min":"0","max":"100","colors":["#035ab1","#e6e600","#ca3838"],"seg1":"","seg2":"","x":690,"y":74,"wires":[]},{"id":"69e427a6.77f4d8","type":"e-mail","z":"34bd759c.1796da","server":"smtp.gmail.com","port":"465","secure":true,"name":"alex.pts2594@gmail.com","dname":"send Gmail ","x":1090,"y":234,"wires":[]},{"id":"f8b3f670.53e638","type":"json","z":"34bd759c.1796da","name":"","property":"payload","action":"","pretty":false,"x":270,"y":274,"wires":[["a470db29.1cbd58","4f1146f9.8e81a8"]]},{"id":"a470db29.1cbd58","type":"function","z":"34bd759c.1796da","name":"temperature extract ","func":"//var pres = msg.payload.pressure;\nvar temp = msg.payload.temperature;\n//var humidity = msg.payload.humidity;\nvar tmp1= parseInt(temp);\nmsg.payload = tmp1;\n\nreturn msg;","outputs":1,"noerr":0,"x":470,"y":280,"wires":[["6c306cdb.d310e4","df3b6a40.aa5138"]]},{"id":"551ad94f.d99f58","type":"switch","z":"34bd759c.1796da","name":"","property":"topic","propertyType":"msg","rules":[{"t":"eq","v":"alex9ufo/DS18B20/temp","vt":"str"},{"t":"eq","v":"alex9ufo/DHT11/temp_humi","vt":"str"}],"checkall":"true","repair":false,"outputs":2,"x":130,"y":214,"wires":[["785e43d.661a0bc"],["f8b3f670.53e638"]]},{"id":"785e43d.661a0bc","type":"json","z":"34bd759c.1796da","name":"","property":"payload","action":"","pretty":false,"x":270,"y":174,"wires":[["a43d7c64.d0297"]]},{"id":"a43d7c64.d0297","type":"function","z":"34bd759c.1796da","name":"data extract","func":"//var pres = msg.payload.pressure;\nvar temp = msg.payload.data.DS18B20;\n//var humidity = msg.payload.humidity;\nvar tmp1= parseInt(temp);\nmsg.payload = tmp1;\n\nreturn msg;","outputs":1,"noerr":0,"x":390,"y":94,"wires":[["8025f79b.06ecf8","3dda0770.b80798","4d0c7e60.1edac","1426fcac.77f2c3"]]},{"id":"4f1146f9.8e81a8","type":"function","z":"34bd759c.1796da","name":"humidity extract","func":"//var pres = msg.payload.pressure;\n//var temp = msg.payload.temperature;\nvar humidity = msg.payload.humidity\nvar tmp1= parseInt(humidity);\nmsg.payload = tmp1;\nreturn msg;","outputs":1,"noerr":0,"x":480,"y":420,"wires":[["c4021ed1.ff08e","a0b2217e.11516"]]},{"id":"6c306cdb.d310e4","type":"ui_chart","z":"34bd759c.1796da","name":"DHT11 溫度紀錄","group":"19f20bd0.2ae064","order":0,"width":"0","height":"0","label":"溫度紀錄","chartType":"line","legend":"true","xformat":"HH:mm:ss","interpolate":"linear","nodata":"","dot":true,"ymin":"0","ymax":"100","removeOlder":"120","removeOlderPoints":"","removeOlderUnit":"60","cutout":0,"useOneColor":false,"colors":["#1f77b4","#aec7e8","#ff7f0e","#2ca02c","#98df8a","#d62728","#ff9896","#9467bd","#c5b0d5"],"useOldStyle":false,"x":700,"y":280,"wires":[[],[]]},{"id":"df3b6a40.aa5138","type":"ui_gauge","z":"34bd759c.1796da","name":"DHT11現在溫度","group":"5590022e.fa566c","order":0,"width":"0","height":"0","gtype":"gage","title":"現在溫度","label":"°C","format":"{{value}}","min":"0","max":"100","colors":["#035ab1","#e6e600","#ca3838"],"seg1":"","seg2":"","x":700,"y":320,"wires":[]},{"id":"c4021ed1.ff08e","type":"ui_chart","z":"34bd759c.1796da","name":"DHT11濕度紀錄","group":"d593b7b4.155088","order":0,"width":"0","height":"0","label":"濕度紀錄","chartType":"line","legend":"true","xformat":"HH:mm:ss","interpolate":"linear","nodata":"","dot":true,"ymin":"0","ymax":"100","removeOlder":"120","removeOlderPoints":"","removeOlderUnit":"60","cutout":0,"useOneColor":false,"colors":["#1f77b4","#aec7e8","#ff7f0e","#2ca02c","#98df8a","#d62728","#ff9896","#9467bd","#c5b0d5"],"useOldStyle":false,"x":700,"y":400,"wires":[[],[]]},{"id":"a0b2217e.11516","type":"ui_gauge","z":"34bd759c.1796da","name":"DHT11現在濕度","group":"5590022e.fa566c","order":0,"width":"0","height":"0","gtype":"gage","title":"現在濕度","label":"°C","format":"{{value}}","min":"0","max":"100","colors":["#035ab1","#e6e600","#ca3838"],"seg1":"","seg2":"","x":700,"y":440,"wires":[]},{"id":"1426fcac.77f2c3","type":"debug","z":"34bd759c.1796da","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":670,"y":160,"wires":[]},{"id":"41a78e1b.8ebeb","type":"mqtt-broker","z":"","name":"mqtt","broker":"broker.mqtt-dashboard.com","port":"1883","clientid":"","usetls":false,"compatmode":true,"keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","closeTopic":"","closePayload":"","willTopic":"","willQos":"0","willPayload":""},{"id":"5590022e.fa566c","type":"ui_group","z":"","name":"DS18B20","tab":"6a28d450.774d9c","order":1,"disp":true,"width":"6","collapse":false},{"id":"19f20bd0.2ae064","type":"ui_group","z":"","name":"DHT11_temp","tab":"6a28d450.774d9c","order":2,"disp":true,"width":"6","collapse":false},{"id":"d593b7b4.155088","type":"ui_group","z":"","name":"DHT11_humi","tab":"6a28d450.774d9c","order":3,"disp":true,"width":"6","collapse":false},{"id":"6a28d450.774d9c","type":"ui_tab","z":"","name":"DS18B20_DHT11","icon":"dashboard","order":5}]
/*
Basic ESP8266 MQTT example
To install the ESP8266 board, (using Arduino 1.6.4+):
- Add the following 3rd party board manager under "File -> Preferences -> Additional Boards Manager URLs":
http://arduino.esp8266.com/stable/package_esp8266com_index.json
- Open the "Tools -> Board -> Board Manager" and click install for the ESP8266"
- Select your ESP8266 in "Tools -> Board"
*/
#include <ESP8266WiFi.h>
#include <PubSubClient.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#include <ArduinoJson.h>
#include <dht.h>
dht DHT;
//********************************************************************
// Update these with values suitable for your network.
//const char* ssid = "PTS-2F";
//const char* password = "";
const char* ssid = "74170287";
const char* password = "24063173";
//const char* ssid = "your_ssid";
//const char* password = "your_password";
#define MQTTid "" //id of this mqtt client
#define MQTTip "broker.mqtt-dashboard.com" //ip address or hostname of the mqtt broker
#define MQTTport 1883 //port of the mqtt broker
#define MQTTuser "alex9ufo" //username of this mqtt client
#define MQTTpsw "alex9981" //password of this mqtt client
//#define MQTTuser "your_username" //username of this mqtt client
//#define MQTTpsw "your_password" //password of this mqtt client
#define MQTTpubQos 2 //qos of publish (see README)
#define MQTTsubQos 1 //qos of subscribe
//********************************************************************
long lastMsg = 0;
char jsonChar[100];
#define DHTTYPE DHT11 //DHT11 or DHT22
#define DHTPIN D5
#define ONE_WIRE_BUS D6
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensor(&oneWire);
float humi, temp_DHT , temp_DS1820 ; // Values read from sensor
char dev_name[50] , json_buffer[512] , json_buffer_status[512] , my_ip_s[16];
boolean pendingDisconnect = false;
void mqttConnectedCb(); // on connect callback
void mqttDisconnectedCb(); // on disconnect callback
void mqttDataCb(char* topic, byte* payload, unsigned int length); // on new message callback
WiFiClient wclient;
PubSubClient client(MQTTip, MQTTport, mqttDataCb, wclient);
/********************************************************************
* FunctionName: mqttConnectedCb
* Description :
*
* Parameters :
* Returns :
*********************************************************************/
void mqttConnectedCb() {
Serial.println("connected");
// Once connected, publish an announcement...
client.publish("alex9ufo/DS18B20/temp", jsonChar, MQTTpubQos, true); // true means retain
client.publish("alex9ufo/DHT11/temp_humi", jsonChar, MQTTpubQos, true); // true means retain
// ... and resubscribe
client.subscribe("alex9ufo/inTopic", MQTTsubQos);
}
/********************************************************************
* FunctionName: mqttDisconnectedCb
* Description :
*
* Parameters :
* Returns :
*********************************************************************/
void mqttDisconnectedCb() {
Serial.println("disconnected");
}
/********************************************************************
* FunctionName: mqttDataCb
* Description :
*
* Parameters :
* Returns :
*********************************************************************/
void mqttDataCb(char* topic, byte* payload, unsigned int length) {
/*
you can convert payload to a C string appending a null terminator to it;
this is possible when the message (including protocol overhead) doesn't
exceeds the MQTT_MAX_PACKET_SIZE defined in the library header.
you can consider safe to do so when the length of topic plus the length of
message doesn't exceeds 115 characters
*/
char* message = (char *) payload;
message[length] = 0;
Serial.print("Message arrived [");
Serial.print(topic);
Serial.print("] ");
Serial.println(message);
}
/********************************************************************
* FunctionName: setup_wifi
* Description :
*
* Parameters :
* Returns :
*********************************************************************/
void setup_wifi() {
delay(10);
// We start by connecting to a WiFi network
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}
/********************************************************************
* FunctionName: setup
* Description :
*
* Parameters :
* Returns :
*********************************************************************/
void setup() {
Serial.begin(115200);
setup_wifi();
//
Serial.println( "Booting" );
Serial.println();
Serial.printf( "Sketch size: %u\n", ESP.getSketchSize() );
Serial.printf( "Free size: %u\n", ESP.getFreeSketchSpace() );
Serial.printf( "Heap: %u\n", ESP.getFreeHeap() );
Serial.printf( "Boot Vers: %u\n", ESP.getBootVersion() );
Serial.printf( "CPU: %uMHz\n", ESP.getCpuFreqMHz() );
Serial.printf( "SDK: %s\n", ESP.getSdkVersion() );
Serial.printf( "Chip ID: %u\n", ESP.getChipId() );
Serial.printf( "Flash ID: %u\n", ESP.getFlashChipId() );
Serial.printf( "Flash Size: %u\n", ESP.getFlashChipRealSize() );
Serial.printf( "Vcc: %u\n", ESP.getVcc() );
Serial.println();
//
show_basicmessage();
}
/********************************************************************
* FunctionName: process_mqtt
* Description :
*
* Parameters :
* Returns :
*********************************************************************/
void process_mqtt() {
if (WiFi.status() == WL_CONNECTED) {
if (client.connected()) {
client.loop();
} else {
// client id, client username, client password, last will topic, last will qos, last will retain, last will message
if (client.connect(MQTTid, MQTTuser, MQTTpsw, MQTTid "/status", 2, true, "0")) {
pendingDisconnect = false;
mqttConnectedCb();
}
}
} else {
if (client.connected())
client.disconnect();
}
if (!client.connected() && !pendingDisconnect) {
pendingDisconnect = true;
mqttDisconnectedCb();
}
}
/********************************************************************
* FunctionName: get_DHT_temp_humi
* Description : Read temperature and humidity from sensor DHT11 / DHT22
* Values are stored in humi and temp_C
* Parameters : None
* Returns : temp_DHT , humi
*********************************************************************/
void get_DHT_temp_humi()
{
int runs=0;
do {
delay(2000);
//temp_f = dht.readTemperature(false);
//humidity = dht.readHumidity();
int chk = DHT.read11(DHTPIN);
temp_DHT= (DHT.temperature) ;
humi = (DHT.humidity);
if(runs > 0)
Serial.println("##Failed to read from DHT sensor! ###");
Serial.print("DHT11 Temperature=");
Serial.print(String(temp_DHT ).c_str());
Serial.print(", Humidity =");
Serial.println(String(humi ).c_str());
runs++;
}
while(isnan(temp_DHT) && isnan(humi));
}
/********************************************************************
* FunctionName: get_DS1820_temp
* Description : Read temperature from sensor DS18B20
* Values are stored in temp_DS1820
* Parameters : None
* Returns : temp_DS1820
*********************************************************************/
void get_DS1820_temp()
{
int runs=0;
do {
delay(2000);
// Get the current temperature from the DS18b20 sensor
sensor.requestTemperatures();
temp_DS1820 =sensor.getTempCByIndex(0);
if(runs > 0)
Serial.println("##Failed to read from DS18B20 sensor! ###");
Serial.print("DS18B20 Temperature=");
Serial.println(String(temp_DS1820).c_str());
runs++;
}
while(isnan(temp_DS1820) > 0);
}
/********************************************************************
* FunctionName:
* Description :
*
* Parameters :
* Returns :
*********************************************************************/
void show_basicmessage()
{
//This one is dynamic one (just for fun). Avoid this in embeded system
//Can be changed to Static one like: StaticJsonBuffer<512> jsonDeviceStatus;
DynamicJsonBuffer jsonDeviceStatus;
JsonObject& jsondeviceStatus = jsonDeviceStatus.createObject();
sprintf(dev_name, "ESP_%d", ESP.getChipId());
IPAddress my_ip_addr = WiFi.localIP();
sprintf(my_ip_s, "%d.%d.%d.%d", my_ip_addr[0],my_ip_addr[1],my_ip_addr[2],my_ip_addr[3]);
String IPaddr = String(my_ip_s).c_str();
jsondeviceStatus ["device_name"] = dev_name;
jsondeviceStatus["type"] = "dth";
jsondeviceStatus["ipaddress"] = IPaddr ;
jsondeviceStatus["bgn"] = 3;
jsondeviceStatus["sdk"] = ESP.getSdkVersion();
jsondeviceStatus["version"] = "1";
jsondeviceStatus["uptime"] = 0;
jsondeviceStatus.printTo(json_buffer_status, sizeof(json_buffer_status));
Serial.println(json_buffer_status);
}
/********************************************************************
* FunctionName: loop
* Description :
*
* Parameters :
* Returns :
*********************************************************************/
void loop() {
process_mqtt();
long now = millis();
if (now - lastMsg > 6000) { // wait for 6sec
lastMsg = now;
// Get the current temperature from the DS18b20 sensor
get_DS1820_temp();
char tChar[10];
dtostrf( temp_DS1820, 6, 2, tChar);
// Convert data to JSON string
String json =
"{\"data\":{"
"\"DS18B20\": \"" + String(temp_DS1820) + "\"}"
"}";
json.toCharArray(jsonChar, json.length()+1);
//this one is static allocation
StaticJsonBuffer<512> jsonBuffer;
JsonObject & root = jsonBuffer.createObject();
get_DHT_temp_humi(); //Read temperature and humidity from sensor Values are stored in humi and temp_C
String hum , temp ;
hum = String(humi).c_str();
temp = String(temp_DHT).c_str();
char dev_name[50];
sprintf(dev_name, "ESP_%d", ESP.getChipId());
root["device_name"] = dev_name;
root["type"] = "dth";
root["temperature"] = temp;
root["humidity"] = hum;
root.printTo(json_buffer, sizeof(json_buffer));
// Convert JSON string to character array
if (client.connected()) {
Serial.println("Publish message: ");
Serial.print("alex9ufo/DS18B20/temp");
Serial.println(json);
// Publish JSON character array to MQTT topic
client.publish("alex9ufo/DS18B20/temp",jsonChar);
Serial.print("alex9ufo/DHT11/temp_humi");
Serial.println(json_buffer);
client.publish("alex9ufo/DHT11/temp_humi" , json_buffer);
} // if (client.connected()) {
} //if (now - lastMsg > 6000)
}
//*********************************************************************/