Greetings,
I wanted measure some weight with a loadcell and ESP8266. After writing some code and testing it. Code worked but load cell didn't return an value. So I decided to delete light sleep function from the void loop. After that it worked awesome. However I can't use light sleep which means ESP8266 will consume way more power than before. My question is:
How can use load cell with light sleep?
This is my code
extern "C" {
#include "gpio.h"
}
// Required for LIGHT_SLEEP_T delay mode
extern "C" {
#include "user_interface.h"
}
#include <EEPROM.h>
#include <HX711_ADC.h>
#define HX711_DOUT D5 //mcu > HX711 dout pin
#define HX711_SCK D6 //mcu > HX711 sck pin
#define INTERRUPT_PIN D4
HX711_ADC LoadCell(HX711_DOUT, HX711_SCK);
//Function prototypes
void startLoadcell(void);
void startperipheral(void);
float measureWeight(void);
void light_sleep();
ICACHE_RAM_ATTR void tareSet(void);
void setup() {
Serial.begin(115200);
startperipheral();
startLoadcell();
}
void loop() {
Serial.println(measureWeight());
Serial.println("Going to Sleep");
light_sleep();//Enter light sleep.
Serial.println("Wake up");
}
void startperipheral(void) {
pinMode(INTERRUPT_PIN, INPUT_PULLUP); //Tare button
attachInterrupt(digitalPinToInterrupt(INTERRUPT_PIN), tareSet, FALLING); //Attach tare button as interrupt
}
void startLoadcell(void) {
LoadCell.begin();//Start loadcell
float calibrationValue;
int calVal_eepromAdress;
EEPROM.begin(512);
EEPROM.get(calVal_eepromAdress, calibrationValue);//Get loadcell calibration dat from EEPROM.
Serial.println(calibrationValue);
unsigned long stabilizingtime = 2000;
boolean _tare = true;
if (LoadCell.getTareTimeoutFlag()) {
Serial.println("Timeout, check MCU>HX711 wiring and pin designations");
while (1);
}
else {
LoadCell.setCalFactor(calibrationValue); // set calibration value (float)
Serial.println("Startup is complete");
}
}
float measureWeight(void) {
static boolean newDataReady = 0;
const int serialPrintInterval = 0;
unsigned long t = 0;
if (LoadCell.update()) newDataReady = true;
if (newDataReady) {
if (millis() > t + serialPrintInterval) {
newDataReady = 0;
t = millis();
return LoadCell.getData(); //Read loadcell
}
}
}
ICACHE_RAM_ATTR void tareSet(void) { // Interrupt for setting the tare
LoadCell.tareNoDelay(); //Set tare
}
void light_sleep() { // Enter light sleep
wifi_station_disconnect();
wifi_set_opmode_current(NULL_MODE);
wifi_fpm_set_sleep_type(LIGHT_SLEEP_T); // set sleep type, the above posters wifi_set_sleep_type() didnt seem to work for me although it did let me compile and upload with no errors
wifi_fpm_open(); // Enables force sleep
gpio_pin_wakeup_enable(GPIO_ID_PIN(2), GPIO_PIN_INTR_LOLEVEL); // GPIO_ID_PIN(2)(also known as D4) corresponds to GPIO2 on ESP8266-01 , GPIO_PIN_INTR_LOLEVEL for a logic low, can also do other interrupts, see gpio.h above
wifi_fpm_do_sleep(36e8); // Sleep for 1 hour possible time
}
Thank you.
