// 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 #include #include 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"); Serial.println("\nsubscribed\n!"); // 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(); Serial.println("load cell begin\n"); LoadCell.begin(); long stabilisingtime = 1000; // tare preciscion can be improved by adding a few seconds of stabilising time Serial.println("load cell start\n"); LoadCell.start(stabilisingtime); Serial.println("load cell scale\n"); 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(); } }