Files
T-DEV-811/IOT/src/main.cpp
2023-03-20 10:23:34 +01:00

120 lines
3.2 KiB
C++

/*
WiFiEsp example: WebClient
This sketch connects to google website using an ESP8266 module to
perform a simple web search.
For more details see: http://yaab-arduino.blogspot.com/p/wifiesp-example-client.html
*/
#include "WiFiEsp.h"
// Emulate Serial1 on pins 6/7 if not present
#ifndef HAVE_HWSERIAL1
#include "SoftwareSerial.h"
SoftwareSerial Serial1(6, 7); // RX, TX
#endif
char ssid[] = "Clement4G"; // TODO: mettre id wifi dans config.ini
char pass[] = "***"; // TODO: mettre pass wifi dans config.ini
int status = WL_IDLE_STATUS; // the Wifi radio's status
//char server[] = "arduino.tips";
char server[] = "iot2.epi.cb85.software";
void printWifiStatus();
// 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);
// 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);
}
// you're connected now, so print out the data
Serial.println("You're connected to the network");
printWifiStatus();
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: iot2.epi.cb85.software");
// client.println("Connection: close");
// client.println();
String data = "{\"value\": \"test123\"}";
client.println("POST /api/collections/poubelle_status/records HTTP/1.1");
client.println("Host: iot2.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();
}
}
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 the server's disconnected, stop the client
if (!client.connected()) {
Serial.println();
Serial.println("Disconnecting from server...");
client.stop();
// do nothing forevermore
while (true);
}
}
void printWifiStatus() {
// print the SSID of the network you're attached to
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 the received signal strength
long rssi = WiFi.RSSI();
Serial.print("Signal strength (RSSI):");
Serial.print(rssi);
Serial.println(" dBm");
}