Last active
October 22, 2020 12:45
-
-
Save jonchen727/24f5db87cfd8d764e27a7cdcca4a4db7 to your computer and use it in GitHub Desktop.
HX711+ESP8266+MQTT
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <ESP8266WiFi.h> | |
#include <PubSubClient.h> | |
#include "HX711.h" | |
const char* ssid = "Your SSID"; | |
const char* password = "Your Password"; | |
const char* mqtt_server = "Your MQTT Server Ip"; | |
WiFiClient espClient; | |
PubSubClient client(espClient); | |
#define DOUT 14 | |
#define CLK 12 | |
char msg_buff[50]; | |
int switchPin = 5; | |
int switchValue; | |
HX711 scale(DOUT, CLK); | |
//Change this calibration factor as per your load cell once it is found you many need to vary it in thousands | |
float calibration_factor = -219.50; //-219.50 worked for my scale | |
//============================================================================================= | |
// SETUP | |
//============================================================================================= | |
void setup() { | |
delay(100); | |
pinMode(BUILTIN_LED, OUTPUT); | |
pinMode(switchPin, INPUT_PULLUP); | |
Serial.begin(115200); | |
Serial.println("HX711 Calibration"); | |
setup_wifi(); | |
client.setServer(mqtt_server, 1883); | |
client.setCallback(callback); | |
scale_setup(); | |
} | |
void scale_setup() { | |
client.subscribe("HX711cal"); | |
Serial.println("HX711 Calibration"); | |
Serial.println("Remove all weight from scale"); | |
Serial.println("After readings begin, place known weight on scale"); | |
Serial.println("Press a,s,d,f to increase calibration factor by 10,100,1000,10000 respectively"); | |
Serial.println("Press z,x,c,v to decrease calibration factor by 10,100,1000,10000 respectively"); | |
Serial.println("Press t for tare"); | |
scale.tare(); //Reset the scale to 0 | |
delay(1000); | |
scale.tare(); | |
delay(500); | |
scale.tare(); | |
digitalWrite(BUILTIN_LED, HIGH); | |
delay(1000); | |
digitalWrite(BUILTIN_LED, LOW); | |
} | |
void setup_wifi(){ | |
delay(10); | |
Serial.println(); | |
Serial.print("Connecting to "); | |
Serial.println(ssid); | |
WiFi.begin(ssid, password); | |
while (WiFi.status() != WL_CONNECTED) { | |
delay(500); | |
Serial.print("."); | |
} | |
Serial.println(""); | |
Serial.println("WiFi connected"); | |
Serial.println("IP address: "); | |
Serial.println(WiFi.localIP()); | |
} | |
void reconnect() { | |
while(!client.connected()) { | |
Serial.print("Attempting MQTT connection..."); | |
if (client.connect("ESP8266Client")) { | |
Serial.println("connected"); | |
client.subscribe("HX711cal"); | |
} else { | |
Serial.print("failed, rc="); | |
Serial.print(client.state()); | |
Serial.println(" try again in 5 seconds"); | |
delay(5000); | |
} | |
} | |
} | |
void callback(char* topic, byte* payload, unsigned int length) { | |
int i = 0; | |
Serial.println("Message arrived: topic: " + String(topic)); | |
Serial.println("Length: " + String(length,DEC)); | |
for(i=0; i<length; i++) { | |
msg_buff[i] = payload[i]; | |
} | |
msg_buff[i] = '\0'; | |
String msgString = String(msg_buff); | |
Serial.println("Payload: " + msgString); | |
if (msgString == "tare"){ // if there is a "1" published to any topic (#) on the broker then: | |
digitalWrite(BUILTIN_LED, LOW); // set pin to the opposite state | |
Serial.println("Switching LED"); | |
scale.tare(5); | |
digitalWrite(BUILTIN_LED, HIGH); | |
} | |
else if (msgString == "+0.1") | |
calibration_factor += 0.1; | |
else if(msgString == "-0.1") | |
calibration_factor -= 0.1; | |
else if(msgString == "+1") | |
calibration_factor += 1; | |
else if(msgString == "-1") | |
calibration_factor -= 1; | |
else if(msgString == "+10") | |
calibration_factor += 10; | |
else if(msgString == "-10") | |
calibration_factor -= 10; | |
else if(msgString == "+100") | |
calibration_factor += 100; | |
else if(msgString == "-100") | |
calibration_factor -= 100; | |
} | |
//============================================================================================= | |
// LOOP | |
//============================================================================================= | |
void loop() { | |
if (!client.connected()) { | |
reconnect(); | |
} | |
client.loop(); | |
float weight = scale.get_units(15); | |
float rounded = round(weight) ; | |
scale.set_scale(calibration_factor); //Adjust to this calibration factor | |
switchValue = digitalRead(switchPin); | |
Serial.print("Reading: "); | |
Serial.print(weight); | |
Serial.print(" g"); //Change this to kg and re-adjust the calibration factor if you follow SI units like a sane person | |
Serial.print(" calibration_factor: "); | |
Serial.print(calibration_factor); | |
Serial.println(); | |
Serial.println(switchValue); | |
client.publish("Scale1",String(rounded).c_str(),true); | |
client.publish("Debug",String(weight).c_str(),true); | |
client.publish("HX711calfactor",String(calibration_factor).c_str(),true); | |
if(switchValue == 1){ | |
digitalWrite(BUILTIN_LED, LOW); | |
scale.tare(5); | |
digitalWrite(BUILTIN_LED, HIGH); | |
} | |
if(Serial.available()) | |
{ | |
char temp = Serial.read(); | |
if(temp == '+' || temp == 'a') | |
calibration_factor += 0.1; | |
else if(temp == '-' || temp == 'z') | |
calibration_factor -= 0.1; | |
else if(temp == 's') | |
calibration_factor += 1; | |
else if(temp == 'x') | |
calibration_factor -= 1; | |
else if(temp == 'd') | |
calibration_factor += 10; | |
else if(temp == 'c') | |
calibration_factor -= 10; | |
else if(temp == 'f') | |
calibration_factor += 100; | |
else if(temp == 'v') | |
calibration_factor -= 100; | |
else if(switchValue == '0'); | |
scale.tare(5); //Reset the scale to zero | |
else if(temp='t') | |
scale.tare(5); //Reset the scale to zero | |
} | |
} | |
//============================================================================================= |
Have you solved this issue. I still strugle with it.
The problem is probably with different libraries for the HX711 this code was pulled from a production environment and was verified to work with my version of the library. You might want to open your specific HX711 library and look at the proper calls are like 17 specifically. You might need to replace HX711 scale(DOUT, CLK);
with HX711 scale;
then add scale.begin(DOUT, CLK);
for example
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi. I found something in your code. I tried to compile and got an error.
Line 176-179
I has to be like (error is gone and beautyfying)
But the code doesn't work either. I just got
then it ends. Some ideas?