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
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:
; 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
*/
#include "WiFiEsp.h"
#include <WiFiEsp.h>
#include <Arduino_JSON.h>
// Emulate Serial1 on pins 6/7 if not present
#ifndef HAVE_HWSERIAL1
@ -24,101 +25,153 @@ int status = WL_IDLE_STATUS; // the Wifi radio's status
char server[] = "iot.epi.cb85.software";
String tocken = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJjb2xsZWN0aW9uSWQiOiJfcGJfdXNlcnNfYXV0aF8iLCJleHAiOjE2ODExMzM4NDMsImlkIjoiZnNlNm9uZ3AxNHN5bHVvIiwidHlwZSI6ImF1dGhSZWNvcmQifQ.QKvfyAQ-lxLHTLyZIQ0FrMYFn5XPTujY_N1Oz6BRHn4";
String token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJjb2xsZWN0aW9uSWQiOiJfcGJfdXNlcnNfYXV0aF8iLCJleHAiOjE2ODExMzM4NDMsImlkIjoiZnNlNm9uZ3AxNHN5bHVvIiwidHlwZSI6ImF1dGhSZWNvcmQifQ.QKvfyAQ-lxLHTLyZIQ0FrMYFn5XPTujY_N1Oz6BRHn4";
void printWifiStatus();
JSONVar* connect();
// Initialize the Ethernet client object
WiFiEspClient client;
void setup() {
// initialize serial for debugging
Serial.begin(115200);
// initialize serial for ESP module
Serial1.begin(115200);
// initialize ESP module
WiFi.init(&Serial1);
Serial.begin(115200);
// initialize serial for ESP module
Serial1.begin(115200);
// initialize ESP module
WiFi.init(&Serial1);
// check for the presence of the shield
if (WiFi.status() == WL_NO_SHIELD) {
Serial.println("WiFi shield not present");
// don't continue
while (true);
}
// check for the presence of the shield
if (WiFi.status() == WL_NO_SHIELD) {
Serial.println("WiFi shield not present");
// don't continue
while (true);
}
// attempt to connect to WiFi network
while (status != WL_CONNECTED) {
Serial.print("Attempting to connect to WPA SSID: ");
Serial.println(ssid);
// Connect to WPA/WPA2 network
status = WiFi.begin(ssid, pass);
}
// attempt to connect to WiFi network
while (status != WL_CONNECTED) {
Serial.print("Attempting to connect to WPA SSID: ");
Serial.println(ssid);
// Connect to WPA/WPA2 network
status = WiFi.begin(ssid, pass);
}
// you're connected now, so print out the data
Serial.println("You're connected to the network");
// you're connected now, so print out the data
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}";
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 "+tocken);
client.println();
client.println(data);
client.println("Connection: close");
client.println();
}
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();
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() {
// if there are incoming bytes available
// from the server, read them and print them
if (!client.available()) {
return;
}
while (client.available()) {
char c = client.read();
Serial.write(c);
}
if (!client.available()) {
return;
}
while (client.available()) {
char c = client.read();
Serial.write(c);
}
// if the server's disconnected, stop the client
if (!client.connected()) {
Serial.println();
Serial.println("Disconnecting from server...");
client.stop();
// if the server's disconnected, stop the client
if (!client.connected()) {
Serial.println();
Serial.println("Disconnecting from server...");
client.stop();
// do nothing forevermore
while (true);
}
// do nothing forevermore
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() {
// print the SSID of the network you're attached to
Serial.print("SSID: ");
Serial.println(WiFi.SSID());
Serial.print("SSID: ");
Serial.println(WiFi.SSID());
// print your WiFi shield's IP address
IPAddress ip = WiFi.localIP();
Serial.print("IP Address: ");
Serial.println(ip);
// print your WiFi shield's IP address
IPAddress ip = WiFi.localIP();
Serial.print("IP Address: ");
Serial.println(ip);
// print the received signal strength
long rssi = WiFi.RSSI();
Serial.print("Signal strength (RSSI):");
Serial.print(rssi);
Serial.println(" dBm");
// print the received signal strength
long rssi = WiFi.RSSI();
Serial.print("Signal strength (RSSI):");
Serial.print(rssi);
Serial.println(" dBm");
}