--
http://nodered.org
Join us on Slack to continue the conversation: http://nodered.org/slack
---
You received this message because you are subscribed to the Google Groups "Node-RED" group.
To unsubscribe from this group and stop receiving emails from it, send an email to node-red+unsubscribe@googlegroups.com.
To post to this group, send email to node...@googlegroups.com.
Visit this group at https://groups.google.com/group/node-red.
To view this discussion on the web, visit https://groups.google.com/d/msgid/node-red/23c4a631-a1bf-4f79-8f2a-f3971ac90c91%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
/*
MQTT "will" message example
- connects to an MQTT server with a will message
- publishes a message
- waits a little bit
- disconnects the socket *without* sending a disconnect packet
You should see the will message published when we disconnect
*/
#include <ESP8266WiFi.h>
#include <PubSubClient.h>
const char *ssid = "myssid"; // cannot be longer than 32 characters!
const char *pass = "mypass"; //
String clientId = "FLOjab1";
// Update these with values suitable for your network.
IPAddress server(172, 16, 0, 109);
WiFiClient wclient;
PubSubClient client(wclient, server);
void setup() {
// Setup console
Serial.begin(115200);
delay(10);
Serial.println();
Serial.println();
}
void loop() {
delay(1000);
if (WiFi.status() != WL_CONNECTED) {
Serial.print("Connecting to ");
Serial.print(ssid);
Serial.println("...");
WiFi.begin(ssid, pass);
if (WiFi.waitForConnectResult() != WL_CONNECTED)
return;
Serial.println("WiFi connected");
}
if (WiFi.status() == WL_CONNECTED) {
MQTT::Connect con("arduinoClient");
con.set_will(clientId, "Lost Connection");
if (client.connect(con)) {
client.publish(clientId, "I am up!");
//delay(1000);
// wclient.stop();
}
// Serial.println("MQTT connection failed.");
delay(500);
}
}
wclient.stop();boolean connect(const char* id, const char* user, const char* pass, const char* willTopic, uint8_t willQos, boolean willRetain, const char* willMessage);client.connect(clientId.c_str(),"outTopic",0,1,"Disconnected");#include <ESP8266WiFi.h>
#include <PubSubClient.h>
// Update these with values suitable for your network.
const char* ssid = "myssid";
const char* password = "mypassword";
const char* mqtt_server = "172.16.0.109";
WiFiClient espClient;
PubSubClient client(espClient);
long lastMsg = 0;
char msg[50];
int value = 0;
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(".");
}
randomSeed(micros());
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}
void callback(char* topic, byte* payload, unsigned int length) {
Serial.print("Message arrived [");
Serial.print(topic);
Serial.print("] ");
for (int i = 0; i < length; i++) {
Serial.print((char)payload[i]);
}
Serial.println();
// Switch on the LED if an 1 was received as first character
if ((char)payload[0] == '1') {
digitalWrite(BUILTIN_LED, LOW); // Turn the LED on (Note that LOW is the voltage level
// but actually the LED is on; this is because
// it is acive low on the ESP-01)
} else {
digitalWrite(BUILTIN_LED, HIGH); // Turn the LED off by making the voltage HIGH
}
}
void reconnect() {
// Loop until we're reconnected
while (!client.connected()) {
Serial.print("Attempting MQTT connection...");
// Create a random client ID
String clientId = "ESP8266Client-";
clientId += String(random(0xffff), HEX);
// Attempt to connect
if (client.connect(clientId.c_str())) {
client.connect(clientId.c_str(),"outTopic",0,1,"Disconnected");
Serial.println("connected");
// Once connected, publish an announcement...
client.publish("outTopic", "hello world");
// ... and resubscribe
client.subscribe("inTopic");
} else {
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 5 seconds");
// Wait 5 seconds before retrying
delay(5000);
}
}
}
void setup() {
pinMode(BUILTIN_LED, OUTPUT); // Initialize the BUILTIN_LED pin as an output
Serial.begin(115200);
setup_wifi();
client.setServer(mqtt_server, 1883);
client.setCallback(callback);
}
void loop() {
if (!client.connected()) {
reconnect();
}
client.loop();
long now = millis();
if (now - lastMsg > 2000) {
lastMsg = now;
++value;
snprintf (msg, 75, "hello world #%ld", value);
Serial.print("Publish message: ");
Serial.println(msg);
client.publish("outTopic", msg);
}
}client.connect(clientId.c_str())[{"id":"261cb3b6.b15f44","type":"function","z":"a1c5a94c.d64cb8","name":"timeNow","func":"var now = new Date();\nflow.set(\"sensorTimeNow\", now);\nreturn msg;","outputs":1,"noerr":0,"x":420,"y":1680,"wires":[["67df8282.14ea3c"]]},{"id":"73657caa.d7d024","type":"function","z":"a1c5a94c.d64cb8","name":"inSensor","func":"var lastUpdate = flow.get(\"sensorTimeNow\")||0;\nvar now = new Date();\nvar threshold = 20; // Inactive sensor limit in sec\nvar msg = {payload: 0}; // Value when data is too old\nif (flow.get(\"sensorTimeNow\") !== 0) {\n lastUpdate = ((now - lastUpdate) / 1000).toFixed(0);\n flow.set(\"sensorLastUpdate\", now);\n if (lastUpdate <= threshold) {\n node.status({fill:\"green\",shape:\"ring\",text:\"Last Update: \"+lastUpdate+\"s\"});\n return[null];\n }\n else {\n node.status({fill:\"red\",shape:\"ring\",text:\"Last Update: \"+lastUpdate+\"s\"});\n return[msg];\n }\n}\nreturn null;","outputs":"1","noerr":0,"x":420,"y":1600,"wires":[["978af557.45bca8"]]},{"id":"4cf6c1f7.5ac818","type":"inject","z":"a1c5a94c.d64cb8","name":"Check every 5sec for old data","topic":"","payload":"","payloadType":"num","repeat":"5","crontab":"","once":false,"x":190,"y":1600,"wires":[["73657caa.d7d024"]]},{"id":"39a96bb4.8572ec","type":"inject","z":"a1c5a94c.d64cb8","name":"Simulate data input for alive sensor","topic":"","payload":"77.9","payloadType":"num","repeat":"","crontab":"","once":false,"x":200,"y":1680,"wires":[["261cb3b6.b15f44"]]},{"id":"67df8282.14ea3c","type":"debug","z":"a1c5a94c.d64cb8","name":"to dashboard guage","active":true,"console":"false","complete":"payload","x":600,"y":1680,"wires":[]},{"id":"978af557.45bca8","type":"debug","z":"a1c5a94c.d64cb8","name":"0 to dashboard if too old","active":true,"console":"false","complete":"payload","x":610,"y":1600,"wires":[]}]
[{"id":"7241794.1b27788","type":"ui_gauge","z":"384607de.b7b0e","name":"","group":"3be931fb.4c8876","order":0,"width":0,"height":0,"gtype":"gage","title":"Water Temp","label":"Celcius","format":"{{value | number:1}}º","min":"0","max":"50","colors":["#0080ff","#0080ff","#ca3838"],"x":709,"y":170,"wires":[]},{"id":"a12d5381.5028b8","type":"ui_gauge","z":"384607de.b7b0e","name":"","group":"3be931fb.4c8876","order":0,"width":0,"height":0,"gtype":"gage","title":"Engine Room Temp","label":"Celcius","format":"{{value | number:1}}º","min":"0","max":"50","colors":["#0080ff","#0080ff","#ca3838"],"x":763,"y":120,"wires":[]},{"id":"7c5ff603.8a9688","type":"mqtt in","z":"384607de.b7b0e","name":"Water temp","topic":"FLOjab/temperature/28ec8bbf3001e","qos":"2","broker":"bc047110.9c1b28","x":320,"y":170,"wires":[["7241794.1b27788"]]},{"id":"861f2bd.7395358","type":"mqtt in","z":"384607de.b7b0e","name":"Engine room temp","topic":"FLOjab/temperature/283b7fbf300e6","qos":"2","broker":"bc047110.9c1b28","x":341,"y":121,"wires":[["a12d5381.5028b8"]]},{"id":"1d175372.dd6595","type":"mqtt in","z":"384607de.b7b0e","name":"Temp Will","topic":"FLOjab/temperature/#","qos":"2","broker":"bc047110.9c1b28","x":308,"y":60,"wires":[["bb8691b3.e37b5"]]},{"id":"bb8691b3.e37b5","type":"function","z":"384607de.b7b0e","name":"set to 0","func":"if(msg.payload == \"Disconnected\" || msg.payload == \"Connected\"){\n msg.payload=0;\nreturn [msg];}","outputs":"1","noerr":0,"x":534,"y":58,"wires":[["a12d5381.5028b8","7241794.1b27788"]]},{"id":"3be931fb.4c8876","type":"ui_group","z":"","name":"Default","tab":"d96a2d5e.127ec8","disp":false,"width":"6"},{"id":"bc047110.9c1b28","type":"mqtt-broker","z":"","broker":"172.16.0.109","port":"1883","clientid":"","usetls":false,"compatmode":true,"keepalive":"60","cleansession":true,"willTopic":"","willQos":"0","willPayload":"","birthTopic":"","birthQos":"0","birthPayload":""},{"id":"d96a2d5e.127ec8","type":"ui_tab","z":"","name":"Instruments","icon":"dashboard"}]