Probléme de d'alimentation Arduino MKR 1310

Bonjour à tous

Dans le cadre d'un système de ruches connectées, j'utilise un MKR 1310 qui transmet les poids et les températures de 4 ruches en LoRa vers un esp32 . Le code est en annexe. Le MKR est alimenté en USB via un chargeur téléphone. lors d'un déplacement, avec changement du chargeur, j'ai remarqué que les données envoyées n'étaient plus correctes avec de grosses variations sauf pour le poids d'une ruche. Une ruche reste stable , alors que les 3 autres déconnent complétement. Les données sont pourtant envoyées toutes en même temps dans une seule trame comme ceci : ID=FLam;TX=1-4;SEQ=120;TE=18.4;P1=45.2;T1=22.1;P2=46.0;T2=22.3; . A notrer que si je place un chargeur bon marché , c'est encore pire , et le seul chargeur Samsung d'origine fonctionne bien. J'ai également fait un essai en branchant une batterie LiPo sur le port prévu à cet effet, aucune amélioration. Un dernier essai sans résultat à été fait en raccordant une alim de 2 amps directement sur le Vin et Gnd . le tableau ci dessous monter que le poids de la ruche 4 est resté stable. Auriez-vous une idée pour améliorer cette situation?
D'avance un grand merci

Column 1 Column 2 Column 3 Column 4 E F G H I J K
Date/Heure Alarme Temp Ext Poids 1 Temp 1 Poids 2 Temp 2 Poids 3 Temp 3 Poids 4 Temp 4
05/06/2026 20:00:44 16,8 30,5 16,4 30,4 16,2 1,5 16,6 13,3 16,5
05/06/2026 19:55:30 16,9 30,5 16,4 30,4 16,2 1,5 16,6 13,2 16,5
05/06/2026 19:50:11 16,9 30,5 16,4 30,4 16,2 1,5 16,6 13,3 16,5
05/06/2026 19:44:55 16,9 30,5 16,4 30,4 16,2 1,5 16,6 13,3 16,4
05/06/2026 19:39:39 16,9 30,5 16,4 30,4 16,2 1,4 16,6 13,3 16,4
05/06/2026 19:34:23 17 30,5 16,3 30,4 16,2 1,2 16,6 13,3 16,3
05/06/2026 18:57:50 :warning: Perte poids ruche 3 16,9 -4,8 16,5 -484,4 16,2 -412 16,6 13,3 16,6
05/06/2026 18:52:34 :warning: Perte poids ruche 1 :warning: Perte poids ruche 2 16,9 -42,2 16,5 -488,7 16,2 -237,2 16,6 13,3 16,6
05/06/2026 18:47:17 :warning: Perte poids ruche 2 :warning: Perte poids ruche 3 16,9 -21,5 16,5 -319,7 16,2 -361,9 16,6 13,3 16,6
05/06/2026 18:41:56 :warning: Perte poids ruche 1 16,9 -21,5 16,5 -276,5 16,2 -325,7 16,6 13,3 16,6
05/06/2026 18:36:41 :warning: Perte poids ruche 2 16,9 1,2 16,5 -444 16,2 -328,4 16,6 13,3 16,6
05/06/2026 18:31:22 :warning: Perte poids ruche 1 :warning: Perte poids ruche 2 :warning: Perte poids ruche 3 16,9 -10,6 16,5 -318,2 16,2 -360,7 16,6 13,3 16,6
05/06/2026 18:26:10 :warning: Perte poids ruche 2 :warning: Perte poids ruche 3 16,9 -0,1 16,5 -234,4 16,2 -346,2 16,6 13,3 16,6
#include <SPI.h>
#include <LoRa.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#include <HX711.h>
#include <math.h>

// =====================================================
// IDENTIFICATION GENERALE
// =====================================================
const char* ID = "FLam";

// Transmetteur 1 : ruches 1 à 4
const int FIRST_HIVE_NUMBER = 1;
const char* TX_ID = "1-4";

// Transmetteur 2 : ruches 5 à 8
// const int FIRST_HIVE_NUMBER = 5;
// const char* TX_ID = "5-8";

// Transmetteur 3 : ruches 9 à 12
// const int FIRST_HIVE_NUMBER = 9;
// const char* TX_ID = "9-12";

// Transmetteur 4 : ruches 13 à 16
// const int FIRST_HIVE_NUMBER = 13;
// const char* TX_ID = "13-16";

const int LOCAL_HIVE_COUNT = 4;

// =====================================================
// HX711 pins
// =====================================================
const int HX711_DOUT[LOCAL_HIVE_COUNT] = {2, 3, 4, 5}; // fil orange
const int HX711_SCK[LOCAL_HIVE_COUNT]  = {6, 8, 9, 10}; // fil vert

// =====================================================
// DS18B20 - un seul bus OneWire
// =====================================================
#define ONE_WIRE_BUS 7

OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);

// =====================================================
// HX711 instances
// =====================================================
HX711 scales[LOCAL_HIVE_COUNT];

// =====================================================
// Adresses DS18B20 ruches
// IMPORTANT : remplacer par vos vraies adresses
// =====================================================
DeviceAddress tempAddress[LOCAL_HIVE_COUNT] = {
  {0x28, 0x1D, 0x87, 0x12, 0x00, 0x00, 0x00, 0xAA}, // Ruche locale 1
  {0x28, 0x2E, 0xB8, 0x14, 0x00, 0x00, 0x00, 0xFC}, // Ruche locale 2
  {0x28, 0x6B, 0x08, 0x15, 0x00, 0x00, 0x00, 0xA2}, // Ruche locale 3
  {0x28, 0x58, 0x55, 0x15, 0x00, 0x00, 0x00, 0x6F}  // Ruche locale 4
};

// =====================================================
// Adresse DS18B20 température extérieure
// IMPORTANT : remplacer par la vraie adresse
// =====================================================
DeviceAddress tempExtAddress =
{0x28, 0x9B, 0x22, 0x52, 0x00, 0x00, 0x00, 0xF3}; // sonde temp ext

// =====================================================
// Calibration et corrections poids
// =====================================================
float calibration[LOCAL_HIVE_COUNT] = {
  -10000.0,
  -10000.0,
  -10000.0,
  -10000.0
};

float correction[LOCAL_HIVE_COUNT] = {
  1.2,
  2.0,
  -8.5,
  -2.0
};

// =====================================================
// Moyennes
// =====================================================
const int numReadings = 20;

float weightReadings[LOCAL_HIVE_COUNT][numReadings];
float weightTotals[LOCAL_HIVE_COUNT] = {0, 0, 0, 0};
float averageWeight[LOCAL_HIVE_COUNT] = {0, 0, 0, 0};

float tempReadings[LOCAL_HIVE_COUNT][numReadings];
float averageTemp[LOCAL_HIVE_COUNT] = {NAN, NAN, NAN, NAN};

float tempExtReadings[numReadings];
float averageTempExt = NAN;

int readIndex = 0;

// =====================================================
// Détection
// =====================================================
bool hxPresent[LOCAL_HIVE_COUNT] = {false, false, false, false};
bool dsPresent[LOCAL_HIVE_COUNT] = {false, false, false, false};
bool tempExtPresent = false;

int dsCount = 0;

unsigned long lastSend = 0;
unsigned int sequenceNumber = 0;

// =====================================================
// OUTILS
// =====================================================
int getHiveNumber(int localIndex) {
  return FIRST_HIVE_NUMBER + localIndex;
}

void clignoterLedOrange() {
  for (int i = 0; i < 2; i++) {
    digitalWrite(LED_BUILTIN, HIGH);
    delay(100);
    digitalWrite(LED_BUILTIN, LOW);
    delay(100);
  }
}

bool detectHX(HX711 &scale) {
  for (int i = 0; i < 5; i++) {
    if (scale.is_ready()) return true;
    delay(50);
  }
  return false;
}

bool readHX(HX711 &scale, float &value, float corr) {
  if (!scale.is_ready()) return false;

  float v = scale.get_units();

  if (isnan(v) || isinf(v)) {
    return false;
  }

  value = v + corr;
  return true;
}

void updateHXPresence() {
  for (int i = 0; i < LOCAL_HIVE_COUNT; i++) {
    hxPresent[i] = detectHX(scales[i]);
  }
}

void updateDSPresence() {
  dsCount = 0;

  for (int i = 0; i < LOCAL_HIVE_COUNT; i++) {
    dsPresent[i] = sensors.isConnected(tempAddress[i]);

    if (dsPresent[i]) {
      dsCount++;
    }
  }

  tempExtPresent = sensors.isConnected(tempExtAddress);

  if (tempExtPresent) {
    dsCount++;
  }
}

float readTempByAddress(DeviceAddress addr) {
  float t = sensors.getTempC(addr);

  if (t == DEVICE_DISCONNECTED_C || t == 85.0 || t < -55.0 || t > 125.0) {
    return NAN;
  }

  return t;
}

float averageValidValues(float values[], int count) {
  float total = 0;
  int validCount = 0;

  for (int i = 0; i < count; i++) {
    if (!isnan(values[i]) && !isinf(values[i])) {
      total += values[i];
      validCount++;
    }
  }

  if (validCount == 0) {
    return NAN;
  }

  return total / validCount;
}

int countHXPresent() {
  int count = 0;

  for (int i = 0; i < LOCAL_HIVE_COUNT; i++) {
    if (hxPresent[i]) {
      count++;
    }
  }

  return count;
}

void printFloatOrNA(String label, float value) {
  Serial.print(label);

  if (isnan(value)) {
    Serial.println("NA");
  } else {
    Serial.println(value, 1);
  }
}

void appendValueToFrame(String &frame, String key, float value) {
  frame += key;
  frame += "=";

  if (isnan(value)) {
    frame += "NA";
  } else {
    frame += String(value, 1);
  }

  frame += ";";
}

// =====================================================
// CONSTRUCTION TRAME LORA
// =====================================================
String buildLoRaFrame(int hxCount) {
  String frame = "";

  frame += "ID=";
  frame += ID;
  frame += ";";

  frame += "TX=";
  frame += TX_ID;
  frame += ";";

  frame += "FIRST=";
  frame += String(FIRST_HIVE_NUMBER);
  frame += ";";

  frame += "SEQ=";
  frame += String(sequenceNumber);
  frame += ";";

  frame += "HX=";
  frame += String(hxCount);
  frame += ";";

  frame += "DS=";
  frame += String(dsCount);
  frame += ";";

  appendValueToFrame(frame, "TE", averageTempExt);

  for (int i = 0; i < LOCAL_HIVE_COUNT; i++) {
    int hiveNumber = getHiveNumber(i);

    if (hxPresent[i]) {
      appendValueToFrame(frame, "P" + String(hiveNumber), averageWeight[i]);
    } else {
      appendValueToFrame(frame, "P" + String(hiveNumber), NAN);
    }
  }

  for (int i = 0; i < LOCAL_HIVE_COUNT; i++) {
    int hiveNumber = getHiveNumber(i);

    if (dsPresent[i]) {
      appendValueToFrame(frame, "T" + String(hiveNumber), averageTemp[i]);
    } else {
      appendValueToFrame(frame, "T" + String(hiveNumber), NAN);
    }
  }

  return frame;
}

// =====================================================
// SETUP
// =====================================================
void setup() {
  Serial.begin(9600);
  pinMode(LED_BUILTIN, OUTPUT);

  Serial.println("MKR1310 - FLam");
  Serial.println("4 HX711 + 5 DS18B20 sur un seul bus OneWire");

  Serial.print("ID rucher : ");
  Serial.println(ID);

  Serial.print("Transmetteur : ");
  Serial.println(TX_ID);

  Serial.print("Ruches : R");
  Serial.print(FIRST_HIVE_NUMBER);
  Serial.print(" à R");
  Serial.println(FIRST_HIVE_NUMBER + LOCAL_HIVE_COUNT - 1);

  // Initialisation HX711
  for (int i = 0; i < LOCAL_HIVE_COUNT; i++) {
    scales[i].begin(HX711_DOUT[i], HX711_SCK[i]);
    scales[i].set_scale(calibration[i]);

    averageWeight[i] = 0;
    weightTotals[i] = 0;
    averageTemp[i] = NAN;

    for (int j = 0; j < numReadings; j++) {
      weightReadings[i][j] = 0;
      tempReadings[i][j] = NAN;
    }
  }

  for (int j = 0; j < numReadings; j++) {
    tempExtReadings[j] = NAN;
  }

  delay(500);

  updateHXPresence();

  Serial.print("HX711 detectes: ");
  Serial.println(countHXPresent());

  // Initialisation DS18B20
  sensors.begin();
  sensors.setWaitForConversion(true);

  delay(300);

  updateDSPresence();

  Serial.print("DS18B20 detectees: ");
  Serial.println(dsCount);

  for (int i = 0; i < LOCAL_HIVE_COUNT; i++) {
    Serial.print("R");
    Serial.print(getHiveNumber(i));
    Serial.print(" DS18B20: ");
    Serial.println(dsPresent[i] ? "OK" : "NA");
  }

  Serial.print("TE DS18B20: ");
  Serial.println(tempExtPresent ? "OK" : "NA");

  // Initialisation LoRa
  if (!LoRa.begin(868E6)) {
    Serial.println("LoRa failed!");

    while (1) {
      digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN));
      delay(200);
    }
  }

 // LoRa.setTxPower(8);
  Serial.println("LoRa OK");
}

// =====================================================
// LOOP
// =====================================================
void loop() {
  unsigned long now = millis();

  if (now - lastSend >= 5000) {

    updateHXPresence();
    updateDSPresence();

    // -------------------------------------------------
    // Moyenne poids
    // -------------------------------------------------
    for (int i = 0; i < LOCAL_HIVE_COUNT; i++) {
      weightTotals[i] -= weightReadings[i][readIndex];

      float newWeight = averageWeight[i];
      bool ok = false;

      if (hxPresent[i]) {
        ok = readHX(scales[i], newWeight, correction[i]);
      }

      weightReadings[i][readIndex] = ok ? newWeight : averageWeight[i];

      weightTotals[i] += weightReadings[i][readIndex];
      averageWeight[i] = weightTotals[i] / numReadings;
    }

    // -------------------------------------------------
    // Moyenne températures
    // -------------------------------------------------
    sensors.requestTemperatures();

    for (int i = 0; i < LOCAL_HIVE_COUNT; i++) {
      float newTemp = NAN;

      if (dsPresent[i]) {
        newTemp = readTempByAddress(tempAddress[i]);
      }

      if (!isnan(newTemp)) {
        tempReadings[i][readIndex] = newTemp;
      } else {
        tempReadings[i][readIndex] = averageTemp[i];
      }

      averageTemp[i] = averageValidValues(tempReadings[i], numReadings);
    }

    float newTempExt = NAN;

    if (tempExtPresent) {
      newTempExt = readTempByAddress(tempExtAddress);
    }

    if (!isnan(newTempExt)) {
      tempExtReadings[readIndex] = newTempExt;
    } else {
      tempExtReadings[readIndex] = averageTempExt;
    }

    averageTempExt = averageValidValues(tempExtReadings, numReadings);

    readIndex = (readIndex + 1) % numReadings;

    int hxCount = countHXPresent();

    // -------------------------------------------------
    // Affichage série MODIFIE : valeurs groupées
    // -------------------------------------------------
    Serial.println();

    Serial.print("ID: ");
    Serial.println(ID);

    Serial.print("TX: ");
    Serial.println(TX_ID);

    Serial.print("SEQ: ");
    Serial.println(sequenceNumber);

    Serial.print("HX711 detectes: ");
    Serial.println(hxCount);

    Serial.print("DS18B20 detectees: ");
    Serial.println(dsCount);

    Serial.println();

    Serial.println("POIDS:");
    for (int i = 0; i < LOCAL_HIVE_COUNT; i++) {
      int hiveNumber = getHiveNumber(i);

      Serial.print("P");
      Serial.print(hiveNumber);
      Serial.print("=");

      if (isnan(averageWeight[i])) {
        Serial.print("NA");
      } else {
        Serial.print(averageWeight[i], 1);
        Serial.print("kg");
      }

      Serial.print("   ");
    }

    Serial.println();
    Serial.println();

    Serial.println("TEMPERATURES:");
    Serial.print("TE=");

    if (isnan(averageTempExt)) {
      Serial.print("NA");
    } else {
      Serial.print(averageTempExt, 1);
      Serial.print("C");
    }

    Serial.print("   ");

    for (int i = 0; i < LOCAL_HIVE_COUNT; i++) {
      int hiveNumber = getHiveNumber(i);

      Serial.print("T");
      Serial.print(hiveNumber);
      Serial.print("=");

      if (isnan(averageTemp[i])) {
        Serial.print("NA");
      } else {
        Serial.print(averageTemp[i], 1);
        Serial.print("C");
      }

      Serial.print("   ");
    }

    Serial.println();
    Serial.println();

    // -------------------------------------------------
    // Trame LoRa
    // -------------------------------------------------
    String frame = buildLoRaFrame(hxCount);

    LoRa.beginPacket();
    LoRa.print(frame);
    LoRa.endPacket();

    Serial.println("Trame LoRa:");
    Serial.println(frame);
    Serial.println("TX OK");

    clignoterLedOrange();

    sequenceNumber++;
    lastSend = now;
  }

  delay(100);
}

Bonjour à tous, pour info, j'ai trouvé le problème et la solution, j'utilisais l'alimentation 5 volts, alors que le MKR 1310 régule beaucoup mieux en 3,3 volts, et les systèmes de pesages sont du fait beaucoup plus stables en 3,3 volts .