Hello,
Today I managed to get
Domoticz to communicate with the
Stepper Motor through
MQTT. How?:
Parts:
NodeMCU
28BYJ-48 stepper motor
ULN2003 driver board
Steps:
1.
This step by step: which allowed me to setup the stepper motor, wifi and mqtt;
my sketch:
Code: Select all
/* ___ ___ ___ _ _ ___ ___ ____ ___ ____
* / _ \ /___)/ _ \| | | |/ _ \ / _ \ / ___) _ \| \
*| |_| |___ | |_| | |_| | |_| | |_| ( (__| |_| | | | |
* \___/(___/ \___/ \__ |\___/ \___(_)____)___/|_|_|_|
* (____/
* This sketch is about how to use the MQTT protocol to control a step motor
* Tutorial URL http://osoyoo.com/2017/05/17/nodemcu-lesson-16-step-motor-mqtt/
* CopyRight www.osoyoo.com
*/
#include <ESP8266WiFi.h>
#include <PubSubClient.h>
#include <Stepper.h>
#include <ArduinoJson.h>
// Update these with values suitable for your network.
const char* ssid = "Vodafone-XXXXXXXX";//put your wifi ssid here
const char* password = "XXXXXXXX";//put your wifi password here
const char* mqtt_server = "192.168.1.XXX";
const int stepsPerRevolution = 2038; // change this to fit the number of steps per revolution
const int stepperSpeed = 6;
// initialize the stepper library on D1,D2,D5,D6
Stepper myStepper(stepsPerRevolution, D1, D2, D5, D6);
WiFiClient espClient;
PubSubClient client(espClient);
void setup_wifi() {
delay(100);
// We start by connecting to a WiFi network
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("Command from MQTT broker is : ");
Serial.println(topic);
//print payload
DynamicJsonBuffer jsonBuffer( MQTT_MAX_PACKET_SIZE );
String messageReceived="";
// Affiche le topic entrant - display incoming Topic
Serial.print("MQTT Message arrived: ");
// decode payload message
for (int i = 0; i < length; i++) {
messageReceived+=((char)payload[i]);
}
// display incoming message
Serial.println(messageReceived);
if(messageReceived=="abrir-estores")
{
myStepper.step(stepsPerRevolution);
Serial.println("Recebida instrucao para abrir os estores" );
}
else if(messageReceived=="fechar-estores")
{
myStepper.step(-stepsPerRevolution);
Serial.println("Recebida instrucao para fechar os estores" );
}
//Serial.println();
int p =(char)payload[0]-'0';
// step one revolution in one direction:
if(p==1)
{
myStepper.step(stepsPerRevolution);
Serial.print(" clockwise" );
}
// step one revolution in the other direction:
else if(p==2)
{
myStepper.step(-stepsPerRevolution);
Serial.print(" counterclockwise" );
}
Serial.println();
}
// Serial.println();
//end callback
void reconnect() {
// Loop until we're reconnected
while (!client.connected())
{
Serial.print("Attempting MQTT connection...");
// Create a random client ID
String clientId = "ESP-EstoresSala-";
clientId += String(random(0xffff), HEX);
// Attempt to connect
//if you MQTT broker has clientID,username and password
//please change following line to if (client.connect(clientId,userName,passWord))
if (client.connect(clientId.c_str()))
{
Serial.println("connected");
//once connected to MQTT broker, subscribe command if any
//client.subscribe("domoticz/#");
//client.subscribe("domoticz/in");
client.subscribe("domoticz/out");
} else {
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 5 seconds");
// Wait 6 seconds before retrying
delay(6000);
}
}
} //end reconnect()
void setup() {
Serial.begin(115200);
setup_wifi();
client.setServer(mqtt_server, 1883);
client.setCallback(callback);
// set the speed at 80 rpm:
myStepper.setSpeed(stepperSpeed);
}
void loop() {
if (!client.connected()) {
reconnect();
}
client.loop();
}
2. Read
this guide "Dummies guide on driving a 28BYJ-48 Stepper Motor with a ULN2003 Driver Board", to better undestard the relation between steps per revolution and speed, among other details.
3. Created a MQTT script to be called in Domoticz to trigger MQTT:
~/domoticz/scripts $ sudo nano mqtt-to-estores-abrir.sh
Code: Select all
#!/bin/bash
mosquitto_pub -m "abrir-estores" -t "domoticz/out"
3.2 Created a second script to close the blinds
~/domoticz/scripts $ sudo nano mqtt-to-estores-fechar.sh
Code: Select all
#!/bin/bash
mosquitto_pub -m "fechar-estores" -t "domoticz/out"
3.1 Changed the mode of both scripts to be executable: (e.g. sudo chmod +x mqtt-to-estores-fechar.sh)
4. Then I've created a dummy device in Domoticz of type switch. After, change the type to Blids;
In the actions of the button, I've added the name of the script to be executed: script://mqtt-to-estores-fechar.sh and script://mqtt-to-estores-abrir.sh
And that was all for now. The next step is to try to understand how to control the position of the roller blinds to know how many steps are required to close or open them and then assembly everything more or less like
this
Please let me know if I can help in any thing.
best regards
Nuno