Hi
I'm trying to use a HX711 module on ESP8266 board following this example:
#include "HX711.h"
// HX711.DOUT - pin #D4
// HX711.PD_SCK - pin #D3
HX711 scale(D4, D3);
void setup() {
Serial.begin(115200);
Serial.println("HX711 Demo");
Serial.println("Before setting up the scale:");
Serial.print("read: \t\t");
Serial.println(scale.read());
Serial.print("read average: \t\t");
Serial.println(scale.read_average(20));
Serial.print("get value: \t\t");
Serial.println(scale.get_value(5));
Serial.print("get units: \t\t");
Serial.println(scale.get_units(5), 1);
scale.set_scale(2280.f);
scale.tare();
Serial.println("After setting up the scale:");
Serial.print("read: \t\t");
Serial.println(scale.read());
Serial.print("read average: \t\t");
Serial.println(scale.read_average(20));
Serial.print("get value: \t\t");
Serial.println(scale.get_value(5));
Serial.print("get units: \t\t");
Serial.println(scale.get_units(5), 1);
Serial.println("Readings:");
}
void loop() {
Serial.print("one reading:\t");
Serial.print(scale.get_units(), 1);
Serial.print("\t| average:\t");
Serial.println(scale.get_units(10), 1);
scale.power_down();
delay(5000);
scale.power_up();
}
but the serial monitor keeps getting this message:
HX711 Demo
Before setting up the scale:
read: -411
read average: -289
get value: -116.00
get units:
Soft WDT reset
ctx: cont
sp: 3ffef230 end: 3ffef490 offset: 01b0
>>>stack>>>
3ffef3e0: 3ffee438 00000000 3ffee438 00000000
3ffef3f0: 00000000 ffffffb2 3ffee358 40201eb1
3ffef400: 0000003c 0000000c 3ffe8490 402029c8
3ffef410: 3ffe8460 00000007 3ffee438 0000000a
3ffef420: 3ffee358 ffffffee 00000004 40202003
3ffef430: 3ffe8484 3ffee358 3ffee438 3ffe8454
3ffef440: 3ffe8460 3ffee358 3ffee358 402020b2
3ffef450: 3ffe8460 3ffee358 3ffee438 40201d24
3ffef460: feefeffe feefeffe feefeffe 3ffee45c
3ffef470: 3fffdad0 00000000 3ffee454 402027ec
3ffef480: feefeffe feefeffe 3ffee470 40100114
<<<stack<<<
ets Jan 8 2013,rst cause:2, boot mode:(1,6)
ets Jan 8 2013,rst cause:4, boot mode:(1,6)
wdt reset
For some reason both software and hardware watchdogs reset the board but I'm not able to understand why. Can you please help me to solve this? Thanks!
Sorry, I mean WeMos D1 board model, it's powered by the micro-USB cable.
SOLUTION:
The hx711 library create problems with esp8266 and yield method:
this is the hx711.cpp code for esp8266
#include <Arduino.h>
#include <HX711.h>
#ifndef ARDUINO_ARCH_ESP8266
// "yield" is not implemented as noop in older Arduino Core releases, so let's define it.
// See also: https://stackoverflow.com/questions/34497758/what-is-the-secret-of-the-arduino-yieldfunction/34498165#34498165
void yield(void) {};
#endif
#ifdef ARDUINO_ARCH_ESP8266
#define USE_WDT
#endif
HX711::HX711(byte dout, byte pd_sck, byte gain) {
begin(dout, pd_sck, gain);
}
HX711::HX711() {
}
HX711::~HX711() {
}
void HX711::begin(byte dout, byte pd_sck, byte gain) {
PD_SCK = pd_sck;
DOUT = dout;
pinMode(PD_SCK, OUTPUT);
pinMode(DOUT, INPUT);
set_gain(gain);
}
bool HX711::is_ready() {
return digitalRead(DOUT) == LOW;
}
void HX711::set_gain(byte gain) {
switch (gain) {
case 128: // channel A, gain factor 128
GAIN = 1;
break;
case 64: // channel A, gain factor 64
GAIN = 3;
break;
case 32: // channel B, gain factor 32
GAIN = 2;
break;
}
digitalWrite(PD_SCK, LOW);
read();
}
long HX711::read() {
// wait for the chip to become ready
while (!is_ready()) {
// Will do nothing on Arduino but prevent resets of ESP8266 (Watchdog Issue)
#ifdef USE_WDT
ESP.wdtFeed();
#else
yield();
#endif
}
unsigned long value = 0;
uint8_t data[3] = { 0 };
uint8_t filler = 0x00;
// pulse the clock pin 24 times to read the data
data[2] = shiftIn(DOUT, PD_SCK, MSBFIRST);
data[1] = shiftIn(DOUT, PD_SCK, MSBFIRST);
data[0] = shiftIn(DOUT, PD_SCK, MSBFIRST);
// set the channel and the gain factor for the next reading using the clock pin
for (unsigned int i = 0; i < GAIN; i++) {
digitalWrite(PD_SCK, HIGH);
digitalWrite(PD_SCK, LOW);
}
// Replicate the most significant bit to pad out a 32-bit signed integer
if (data[2] & 0x80) {
filler = 0xFF;
} else {
filler = 0x00;
}
// Construct a 32-bit signed integer
value = ( static_cast<unsigned long>(filler) << 24
| static_cast<unsigned long>(data[2]) << 16
| static_cast<unsigned long>(data[1]) << 8
| static_cast<unsigned long>(data[0]) );
return static_cast<long>(value);
}
long HX711::read_average(byte times) {
long sum = 0;
for (byte i = 0; i < times; i++) {
sum += read();
#ifdef USE_WDT
ESP.wdtFeed();
#else
yield();
#endif
}
return sum / times;
}
double HX711::get_value(byte times) {
return read_average(times) - OFFSET;
}
float HX711::get_units(byte times) {
return get_value(times) / SCALE;
}
void HX711::tare(byte times) {
double sum = read_average(times);
set_offset(sum);
}
void HX711::set_scale(float scale) {
SCALE = scale;
}
float HX711::get_scale() {
return SCALE;
}
void HX711::set_offset(long offset) {
OFFSET = offset;
}
long HX711::get_offset() {
return OFFSET;
}
void HX711::power_down() {
digitalWrite(PD_SCK, LOW);
digitalWrite(PD_SCK, HIGH);
}
void HX711::power_up() {
digitalWrite(PD_SCK, LOW);
}
Wawa
April 16, 2018, 9:21am
5
Only HX711 boards with split analogue/digital supply can be used with 3.3volt processors.
The analogue part of the HX711 board must be powered with 5volt, for a stable 4.2volt for the load cell.
Leo..