#include #include #include #undef DEBUG String swarm_name = "ArcticMonkeys"; const char* ssid = "disasterarea"; const char* pass = "dMiadgSp"; // 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"; float loadcell_scale = 68906.0; bool calibrate = false; long t; //HX711 constructor (dout pin, sck pin) //HX711_ADC LoadCell(4, 5); // tiny board HX711_ADC LoadCell(D1, D0); // NodeMCU WiFiClient Net; MQTTClient MQTT; void mqtt_rx(String &topic, String &payload) { Serial.println("incoming: " + topic + " - " + payload); } void setup_wifi() { Serial.print("Connecting to "); Serial.println(ssid); WiFi.mode(WIFI_STA); WiFi.begin(ssid, pass); while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("W"); } 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!"); MQTT.subscribe(config_topic); Serial.print("MQTT subscribed: "); Serial.println(config_topic); } void setup_mqtt() { MQTT.begin(mqtt_broker, Net); MQTT.onMessage(mqtt_rx); mqtt_connect(); } 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("Loadcell stabilize"); LoadCell.start(stabilisingtime); 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 (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) { 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(); } } 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"); } } void loop() { MQTT.loop(); if (calibrate) { do_calibrate(); } else { do_publish(); } }