Adding stepper motor to tasmota, to control blinds

1,393 views
Skip to first unread message

Kim Skatun

unread,
Aug 2, 2018, 2:55:52 AM8/2/18
to SonoffUsers
Hi,
So i have 3dprinted some parts to electrify my ikea roller blinds. They uses a small 5-12v stepeprmotor driven by uln2003 stepper board, now I would like to expend the tasmota softaware to include this. Below is my attempt, but I do not know how to get the MQTT messages into my arduino sketch, any tips would be great:

#include <Stepper.h>

int STEPS_PER_MOTOR_REVOLUTION=200;
int MOTOR_SPEED = 20;
int PINA;
int PINB;
int PINC;
int PIND;

Stepper small_stepper;
// FROM WEBserver  Selection pinA,pinB,pinC,pinD
void init(int pinA,int pinB,int pinC,int pinD) {
  // Declare 'small_stepper' variable
  PINA=pinA;
  PINB=pinB;
  PINC=pinC;
  PIND=pinD;
  
  small_stepper(STEPS_PER_MOTOR_REVOLUTION, PINA, PINB, PINC, PIND);
}

// FROM MQTT STEPS_PER_MOTOR_REVOLUTION XXX
void setRev(int rev){
  STEPS_PER_MOTOR_REVOLUTION=rev;
  small_stepper = nothing;
  small_stepper(STEPS_PER_MOTOR_REVOLUTION, PINA, PINB, PINC, PIND);
}


// FROM MQTT SPEED XXX
void setSpeed(int speed){
  MOTOR_SPEED=speed;
  small_stepper.setSpeed(MOTOR_SPEED); 
}

// FROM MQTT SPEED 
int getSpeed(){
  return MOTOR_SPEED;
}


// FROM MQTT STEP XXX
void moveNumberOfSteps(int steps)
  small_stepper.step(steps);
}

// FROM MQTT STEP +
void moveForward()
  small_stepper.step(STEPS_PER_MOTOR_REVOLUTION);
}

// FROM MQTT STEP -
void moveBackward()
  small_stepper.step(-STEPS_PER_MOTOR_REVOLUTION);
}


Phil

unread,
Aug 2, 2018, 10:39:01 AM8/2/18
to SonoffUsers
Enter code here.../*
 Basic MQTT example

 This sketch demonstrates the basic capabilities of the library.
 It connects to an MQTT server then:
  - publishes "hello world" to the topic "outTopic"
  - subscribes to the topic "inTopic", printing out any messages
    it receives. NB - it assumes the received payloads are strings not binary

 It will reconnect to the server if the connection is lost using a blocking
 reconnect function. See the 'mqtt_reconnect_nonblocking' example for how to
 achieve the same result without blocking the main loop.
 
*/

#include <SPI.h>
#include <Ethernet.h>
#include <PubSubClient.h>

// Update these with values suitable for your network.
byte mac[]    = {  0xDE, 0xED, 0xBA, 0xFE, 0xFE, 0xED };
IPAddress ip(172, 16, 0, 100);
IPAddress server(172, 16, 0, 2);

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();
}

EthernetClient ethClient;
PubSubClient client(ethClient);

void reconnect() {
  // Loop until we're reconnected
  while (!client.connected()) {
    Serial.print("Attempting MQTT connection...");
    // Attempt to connect
    if (client.connect("arduinoClient")) {
      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()
{
  Serial.begin(57600);

  client.setServer(server, 1883);
  client.setCallback(callback);

  Ethernet.begin(mac, ip);
  // Allow the hardware to sort itself out
  delay(1500);
}

void loop()
{
  if (!client.connected()) {
    reconnect();
  }
  client.loop();
}

 
found in the pubsubclient arduino examples shows how to subscribe to a mqtt topic and receive the payload.. 

the tasmota payload is a json object  so check out the arduino json library examples for a roadmap on handling that.  
     

 
 

Kim Skatun

unread,
Aug 3, 2018, 3:39:35 AM8/3/18
to SonoffUsers
Thanks Phil,
however I would like to add it to the Tasmota server so that everyone can choose stepper motor as a sensor. The code you supplied were for standalone MQTT and not on how to integrate my sensor into tasmota. I will keep looking :)

 
 

Philip Knowles

unread,
Aug 3, 2018, 3:59:14 AM8/3/18
to Kim Skatun, SonoffUsers

You need to talk to Theo and add as a Pull Request in the Tasmota GitHub.

 

Regards

 

Phil K

 

Sent from Mail for Windows 10

--
You received this message because you are subscribed to the Google Groups "SonoffUsers" group.
To unsubscribe from this group and stop receiving emails from it, send an email to sonoffusers...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

 

Kim Skatun

unread,
Aug 6, 2018, 9:52:55 AM8/6/18
to SonoffUsers
Well before I can do a pull request I need a working code, and I need some help as stated to figure out how to parse the MQTT messages, or rather how to structure a new sensor.

Ken McMullan

unread,
Mar 27, 2019, 6:28:12 PM3/27/19
to SonoffUsers
Did this project gain any ground? I found it as I now also have the objective of adding a stepper motor to TASMOTA, and am also struggling with the architecture.

Phil

unread,
Mar 28, 2019, 8:13:24 PM3/28/19
to SonoffUsers
I dont know if the pull request was even made? 
atm i think the quickest route to results would be lifting one of the many esp8266-mqtt-stepper motor projects already out there
and including the new device mqtt options within your tasmota system 

a probable obstacle to including in tasmota is the range of different driver options and control methods but i am theorising as my coding is limited to lift and superficial edit these days.   
Reply all
Reply to author
Forward
0 new messages