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,101 +25,153 @@ 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;
void setup() { void setup() {
// initialize serial for debugging // initialize serial for debugging
Serial.begin(115200); Serial.begin(115200);
// initialize serial for ESP module // initialize serial for ESP module
Serial1.begin(115200); Serial1.begin(115200);
// initialize ESP module // initialize ESP module
WiFi.init(&Serial1); WiFi.init(&Serial1);
// check for the presence of the shield // check for the presence of the shield
if (WiFi.status() == WL_NO_SHIELD) { if (WiFi.status() == WL_NO_SHIELD) {
Serial.println("WiFi shield not present"); Serial.println("WiFi shield not present");
// don't continue // don't continue
while (true); while (true);
} }
// attempt to connect to WiFi network // attempt to connect to WiFi network
while (status != WL_CONNECTED) { while (status != WL_CONNECTED) {
Serial.print("Attempting to connect to WPA SSID: "); Serial.print("Attempting to connect to WPA SSID: ");
Serial.println(ssid); Serial.println(ssid);
// Connect to WPA/WPA2 network // Connect to WPA/WPA2 network
status = WiFi.begin(ssid, pass); status = WiFi.begin(ssid, pass);
} }
// you're connected now, so print out the data // you're connected now, so print out the data
Serial.println("You're connected to the network"); Serial.println("You're connected to the network");
printWifiStatus(); printWifiStatus();
Serial.println();
Serial.println("Starting connection to server...");
// 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);
Serial.println();
Serial.println("Starting connection to server...");
// if you get a connection, report back via serial
if (client.connectSSL(server, 443)) {
Serial.println("Connected to server");
// Make a HTTP request
// client.println("GET /api/collections/poubelle_status/records HTTP/1.1");
// client.println("Host: iot.epi.cb85.software");
// client.println("Connection: close");
// client.println();
String data = "{\"value\": \"123\",\"trash_id\": \"gdnuxl0wlgurtj3\",\"unit\": \"test\",\"status\": true}"; String data = "{\"value\": \"123\",\"trash_id\": \"gdnuxl0wlgurtj3\",\"unit\": \"test\",\"status\": true}";
client.println("POST /api/collections/data/records HTTP/1.1");
client.println("Host: iot.epi.cb85.software");
client.println("Content-Type: application/json"); if (client.connectSSL(server, 443)) {
client.println("Content-Length: " + String(data.length())); Serial.println("Connected to server");
client.println("Authorization: Bearer "+tocken); // Make a HTTP request
client.println(); // client.println("GET /api/collections/poubelle_status/records HTTP/1.1");
client.println(data); // client.println("Host: iot.epi.cb85.software");
client.println("Connection: close"); // client.println("Connection: close");
client.println(); // client.println();
} client.println("POST /api/collections/data/records 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("Authorization: Bearer " + token);
client.println();
client.println(data);
client.println("Connection: close");
client.println();
Serial.print("POUET");
}
} }
void loop() { void loop() {
// if there are incoming bytes available // if there are incoming bytes available
// from the server, read them and print them // from the server, read them and print them
if (!client.available()) { if (!client.available()) {
return; return;
} }
while (client.available()) { while (client.available()) {
char c = client.read(); char c = client.read();
Serial.write(c); Serial.write(c);
} }
// if the server's disconnected, stop the client // if the server's disconnected, stop the client
if (!client.connected()) { if (!client.connected()) {
Serial.println(); Serial.println();
Serial.println("Disconnecting from server..."); Serial.println("Disconnecting from server...");
client.stop(); client.stop();
// do nothing forevermore // do nothing forevermore
while (true); while (true);
} }
}
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
Serial.print("SSID: "); Serial.print("SSID: ");
Serial.println(WiFi.SSID()); Serial.println(WiFi.SSID());
// print your WiFi shield's IP address // print your WiFi shield's IP address
IPAddress ip = WiFi.localIP(); IPAddress ip = WiFi.localIP();
Serial.print("IP Address: "); Serial.print("IP Address: ");
Serial.println(ip); Serial.println(ip);
// print the received signal strength // print the received signal strength
long rssi = WiFi.RSSI(); long rssi = WiFi.RSSI();
Serial.print("Signal strength (RSSI):"); Serial.print("Signal strength (RSSI):");
Serial.print(rssi); Serial.print(rssi);
Serial.println(" dBm"); Serial.println(" dBm");
} }