Hi There,
My name is Thomas. I am french. Sorry for my english.
I worked with studients to create a prototype with ESP32 Lolin32 V1.0.0.
It is a scale with loader cells and HX711.
The C program worked well in Arduino IDE at school with the studients. But When I tried to upload the C Code with my computer at home nothing happens in the Monitor Serial. To be accurate one thing happens : white square appears sometimes when I pressed on the load cell.
The studients created a interface with DELs on breadboard. It works well. So the board is correct and all the connection works well also.
Another code can print in Monitor serial datas so I think the Monitor serial is OK.
I used Linux Ubuntu Budgie and windows 11 with Arduino IDE 2.3. I can see the square in both situation.
So everything is ok in the OS.
I changed the HX711 librairie. Nothing changed.
So do you have an idea of this issue?
/*
Description du programe
Created for arduino-esp32 en Janvier, 2024
par
*/
//Declaration des bibliotheques utilisees dans ce programme
#include <WiFi.h>
#include <WiFiClient.h>
#include <WiFiAP.h>
#include "HX711.h"
// Declaration du nom du reseau wifi ainsi que le mot de passe du reseau.
const char *ssid = "yourAP";
const char *password = "yourPassword";
// Définir les broches pour les amplificateurs HX711 et les LEDs
const int ampli1_dout = 4;
const int ampli1_sck = 0;
const int ampli2_dout = 5;
const int ampli2_sck = 18;
// declaration du bouton reset
const int reset = 39;
int etatreset =0;
const int ledreset = 33;
const int ledRG = 32; // LED rouge gauche
const int ledJ2G = 13; // LED jaune 2 gauche
const int ledJ1G = 1; // LED Jaune 1 gauche
const int ledV = 26; // LED verte
const int ledJ1D = 27; // LED jaune 1 droite
const int ledJ2D = 14; // LED jaune 2 droite
const int ledRD = 12; // LED rouge droite
int DeltaMesure = 500;
HX711 ampli1;
HX711 ampli2;
WiFiServer server(80);
void setup() {
Serial.begin(9600);
// Configuration du mode des pins des Leds
pinMode(ledRG, OUTPUT);
pinMode(ledJ2G, OUTPUT);
pinMode(ledJ1G, OUTPUT);
pinMode(ledV, OUTPUT);
pinMode(ledJ1D, OUTPUT);
pinMode(ledJ2D, OUTPUT);
pinMode(ledRD, OUTPUT);
pinMode(ledreset, OUTPUT);
pinMode(reset, INPUT);
// Configurer les amplificateurs HX711
ampli1.begin(ampli1_dout, ampli1_sck);
ampli2.begin(ampli2_dout, ampli2_sck);
// Tare les amplificateurs HX711
ampli1.tare();
ampli2.tare();
digitalWrite(ledJ1G, LOW);
Serial.println();
Serial.println("Configuring access point...");
// You can remove the password parameter if you want the AP to be open.
// a valid password must have more than 7 characters
if (!WiFi.softAP(ssid, password)) {
log_e("Soft AP creation failed.");
while(1);
}
IPAddress myIP = WiFi.softAPIP();
Serial.print("AP IP address: ");
Serial.println(myIP);
server.begin();
Serial.println("Server started");
}
// Fonction pour allumer une LED spécifique et éteindre les autres
void allumerLED(int pin) {
digitalWrite(ledRG, LOW);
digitalWrite(ledJ2G, LOW);
digitalWrite(ledJ1G, LOW);
digitalWrite(ledV, LOW);
digitalWrite(ledJ1D, LOW);
digitalWrite(ledJ2D, LOW);
digitalWrite(ledRD, LOW);
digitalWrite(ledreset, LOW);
digitalWrite(pin, HIGH);
}
void loop() {
// Lire les valeurs des cellules de charge via les amplificateurs HX711
long valeur1 = ampli1.get_units(1);
long valeur2 = ampli2.get_units(1);
// Lire la valeur du bouton
etatreset = digitalRead(reset);
// Calculer la différence entre les valeurs
long difference = valeur1 - valeur2;
int facteurdecharge = 40; // déterminer apres des mesures avec des poids que l'on connait
int poids = abs(difference)/facteurdecharge; // conversion de la différence en gramme
float poidsTot = (abs(valeur1) + abs(valeur2))/40; //poids total de la personne en gramme
float ratio1 = (abs(valeur1)/40)/poidsTot; // calcul du ratio mesuré par le premier ampli
float ratio2 = (abs(valeur2)/40)/poidsTot; // calcul du ratio mesuré par le deuxieme ampli
//si le bouton reset est appuyé alors on tare les ampli
if (etatreset == LOW){
Serial.println("reset");
ampli1.tare();
ampli2.tare();
allumerLED(ledreset);
}
// sinon on mesure les valeurs
else{
if (poidsTot<= 2000) {
allumerLED(ledV);
ratio1 = 0.5;
ratio2 = 0.5;
}
else{
// allumer les LEDs en fonction du ratio
if (ratio1 < 0.15) {
allumerLED(ledRG);
} else if ((ratio1 >= 0.15) && (ratio1 < 0.30)) {
allumerLED(ledJ2G);
} else if ((ratio1 >= 0.30) && (ratio1 < 0.41)){
allumerLED(ledJ1G);
} else if ((ratio1 >= 0.41) && (ratio1 < 0.59)){
allumerLED(ledV);
}else if ((ratio1 >= 0.59) && (ratio1 < 0.70)){
allumerLED(ledJ1D);
} else if ((ratio1 >= 0.70) && (ratio1 < 0.85)){
allumerLED(ledJ2D);
} else if (ratio1 >= 0.85) {
allumerLED(ledRD);
}
}
delay(DeltaMesure); // Attendre DeltaMesure miliseconde entre chaque itération
}
WiFiClient client = server.available(); // listen for incoming clients
//definition de la page HTML
client.println("HTTP/1.1 200 OK");
client.println("Content-type:text/html");
client.println();
client.println("<html><head>");
client.println("<title>Interface </title>");
//client.println("<meta http-equiv=\"refresh\" content=\"1\">");
//definition des elements visuels de la page
client.println("<style>body {font-family: Arial, sans-serif;margin: 0;padding: 0;}h1{text-align: center;margin-top: 50px;font-size: 50px;}.container {max-width: 600px;margin: 0 auto;padding: 20px;background-color: rgba(255, 255, 255, 0.8);border-radius: 10px;box-shadow: 0 0 10px rgba(0, 0, 0, 0.3);margin-top: 50px;}.demi-cercle-container {position: relative;width: 200px;height: 100px;overflow: visbile;margin: 0 auto;}.demi-cercle {position: absolute;width: 100%;height: 100%;background: conic-gradient( at 50% 0%,#FF0000 25% 32.5%,#ffa200 32.5% 40%,#FFFF00 40% 44.5%,#98fb98 44.5% 55.5%,#FFFF00 55.5% 60%,#ffa200 60% 67.5%,#FF0000 67.5% 75%) bottom;border-radius: 0 0 100px 100px;border: 0px solid #000;transform: rotate(180deg);}.aiguille {position: absolute;width: 2px;height: 95px;bottom: 0;left: 50%;background-color: #000;transform-origin: 0% 100%;transform: translateX(-50%) rotate(0deg);transition: transform 0.5s ease-in-out;}.button-container {text-align: center;margin-top: 20px;}button {padding: 10px 20px;font-size: 16px;cursor: pointer;}.background-content {background-color: #add8e6;background-repeat: repeat-x;background-position: top center;padding-top: 80px;text-align: center;}</style>");
client.println("</head><body>");
client.println("<div class=\"background-content\"><h1>test</h1><div class=\"container\"><p>Ratio1 : <span id=\"ratio1Valeur\"></span></p><div class=\"demi-cercle-container\"><div class=\"demi-cercle\"></div><div class=\"aiguille\"></div></div><div class=\"button-container\"><button onclick=\"resetArduinoValues()\">Reinitialiser les valeurs</button></div></div></div>");
if (client && client.connected()) { // if you get a client,
Serial.println("New Client."); // print a message out the serial port
String currentLine = ""; // make a String to hold incoming data from the client
while (client.connected()) { // loop while the client's connected
if (client.available()) { // if there's bytes to read from the client,
char c = client.read(); // read a byte, then
Serial.write(c); // print it out the serial monitor
// HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK)
// String et boucle if créent le bouton de reset.
String req = client.readStringUntil('\r'); // loop while the client's connected
if (req.indexOf("/toggle") != -1) {
// Exécuter les actions associées
Serial.println("reset");
ampli1.tare();
ampli2.tare();
allumerLED(ledreset);
// Répondre au client avec une confirmation
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println();
delay(500);
}
// Lire les valeurs des cellules de charge via les amplificateurs HX711
long valeur1 = ampli1.get_units(1);
long valeur2 = ampli2.get_units(1);
// Lire la valeur du bouton
etatreset = digitalRead(reset);
// Calculer la différence entre les valeurs
long difference = valeur1 - valeur2;
int facteurdecharge = 40; // déterminer apres des mesures avec des poids que l'on connait
int poids = abs(difference)/facteurdecharge; // conversion de la différence en gramme
float poidsTot = (abs(valeur1) + abs(valeur2))/40; //poids total de la personne en gramme
float ratio1 = (abs(valeur1)/40)/poidsTot; // calcul du ratio mesuré par le premier ampli
float ratio2 = (abs(valeur2)/40)/poidsTot; // calcul du ratio mesuré par le deuxieme ampli
//si le bouton reset est appuyé alors on tare les ampli
if (etatreset == LOW){
Serial.println("reset");
ampli1.tare();
ampli2.tare();
allumerLED(ledreset);
}
// sinon on mesure les valeurs
else{
if (poidsTot<= 2000) {
allumerLED(ledV);
ratio1 = 0.5;
ratio2 = 0.5;
}
else{
// allumer les LEDs en fonction du ratio
if (ratio1 < 0.15) {
allumerLED(ledRG);
} else if ((ratio1 >= 0.15) && (ratio1 < 0.30)) {
allumerLED(ledJ2G);
} else if ((ratio1 >= 0.30) && (ratio1 < 0.41)){
allumerLED(ledJ1G);
} else if ((ratio1 >= 0.41) && (ratio1 < 0.59)){
allumerLED(ledV);
}else if ((ratio1 >= 0.59) && (ratio1 < 0.70)){
allumerLED(ledJ1D);
} else if ((ratio1 >= 0.70) && (ratio1 < 0.85)){
allumerLED(ledJ2D);
} else if (ratio1 >= 0.85) {
allumerLED(ledRD);
}
}
delay(DeltaMesure); // Attendre DeltaMesure miliseconde entre chaque itération
}
Serial.print(" | ratio1 ");
Serial.print(ratio1);
Serial.print(" | ratio2 ");
Serial.println(ratio2);
// The HTTP response ends with another blank line:
// Ajoutez le code HTML, CSS et JavaScript pour le cadran en demi-rond ici
// Ajoutez le code JavaScript pour mettre à jour l'aiguille ici
client.println();
client.println("<script>function toggle() {var xhr = new XMLHttpRequest();xhr.open('GET', '/toggle', true);xhr.send();}function updateAiguille(value) {const aiguille = document.getElementsByClassName('aiguille')[0];const angle = (value - 0.5) * 180;aiguille.style.transform = `translateX(-50%) rotate(${angle}deg)`;}function updateRatio1(valeurtest1) {const ratio1Valeur = document.getElementById('ratio1Valeur');ratio1Valeur.textContent = valeurtest1.toFixed(2);}setInterval(() => {const valeurCapteur = "+ String(ratio1) +";updateAiguille(valeurCapteur);const valeurtest = "+ String(ratio1) +";updateRatio1(valeurtest);}, 100);</script>");
client.println("</body></html>");
}
}
}
}