Rajoute de parametre et amélioration des enregistrements
This commit is contained in:
parent
f5766a2817
commit
738c87accf
|
|
@ -0,0 +1,5 @@
|
|||
.pio
|
||||
.vscode/.browse.c_cpp.db*
|
||||
.vscode/c_cpp_properties.json
|
||||
.vscode/launch.json
|
||||
.vscode/ipch
|
||||
|
|
@ -0,0 +1,10 @@
|
|||
{
|
||||
// See http://go.microsoft.com/fwlink/?LinkId=827846
|
||||
// for the documentation about the extensions.json format
|
||||
"recommendations": [
|
||||
"platformio.platformio-ide"
|
||||
],
|
||||
"unwantedRecommendations": [
|
||||
"ms-vscode.cpptools-extension-pack"
|
||||
]
|
||||
}
|
||||
|
|
@ -0,0 +1,37 @@
|
|||
|
||||
This directory is intended for project header files.
|
||||
|
||||
A header file is a file containing C declarations and macro definitions
|
||||
to be shared between several project source files. You request the use of a
|
||||
header file in your project source file (C, C++, etc) located in `src` folder
|
||||
by including it, with the C preprocessing directive `#include'.
|
||||
|
||||
```src/main.c
|
||||
|
||||
#include "header.h"
|
||||
|
||||
int main (void)
|
||||
{
|
||||
...
|
||||
}
|
||||
```
|
||||
|
||||
Including a header file produces the same results as copying the header file
|
||||
into each source file that needs it. Such copying would be time-consuming
|
||||
and error-prone. With a header file, the related declarations appear
|
||||
in only one place. If they need to be changed, they can be changed in one
|
||||
place, and programs that include the header file will automatically use the
|
||||
new version when next recompiled. The header file eliminates the labor of
|
||||
finding and changing all the copies as well as the risk that a failure to
|
||||
find one copy will result in inconsistencies within a program.
|
||||
|
||||
In C, the convention is to give header files names that end with `.h'.
|
||||
|
||||
Read more about using header files in official GCC documentation:
|
||||
|
||||
* Include Syntax
|
||||
* Include Operation
|
||||
* Once-Only Headers
|
||||
* Computed Includes
|
||||
|
||||
https://gcc.gnu.org/onlinedocs/cpp/Header-Files.html
|
||||
|
|
@ -0,0 +1,46 @@
|
|||
|
||||
This directory is intended for project specific (private) libraries.
|
||||
PlatformIO will compile them to static libraries and link into the executable file.
|
||||
|
||||
The source code of each library should be placed in a separate directory
|
||||
("lib/your_library_name/[Code]").
|
||||
|
||||
For example, see the structure of the following example libraries `Foo` and `Bar`:
|
||||
|
||||
|--lib
|
||||
| |
|
||||
| |--Bar
|
||||
| | |--docs
|
||||
| | |--examples
|
||||
| | |--src
|
||||
| | |- Bar.c
|
||||
| | |- Bar.h
|
||||
| | |- library.json (optional. for custom build options, etc) https://docs.platformio.org/page/librarymanager/config.html
|
||||
| |
|
||||
| |--Foo
|
||||
| | |- Foo.c
|
||||
| | |- Foo.h
|
||||
| |
|
||||
| |- README --> THIS FILE
|
||||
|
|
||||
|- platformio.ini
|
||||
|--src
|
||||
|- main.c
|
||||
|
||||
Example contents of `src/main.c` using Foo and Bar:
|
||||
```
|
||||
#include <Foo.h>
|
||||
#include <Bar.h>
|
||||
|
||||
int main (void)
|
||||
{
|
||||
...
|
||||
}
|
||||
|
||||
```
|
||||
|
||||
The PlatformIO Library Dependency Finder will find automatically dependent
|
||||
libraries by scanning project source files.
|
||||
|
||||
More information about PlatformIO Library Dependency Finder
|
||||
- https://docs.platformio.org/page/librarymanager/ldf.html
|
||||
147
main.cpp
147
main.cpp
|
|
@ -1,147 +0,0 @@
|
|||
#include <WiFi.h>
|
||||
#include <WebSocketsClient.h>
|
||||
#include <HTTPClient.h>
|
||||
#include <map>
|
||||
|
||||
//Association string -> fonction
|
||||
typedef void (*CommandFunction)(String);
|
||||
std::map<String, CommandFunction> commandMap;
|
||||
|
||||
//Infos WiFi
|
||||
const char* ssid = "StCaillou";
|
||||
const char* password = "02070500";
|
||||
|
||||
//Infos WebSocket
|
||||
WebSocketsClient webSocket;
|
||||
const char* websocket_host = "193.70.38.222";
|
||||
const uint16_t websocket_port = 4000;
|
||||
const char* websocket_path = "/ws";
|
||||
|
||||
|
||||
String mac = WiFi.macAddress();
|
||||
|
||||
//-------------------------------------------------------------------------------
|
||||
//Configuration des informations de connection sur l'api CANDLE
|
||||
const String urlCANDLE = "https://auth.collineos.ovh"; //pseudo & mdp comme champ
|
||||
//-------------------------------------------------------------------------------
|
||||
|
||||
//Déclaration de prototypes pour éviter les non définitions au sein du fichier
|
||||
void login();
|
||||
void saveFunc(String interfaceName, CommandFunction func);
|
||||
void saveFunc(String interfaceName, CommandFunction func, String params);
|
||||
void defineInterface();
|
||||
|
||||
|
||||
void webSocketEvent(WStype_t type, uint8_t * payload, size_t length) {
|
||||
switch(type) {
|
||||
case WStype_DISCONNECTED:
|
||||
Serial.println("[DEBUG] Déconnecté !");
|
||||
break;
|
||||
case WStype_CONNECTED:
|
||||
Serial.printf("[DEBUG] Connecté à: %s\n", payload);
|
||||
login();
|
||||
defineInterface();
|
||||
break;
|
||||
case WStype_TEXT: {
|
||||
String message = (char*)payload;
|
||||
Serial.printf("[DEBUG] Message reçu: %s\n", message.c_str());
|
||||
|
||||
// Parse le message : "ACTION$COMMAND$PARAM"
|
||||
int firstIndex = message.indexOf('$');
|
||||
int secondIndex = message.indexOf('$', firstIndex + 1);
|
||||
|
||||
if (firstIndex != -1 && message.substring(0, firstIndex) == "ACTION") {
|
||||
String command = message.substring(firstIndex + 1, secondIndex);
|
||||
String param = (secondIndex != -1) ? message.substring(secondIndex + 1) : "";
|
||||
if (commandMap.find(command) != commandMap.end()) {
|
||||
CommandFunction func = commandMap[command];
|
||||
func(param);
|
||||
} else {
|
||||
Serial.println("Commande inconnue: " + command);
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
case WStype_ERROR:
|
||||
Serial.println("[DEBUG] Erreur !");
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
void setup() {
|
||||
Serial.begin(115200);
|
||||
delay(500);
|
||||
|
||||
// Connexion au Wi-Fi
|
||||
Serial.print("Connexion WiFi à ");
|
||||
Serial.println(ssid);
|
||||
WiFi.begin(ssid, password);
|
||||
|
||||
while (WiFi.status() != WL_CONNECTED) {
|
||||
delay(500);
|
||||
Serial.print(".");
|
||||
}
|
||||
Serial.println("\n WiFi connecté");
|
||||
Serial.print("IP locale: ");
|
||||
Serial.println(WiFi.localIP());
|
||||
|
||||
// Connexion au WebSocket
|
||||
webSocket.begin(websocket_host, websocket_port, websocket_path);
|
||||
webSocket.onEvent(webSocketEvent);
|
||||
webSocket.setReconnectInterval(5000);
|
||||
|
||||
}
|
||||
|
||||
//----------------------------------------------------------------------
|
||||
//Ici on définit les actions sous formes d'actions
|
||||
//Pour le moment les parametre ne sont pas encore totalement développer
|
||||
//Correction prochaine
|
||||
//----------------------------------------------------------------------
|
||||
|
||||
void ping(String param){
|
||||
Serial.println("PONG!");
|
||||
}
|
||||
|
||||
void defineInterface(){
|
||||
//Permet de définir les actions qu'un utilisateur peut entreprendre
|
||||
saveFunc("PING", ping);
|
||||
}
|
||||
|
||||
//----------------------------------------------------------------------
|
||||
//Ces fonctions permettent le fonctionnement général de la communication
|
||||
//----------------------------------------------------------------------
|
||||
|
||||
void login(){//Il faut rajouter le token
|
||||
webSocket.sendTXT("LOGIN$"+mac);
|
||||
}
|
||||
|
||||
void saveFunc(String interfaceName, CommandFunction func, String params){
|
||||
Serial.println("ENVOIE DE LINTERFACE AVEC PARAMS");
|
||||
commandMap[interfaceName] = func;
|
||||
webSocket.sendTXT("REGISTER$"+interfaceName + "$" + params);
|
||||
}
|
||||
|
||||
void saveFunc(String interfaceName, CommandFunction func){
|
||||
Serial.println("ENVOIE DE LINTERFACE SANS PARAMS");
|
||||
commandMap[interfaceName] = func;
|
||||
webSocket.sendTXT("REGISTER$"+interfaceName + "$");
|
||||
}
|
||||
|
||||
void sendValue(String valueName, String value){
|
||||
webSocket.sendTXT("VALUE$"+valueName+"$"+value);
|
||||
}
|
||||
|
||||
//--------------------------------------------------------
|
||||
|
||||
//--------------------------------------------------------
|
||||
//Le void loop ne doit pas être bloquant ( on utilisera millis )
|
||||
//--------------------------------------------------------
|
||||
void loop() {
|
||||
|
||||
webSocket.loop();
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,16 @@
|
|||
; PlatformIO Project Configuration File
|
||||
;
|
||||
; Build options: build flags, source filter
|
||||
; Upload options: custom upload port, speed and extra flags
|
||||
; Library options: dependencies, extra library storages
|
||||
; Advanced options: extra scripting
|
||||
;
|
||||
; Please visit documentation for the other options and examples
|
||||
; https://docs.platformio.org/page/projectconf.html
|
||||
|
||||
[env:upesy_wroom]
|
||||
platform = espressif32
|
||||
board = upesy_wroom
|
||||
framework = arduino
|
||||
monitor_speed = 115200
|
||||
lib_deps = links2004/WebSockets@^2.7.1
|
||||
|
|
@ -0,0 +1,178 @@
|
|||
#include <WiFi.h>
|
||||
#include <WebSocketsClient.h>
|
||||
#include <HTTPClient.h>
|
||||
#include <map>
|
||||
|
||||
// --------------------------------------
|
||||
// Configuration de l'appareil
|
||||
// --------------------------------------
|
||||
const String name = "DevDevice";
|
||||
const String place = "TP";
|
||||
|
||||
// Association string -> fonction
|
||||
typedef void (*CommandFunction)(String);
|
||||
std::map<String, CommandFunction> commandMap;
|
||||
|
||||
// Infos WiFi
|
||||
const char* ssid = "StCaillou";
|
||||
const char* password = "02070500";
|
||||
|
||||
// Infos WebSocket
|
||||
WebSocketsClient webSocket;
|
||||
const char* websocket_host = "193.70.38.222";
|
||||
const uint16_t websocket_port = 4000;
|
||||
const char* websocket_path = "/ws";
|
||||
|
||||
// Fonction utilitaire de split
|
||||
String getValue(String data, char separator, int index)
|
||||
{
|
||||
int found = 0;
|
||||
int strIndex[] = { 0, -1 };
|
||||
int maxIndex = data.length() - 1;
|
||||
|
||||
for (int i = 0; i <= maxIndex && found <= index; i++) {
|
||||
if (data.charAt(i) == separator || i == maxIndex) {
|
||||
found++;
|
||||
strIndex[0] = strIndex[1] + 1;
|
||||
strIndex[1] = (i == maxIndex) ? i + 1 : i;
|
||||
}
|
||||
}
|
||||
return found > index ? data.substring(strIndex[0], strIndex[1]) : "";
|
||||
}
|
||||
|
||||
String mac = "";
|
||||
|
||||
// -----------------------------------------------------------------------
|
||||
// Déclaration des prototypes
|
||||
// -----------------------------------------------------------------------
|
||||
void login();
|
||||
void defineInterface();
|
||||
void saveFunc(String interfaceName, CommandFunction func);
|
||||
void saveFunc(String interfaceName, CommandFunction func, String params);
|
||||
void sendValue(String valueName, String value);
|
||||
void ping(String param);
|
||||
|
||||
// -----------------------------------------------------------------------
|
||||
// Gestion des événements WebSocket
|
||||
// -----------------------------------------------------------------------
|
||||
void webSocketEvent(WStype_t type, uint8_t* payload, size_t length) {
|
||||
switch (type) {
|
||||
case WStype_DISCONNECTED:
|
||||
Serial.println("[DEBUG] Déconnecté !");
|
||||
break;
|
||||
|
||||
case WStype_CONNECTED:
|
||||
Serial.printf("[DEBUG] Connecté à: %s\n", payload);
|
||||
login(); // Envoie uniquement le LOGIN
|
||||
break;
|
||||
|
||||
case WStype_TEXT: {
|
||||
String message = (char*)payload;
|
||||
Serial.printf("[DEBUG] Message reçu: %s\n", message.c_str());
|
||||
|
||||
// --- 1️⃣ Attente du message de validation ---
|
||||
if (message.startsWith("OKPOURENREGISTREMENT")) {
|
||||
Serial.println("[DEBUG] Serveur prêt → envoi des interfaces");
|
||||
defineInterface();
|
||||
return;
|
||||
}
|
||||
|
||||
// --- 2️⃣ Commande d’action reçue ---
|
||||
if (message.startsWith("ACTION$")) {
|
||||
int firstIndex = message.indexOf('$');
|
||||
int secondIndex = message.indexOf('$', firstIndex + 1);
|
||||
String command = message.substring(firstIndex + 1, secondIndex);
|
||||
String param = (secondIndex != -1) ? message.substring(secondIndex + 1) : "";
|
||||
|
||||
if (commandMap.find(command) != commandMap.end()) {
|
||||
CommandFunction func = commandMap[command];
|
||||
func(param);
|
||||
} else {
|
||||
Serial.println("Commande inconnue: " + command);
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
case WStype_ERROR:
|
||||
Serial.println("[DEBUG] Erreur WebSocket !");
|
||||
break;
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// -----------------------------------------------------------------------
|
||||
// Fonctions principales
|
||||
// -----------------------------------------------------------------------
|
||||
void setup() {
|
||||
Serial.begin(115200);
|
||||
delay(500);
|
||||
|
||||
// Connexion WiFi
|
||||
Serial.print("Connexion WiFi à ");
|
||||
Serial.println(ssid);
|
||||
WiFi.begin(ssid, password);
|
||||
|
||||
while (WiFi.status() != WL_CONNECTED) {
|
||||
delay(500);
|
||||
Serial.print(".");
|
||||
}
|
||||
|
||||
Serial.println("\n✅ WiFi connecté");
|
||||
Serial.print("IP locale: ");
|
||||
Serial.println(WiFi.localIP());
|
||||
|
||||
mac = WiFi.macAddress();
|
||||
|
||||
// Connexion WebSocket
|
||||
webSocket.begin(websocket_host, websocket_port, websocket_path);
|
||||
webSocket.onEvent(webSocketEvent);
|
||||
webSocket.setReconnectInterval(5000);
|
||||
}
|
||||
|
||||
// -----------------------------------------------------------------------
|
||||
// Exemples de commandes (interfaces)
|
||||
// -----------------------------------------------------------------------
|
||||
void ping(String param) {
|
||||
Serial.println("PONG!");
|
||||
sendValue("Debug" ,"PONG!");
|
||||
}
|
||||
|
||||
void defineInterface() {
|
||||
// Définit les actions qu’un utilisateur peut exécuter
|
||||
saveFunc("PING", ping, "debugval");
|
||||
}
|
||||
|
||||
// -----------------------------------------------------------------------
|
||||
// Communication avec le serveur
|
||||
// -----------------------------------------------------------------------
|
||||
void login() {
|
||||
Serial.println("[DEBUG] Envoi LOGIN...");
|
||||
webSocket.sendTXT("LOGIN$" + mac + "$"+name+"$"+place);
|
||||
}
|
||||
|
||||
void saveFunc(String interfaceName, CommandFunction func, String params) {
|
||||
Serial.printf("[DEBUG] Envoi interface '%s' avec params: %s\n",
|
||||
interfaceName.c_str(), params.c_str());
|
||||
commandMap[interfaceName] = func;
|
||||
webSocket.sendTXT("REGISTER$" + interfaceName + "$" + params);
|
||||
}
|
||||
|
||||
void saveFunc(String interfaceName, CommandFunction func) {
|
||||
Serial.printf("[DEBUG] Envoi interface '%s' sans params\n", interfaceName.c_str());
|
||||
commandMap[interfaceName] = func;
|
||||
webSocket.sendTXT("REGISTER$" + interfaceName + "$");
|
||||
}
|
||||
|
||||
void sendValue(String valueName, String value) {
|
||||
webSocket.sendTXT("VALUE$" + valueName + "$" + value);
|
||||
}
|
||||
|
||||
// -----------------------------------------------------------------------
|
||||
// Boucle principale
|
||||
// -----------------------------------------------------------------------
|
||||
void loop() {
|
||||
webSocket.loop();
|
||||
}
|
||||
|
|
@ -0,0 +1,11 @@
|
|||
|
||||
This directory is intended for PlatformIO Test Runner and project tests.
|
||||
|
||||
Unit Testing is a software testing method by which individual units of
|
||||
source code, sets of one or more MCU program modules together with associated
|
||||
control data, usage procedures, and operating procedures, are tested to
|
||||
determine whether they are fit for use. Unit testing finds problems early
|
||||
in the development cycle.
|
||||
|
||||
More information about PlatformIO Unit Testing:
|
||||
- https://docs.platformio.org/en/latest/advanced/unit-testing/index.html
|
||||
Loading…
Reference in New Issue