2024年7月3日 星期三

WOKWI Stepper Motor & MQTT

WOKWI Stepper Motor & MQTT





#include <Stepper.h>
#include <WiFi.h>
#include <PubSubClient.h>

float stepmax = 600;
// (A- ==> 26 , A+ ==> 27 , B+ ==> 14 , B- ==> 12  )
// (A- ==> 21 , A+ ==> 19 , B+ ==> 18 , B- ==> 5 )
// initialize the stepper library on pins 8 through 11:
Stepper Stepper1(stepmax, 12, 14, 27, 26);
Stepper Stepper2(stepmax, 21,19, 18, 5);

// Update these with values suitable for your network.
const char* ssid =  "Wokwi-GUEST";   // your network SSID (name)
const char* password = "";   // your network password
//const char* mqtt_server = "broker.mqttdashboard.com";// choose your mqtt server
const char* mqtt_server = "broker.mqttgo.io";

WiFiClient espClient;
PubSubClient client(espClient);
long lastMsg = 0;
char msg[50];
int value = 0;
//=====================================================================
int i = 0;
float step1=0;
float step2=0;

float steptarget1 = 500;
//float stepmax = 600;
float steptarget2 = 300;

float deltaT = 5;
float speed1;
float speed2;


//=========================================================
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.print(topic);
  Serial.print(" Rotation angle is:");
  Serial.print("   ");
  String message="";

  for(int i=0;i<length;i++)
  {
    message += ((char)payload[i]);
    //Serial.print((char)payload[i]);
  }
  message.trim();
  int show= message.toInt();
  steptarget1 = show * 1.0;
  steptarget2 = show * 1.0;

  if (String(topic)=="alex9ufo/stepcommand1") {
    Stepper1.step(steptarget1);
   
    Serial.print(show);   //print the rotation angle
    Serial.print("]");
    Serial.println();
  }

  if (String(topic)=="alex9ufo/stepcommand2") {
    Stepper2.step(steptarget2);

    Serial.print(show);   //print the rotation angle
    Serial.print("]");
    Serial.println();
  }
//  Stepper1.step(steptarget1);
//  Stepper2.step(steptarget2);

}//end callback

//=========================================================
void reconnect() {
  // Loop until we're reconnected
  while (!client.connected())
  {
    Serial.print("Attempting MQTT connection...");
    // Create a random client ID
    String clientId = "ESP32Client-";
    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("alex9ufo/stepcommand1");
      client.subscribe("alex9ufo/stepcommand2");
    } 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() {
  // initialize the serial port:
  Serial.begin(115200);
  speed1 = ((steptarget1*60)/(deltaT*stepmax));
  speed2 = ((steptarget2*60)/(deltaT*stepmax));
  Stepper1.setSpeed(speed1);
  Stepper2.setSpeed(speed2);

  Serial.print("speed1==>");
  Serial.println(speed1);

  Serial.print("speed2==>");
  Serial.println(speed2);

  setup_wifi();
  client.setServer(mqtt_server, 1883);

}
//=========================================================
void loop() {

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

}





沒有留言:

張貼留言

2024_09 作業3 以Node-Red 為主

 2024_09 作業3  (以Node-Red 為主  Arduino 可能需要配合修改 ) Arduino 可能需要修改的部分 1)mqtt broker  2) 主題Topic (發行 接收) 3) WIFI ssid , password const char br...