fin du poc mantenant c'est tout droit

This commit is contained in:
Clement 2023-03-27 17:18:54 +02:00
parent 212f662882
commit 5cad486150
2 changed files with 124 additions and 70 deletions

View File

@ -36,7 +36,8 @@ monitor_flags =
; librairies ; librairies
lib_deps = lib_deps =
bportaluri/WiFiEsp@^2.2.2 bportaluri/WiFiEsp@^2.2.2 ; gestion des commande 'AT' de l'esp01
arduino-libraries/Arduino_JSON@^0.2.0 ; gestion des json
; example: ; example:
; erropix/ESP32 AnalogWrite@^0.2 ; erropix/ESP32 AnalogWrite@^0.2

View File

@ -7,7 +7,8 @@
For more details see: http://yaab-arduino.blogspot.com/p/wifiesp-example-client.html For more details see: http://yaab-arduino.blogspot.com/p/wifiesp-example-client.html
*/ */
#include "WiFiEsp.h" #include <WiFiEsp.h>
#include <Arduino_JSON.h>
// Emulate Serial1 on pins 6/7 if not present // Emulate Serial1 on pins 6/7 if not present
#ifndef HAVE_HWSERIAL1 #ifndef HAVE_HWSERIAL1
@ -24,10 +25,12 @@ int status = WL_IDLE_STATUS; // the Wifi radio's status
char server[] = "iot.epi.cb85.software"; char server[] = "iot.epi.cb85.software";
String tocken = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJjb2xsZWN0aW9uSWQiOiJfcGJfdXNlcnNfYXV0aF8iLCJleHAiOjE2ODExMzM4NDMsImlkIjoiZnNlNm9uZ3AxNHN5bHVvIiwidHlwZSI6ImF1dGhSZWNvcmQifQ.QKvfyAQ-lxLHTLyZIQ0FrMYFn5XPTujY_N1Oz6BRHn4"; String token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJjb2xsZWN0aW9uSWQiOiJfcGJfdXNlcnNfYXV0aF8iLCJleHAiOjE2ODExMzM4NDMsImlkIjoiZnNlNm9uZ3AxNHN5bHVvIiwidHlwZSI6ImF1dGhSZWNvcmQifQ.QKvfyAQ-lxLHTLyZIQ0FrMYFn5XPTujY_N1Oz6BRHn4";
void printWifiStatus(); void printWifiStatus();
JSONVar* connect();
// Initialize the Ethernet client object // Initialize the Ethernet client object
WiFiEspClient client; WiFiEspClient client;
@ -62,6 +65,17 @@ void setup() {
Serial.println(); Serial.println();
Serial.println("Starting connection to server..."); Serial.println("Starting connection to server...");
// if you get a connection, report back via serial // if you get a connection, report back via serial
String temp = JSONVar::stringify((*connect())["token"]);
token = temp.substring(1,temp.length()-1);
Serial.println(token);
String data = "{\"value\": \"123\",\"trash_id\": \"gdnuxl0wlgurtj3\",\"unit\": \"test\",\"status\": true}";
if (client.connectSSL(server, 443)) { if (client.connectSSL(server, 443)) {
Serial.println("Connected to server"); Serial.println("Connected to server");
// Make a HTTP request // Make a HTTP request
@ -69,17 +83,17 @@ void setup() {
// client.println("Host: iot.epi.cb85.software"); // client.println("Host: iot.epi.cb85.software");
// client.println("Connection: close"); // client.println("Connection: close");
// client.println(); // client.println();
String data = "{\"value\": \"123\",\"trash_id\": \"gdnuxl0wlgurtj3\",\"unit\": \"test\",\"status\": true}";
client.println("POST /api/collections/data/records HTTP/1.1"); client.println("POST /api/collections/data/records HTTP/1.1");
client.println("Host: iot.epi.cb85.software"); client.println("Host: iot.epi.cb85.software");
client.println("Content-Type: application/json"); client.println("Content-Type: application/json");
client.println("Content-Length: " + String(data.length())); client.println("Content-Length: " + String(data.length()));
client.println("Authorization: Bearer "+tocken); client.println("Authorization: Bearer " + token);
client.println(); client.println();
client.println(data); client.println(data);
client.println("Connection: close"); client.println("Connection: close");
client.println(); client.println();
Serial.print("POUET");
} }
} }
@ -105,6 +119,45 @@ void loop() {
} }
} }
JSONVar* connect() {
String data = "{\"identity\":\"patrick\",\"password\":\"Patrick123\"}";
JSONVar* json = new JSONVar();
if (!client.connectSSL(server, 443)) {
Serial.println("NOT Connected to server");
return json;
}
client.println("POST /api/collections/users/auth-with-password HTTP/1.1");
client.println("Host: iot.epi.cb85.software");
client.println("Content-Type: application/json");
client.println("Content-Length: " + String(data.length()));
client.println();
client.println(data);
client.println("Connection: close");
client.println();
while (!client.available()) {
}
String responce = "";
while (client.available()) {
responce += (char)client.read();
}
Serial.println("---------------");
Serial.println(responce);
Serial.println("---------------");
String sortie = responce.substring(responce.indexOf("{"),responce.lastIndexOf("}")+1);
*json = JSON.parse(sortie);
return json;
}
void printWifiStatus() { void printWifiStatus() {
// print the SSID of the network you're attached to // print the SSID of the network you're attached to