diff options
| author | Manuel Traut <manut@linutronix.de> | 2018-05-26 11:52:37 +0200 |
|---|---|---|
| committer | Manuel Traut <manut@linutronix.de> | 2018-06-01 23:26:54 +0200 |
| commit | e1da1c1872ee9168aad27e41e4769a32f25e3b46 (patch) | |
| tree | 244d2029b34ab173604c48dba2afc882f1ae0b04 | |
| parent | fdee85090126648cf2a7b22a7483e37eec3b6a38 (diff) | |
code cleanup
Signed-off-by: Manuel Traut <manut@linutronix.de>
| -rw-r--r-- | mqttweightwatcher.ino | 136 |
1 files changed, 72 insertions, 64 deletions
diff --git a/mqttweightwatcher.ino b/mqttweightwatcher.ino index ecd9ccc..6ba5268 100644 --- a/mqttweightwatcher.ino +++ b/mqttweightwatcher.ino @@ -1,57 +1,35 @@ -// 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"; +#undef DEBUG -long t; +String swarm_name = "ArcticMonkeys"; -WiFiClient net; -MQTTClient client; +const char* ssid = "disasterarea"; +const char* pass = "dMiadgSp"; -//HX711 constructor (dout pin, sck pin) -HX711_ADC LoadCell(D1, D0); +// Note: Local domain names (e.g. "Computer.local" on OSX) are not supported by Arduino. +// You need to set the IP address directly. +const char* mqtt_broker = "10.0.0.1"; -unsigned long lastMillis = 0; +float loadcell_scale = 68906.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!"); +bool calibrate = false; +long t; - client.subscribe("/huhu"); +//HX711 constructor (dout pin, sck pin) +//HX711_ADC LoadCell(4, 5); // tiny board +HX711_ADC LoadCell(D1, D0); // NodeMCU - Serial.println("\nsubscribed\n!"); - // client.unsubscribe("/hello"); -} +WiFiClient Net; +MQTTClient MQTT; -void messageReceived(String &topic, String &payload) { +void mqtt_rx(String &topic, String &payload) { Serial.println("incoming: " + topic + " - " + payload); } -void setup() { - Serial.begin(115200); - +void setup_wifi() { Serial.print("Connecting to "); Serial.println(ssid); WiFi.mode(WIFI_STA); @@ -59,52 +37,84 @@ void setup() { while (WiFi.status() != WL_CONNECTED) { delay(500); - Serial.print("."); + Serial.print("W"); } - Serial.println(""); Serial.println("WiFi connected"); Serial.println("IP address: "); Serial.println(WiFi.localIP()); +} + +void mqtt_connect() { + String config_topic = "/"+swarm_name+"/config"; + + Serial.print("MQTT connecting to broker "); + Serial.println(mqtt_broker); + + while (!MQTT.connect(mqtt_broker)) { + Serial.print("M"); + delay(1000); + } + Serial.println("\nMQTT connected!"); - // 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); + MQTT.subscribe(config_topic); + Serial.print("MQTT subscribed: "); + Serial.println(config_topic); +} - connect(); +void setup_mqtt() { + MQTT.begin(mqtt_broker, Net); + MQTT.onMessage(mqtt_rx); + mqtt_connect(); +} - Serial.println("load cell begin\n"); +void setup_loadcell() { + Serial.println("Loadcell setup"); LoadCell.begin(); long stabilisingtime = 1000; // tare preciscion can be improved by adding a few seconds of stabilising time - Serial.println("load cell start\n"); + Serial.println("Loadcell stabilize"); LoadCell.start(stabilisingtime); - Serial.println("load cell scale\n"); - LoadCell.setCalFactor(68906.0); // user set calibration factor (float) - Serial.println("Startup + tare is complete"); + Serial.print("Loadcell set scale"); + Serial.println(loadcell_scale); + LoadCell.setCalFactor(loadcell_scale); // user set calibration factor (float) + Serial.println("Loadcell startup + tare complete"); +} + +void setup() { + setup_wifi(); + setup_mqtt(); + setup_loadcell(); } void do_publish() { delay(10); // <- fixes some issues with WiFi stability - if (!client.connected()) { - connect(); + if (WiFi.status() != WL_CONNECTED) { + setup_wifi(); + } + if (!MQTT.connected()) { + mqtt_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); + static String old_val = "0.00"; + String val = String(LoadCell.getData()); + if (val == "-0.00") + val = "0.00"; + // do string compare to compare only two digits after the comma + if (old_val != val) { + #ifdef DEBUG + Serial.print("Load_cell output val: "); + Serial.println(val); + #endif + old_val = val; + MQTT.publish(String("/"+swarm_name+"/weight"), val); + } t = millis(); - sprintf(s1_str, "%f\n", i); - lastMillis = millis(); - client.publish("/weight1", s1_str); } - } void do_calibrate() { @@ -142,10 +152,8 @@ void do_calibrate() { } } -bool calibrate = false; - void loop() { - client.loop(); + MQTT.loop(); if (calibrate) { do_calibrate(); |
