Bonjour
Je suis en train de réaliser un projet à base de ESP32, capteur d'humidité DHT22, lecteur de carde SD et HX711.
Pour ma loadcell, j'utilise la lib HX711_ADC qui est celle recommandée par le vendeur et la seule qui fonctionne correctement pour mon cas.
Voici mon souci: lorsque je fais tourner un sketch exemple de la lib HX711-ADC, aucun problème. Une fois pour factor modifié, je tombe au gramme au poids effectif.
En revanche, avec le meme cablage mais tout le reste du projet en //, j'ai une valeur -20378.01 lue et elle ne bouge pas.
Dans la lib HX711_ADC il est indiqué que si le MCU est deja bien solicité, c'est mieux de le faire tourner avec un interrupt.
C'est ce que j'ai fait mais rien n'a changé.
Honnetement je commence à bloquer.
Les exemples de la libHX711_ADC fonctionnent parfaitement et sur mon projet plus rien alors que tous les indicateurs sont au vert;, aucun message d'erreur ni rien.
#include "DHT.h"
#include "FS.h"
#include "SD.h"
#include "SPI.h"
#include <ArduinoJson.h>
#include <WiFi.h>
#include "ESPAsyncWebServer.h"
#include <HX711_ADC.h>
#define DHTPIN 17 // Digital pin connected to the DHT sensor
#define DHTTYPE DHT22 // DHT 22 (AM2302), AM2321
const int HX711_dout = 16; //mcu > HX711 dout pin
const int HX711_sck = 4; //mcu > HX711 sck pin
//HX711 constructor:
HX711_ADC LoadCell(HX711_dout, HX711_sck);
volatile boolean newDataReady = false;
long pointold = 0;
long pointnew = 0;
float weight1 = 0;
float weight2 = 0;
//Webserver start
const char* ssid = "ESP32-Access-Point";
const char* password = "123456789";
AsyncWebServer server(80);
String header;
//DHT start
DHT dht(DHTPIN, DHTTYPE);
double round2(double value) {
return (int)(value * 100 + 0.5) / 100.0;
}
String readJSON() {
String finalText="";
File file = SD.open("/data/data.json", FILE_READ);
while (file.available()) {
finalText+=(char)file.read();
}
file.close();
Serial.print("JSON txt sent:");
//Serial.println(finalText);
return String(finalText);
}
//interrupt routine:
void dataReadyISR() {
if (LoadCell.update()) {
newDataReady = 1;
}
}
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
//HX711 setup & start
unsigned long stabilizingtime = 2000; // preciscion right after power-up can be improved by adding a few seconds of stabilizing time
boolean _tare = false;
float calibrationValue = -411.65;
LoadCell.start(stabilizingtime, _tare);
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");
}
//interrupt HX711
attachInterrupt(digitalPinToInterrupt(HX711_dout), dataReadyISR, FALLING);
Serial.println("Startup is complete");
//DHT22 begin
dht.begin();
//Webserver start
Serial.print("Setting AP (Access Point)…");
// Remove the password parameter, if you want the AP (Access Point) to be open
WiFi.softAP(ssid, password);
IPAddress IP = WiFi.softAPIP();
Serial.print("AP IP address: ");
Serial.println(IP);
//SD Start
delay(1000);
if(!SD.begin()){
Serial.println("Card Mount Failed");
return;
}
uint8_t cardType = SD.cardType();
if(cardType == CARD_NONE){
Serial.println("SDSC");
} else if(cardType == CARD_SDHC){
Serial.println("SDHC");
} else {
Serial.println("UNKNOWN");
}
File file = SD.open("/data/pointnew.txt", FILE_READ);
pointnew =f ile.parseInt();
Serial.print("last starting point:");
Serial.println(pointnew);
file.close();
//Webserver commands
server.on("/", HTTP_GET, [](AsyncWebServerRequest *request){
request->send(SD, "/index.html", "text/html");
});
server.serveStatic("/", SD, "/");
server.on("/JSON", HTTP_GET, [](AsyncWebServerRequest *request){
request->send_P(200, "text/plain", readJSON().c_str());
});
server.begin();
}
void loop() {
// put your main code here, to run repeatedly:
delay(2000);
//Read DHT22
float h = dht.readHumidity();
float t = dht.readTemperature();
if (isnan(h) || isnan(t)) {
Serial.println(F("Failed to read from DHT sensor!"));
return;
}
//If interrupt, read HX711
if (newDataReady) {
weight1 = LoadCell.getData();
Serial.println(weight1);
newDataReady = 0;
}
//Debug variables
Serial.print(F("pointnew:"));
Serial.print(pointnew);
Serial.print(F("Humidity: "));
Serial.print(h);
Serial.print(F("% Temperature: "));
Serial.print(t);
Serial.print(F("°C "));
Serial.print(F("weight1:"));
Serial.println(weight1);
pointnew = pointnew +1;
//JSON
DynamicJsonDocument doc(10000);
// Read the file
File file = SD.open("/data/data.json", FILE_READ);
deserializeJson(doc, file);
file.close();
// Append new element
JsonObject obj = doc.createNestedObject();
obj["point"] = pointnew;
obj["temp"] = round2(t);
obj["humidity"] = round2(h);
obj["weight1"] = round2(weight1);
obj["weight2"] = round2(weight2);
// Write the file
file = SD.open("/data/data.json", FILE_WRITE);
serializeJson(doc, file);
file.close();
//SD.open("/data/pointnew.txt", FILE_REMOVE);
File file2 = SD.open("/data/pointnew.txt", FILE_WRITE);
file2.print(pointnew);
//Serial.println(pointnew);
file2.close();
}
Merci pour votre aide