/*
Node-RED Code
[
{
"id": "a02ceb0db58efd15",
"type": "ui_button",
"z": "c86ece8419d4d866",
"name": "",
"group": "82f112d3b8b8f20f",
"order": 0,
"width": 0,
"height": 0,
"passthru": false,
"label": "50%",
"tooltip": "",
"color": "",
"bgcolor": "",
"icon": "",
"payload": "50%",
"payloadType": "str",
"topic": "topic",
"topicType": "msg",
"x": 210,
"y": 200,
"wires": [
[
"6e3edab28d48efb6"
]
]
},
{
"id": "0e250d2792c4a945",
"type": "ui_button",
"z": "c86ece8419d4d866",
"name": "",
"group": "82f112d3b8b8f20f",
"order": 1,
"width": 0,
"height": 0,
"passthru": false,
"label": "0%",
"tooltip": "",
"color": "",
"bgcolor": "",
"icon": "",
"payload": "0%",
"payloadType": "str",
"topic": "topic",
"topicType": "msg",
"x": 210,
"y": 100,
"wires": [
[
"6e3edab28d48efb6"
]
]
},
{
"id": "f28b444be950f7d6",
"type": "ui_button",
"z": "c86ece8419d4d866",
"name": "",
"group": "82f112d3b8b8f20f",
"order": 2,
"width": 0,
"height": 0,
"passthru": false,
"label": "100%",
"tooltip": "",
"color": "",
"bgcolor": "",
"icon": "",
"payload": "100%",
"payloadType": "str",
"topic": "topic",
"topicType": "msg",
"x": 210,
"y": 300,
"wires": [
[
"6e3edab28d48efb6"
]
]
},
{
"id": "c34912c76eb205a8",
"type": "ui_gauge",
"z": "c86ece8419d4d866",
"name": "",
"group": "82f112d3b8b8f20f",
"order": 3,
"width": 0,
"height": 0,
"gtype": "gage",
"title": "gauge",
"label": "units",
"format": "{{value}}",
"min": 0,
"max": "100",
"colors": [
"#00b500",
"#e6e600",
"#ca3838"
],
"seg1": "",
"seg2": "",
"x": 610,
"y": 280,
"wires": []
},
{
"id": "4155e022a72a9a7d",
"type": "mqtt out",
"z": "c86ece8419d4d866",
"name": "",
"topic": "/ThinkIOT/Servo-nodered",
"qos": "2",
"retain": "true",
"respTopic": "",
"contentType": "",
"userProps": "",
"correl": "",
"expiry": "",
"broker": "de52fe7462576919",
"x": 610,
"y": 120,
"wires": []
},
{
"id": "6e3edab28d48efb6",
"type": "function",
"z": "c86ece8419d4d866",
"name": "States",
"func": "\nreturn msg;",
"outputs": 1,
"timeout": 0,
"noerr": 0,
"initialize": "",
"finalize": "",
"libs": [],
"x": 390,
"y": 180,
"wires": [
[
"4155e022a72a9a7d",
"c34912c76eb205a8"
]
]
},
{
"id": "82f112d3b8b8f20f",
"type": "ui_group",
"name": "ThinkIOT",
"tab": "ebaca775dab9aa72",
"order": 1,
"disp": true,
"width": "6",
"collapse": false
},
{
"id": "de52fe7462576919",
"type": "mqtt-broker",
"name": "mosquitto",
"broker": "test.mosquitto.org",
"port": "1883",
"clientid": "",
"autoConnect": true,
"usetls": false,
"protocolVersion": "4",
"keepalive": "60",
"cleansession": true,
"autoUnsubscribe": true,
"birthTopic": "",
"birthQos": "0",
"birthRetain": "false",
"birthPayload": "",
"birthMsg": {},
"closeTopic": "",
"closeQos": "0",
"closeRetain": "false",
"closePayload": "",
"closeMsg": {},
"willTopic": "",
"willQos": "0",
"willRetain": "false",
"willPayload": "",
"willMsg": {},
"userProps": "",
"sessionExpiry": ""
},
{
"id": "ebaca775dab9aa72",
"type": "ui_tab",
"name": "ESP32",
"icon": "dashboard",
"disabled": false,
"hidden": false
}
]
*/
// code for controlling servo motor using nodered dashboard
#include <WiFi.h>
#include <PubSubClient.h>
#include <ESP32Servo.h>
Servo myservo;// servo onject
const char* ssid = "Wokwi-GUEST";
const char* password = "";
const char* mqtt_server = "test.mosquitto.org";// MQTT broker
WiFiClient espClient;
PubSubClient client(espClient);
long lastMsg = 0;
char msg[50];
int value = 0;
void setup() {
Serial.begin(115200);
myservo.attach(5);
setup_wifi();
client.setServer(mqtt_server, 1883);
client.setCallback(callback);
}
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) {
String string;
Serial.print("Message arrived [");
Serial.print(topic);
Serial.print("] ");
for (int i = 0; i < length; i++) {
string+=((char)payload[i]);
}
Serial.print(string);
if (topic ="/ThinkIOT/Servo-nodered") /// esp32 subscribe topic
Serial.print(" ");
int status = string.toInt();
int pos = map(status, 1, 100, 0, 180);
Serial.println(pos);
myservo.write(pos);
delay(15);
}
void reconnect() {
while (!client.connected()) {
Serial.print("Attempting MQTT connection...");
if (client.connect("ESPClient")) {
Serial.println("connected");
client.subscribe("/ThinkIOT/Servo-nodered");
} else {
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 5 seconds");
delay(5000);
}}}
void loop() {
if (!client.connected()) {
reconnect();
}
client.loop();
delay(100);
}