parent
64fb562be1
commit
417fd618f3
@ -1,40 +1,99 @@
|
||||
#include "i2c_comms.h"
|
||||
#include <Arduino.h>
|
||||
|
||||
#include "EspMQTTClient.h"
|
||||
|
||||
#include "i2c_comms.h"
|
||||
|
||||
#define MOTOR_STATUS_UPDATE_PERIOD_MS 500
|
||||
#define HEARTBEAT_PERIOD_MS 1000
|
||||
|
||||
EspMQTTClient mqtt_client("devtest", // Wifi SSID
|
||||
"87usc6rs", // WiFi password
|
||||
"10.9.2.101", // MQTT Broker server ip
|
||||
"", // Can be omitted if not needed
|
||||
"", // Can be omitted if not needed
|
||||
"Airseeder", // Client name that uniquely identify your device
|
||||
1883 // The MQTT port, default to 1883. this line can be omitted
|
||||
);
|
||||
|
||||
void setup() {
|
||||
Serial.setTimeout(1);
|
||||
Serial.begin(57600);
|
||||
i2c_comms_setup();
|
||||
|
||||
// Hopefully this lets us get missed messages during a connection.
|
||||
mqtt_client.enableMQTTPersistence();
|
||||
|
||||
mqtt_client.setMqttReconnectionAttemptDelay(10000);
|
||||
mqtt_client.setWifiReconnectionAttemptDelay(25000);
|
||||
|
||||
Serial.println("Connected to MQTT broker");
|
||||
}
|
||||
|
||||
char msg_buffer[100];
|
||||
|
||||
|
||||
void onRollerControlMessage(const String &message) {}
|
||||
|
||||
// Builtin function name used by EspMQTTClient
|
||||
void onConnectionEstablished() {
|
||||
Serial.println("Connected to MQTT broker");
|
||||
|
||||
mqtt_client.subscribe("airseeder/set/rollercontrol", onRollerControlMessage);
|
||||
|
||||
sprintf(msg_buffer, "%u", mqtt_client.getConnectionEstablishedCount());
|
||||
mqtt_client.publish("airseeder/status/connect_count", msg_buffer, true);
|
||||
}
|
||||
|
||||
motor_status_packet_t motor_status;
|
||||
|
||||
motor_control_group_t motor_control_group;
|
||||
|
||||
unsigned long now = 0;
|
||||
unsigned long last_motor_status = 0;
|
||||
|
||||
motor_control_group_t motor_control_group;
|
||||
unsigned long last_heartbeat = 0;
|
||||
|
||||
void loop() {
|
||||
now = millis();
|
||||
|
||||
if (now - last_motor_status > 500) {
|
||||
if (now - last_motor_status > MOTOR_STATUS_UPDATE_PERIOD_MS) {
|
||||
last_motor_status = now;
|
||||
request_motor_status_update();
|
||||
Serial.println("--Rq motor update--");
|
||||
}
|
||||
|
||||
if (get_motor_status_update(&motor_status)) {
|
||||
Serial.print("Dsr:");
|
||||
Serial.print(motor_status.motor_1_control.desired_speed);
|
||||
Serial.print(" Rsp:");
|
||||
Serial.print(motor_status.roller_1_speed);
|
||||
Serial.print(" Err:");
|
||||
Serial.print(motor_status.motor_1_control.desired_speed - motor_status.roller_1_speed);
|
||||
Serial.print(" Mdty:");
|
||||
Serial.println(motor_status.motor_1_duty);
|
||||
// Message format is "[timestamp] [enabled] [desired_speed] [actual_speed] [revs] [motor_duty] [motor_current]".
|
||||
// [timestamp] is an unsigned integer, the number of milliseconds since power on
|
||||
// [enabled] is a boolean, written as "0" or "1". Shows if the motor is active at all
|
||||
// [desired_speed] and [actual_speed] are floats, in revs/second
|
||||
// [revs] is a float, showing a count of total revolutions, wrapping at 10,000.
|
||||
// [motor_duty] is a float from 0.00 to 1.00 representing how much of the available PWM duty cycle the motor is
|
||||
// being driven with. [motor_current] is an estimated average current draw of the motor in amps (a float).
|
||||
// Floats are given with 2 digits after the decimal (dynamic before the decimal)
|
||||
|
||||
sprintf(msg_buffer, "%lu %d %.2f %.2f %.2f %.2f 0.00", now, motor_status.motor_1_control.enabled,
|
||||
motor_status.motor_1_control.desired_speed, motor_status.roller_1_speed, motor_status.roller_1_revs,
|
||||
motor_status.motor_1_duty);
|
||||
mqtt_client.publish("airseeder/status/roller1", msg_buffer, true);
|
||||
sprintf(msg_buffer, "%lu %d %.2f %.2f %.2f %.2f 0.00", now, motor_status.motor_2_control.enabled,
|
||||
motor_status.motor_2_control.desired_speed, motor_status.roller_2_speed, motor_status.roller_2_revs,
|
||||
motor_status.motor_2_duty);
|
||||
mqtt_client.publish("airseeder/status/roller2", msg_buffer, true);
|
||||
sprintf(msg_buffer, "%lu %d %.2f %.2f %.2f %.2f 0.00", now, motor_status.motor_3_control.enabled,
|
||||
motor_status.motor_3_control.desired_speed, motor_status.roller_3_speed, motor_status.roller_3_revs,
|
||||
motor_status.motor_3_duty);
|
||||
mqtt_client.publish("airseeder/status/roller3", msg_buffer, true);
|
||||
}
|
||||
|
||||
if (now - last_heartbeat > HEARTBEAT_PERIOD_MS) {
|
||||
last_heartbeat = now;
|
||||
sprintf(msg_buffer, "%lu", now);
|
||||
mqtt_client.publish("airseeder/status/heartbeat", msg_buffer, true);
|
||||
}
|
||||
|
||||
motor_control_group.motor_1.enabled = true;
|
||||
motor_control_group.motor_1.desired_speed = ((millis() % 10000) / 10000.0) * 0.65;
|
||||
set_motor_control(motor_control_group);
|
||||
|
||||
mqtt_client.loop();
|
||||
}
|
||||
Loading…
Reference in new issue