Voici le sketch, si tu as des question n’hésite pas.
// Libraries
#include <Arduino.h>
#include <Adafruit_Sensor.h>
#include <DHT.h>
#include <SPI.h>
#include <Ethernet.h>
#include <Bridge.h>
#include <HttpClient.h>
// Enter a MAC address for your controller below.
// Newer Ethernet shields have a MAC address printed on a sticker on the shield
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
// if you don't want to use DNS (and reduce your sketch size)
// use the numeric IP instead of the name for the server:
//IPAddress server(74,125,232,128); // numeric IP for Google (no DNS)
char server[] = "www.google.com"; // name address for Google (using DNS)
// Set the static IP address to use if the DHCP fails to assign
IPAddress ip(192, 168, 0, 177);
IPAddress myDns(192, 168, 0, 1);
#define DHT1PIN 2 // Pin du DHT
#define DHT2PIN 4 // Pin du DHT
#define DHT3PIN 7 // Pin du DHT
#define DHT4PIN 8 // Pin du DHT
#define DHTTYPE DHT22 // ou DHT 11 à modifer en fonction
DHT dht1(DHT1PIN, DHTTYPE);
DHT dht2(DHT2PIN, DHTTYPE);
DHT dht3(DHT3PIN, DHTTYPE);
DHT dht4(DHT4PIN, DHTTYPE);
// Comme ça c'est alloué une bonne fois pour toute dès la compilation,
// et il n'y aura pas d'allocation dynamique avec les effets de bords du à l'utilisation de String. Donc plus fiable, pas de comportement suspicieux ensuite.
// En réalité les microcontroleurs n'aime pas trop les String, elles sont consommatrices de mémoire sur le long terme (alloc dynamique, duplication etc)
// Il est préférable d'utiliser des array de char, mais on est obligé de fixer la taille
const char API_KEY[] = "TONAPIKEYJEEDOM"; // ici la taille est fixé à la compilation par la valeur qu'on lui a attribué. Ne
const char BASE_URL[] = "GET /core/api/jeeApi.php?plugin=virtual&apikey=";
const char HOST[] = "Host: jee.jeedom.com";
const char URL_SUFFIX[] = "&type=virtual&id=";
const char URL_VALUE[] = "&value=";
const char HTTP[] = " HTTP/1.1";
char stringBuffer[200]; // Attention : bien dimensionner le tableau. Ne peut être changé lors de l'execution
EthernetClient client;
void setup(){
Serial.begin(9600);
// start the Ethernet connection:
Serial.println("Initialize Ethernet with DHCP:");
if (Ethernet.begin(mac) == 0) {
Serial.println("Failed to configure Ethernet using DHCP");
// Check for Ethernet hardware present
if (Ethernet.hardwareStatus() == EthernetNoHardware) {
Serial.println("Ethernet shield was not found. Sorry, can't run without hardware. :(");
while (true) {
delay(1); // do nothing, no point running without Ethernet hardware
}
}
if (Ethernet.linkStatus() == LinkOFF) {
Serial.println("Ethernet cable is not connected.");
}
// try to congifure using IP address instead of DHCP:
Ethernet.begin(mac, ip, myDns);
} else {
Serial.print(" DHCP assigned IP ");
Serial.println(Ethernet.localIP());
}
// Serial port for debugging purposes
dht1.begin();
dht2.begin();
dht3.begin();
dht4.begin();
}
void loop(){
// Exemple non bloquant (delay bloque l'execution du programme, c'est à dire, pendant ce temps le micro ne fait rien, ne peut rien recevoir etc
// pas grave quand il s'agit de millisec mais pourrait être bloquant si l'on voudrait faire quelque chose sur la réception d'une commande
// pas le cas ici, si c'est juste pour envoyer des valeurs, mais c'est pour l'exemple
// on pourrait par exemple vouloir laisser la possibilité à l'utilisateur d'envoyer des commandes/mettre à jour des paramètres depuis jeedom (changer la valeur du timer etc)
// static variable : ne sera pas réinitialisée à chaque loop
static unsigned long previousMillis = 0;
// variable contenant le temps
unsigned long currentMillis = millis();
unsigned long interval = 10000;
// on vérifie le temps écoulé, et on effectue la tache si c'est le cas
if (currentMillis - previousMillis >= interval) {
// on met à jour notre variable contenant le temps
previousMillis = currentMillis;
// sensors
readAndSendSensors();
}
}
void readAndSendSensors() {
int id1t = 1; // id du virtuel
int value1t = dht1.readTemperature();
int id2t = 2; // id du virtuel
int value2t = dht2.readTemperature();
int id3t = 3; // id du virtuel
int value3t = dht3.readTemperature();
int id4t = 4; // id du virtuel
int value4t = dht4.readTemperature();
sendToJeedom(value1t, id1t);
sendToJeedom(value2t, id2t);
sendToJeedom(value3t, id3t);
sendToJeedom(value4t, id4t);
int id1h = 5; // id du virtuel
int value1h = dht1.readHumidity();
int id2h = 6; // id du virtuel
int value2h = dht2.readHumidity();
int id3h = 7; // id du virtuel
int value3h = dht3.readHumidity();
int id4h = 8; // id du virtuel
int value4h = dht4.readHumidity();
sendToJeedom(value1h, id1h);
sendToJeedom(value2h, id2h);
sendToJeedom(value3h, id3h);
sendToJeedom(value4h, id4h);
}
void sendToJeedom(int value, int id)
{
sprintf(stringBuffer, "%s%s%s%d%s%d%s", BASE_URL, API_KEY, URL_SUFFIX, id, URL_VALUE, value, HTTP); // concaténation
// give the Ethernet shield a second to initialize:
delay(1000);
Serial.print("connecting to ");
Serial.print(server);
Serial.println("...");
// if you get a connection, report back via serial:
if (client.connect(server, 80)) {
Serial.print("connected to ");
Serial.println(client.remoteIP());
// Make a HTTP request:
client.println(stringBuffer);
client.println(HOST);
client.println("Connection: close");
client.println();
} else {
// if you didn't get a connection to the server:
Serial.println("connection failed");
}
//client.get(stringBuffer);
Serial.println(stringBuffer); // ou on peut aussi utiliser printf("Send to %s\n", buff);
}
Le code à été amélioré par un ami qui s’y connait vraiment bien, car au début je passais pas des String et mon code était blocant, les modifications sont expliquées.