diff --git a/IOT/platformio.ini b/IOT/platformio.ini index 82e4637..19aa04e 100644 --- a/IOT/platformio.ini +++ b/IOT/platformio.ini @@ -36,6 +36,7 @@ monitor_flags = ; librairies lib_deps = + bportaluri/WiFiEsp@^2.2.2 ; example: ; erropix/ESP32 AnalogWrite@^0.2 diff --git a/IOT/src/main.cpp b/IOT/src/main.cpp index cff0213..d71f9e9 100644 --- a/IOT/src/main.cpp +++ b/IOT/src/main.cpp @@ -1,12 +1,109 @@ -#include -#include "Program.h" +// #include +// #include "Program.h" -Program* program; +// Program* program; + +// void setup() { +// program = new Program(); +// } + +// void loop() { +// program->loop(); +// } + +#include + +#include + +const char ssid[] = "Clement4G"; // your network SSID (name) +const char pass[] = "Clement123"; // your network password + +int status = WL_IDLE_STATUS; // the Wifi radio's status + +char server[] = "arduino.cc"; + +// Initialize the Ethernet client object +WiFiEspClient client; void setup() { - program = new Program(); + // 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); + } + + Serial.println("You're connected to the network"); + + + Serial.println(); + Serial.println("Starting connection to server..."); + // if you get a connection, report back via serial + if (client.connect(server, 80)) { + Serial.println("Connected to server"); + // Make a HTTP request + client.println("GET /asciilogo.txt HTTP/1.1"); + client.println("Host: arduino.cc"); + client.println("Connection: close"); + client.println(); + Serial.print("POUET"); + } else { + Serial.print(client.status()); + } +} + +void printWifiData() { + // print your WiFi shield's IP address + IPAddress ip = WiFi.localIP(); + Serial.print("IP Address: "); + Serial.println(ip); + + // print your MAC address + byte mac[6]; + WiFi.macAddress(mac); + char buf[20]; + sprintf(buf, "%02X:%02X:%02X:%02X:%02X:%02X", mac[5], mac[4], mac[3], mac[2], mac[1], mac[0]); + Serial.print("MAC address: "); + Serial.println(buf); } void loop() { - program->loop(); + // print the network connection information every 10 seconds + //Serial.println(); + printWifiData(); + + // if there are incoming bytes available + // from the server, read them and print them + Serial.println(client.status()); + 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); + } + + //delay(10000); } \ No newline at end of file