Bonjour,
Pour réaliser un projet de ruche connectée
Je souhaite envoyer des données en json avec le module xbee à la centrale lorsqu'elle le demande, la centrale doit m'envoyer une requête pour acquérir les différentes données tels que le poids, l'humidité ou encore la température. Voici mon code :
#include <ArduinoJson.h>
#include <DHT.h>
#include <Wire.h>
#include <Adafruit_HMC5883_U.h>
#include <HX711.h>
#include <SoftwareSerial.h>
// DHT22
#define DHTPIN 8
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);
// HX711
#define LOADCELL_DOUT_PIN 13
#define LOADCELL_SCK_PIN 12
HX711 scale;
float calibration_factor = 7050.0; // À ajuster selon ton capteur
// GY-271
Adafruit_HMC5883_Unified mag = Adafruit_HMC5883_Unified(12345);
// XBee
SoftwareSerial XBee(2, 3);
void setup() {
Serial.begin(9600);
XBee.begin(9600);
dht.begin();
// Init HX711
scale.begin(LOADCELL_DOUT_PIN, LOADCELL_SCK_PIN);
scale.set_scale(calibration_factor); // Appliquer le facteur d'étalonnage
scale.tare(); // Remise à zéro
// Init GY-271
if (!mag.begin()) {
Serial.println("GY-271 not detected!");
while (1);
}
Serial.println ("Initialisation...");
delay(3000);
Serial.println("Modules prets!");
Serial.println("\n");
}
void loop() {
// Lecture DHT22
float temperature = dht.readTemperature();
float humidity = dht.readHumidity();
// Lecture HX711
float weight = 0;
if (scale.is_ready()) {
weight = 0.454 * scale.get_units();
}
// Lecture GY-271
sensors_event_t event;
mag.getEvent(&event);
float magX = event.magnetic.x;
float magY = event.magnetic.y;
float magZ = event.magnetic.z;
// Création JSON
String json = "";
json += "temperature:" + String(temperature, 1) + "°C\n";
json += "humidite:" + String(humidity, 1) + "%\n";
json += "poids:" + String(weight, 2) + "kg\n";
json += "position:\n";
json += "x:" + String(magX, 1) + "\n";
json += "y:" + String(magY, 1) + "\n";
json += "z:" + String(magZ, 1) + "\n";
// Envoi XBee + debug
XBee.println(json);
Serial.println(json);
delay(10000);
}
Que dois-je ajouter à mon code pour que cela fonctionne ?
Merci d'avance