Newbie needs help with ESP8266, HX711 and a load cell


#1

Can’t figure out how to implement a load cell. I use ESP8266 NodeMUC and a HX711 is conected between my load cell and ESP8266 as well as a DHT22. The temperature and humidity sensor works well on Cyaenne dashboard but I can’t figure out any useful code to the load cell. Appreciate any help … thanks.


#2

try this code and see if nodemcu reads any values:

#include <HX711.h>

// Scale Settings
const int SCALE_DOUT_PIN = D2;
const int SCALE_SCK_PIN = D3;

HX711 scale(SCALE_DOUT_PIN, SCALE_SCK_PIN);

void setup() {
  Serial.begin(115200);
  scale.set_scale();// <- set here calibration factor!!!
  scale.tare();
}

void loop() {
  float weight = scale.get_units(1);
  Serial.println(String(weight, 2));
}

#3

Thank you… much appreciated.
Where would I insert your suggestion into my code with DHT22 and a LED?

#define CAYENNE_DEBUG
#define CAYENNE_PRINT Serial
#include <CayenneMQTTESP8266.h>
#include <DHT.h>
// WiFi network info.
char ssid[] = "";
char wifiPassword[] = "";

// Cayenne authentication info. This should be obtained from the Cayenne Dashboard.
char username[] = "";
char password[] = "";
char clientID[] = "";

DHT dht(D2, DHT22);

unsigned long lastMillis = 0;

void setup() {
	Serial.begin(9600);
	Cayenne.begin(username, password, clientID, ssid, wifiPassword);
  pinMode(2, OUTPUT);
}

void loop() {
	Cayenne.loop();
  float temp = dht.readTemperature(false);  //Fahrenheit
  float hum = dht.readHumidity();
  Cayenne.virtualWrite(1, temp, TYPE_TEMPERATURE, UNIT_CELSIUS);
  Cayenne.virtualWrite(2, hum, TYPE_RELATIVE_HUMIDITY, UNIT_PERCENT);
}

CAYENNE_IN(0)
{
  digitalWrite(2, !getValue.asInt());
}


// Default function for sending sensor data at intervals to Cayenne.
// You can also use functions for specific channels, e.g CAYENNE_OUT(1) for sending channel 1 data.
CAYENNE_OUT_DEFAULT()
{
	// Write data to Cayenne here. This example just sends the current uptime in milliseconds on virtual channel 0.
	Cayenne.virtualWrite(0, millis());
	// Some examples of other functions you can use to send data.
	//Cayenne.celsiusWrite(1, 22.0);
	//Cayenne.luxWrite(2, 700);
	//Cayenne.virtualWrite(3, 50, TYPE_PROXIMITY, UNIT_CENTIMETER);
}

// Default function for processing actuator commands from the Cayenne Dashboard.
// You can also use functions for specific channels, e.g CAYENNE_IN(1) for channel 1 commands.
CAYENNE_IN_DEFAULT()
{
	CAYENNE_LOG("Channel %u, value %s", request.channel, getValue.asString());
	//Process message here. If there is an error set an error message using getValue.setError(), e.g getValue.setError("Error message");
}

#5

Hmm… it seems that NudeMCU can’t handle that code.


#6

@kardan first find a HX11 code that works on nodemcu. then it is easy to add the cayenne code.


#7

I’ll try that. Thanks.


#8

It doesn’t seem to work with NodeMcu and the hx711 codes that I used previously with Arduino Uno. I might try with Uno and a wifi shild instead.
I’m struggling with a beehive hobby project where data will be used primarily to track nectar collection and to let me know when the bees need additional boxes for honey storage. I think measured data (wight, temperature) also will correlate strongly with problems like diseases, hive swarming and allow me to take quick action to correct problems in the beehive.
I’ve built a scale with a 100kg load cell to be placed under the hive. The scale works well with github.com/bogde/HX711 code on an Arduino Uno.


#9

try with mega and not uno as the library is for mega.

and a quick google search gave this https://blog.squix.org/2016/07/esp8266-turn-a-9-body-scale-into-a-smart-scale-part-1.html
so do a bit of search and see if you can get your HX711 working with nodemcu. once that is done it is easy to use cayenne.


#10

I’ve searched a lot, but missed that page. Thanks for the response.


#11

Now it works with NodeMCU and your previously posted hx711 code and another hx711 sketch too. I don’t know what went wrong earlier but now I’m happy to be on the road again.
.
Thanks a lot for response!


#12

No, I don’t manage to implement the hx711 code in my Cayenne sketch. Have tried but it doesn’t work. Temperature and humidity works but not the scale. Any help would be appreciated.

 #define CAYENNE_DEBUG
#define CAYENNE_PRINT Serial
#include <CayenneMQTTESP8266.h>
#include <DHT.h>
#include <HX711.h>

// WiFi network info.
char ssid[] = "";
char wifiPassword[] = "";

// Cayenne authentication info. This should be obtained from the Cayenne Dashboard.
char username[] = "";
char password[] = "";
char clientID[] = "";

// Scale Settings
const int SCALE_DOUT_PIN = D2;
const int SCALE_SCK_PIN = D3;

HX711 scale(SCALE_DOUT_PIN, SCALE_SCK_PIN);

DHT dht(D4, DHT22);

unsigned long lastMillis = 0;

void setup() {
	Serial.begin(9600);
  scale.set_scale(-42820);// <- set here calibration factor!!!
  scale.tare();
 
	Cayenne.begin(username, password, clientID, ssid, wifiPassword);
  pinMode(4, OUTPUT);
}

void loop() {
	Cayenne.loop();
  float temp = dht.readTemperature(false);  //Fahrenheit
  float hum = dht.readHumidity();
  float weight = scale.get_units(1);
  Serial.println(String(weight, 2));
  
  Cayenne.virtualWrite(1, temp, TYPE_TEMPERATURE, UNIT_CELSIUS);
  Cayenne.virtualWrite(2, hum, TYPE_RELATIVE_HUMIDITY, UNIT_PERCENT);

}

CAYENNE_IN(0)
{
  digitalWrite(2, !getValue.asInt());
}


// Default function for sending sensor data at intervals to Cayenne.
// You can also use functions for specific channels, e.g CAYENNE_OUT(1) for sending channel 1 data.
CAYENNE_OUT_DEFAULT()
{
	// Write data to Cayenne here. This example just sends the current uptime in milliseconds on virtual channel 0.
	Cayenne.virtualWrite(0, millis());
	// Some examples of other functions you can use to send data.
	//Cayenne.celsiusWrite(1, 22.0);
	//Cayenne.luxWrite(2, 700);
	//Cayenne.virtualWrite(3, 50, TYPE_PROXIMITY, UNIT_CENTIMETER);
}

// Default function for processing actuator commands from the Cayenne Dashboard.
// You can also use functions for specific channels, e.g CAYENNE_IN(1) for channel 1 commands.
CAYENNE_IN_DEFAULT()
{
	CAYENNE_LOG("Channel %u, value %s", request.channel, getValue.asString());
	//Process message here. If there is an error set an error message using getValue.setError(), e.g getValue.setError("Error message");
}

#13
 #define CAYENNE_DEBUG
#define CAYENNE_PRINT Serial
#include <CayenneMQTTESP8266.h>
#include <DHT.h>
#include <HX711.h>
unsigned long lastMillis = 0;

// WiFi network info.
char ssid[] = "";
char wifiPassword[] = "";

// Cayenne authentication info. This should be obtained from the Cayenne Dashboard.
char username[] = "";
char password[] = "";
char clientID[] = "";

// Scale Settings
const int SCALE_DOUT_PIN = D2;
const int SCALE_SCK_PIN = D3;

HX711 scale(SCALE_DOUT_PIN, SCALE_SCK_PIN);

DHT dht(D4, DHT22);

unsigned long lastMillis = 0;

void setup() {
	Serial.begin(9600);
  scale.set_scale(-42820);// <- set here calibration factor!!!
  scale.tare();
 
	Cayenne.begin(username, password, clientID, ssid, wifiPassword);
  pinMode(4, OUTPUT);
}

void loop() {
	Cayenne.loop();
  float temp = dht.readTemperature(false);  //Fahrenheit
  float hum = dht.readHumidity();
  float weight = scale.get_units(1);
  Serial.println(String(weight, 2));
  	if (millis() - lastMillis > 10000) {
		lastMillis = millis();
  Cayenne.virtualWrite(3, weight);
  Cayenne.virtualWrite(1, temp, TYPE_TEMPERATURE, UNIT_CELSIUS);
  Cayenne.virtualWrite(2, hum, TYPE_RELATIVE_HUMIDITY, UNIT_PERCENT);
}
}

CAYENNE_IN(0)
{
  digitalWrite(2, !getValue.asInt());
}

#14

Thank you… Now I get a static value when the sketch is uploaded but it will not update when I change the load on the scale. If I change the load I hav to upload the sketch again to get a different value.
The scale also provides an incorrect value, but it may be possible to correct with the calibration factor.


#15

Made some small changes and now it seems to work :grinning:. Thanks for support.


#16

glad to hear it is working. once done with your project share it with the community.


#17

It’s a hard environment, even for bees. Normally they survive the winter even if it sometime is 30C below zero. If my bees is alive in spring I’ll test my equipment and write some about the project.
In addition to the scale, I intend to install two temperature sensors, one inside and one outside the beehive and a sensor that measures the humidity outside. Maybe also a sensor that detects light. We’ll see if NodeNUC can handle all this and if the wifi reaches 30 meters from the hive to my house.
I may ask some additional questions in the future on this forum and thanks alot for all help!


#18

wow nice project. all the best.

nodemcu cab surely handle it. there are lot of project in the community, you can go through them and get all the things you need.

for your case project, do a bit a study on lora technolgy https://mydevices.com/cayenne/docs/lora/


#19

Yes, I have looked a bit at lora. It may be relevant later for a similar project with a beehive that is further away. They will get power from solar power is my plan.


#20

Now I’m a bit confused. Everything worked properly until yesterday when the scale ceased to work. My temperature sensor still works. Have tried to reload the code again but it doesn’t help. In the mobile app, everything has disappeared. What happeneds??

#define CAYENNE_DEBUG
#define CAYENNE_PRINT Serial
#include <CayenneMQTTESP8266.h>
#include <DHT.h>
#include <HX711.h>
#include “ESP8266WiFi.h”
// WiFi network info.
char ssid[] = “”;
char wifiPassword[] = “”;

// Cayenne authentication info. This should be obtained from the Cayenne Dashboard.
char username[] = “”;
char password[] = “”;
char clientID[] = “”;

DHT dht(D4, DHT22);

// Scale Settings
const int SCALE_DOUT_PIN = D2;
const int SCALE_SCK_PIN = D3;

HX711 scale(SCALE_DOUT_PIN, SCALE_SCK_PIN);

void setup() {
Serial.begin(9600);
Cayenne.begin(username, password, clientID, ssid, wifiPassword);
pinMode(D4, OUTPUT);

scale.set_scale(-42800);// <- set here calibration factor!!!
scale.tare();
}

void loop() {
Cayenne.loop();

float temp = dht.readTemperature(false); //Fahrenheit
float hum = dht.readHumidity();
float weight = scale.get_units(1);

Serial.println(String(weight, 2));

scale.power_down(); // put the ADC in sleep mode
delay(5000);
scale.power_up();

Cayenne.virtualWrite(1, temp, TYPE_TEMPERATURE, UNIT_CELSIUS);
Cayenne.virtualWrite(2, hum, TYPE_RELATIVE_HUMIDITY, UNIT_PERCENT);
Cayenne.virtualWrite(3, weight);

}


#21

Sorry, the error with the scale was in my lab environment but the problem with the mobile app is still there.