-
Notifications
You must be signed in to change notification settings - Fork 0
/
code.ino
92 lines (75 loc) · 1.94 KB
/
code.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#include <Servo.h>
#include "HX711.h"
#include "ESP8266WiFi.h"
#include "Adafruit_MQTT.h"
#include "Adafruit_MQTT_Client.h"
// Servo
Servo ser;
int pos = 0;
// Load cell
HX711 scale;
float weight;
// Millis timing
const unsigned long eventInterval = 3000;
unsigned long previousTime = 0;
// Wifi and MQTT config
#define WLAN_SSID "ssid"
#define WLAN_PASS "password"
#define AIO_SERVER "io.adafruit.com"
#define AIO_SERVERPORT 1883
#define AIO_USERNAME "aio_username"
#define AIO_KEY "aio_key"
WiFiClient client;
Adafruit_MQTT_Client mqtt(&client, AIO_SERVER, AIO_SERVERPORT, AIO_USERNAME, AIO_KEY);
// MQTT subscribe
Adafruit_MQTT_Subscribe servo = Adafruit_MQTT_Subscribe(&mqtt, AIO_USERNAME "/feeds/servo");
// MQTT publish
Adafruit_MQTT_Publish weight = Adafruit_MQTT_Publish(&mqtt, AIO_USERNAME "/feeds/weight");
void setup() {
Serial.begin(115200);
// Servo
ser.attach(2);
ser.write(0);
// Load cell
scale.begin(A1, A2);
scale.set_scale();
scale.tare();
// Wifi
WiFi.begin(WLAN_SSID, WLAN_PASS);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
}
// MQTT
mqtt.subscribe(&servo);
}
void loop() {
// Connect to MQTT
mqtt.connect();
// Handle MQTT messages
Adafruit_MQTT_Subscribe *subscription;
while ((subscription = mqtt.readSubscription(10000))) {
if (subscription == &servo) {
int servo_State = atoi((char *)servo.lastread);
if (servo_State == 1) {
ser.write(180);
} else {
ser.write(0);
}
}
}
// Millis timing
unsigned long currentTime = millis();
if (currentTime - previousTime >= eventInterval) {
// Read weight from load cell
weight = scale.get_units();
// Publish weight to MQTT
weight.publish(weight);
// Display weight on Serial Monitor
Serial.print("Weight: ");
Serial.println(weight);
previousTime = currentTime;
}
// Disconnect MQTT
mqtt.disconnect();
delay(20);
}