here is a example of a nodemcu with 4 relays and a switch plus temperature,a steelseries gauge and turns on led 13 on arduino..
Code:
[{"id":"3304551a.2e55fa","type":"arduino out","z":"5145140c.93970c","name":"","pin":"13","state":"OUTPUT","arduino":"8787c49b.ad4d98","x":480,"y":120,"wires":[]},{"id":"bba99501.874b08","type":"inject","z":"5145140c.93970c","name":"OFF","topic":"B","payload":"0","payloadType":"str","repeat":"","crontab":"","once":false,"x":110,"y":140,"wires":[["96cbf7b7.a2f508"]]},{"id":"c070d488.bf62d8","type":"inject","z":"5145140c.93970c","name":"on","topic":"B","payload":"1","payloadType":"str","repeat":"","crontab":"","once":false,"x":110,"y":100,"wires":[["96cbf7b7.a2f508"]]},{"id":"2dd1384b.20ef18","type":"mqtt out","z":"5145140c.93970c","name":"","topic":"B","qos":"","retain":"","broker":"1e9c9256.67bcae","x":470,"y":40,"wires":[]},{"id":"96cbf7b7.a2f508","type":"ui_switch","z":"5145140c.93970c","name":"","label":"switch","group":"e3d20b81.e5e498","order":0,"width":"2","height":"1","passthru":true,"decouple":"false","topic":"B","style":"","onvalue":"1","onvalueType":"str","onicon":"","oncolor":"","offvalue":"0","offvalueType":"str","officon":"","offcolor":"","x":290,"y":80,"wires":[["2dd1384b.20ef18","3304551a.2e55fa"]]},{"id":"10dd3391.341a5c","type":"mqtt in","z":"5145140c.93970c","name":"","topic":"B","qos":"2","broker":"1e9c9256.67bcae","x":110,"y":40,"wires":[["96cbf7b7.a2f508"]]},{"id":"5706b9b6.dc01b8","type":"ui_template","z":"5145140c.93970c","group":"a17208f5.0eab18","name":"Temp","order":2,"width":"6","height":"6","format":"<script src=\"/myjs/tween-min.js\"></script>\n<script src=\"/myjs/steelseries-min.js\"></script>\n<script>\nvar radial4;\n (function(scope){ \n scope.$watch('msg', function(msg) {\n if (typeof(msg.value) != \"undefined\") radial4.setValueAnimated(msg.value);\n //if (typeof(msg.threshold) != \"undefined\") radial4.setThreshold(msg.threshold);\n // if (typeof(msg.odo) != \"undefined\")radial4.setOdoValue(msg.odo); \n //if (typeof(msg.userLed) != \"undefined\") radial4.setUserLedOnOff(msg.userLed); \n // if (typeof(msg.trend) != \"undefined\")\n {\n // if (msg.trend==1) radial4.setTrend(steelseries.TrendState.UP);\n //if (msg.trend==0) radial4.setTrend(steelseries.TrendState.STEADY);\n // if (msg.trend==-1) radial4.setTrend(steelseries.TrendState.DOWN);\n // if (msg.trend==-2) radial4.setTrend(steelseries.TrendState.OFF);\n }\n });\n })(scope);\n\n var sections = [steelseries.Section(0, 25, 'rgba(0, 0, 220, 0.3)'),\n steelseries.Section(25, 50, 'rgba(0, 220, 0, 0.3)'),\n steelseries.Section(50, 75, 'rgba(220, 220, 0, 0.3)') ],\n\n // Define one area\n areas = [steelseries.Section(75, 100, 'rgba(220, 0, 0, 0.3)')],\n\n radial4 = new steelseries.Radial('canvasRadial4', {\n gaugeType: steelseries.GaugeType.TYPE4,\n size: 292,\n section: sections,\n area: areas,\n titleString: \"Temperature\",\n unitString: \"Degrees F\",\n threshold: 80,\n //thresholdRising: false,\n // userLedVisible: true,\n // useOdometer: true,\n // lcdVisible: true,\n // trendVisible: true\n });\n \n radial4.setFrameDesign(steelseries.FrameDesign.BRASS);\n radial4.setValueAnimated(0);\n radial4.setPointerType(steelseries.PointerType.TYPE9);\n // radial4.setThreshold(50);\n //radial4.blinkUserLed(0);\n // radial4.setOdoValue(0);\n\n\n</script>\n\n<canvas id=\"canvasRadial4\" width=\"201\" height=\"201\"></canvas>\n","storeOutMessages":true,"fwdInMessages":true,"x":370,"y":200,"wires":[["92922286.ea901"]]},{"id":"a1998d1c.b6477","type":"mqtt in","z":"5145140c.93970c","name":"","topic":"outTopictemp","qos":"2","broker":"1e9c9256.67bcae","x":110,"y":200,"wires":[["f33eff68.0f275"]]},{"id":"92922286.ea901","type":"debug","z":"5145140c.93970c","name":"","active":true,"console":"false","complete":"payload","x":570,"y":200,"wires":[]},{"id":"f33eff68.0f275","type":"function","z":"5145140c.93970c","name":"","func":"msg.value=msg.payload;\nreturn msg;","outputs":1,"noerr":0,"x":250,"y":200,"wires":[["5706b9b6.dc01b8"]]},{"id":"8787c49b.ad4d98","type":"arduino-board","z":"","device":"/dev/ttyACM0"},{"id":"1e9c9256.67bcae","type":"mqtt-broker","z":"","broker":"localhost","port":"1883","tls":"3fb1b783.f68d18","clientid":"","usetls":false,"compatmode":true,"keepalive":"60","cleansession":true,"willTopic":"","willQos":"0","willPayload":"","birthTopic":"","birthQos":"0","birthPayload":""},{"id":"e3d20b81.e5e498","type":"ui_group","z":"","name":"compass","tab":"bdb776e4.17a1e8","order":1,"disp":true,"width":"6"},{"id":"a17208f5.0eab18","type":"ui_group","z":"","name":"horizon","tab":"bdb776e4.17a1e8","order":3,"disp":true,"width":"6"},{"id":"3fb1b783.f68d18","type":"tls-config","z":"","name":"","cert":"","key":"","ca":"","verifyservercert":true},{"id":"bdb776e4.17a1e8","type":"ui_tab","z":"","name":"Home","icon":"dashboard"}]
Code:
#include <ESP8266WiFi.h>
#include <PubSubClient.h>
#include <OneWire.h>
#include <DallasTemperature.h>
// wifi and MQTT broker vars
const char* ssid = "OpenPlotter"; // your network ssid
const char* password = "*******"; // password
const char* mqtt_server = "10.10.10.1"; // your mqtt server
boolean pin1 = false;
boolean pin2 = false;
boolean pin3 = false;
boolean pin4 = false;
boolean pin5 = false;
//boolean pin6 = false;
const int misc_sens_pin = 12; // temp was after thought 12 was free,. change to pin you like
const int mqtt_interval = 60000;
WiFiClient espClient;
PubSubClient client(espClient);
OneWire oneWire(misc_sens_pin);
DallasTemperature DS18B20(&oneWire);
long lastMsg = 0;
char msg[50];
int value = 0;
void setup_wifi() {
delay(10);
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());
}
void callback(char* topic, byte* payload, unsigned int length) {
if ( (char)topic[0] == 'A') {
if ((char)payload[0] == '1') {
digitalWrite(16, LOW); // you will need to change pin # if done elswere, it is 16 now thats the onboard led for testing
}
if ((char)payload[0] == '0') {
digitalWrite(16, HIGH); // and here same as above and for any pin changes
}
}
if ( (char)topic[0] == 'B') {
if ((char)payload[0] == '1') {
digitalWrite(4, LOW); // Turn the LED on (Note that LOW is the voltage level
}
if ((char)payload[0] == '0') {
digitalWrite(4, HIGH); // Turn the LED on (Note that LOW is the voltage level
}
}
if ( (char)topic[0] == 'C') {
if ((char)payload[0] == '1') {
digitalWrite(0, LOW); // Turn the LED on (Note that LOW is the voltage level
}
if ((char)payload[0] == '0') {
digitalWrite(0, HIGH); // Turn the LED on (Note that LOW is the voltage level
}
}
if ( (char)topic[0] == 'D') {
if ((char)payload[0] == '1') {
digitalWrite(2, LOW); // Turn the LED on (Note that LOW is the voltage level
}
if ((char)payload[0] == '0') {
digitalWrite(2, HIGH); // Turn the LED on (Note that LOW is the voltage level
}
}
}
void reconnect() {
// Loop until we're reconnected
while (!client.connected()) {
Serial.print("Attempting MQTT connection...");
// Attempt to connect
//if (client.connect("ESP8266Client5"))
if (client.connect("esp82661","pi", "*******")){ //Your mqtt password in op*****
Serial.println("connected");
// ... and resubscribe
client.subscribe("A");
client.subscribe("B");
client.subscribe("C");
client.subscribe("D");
} 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(16, OUTPUT);
pinMode(4, OUTPUT);
pinMode(0, OUTPUT);
pinMode(2, OUTPUT);
pinMode(14, OUTPUT); // if you changed temp pin then it needs to be input..
pinMode(12, INPUT); //temp pin
Serial.begin(115200);
setup_wifi();
client.setServer(mqtt_server, 1883);
client.setCallback(callback);
}
void loop() {
float temp;
char tempStr[5];
if (!client.connected()) {
reconnect();
}
client.loop();
long now = millis();
if (now - lastMsg > mqtt_interval) {
lastMsg = now;
++value;
DS18B20.requestTemperatures();
temp = DS18B20.getTempCByIndex(0);
temp = (temp * 9.0) / 5.0 + 32.0; // temp C to F
String(temp).toCharArray(tempStr, 2);
String(temp).toCharArray(tempStr, 5);
client.publish("outTopictemp", tempStr);
client.publish("outTopic", "Relay Board OK");
}
}