Je suis sur une Arduino OPTA, je cherche à envoyé des états de switch branché sur cette dernière vers une base données en MySQL (avec phpmyadmin) en passant par mon câble réseau et en HTTP, j'ai bien crée un code et une .php associé pour faire passer les infos, quand je teste mon code, je ne peut voir les états que quand mon câble usb c vers usb est branché, ( j'ai essayé de ping mon OPTA sur le cmd de mon serveur et ça marche ), j'ai donc l'impression que mes états se transmette via le usb c et non par la RJ-45 ?
Bonjour @gabrielcirad
Publies ici ton code
J'ai remplacé les adresses, confidentielles, de toute façon je sais que la partie adresses est bonne.
J'ai également le code php correspondant stv
#include <Ethernet.h>
#include <SPI.h>
#include <Wire.h>
#include "RTClib.h"
RTC_DS3231 rtc;
//---------------------------------------------//Partie comm réseau//---------------------------------------------//
// Paramètres réseau
byte mac[] = { 0xXX, 0xXX, 0xXX, 0xXX, 0xXX, 0xXX }; // Adresse MAC personnalisée si nécessaire
IPAddress ip(XXX, XXX, XXX, XXX); // Nouvelle adresse IP souhaitée
IPAddress gateway(XXX, XXX, XXX, XXX); // Passerelle
IPAddress subnet(XXX, XXX, XXX, XXX); // Masque de sous-réseau
IPAddress dns(XXX, XXX, XXX, XXX); // Serveur DNS
IPAddress server(XXX, XXX, XXX, XXX); // IP du serveur avec WampServer
//---------------------------------------------//Fin Partie comm réseau//---------------------------------------------//
//---------------------------------------------//Partie alarme//---------------------------------------------//
const int switchPin1 = A0;
int switchState1 = 0;
const int switchPin2 = A1;
int switchState2 = 0;
const int switchPin3 = A2;
int switchState3 = 0;
const int switchPin4 = A3;
int switchState4 = 0;
const int relaisPin1 = D0;
int relaisState1 = 0;
//---------------------------------------------//Fin Partie alarme//---------------------------------------------//
// ---------------------------------------------//Partie Forcage//---------------------------------------------//
const int switchPin5 = A4;
int switchState5 = 0;
int currentSwitchState5 = digitalRead(switchPin5);
// ---------------------------------------------//Fin Partie Forcage//---------------------------------------------//
//---------------------------------------------//Partie defaut alarme//---------------------------------------------//
int switchStateD1 = 0;
unsigned long lastChangeTime1;
const unsigned long timeout1 = 3 * 60 * 1000; // 3 minutes en millisecondes
int switchStateD2 = 0;
unsigned long lastChangeTime2;
const unsigned long timeout2 = 3 * 60 * 1000; // 3 minutes en millisecondes
int switchStateD3 = 0;
unsigned long lastChangeTime3;
const unsigned long timeout3 = 3 * 60 * 1000; // 3 minutes en millisecondes
//---------------------------------------------//Fin Partie defaut alarme//---------------------------------------------//
// ---------------------------------------------//Partie comm réseau//---------------------------------------------//
EthernetClient client;
// Fonction pour encoder une chaîne en URL
String urlencode(const String &str) {
String encoded = "";
char c;
for (int i = 0; i < str.length(); i++) {
c = str.charAt(i);
if (isalnum(c)) {
encoded += c;
} else {
encoded += '%';
if (c < 16) encoded += '0';
encoded += String(c, HEX);
}
}
return encoded;
}
// Fonction pour envoyer une requête HTTP //Objectif : Établir une connexion à un serveur sur le port 80 pour envoyer une requête HTTP.
void sendHTTPRequest(int switchState, const String &message) {
if (client.connect(server, 80)) {
Serial.println("Connecté au serveur");
// Encoder le message
String encodedMessage = urlencode(message);
// Créer et envoyer la requête HTTP
client.print("GET /insert_switch_status.php?etat_switch=");
client.print(switchState);
client.print("&message=");
client.print(encodedMessage);
client.println(" HTTP/1.1");
client.println("Host: 195.221.175.111");
client.println("Connection: close");
client.println();
// Lire la réponse du serveur
while (client.connected()) {
if (client.available()) {
char c = client.read();
Serial.print(c);
}
}
client.stop();
} else {
Serial.println("Échec de la connexion");
//---------------------------------------------//Fin Partie comm réseau//---------------------------------------------//
}
}
void setup() {
// Initialisation de la connexion Ethernet
Ethernet.begin(mac, ip, dns, gateway, subnet);
Serial.begin(9600);
//---------------------------------------------//Partie alarme//---------------------------------------------//
// Configuration de la pin du switch en entrée
pinMode(switchPin1, INPUT);
pinMode(switchPin2, INPUT);
pinMode(switchPin3, INPUT);
pinMode(switchPin4, INPUT);
pinMode(relaisPin1, OUTPUT);
//---------------------------------------------//Fin Partie alarme//---------------------------------------------//
//---------------------------------------------//Partie Forcage//---------------------------------------------//
pinMode(switchPin5, INPUT);
// ---------------------------------------------//Fin Partie Forcage//---------------------------------------------//
//---------------------------------------------//Partie comm réseau//---------------------------------------------//
// Vérifier si la connexion Ethernet est réussie
if (Ethernet.hardwareStatus() == EthernetNoHardware) {
Serial.println("Le bouclier Ethernet n'a pas été trouvé.");
while (true);
}
if (Ethernet.linkStatus() == LinkOFF) {
Serial.println("Le câble Ethernet n'est pas connecté.");
}
Serial.println("Configuration terminée, en attente des changements de l'état du switch...");
//---------------------------------------------//Fin Partie comm réseau//---------------------------------------------//
// ---------------------------------------------//Partie defaut alarme//---------------------------------------------//
switchStateD1 = digitalRead(switchPin1);
switchStateD2 = digitalRead(switchPin2);
switchStateD3 = digitalRead(switchPin3);
lastChangeTime1 = millis();
lastChangeTime2 = millis();
lastChangeTime3 = millis();
Serial.begin(9600);
// --------------------------------------------//Fin Partie defaut alarme//------------------------------------------//
//---------------------------------------------//Partie Test lundu du mois//----------------------------------------//
// Initialisation du moniteur série
Serial.begin(9600);
// --------------------------------------------//Fin Partie Test lundi du mois//-------------------------------------//
}
void loop() {
//---------------------------------------------//Partie alarme//---------------------------------------------//
// Lire l'état du switch 4 Mise en route ou a l'arret du système d'alarmes
int currentSwitchState4 = digitalRead(switchPin4);
// Si l'état a changé, envoyer les données
if (currentSwitchState4 != switchState4) { //On compare l'état actuel du switch 4 à l'état précédent (switchState4).
switchState4 = currentSwitchState4; //Mise à jour de la variable switchState4 avec le nouvel état.
String message = (switchState4 == HIGH) ? "MISE A L'ARRET BATIMENT 11" : "MISE EN ROUTE BATIMENT 11";
sendHTTPRequest(switchState4, message); //Appel d’une fonction sendHTTPRequest() qui envoie une requête HTTP vers le serveur.
}
// Lire l'état du switch 1
int currentSwitchState1 = digitalRead(switchPin1);
// Si l'état a changé, envoyer les données
if (currentSwitchState1 != switchState1) { //On compare l'état actuel du switch 1 à l'état précédent (switchState1).
switchState1 = currentSwitchState1; //Mise à jour de la variable switchState1 avec le nouvel état.
String message = (switchState1 == HIGH) ? "DETECTION ALARME 1 BATIMENT 11" : "DEFAUT ALARME 1 BATIMENT 11"; //Si le switch est HIGH, le message est "DETECTION ALARME 1 BATIMENT 11". Sinon, le message est "DEFAUT ALARME 1 BATIMENT 11".
sendHTTPRequest(switchState1, message); //Appel d’une fonction sendHTTPRequest() qui envoie une requête HTTP vers le serveur.
if (switchState4 == 1 || currentSwitchState5 == 1) { // currentSwitchState5 == 1 option forcage //
digitalWrite(relaisPin1, HIGH);
}
}
delay(1000); // Attendre 1 seconde avant de vérifier à nouveau
// Lire l'état du switch 2
int currentSwitchState2 = digitalRead(switchPin2);
// Si l'état a changé, envoyer les données
if (currentSwitchState2 != switchState2) {
switchState2 = currentSwitchState2;
String message = (switchState2 == HIGH) ? "DETECTION ALARME 2 BATIMENT 11" : "DEFAUT ALARME 2 BATIMENT 11";
sendHTTPRequest(switchState2, message);
if (switchState4 == 1 || currentSwitchState5 == 1) {
digitalWrite(relaisPin1, HIGH);
}
}
delay(1000); // Attendre 1 seconde avant de vérifier à nouveau
// Lire l'état du switch 3
int currentSwitchState3 = digitalRead(switchPin3);
// Si l'état a changé, envoyer les données
if (currentSwitchState3 != switchState3) {
switchState3 = currentSwitchState3;
String message = (switchState3 == HIGH) ? "DETECTION ALARME 3 BATIMENT 11" : "DEFAUT ALARME 3 BATIMENT 11";
sendHTTPRequest(switchState3, message);
if (switchState4 == 1 || currentSwitchState5 == 1) {
digitalWrite(relaisPin1, HIGH);
}
}
delay(1000); // Attendre 1 seconde avant de vérifier à nouveau
//---------------------------------------------//Fin Partie alarme//---------------------------------------------//
// ---------------------------------------------//Partie defaut alarme//---------------------------------------------//
int currentSwitchStateD1 = digitalRead(switchPin1);
// Vérifie si l'état du switch a changé
if (currentSwitchStateD1 != switchStateD1) {
switchStateD1 = currentSwitchStateD1;
lastChangeTime1 = millis(); // Réinitialise le temps
}
// Vérifie si 1 minute s'est écoulée sans changement
if (millis() - lastChangeTime1 >= timeout1) {
Serial.println("defaut alarme 1");
if (switchState4 == 1 || currentSwitchState5 == 1) {
digitalWrite(relaisPin1, HIGH);
}
lastChangeTime1 = millis(); // Pour éviter que le message se répète toutes les millisecondes
}
delay(100); // Petite pause pour éviter d'inonder le microcontrôleur
int currentSwitchStateD2 = digitalRead(switchPin2);
// Vérifie si l'état du switch a changé
if (currentSwitchStateD2 != switchStateD2) {
switchStateD2 = currentSwitchStateD2;
lastChangeTime2 = millis(); // Réinitialise le temps
}
// Vérifie si 1 minute s'est écoulée sans changement
if (millis() - lastChangeTime2 >= timeout2) {
Serial.println("defaut alarme 2");
if (switchState4 == 1 || currentSwitchState5 == 1) {
digitalWrite(relaisPin1, HIGH);
}
lastChangeTime2 = millis(); // Pour éviter que le message se répète toutes les millisecondes
}
delay(100); // Petite pause pour éviter d'inonder le microcontrôleur
int currentSwitchStateD3 = digitalRead(switchPin3);
// Vérifie si l'état du switch a changé
if (currentSwitchStateD3 != switchStateD3) {
switchStateD3 = currentSwitchStateD3;
lastChangeTime3 = millis(); // Réinitialise le temps
}
// Vérifie si 1 minute s'est écoulée sans changement
if (millis() - lastChangeTime3 >= timeout3) {
Serial.println("defaut alarme 3");
if (switchState4 == 1 || currentSwitchState5 == 1) {
digitalWrite(relaisPin1, HIGH);
}
lastChangeTime3 = millis(); // Pour éviter que le message se répète toutes les millisecondes
}
delay(100); // Petite pause pour éviter d'inonder le microcontrôleur
//--------------------------------------------//Fin Partie defaut alarme//------------------------------------------//
//---------------------------------------------//Partie Forcage//---------------------------------------------//
int currentSwitchState5 = digitalRead(switchPin5);
//---------------------------------------------//Fin Partie Forcage//---------------------------------------------//
//---------------------------------------------//Partie Test lundu du mois//----------------------------------------//
// Obtenir l'heure actuelle de l'horloge interne
time_t now = time(nullptr);
struct tm *timeinfo = localtime(&now);
// Vérifier si c'est le premier lundi du mois à 12h20
if (timeinfo->tm_wday == 1 && timeinfo->tm_mday <= 7 && timeinfo->tm_hour == 12 && timeinfo->tm_min == 20) {
// Envoyer le message
Serial.println("ALARMES BATIMENT 11 FONCTIONNELLES - Premier lundi du mois à 12h20");
delay(60000); // Attendre 1 minute pour éviter les doublons dans la même minute
}
// Petite pause pour éviter une surcharge du processeur
delay(200);
//--------------------------------------------//Fin Partie Test lundi du mois//-------------------------------------//
}
avez-vous une idée ?
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.