Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 0 additions & 10 deletions src/def.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,37 +2,32 @@
#include <string>
#include <ArduinoJson.h>

//structure pour garder l'état passé des lampes
struct LampStates
{
bool oldLamp1State = false;
bool oldLamp2State = false;
};

//strucuture pour représenter le flag de sauvegarde de l'état de chaque lampe
struct SaveStateActivity
{
bool checkLampe1 = false;
bool ckeckLampe2 = false;
};

//Strucuture pour les infos du wifi
struct hostPointConfig
{
String ssid ;
String password ;
bool isvalide = false;
};

//structure date
struct date
{
int annee;
int mois;
int jour;
};

//structure pour représenter le temps
struct Time
{
long heure;
Expand Down Expand Up @@ -88,15 +83,13 @@ struct Time
} ;


//structure pour représenter une configuration de tâche donnée (allumage , extinction)
struct TimeConfig
{
Time onTime ;
Time ofTime;
bool isvalide = false;
};

//structure pour représenter quatre tâche (c'est pour faciliter le stockage dans la mémoire eeprom)
struct FourConfig
{
TimeConfig tache_1_lamp_1;
Expand All @@ -106,7 +99,6 @@ struct FourConfig
bool isvalide = false;
};

// Fonction qui convertit une chaîne JSON en structure TimeConfig
TimeConfig convertToTimeConfig(const String &str) {
StaticJsonDocument<200> doc;
DeserializationError error = deserializeJson(doc, str);
Expand All @@ -120,12 +112,10 @@ TimeConfig convertToTimeConfig(const String &str) {
return cfg;
}

//alummage config
cfg.onTime.heure = doc["allumage"]["heure"] | 0;
cfg.onTime.minute = doc["allumage"]["minute"] | 0;
cfg.onTime.seconde = doc["allumage"]["seconde"] | 0;

//extinction config
cfg.ofTime.heure = doc["extinction"]["heure"] | 0;
cfg.ofTime.minute = doc["extinction"]["minute"] | 0;
cfg.ofTime.seconde = doc["extinction"]["seconde"] | 0;
Expand Down
98 changes: 6 additions & 92 deletions src/gsm_manager.h
Original file line number Diff line number Diff line change
@@ -1,102 +1,19 @@
#pragma once
//définition de l'entête indiquand le type de module gsm qui serait utilisé
//ici le SIM800L
#include <ESP8266WiFi.h>
#include <SoftwareSerial.h>
#include <ArduinoHttpClient.h>
#include <ArduinoJson.h>
#include "def.h"

#define RX 14 // GPIO14 = D5
#define TX 12 // GPIO12 = D6

/* const char apn[] = "internet.mtn.bj";
const String url ="/v2.1/get-time-zone?key="+API_KEY+"&format=json&by=zone&zone=Africa/Porto-Novo"; */
#define RX 14
#define TX 12


namespace gsm
{

/* Time getNowTime()
{
//Ce bloc permet d'initialiser le module gsm
//D'initialiser un client en se basant sur ce module
// Et de faire les configurations nécessaires pour faire la requête vers l'api de récup de l'heure
Time now = {0,0,0,false};
SoftwareSerial gsm_module(RX, TX);
TinyGsm modem(gsm_module);
TinyGsmClient client(modem);
HttpClient http(client, "api.timezonedb.com", 80);
gsm_module.begin(9600);
Serial.println("Initialisation du modem...");
modem.restart();
delay(3000);
Serial.println("Connexion au réseau...");
//3 essai de connexion max , après 3 essai on continue
for(byte i = 0; i < 3; i++)
{
if(!modem.gprsConnect(apn, "", ""))
{
Serial.println("Échec GPRS");
now.valide = false;
modem.restart();
delay(3000);
Serial.println("Connexion au réseau...");
}
else
return Time{0,0,0,false};
}
//lancement de la requête verss l'api
Serial.println("Requête HTTP...");
http.get(url);

int statusCode = http.responseStatusCode();
String response = http.responseBody();

/* Serial.print("Code HTTP: ");
Serial.println(statusCode);
Serial.println("Réponse JSON:");
Serial.println(response); */

//jsonification de la réponse envoyé par l'api
/* modem.gprsDisconnect();
DynamicJsonDocument doc(1024);
DeserializationError error = deserializeJson(doc,response);
if(error)
{
now.valide = false;
return now;
}

//Ce bloc sert à extraire l'heure de la chaine de caractère envoyé par l'api.
String data = doc["formatted"];
String date = data.substring(0,10);
data = data.substring(11,19);
String para;
String dataLists[3];
byte pos = 0;
for(byte i = 0; i < data.length() ; i++)
{
if(data[i] == ':' || (i+1 == data.length()))
{
para += (i+1 == data.length()) ? data[i] : ' ';
//Serial.println(para);
dataLists[pos] = para;
pos ++;
para = "";
}
else
para += data[i];
}

now = Time{dataLists[0].toInt(),dataLists[1].toInt(),dataLists[2].toInt(),true};
Serial.println(date.substring(8,9));
return now;
} */

//fonction2
Time getNetworkTime(SoftwareSerial &sim800,date &date) {
Time currentTime = {-1, -1, -1}; // Valeurs par défaut si erreur
Time currentTime = {-1, -1, -1};
date = {0,0,0};

sim800.println("AT+CCLK?");
Expand All @@ -106,18 +23,15 @@ Time getNetworkTime(SoftwareSerial &sim800,date &date) {
while (sim800.available()) {
char c = sim800.read();
response += c;
//Serial.println(c);
}

// Exemple de réponse attendue : +CCLK: "25/08/06,08:45:30+04"
int index = response.indexOf("\"");
if (index != -1) {
String dateTimeStr = response.substring(index + 1, response.indexOf("\"", index + 1));

// On sépare la partie temps
int timeSepIndex = dateTimeStr.indexOf(",");
if (timeSepIndex != -1) {
String timePart = dateTimeStr.substring(timeSepIndex + 1); // "08:45:30+04"
String timePart = dateTimeStr.substring(timeSepIndex + 1);
int h = timePart.substring(0, 2).toInt();
int m = timePart.substring(3, 5).toInt();
int s = timePart.substring(6, 8).toInt();
Expand All @@ -126,7 +40,7 @@ Time getNetworkTime(SoftwareSerial &sim800,date &date) {
currentTime.minute = m;
currentTime.seconde = s;

String datePart = dateTimeStr.substring(0, timeSepIndex); // "25/08/06"
String datePart = dateTimeStr.substring(0, timeSepIndex);
int yy = datePart.substring(0, 2).toInt();
int mm = datePart.substring(3, 5).toInt();
int dd = datePart.substring(6, 8).toInt();
Expand All @@ -138,4 +52,4 @@ Time getNetworkTime(SoftwareSerial &sim800,date &date) {
return currentTime;
}

}//end gsm
}
Loading