Hello
I'm successfully sending MQTT messages from Node-Red (Altitude and Azimuth from the SunPos node) as a number and reading them in an Arduino sketch where I'd like to move a Stepper Motor to the degrees supplied by the SunPos node, I also have the Steppers moving with the test sketches.
I'm using AysncMQTT Client, could someone please advise on where to correctly place that part in the code?
The code is still a work in progress and not properly commented I'm afraid.
I wish to also later enable and disable the motors after every move to save power.
#include <WiFi.h>
extern "C" {
#include "freertos/FreeRTOS.h"
#include "freertos/timers.h"
}
#include <AsyncMqttClient.h>
#include <AccelStepper.h>
#define WIFI_SSID "xxxxxxx"
#define WIFI_PASSWORD "xxxxxxxxx"
#define MQTT_HOST IPAddress(192, 168, 0, 3)
//#define MQTT_HOST IPAddress(192, 168, 1, 188)
#define MQTT_PORT 1883
// Define two steppers and the pins they will use
AccelStepper stepper1(AccelStepper::DRIVER, 36, 39);
//AccelStepper stepper2(AccelStepper::DRIVER, 14, 12);
int pos1 = 3600;
//int pos2 = 5678;
String temperatureString = ""; // Variable to hold the temperature reading
unsigned long previousMillis0 = 0; // Stores last time temperature was published
unsigned long previousMillis1 = 0;
const long interval = 5000; // interval at which to publish sensor readings
AsyncMqttClient mqttClient;
TimerHandle_t mqttReconnectTimer;
TimerHandle_t wifiReconnectTimer;
void connectToWifi() {
Serial.println("Connecting to Wi-Fi...");
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
}
void connectToMqtt() {
Serial.println("Connecting to MQTT...");
mqttClient.connect();
}
void WiFiEvent(WiFiEvent_t event) {
Serial.printf("[WiFi-event] event: %d\n", event);
switch (event) {
case SYSTEM_EVENT_STA_GOT_IP:
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
connectToMqtt();
break;
case SYSTEM_EVENT_STA_DISCONNECTED:
Serial.println("WiFi lost connection");
xTimerStop(mqttReconnectTimer, 0); // ensure we don't reconnect to MQTT while reconnecting to Wi-Fi
xTimerStart(wifiReconnectTimer, 0);
break;
}
}
void onMqttConnect(bool sessionPresent) {
Serial.println("Connected to MQTT.");
Serial.print("Session present: ");
Serial.println(sessionPresent);
uint16_t packetIdSub0 = mqttClient.subscribe("azimuth", 0);
uint16_t packetIdSub1 = mqttClient.subscribe("altitude", 0);
Serial.print("Subscribing at QoS 0, packetId: ");
Serial.println(packetIdSub0);
Serial.println(packetIdSub1);
}
void onMqttDisconnect(AsyncMqttClientDisconnectReason reason) {
Serial.println("Disconnected from MQTT.");
if (WiFi.isConnected()) {
xTimerStart(mqttReconnectTimer, 0);
}
}
void onMqttSubscribe(uint16_t packetId, uint8_t qos) {
//Serial.println("Subscribe acknowledged.");
//Serial.print(" packetId: ");
//Serial.println(packetId);
//Serial.print(" qos: ");
//Serial.println(qos);
}
void onMqttUnsubscribe(uint16_t packetId) {
//Serial.println("Unsubscribe acknowledged.");
//Serial.print(" packetId: ");
//Serial.println(packetId);
}
void onMqttMessage(char* topic, char* payload, AsyncMqttClientMessageProperties properties, size_t len, size_t index, size_t total) {
String messageTemp;
for (int i = 0; i < len; i++) {
Serial.print((char)payload[i]);
messageTemp += (char)payload[i];
}
if (strcmp(topic, "azimuth") == 0) {
stepper1.moveTo(messageTemp);
stepper1.run();
delay(5);
Serial.print("moved to: ");
}
Serial.println(" Publish received.");
Serial.print(" topic: ");
Serial.println(topic);
//Serial.print(" qos: ");
//Serial.println(properties.qos);
//Serial.print(" dup: ");
//Serial.println(properties.dup);
//Serial.print(" retain: ");
//Serial.println(properties.retain);
//Serial.print(" len: ");
//Serial.println(len);
//Serial.print(" index: ");
//Serial.println(index);
//Serial.print(" total: ");
//Serial.println(total);
}
void onMqttPublish(uint16_t packetId) {
Serial.println("Publish acknowledged.");
Serial.print(" packetId: ");
Serial.println(packetId);
}
void setup() {
Serial.begin(115200);
Serial.println();
stepper1.setCurrentPosition(0);
stepper1.setMaxSpeed(1000);
stepper1.setAcceleration(1000);
//stepper2.setMaxSpeed(2000);
//stepper2.setAcceleration(800);
stepper1.setEnablePin(34);
mqttReconnectTimer = xTimerCreate("mqttTimer", pdMS_TO_TICKS(2000), pdFALSE, (void*)0, reinterpret_cast<TimerCallbackFunction_t>(connectToMqtt));
wifiReconnectTimer = xTimerCreate("wifiTimer", pdMS_TO_TICKS(2000), pdFALSE, (void*)0, reinterpret_cast<TimerCallbackFunction_t>(connectToWifi));
WiFi.onEvent(WiFiEvent);
mqttClient.onConnect(onMqttConnect);
mqttClient.onDisconnect(onMqttDisconnect);
mqttClient.onSubscribe(onMqttSubscribe);
mqttClient.onUnsubscribe(onMqttUnsubscribe);
mqttClient.onMessage(onMqttMessage);
mqttClient.onPublish(onMqttPublish);
mqttClient.setServer(MQTT_HOST, MQTT_PORT);
connectToWifi();
}
void loop() {
}