summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorManuel Traut <manut@ford.mecka.net>2018-05-24 23:12:55 +0200
committerManuel Traut <manut@ford.mecka.net>2018-05-24 23:12:55 +0200
commitedf02979ec495207326d5a4facbcfa8661bcd5c2 (patch)
treecc36a7ad8a97969bbd6af42baa5a65baf885a243
initial working version
now it's time to cleanup the codebase! Signed-off-by: Manuel Traut <manut@ford.mecka.net>
-rw-r--r--mqttweightwatcher.ino150
1 files changed, 150 insertions, 0 deletions
diff --git a/mqttweightwatcher.ino b/mqttweightwatcher.ino
new file mode 100644
index 0000000..057e798
--- /dev/null
+++ b/mqttweightwatcher.ino
@@ -0,0 +1,150 @@
+// This example uses an ESP32 Development Board
+// to connect to shiftr.io.
+//
+// You can check on your device after a successful
+// connection here: https://shiftr.io/try.
+//
+// by Joël Gähwiler
+// https://github.com/256dpi/arduino-mqtt
+
+#include <ESP8266WiFi.h>
+#include <MQTT.h>
+#include <HX711_ADC.h>
+
+const char* ssid = "disasterarea";
+const char* pass = "dMiadgSp";
+
+long t;
+
+WiFiClient net;
+MQTTClient client;
+
+//HX711 constructor (dout pin, sck pin)
+HX711_ADC LoadCell(D1, D0);
+
+unsigned long lastMillis = 0;
+
+void connect() {
+ Serial.print("checking wifi...");
+ while (WiFi.status() != WL_CONNECTED) {
+ Serial.print(".");
+ delay(1000);
+ }
+
+ Serial.print("\nconnecting...");
+ while (!client.connect("10.0.0.120")) {
+ Serial.print(".");
+ delay(1000);
+ }
+
+ Serial.println("\nconnected!");
+
+ client.subscribe("/huhu");
+ // client.unsubscribe("/hello");
+}
+
+void messageReceived(String &topic, String &payload) {
+ Serial.println("incoming: " + topic + " - " + payload);
+}
+
+void setup() {
+ Serial.begin(115200);
+
+ Serial.print("Connecting to ");
+ Serial.println(ssid);
+ WiFi.mode(WIFI_STA);
+ WiFi.begin(ssid, pass);
+
+ while (WiFi.status() != WL_CONNECTED) {
+ delay(500);
+ Serial.print(".");
+ }
+
+ Serial.println("");
+ Serial.println("WiFi connected");
+ Serial.println("IP address: ");
+ Serial.println(WiFi.localIP());
+
+ // Note: Local domain names (e.g. "Computer.local" on OSX) are not supported by Arduino.
+ // You need to set the IP address directly.
+ client.begin("10.0.0.120", net);
+ client.onMessage(messageReceived);
+
+ connect();
+
+ LoadCell.begin();
+ long stabilisingtime = 2000; // tare preciscion can be improved by adding a few seconds of stabilising time
+ LoadCell.start(stabilisingtime);
+ LoadCell.setCalFactor(68906.0); // user set calibration factor (float)
+ Serial.println("Startup + tare is complete");
+}
+
+void do_publish() {
+ delay(10); // <- fixes some issues with WiFi stability
+
+ if (!client.connected()) {
+ connect();
+ }
+
+ LoadCell.update();
+
+ //get smoothed value from data set + current calibration factor
+ if (millis() > t + 250) {
+ char s1_str[255];
+ float i = LoadCell.getData();
+ Serial.print("Load_cell output val: ");
+ Serial.println(i);
+ t = millis();
+ sprintf(s1_str, "%f\n", i);
+ lastMillis = millis();
+ client.publish("/weight1", s1_str);
+ }
+
+}
+
+void do_calibrate() {
+
+ LoadCell.update();
+
+ if (millis() > t + 250) {
+ float i = LoadCell.getData();
+ float v = LoadCell.getCalFactor();
+ Serial.print("Load_cell output val: ");
+ Serial.print(i);
+ Serial.print(" Load_cell calFactor: ");
+ Serial.println(v);
+ t = millis();
+ }
+
+ //receive from serial terminal
+ if (Serial.available() > 0) {
+ float i;
+ char inByte = Serial.read();
+ if (inByte == 'l') i = -1.0;
+ else if (inByte == 'L') i = -10.0;
+ else if (inByte == 'h') i = 1.0;
+ else if (inByte == 'H') i = 10.0;
+ else if (inByte == 't') LoadCell.tareNoDelay();
+ if (i != 't') {
+ float v = LoadCell.getCalFactor() + i;
+ LoadCell.setCalFactor(v);
+ }
+ }
+
+ //check if last tare operation is complete
+ if (LoadCell.getTareStatus() == true) {
+ Serial.println("Tare complete");
+ }
+}
+
+bool calibrate = false;
+
+void loop() {
+ client.loop();
+
+ if (calibrate) {
+ do_calibrate();
+ } else {
+ do_publish();
+ }
+}