Hi,
Im trying to control a stepper motor with a nodemcu hooked to it. I also have a buck converter added in the wireing.
The problem is that when i plug in the power the motor will start to move until I stop it manually witch i think is very odd. I dont want it to do that.
Apart from this the motor and control interface works as intended. I can set max min position and start and stop it as intended.
I have attached the wireing along with the code.
I have not made the code it is taken from a project from here: Motor on a roller blind by nidayand - Thingiverse
BOM:
28BYJ-48 stepper motor
UNL2003
Nodemcu
Buck converter
Anyone that can explain why it starts to move when it gets power?
Code split in two posts because it extends the max character allowed in forum...
#include <Stepper_28BYJ_48.h>
#include <ESP8266WiFi.h>
#include <ESP8266mDNS.h>
#include <PubSubClient.h>
#include <WiFiUdp.h>
#include <WiFiManager.h>
#include <ArduinoJson.h>
#include "FS.h"
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#include <WebSocketsServer.h>
#include <ArduinoOTA.h>
#include "NidayandHelper.h"
#include "index_html.h"
//--------------- CHANGE PARAMETERS ------------------
//Configure Default Settings for Access Point logon
String APid = "BlindsConnectAP"; //Name of access point
String APpw = "nidayand"; //Hardcoded password for access point
//----------------------------------------------------
// Version number for checking if there are new code releases and notifying the user
String version = "1.3.1";
NidayandHelper helper = NidayandHelper();
//Fixed settings for WIFI
WiFiClient espClient;
PubSubClient psclient(espClient); //MQTT client
char mqtt_server[40]; //WIFI config: MQTT server config (optional)
char mqtt_port[6] = "1883"; //WIFI config: MQTT port config (optional)
char mqtt_uid[40]; //WIFI config: MQTT server username (optional)
char mqtt_pwd[40]; //WIFI config: MQTT server password (optional)
String outputTopic; //MQTT topic for sending messages
String inputTopic; //MQTT topic for listening
boolean mqttActive = true;
char config_name[40]; //WIFI config: Bonjour name of device
char config_rotation[40] = "false"; //WIFI config: Detault rotation is CCW
String action; //Action manual/auto
int path = 0; //Direction of blind (1 = down, 0 = stop, -1 = up)
int setPos = 0; //The set position 0-100% by the client
long currentPosition = 0; //Current position of the blind
long maxPosition = 2000000; //Max position of the blind. Initial value
boolean loadDataSuccess = false;
boolean saveItNow = false; //If true will store positions to SPIFFS
bool shouldSaveConfig = false; //Used for WIFI Manager callback to save parameters
boolean initLoop = true; //To enable actions first time the loop is run
boolean ccw = true; //Turns counter clockwise to lower the curtain
Stepper_28BYJ_48 small_stepper(D1, D3, D2, D4); //Initiate stepper driver
ESP8266WebServer server(80); // TCP server at port 80 will respond to HTTP requests
WebSocketsServer webSocket = WebSocketsServer(81); // WebSockets will respond on port 81
bool loadConfig() {
if (!helper.loadconfig()){
return false;
}
JsonVariant json = helper.getconfig();
//Store variables locally
currentPosition = long(json["currentPosition"]);
maxPosition = long(json["maxPosition"]);
strcpy(config_name, json["config_name"]);
strcpy(mqtt_server, json["mqtt_server"]);
strcpy(mqtt_port, json["mqtt_port"]);
strcpy(mqtt_uid, json["mqtt_uid"]);
strcpy(mqtt_pwd, json["mqtt_pwd"]);
strcpy(config_rotation, json["config_rotation"]);
return true;
}
/**
Save configuration data to a JSON file
on SPIFFS
*/
bool saveConfig() {
StaticJsonBuffer<200> jsonBuffer;
JsonObject& json = jsonBuffer.createObject();
json["currentPosition"] = currentPosition;
json["maxPosition"] = maxPosition;
json["config_name"] = config_name;
json["mqtt_server"] = mqtt_server;
json["mqtt_port"] = mqtt_port;
json["mqtt_uid"] = mqtt_uid;
json["mqtt_pwd"] = mqtt_pwd;
json["config_rotation"] = config_rotation;
return helper.saveconfig(json);
}
/*
Connect to MQTT server and publish a message on the bus.
Finally, close down the connection and radio
*/
void sendmsg(String topic, String payload) {
if (!mqttActive)
return;
helper.mqtt_publish(psclient, topic, payload);
}
/****************************************************************************************
*/
void processMsg(String res, uint8_t clientnum){
/*
Check if calibration is running and if stop is received. Store the location
*/
if (action == "set" && res == "(0)") {
maxPosition = currentPosition;
saveItNow = true;
}
/*
Below are actions based on inbound MQTT payload
*/
if (res == "(start)") {
/*
Store the current position as the start position
*/
currentPosition = 0;
path = 0;
saveItNow = true;
action = "manual";
} else if (res == "(max)") {
/*
Store the max position of a closed blind
*/
maxPosition = currentPosition;
path = 0;
saveItNow = true;
action = "manual";
} else if (res == "(0)") {
/*
Stop
*/
path = 0;
saveItNow = true;
action = "manual";
} else if (res == "(1)") {
/*
Move down without limit to max position
*/
path = 1;
action = "manual";
} else if (res == "(-1)") {
/*
Move up without limit to top position
*/
path = -1;
action = "manual";
} else if (res == "(update)") {
//Send position details to client
int set = (setPos * 100)/maxPosition;
int pos = (currentPosition * 100)/maxPosition;
sendmsg(outputTopic, "{ \"set\":"+String(set)+", \"position\":"+String(pos)+" }");
webSocket.sendTXT(clientnum, "{ \"set\":"+String(set)+", \"position\":"+String(pos)+" }");
} else if (res == "(ping)") {
//Do nothing
} else {
/*
Any other message will take the blind to a position
Incoming value = 0-100
path is now the position
*/
path = maxPosition * res.toInt() / 100;
setPos = path; //Copy path for responding to updates
action = "auto";
int set = (setPos * 100)/maxPosition;
int pos = (currentPosition * 100)/maxPosition;
//Send the instruction to all connected devices
sendmsg(outputTopic, "{ \"set\":"+String(set)+", \"position\":"+String(pos)+" }");
webSocket.broadcastTXT("{ \"set\":"+String(set)+", \"position\":"+String(pos)+" }");
}
}
void webSocketEvent(uint8_t num, WStype_t type, uint8_t * payload, size_t length) {
switch(type) {
case WStype_TEXT:
Serial.printf("[%u] get Text: %s\n", num, payload);
String res = (char*)payload;
//Send to common MQTT and websocket function
processMsg(res, num);
break;
}
}
void mqttCallback(char* topic, byte* payload, unsigned int length) {
Serial.print("Message arrived [");
Serial.print(topic);
Serial.print("] ");
String res = "";
for (int i = 0; i < length; i++) {
res += String((char) payload[i]);
}
processMsg(res, NULL);
}
/**
Turn of power to coils whenever the blind
is not moving
*/
void stopPowerToCoils() {
digitalWrite(D1, LOW);
digitalWrite(D2, LOW);
digitalWrite(D3, LOW);
digitalWrite(D4, LOW);
}
/*
Callback from WIFI Manager for saving configuration
*/
void saveConfigCallback () {
shouldSaveConfig = true;
}
void handleRoot() {
server.send(200, "text/html", INDEX_HTML);
}
void handleNotFound(){
String message = "File Not Found\n\n";
message += "URI: ";
message += server.uri();
message += "\nMethod: ";
message += (server.method() == HTTP_GET)?"GET":"POST";
message += "\nArguments: ";
message += server.args();
message += "\n";
for (uint8_t i=0; i<server.args(); i++){
message += " " + server.argName(i) + ": " + server.arg(i) + "\n";
}
server.send(404, "text/plain", message);
}
//r1kkie
