From 5067438b9d79a94635a1fbb403707153c34acf55 Mon Sep 17 00:00:00 2001 From: Florian Hoss Date: Thu, 21 Jul 2022 13:34:24 +0200 Subject: [PATCH] Basic wifi setup for esp32 and esp8266 --- ESPTest/platformio.ini | 9 ++++- ESPTest/src/main.cpp | 91 ++++++++++++++++-------------------------- 2 files changed, 42 insertions(+), 58 deletions(-) diff --git a/ESPTest/platformio.ini b/ESPTest/platformio.ini index 2fda6eb..f69077e 100644 --- a/ESPTest/platformio.ini +++ b/ESPTest/platformio.ini @@ -9,10 +9,17 @@ ; https://docs.platformio.org/page/projectconf.html [platformio] -default_envs = esp32 +default_envs = esp8266 [env:esp32] platform = espressif32 board = esp32dev framework = arduino monitor_speed = 115200 + +[env:esp8266] +platform = espressif8266 +board = d1_mini +framework = arduino +monitor_speed = 115200 + diff --git a/ESPTest/src/main.cpp b/ESPTest/src/main.cpp index fee8ca1..6cd1b08 100644 --- a/ESPTest/src/main.cpp +++ b/ESPTest/src/main.cpp @@ -1,72 +1,49 @@ #include -#include "WiFi.h" #include "theSecrets.h" -#include "freertos/FreeRTOS.h" -#include "freertos/task.h" -#include "esp_wifi.h" -#include "esp_event.h" -#include "nvs_flash.h" -#include "lwip/err.h" -#include "lwip/sockets.h" -#include "lwip/sys.h" -#include +#ifdef ESP32 +#include "WiFi.h" +#elif defined(ESP8266) +#include "ESP8266WiFi.h" +#define LED 2 +#define DELAY 2000 +#endif -void connectToWifi() +void toggleLED() { - WiFi.begin(ssid, psk); - while (WiFi.status() != WL_CONNECTED) - { - delay(200); - } -} - -void handle_wifi_events(WiFiEvent_t event, WiFiEventInfo_t info) -{ - Serial.print("[WiFi] "); - switch (event) - { - case ARDUINO_EVENT_WIFI_STA_START: - Serial.printf("Attempting to connect to WPA SSID: %s\n", ssid); - break; - case ARDUINO_EVENT_WIFI_STA_CONNECTED: - Serial.println("connected"); - break; - case ARDUINO_EVENT_WIFI_STA_DISCONNECTED: - Serial.println("disconnected"); - break; - case ARDUINO_EVENT_WIFI_STA_GOT_IP: - Serial.print("IP address: "); - Serial.println(WiFi.localIP()); - break; - default: - Serial.println(event); - } -} - -void setup_wifi() -{ - WiFi.mode(WIFI_STA); - WiFi.onEvent(handle_wifi_events, ARDUINO_EVENT_MAX); - connectToWifi(); -} - -static void intercept_task(void *pvParameters) -{ - Serial.println("Intercept"); - vTaskDelete(NULL); + if (digitalRead(LED) == LOW) + digitalWrite(LED, HIGH); + else + digitalWrite(LED, LOW); } void setup() { - ESP_ERROR_CHECK(nvs_flash_init()); - ESP_ERROR_CHECK(esp_netif_init()); - ESP_ERROR_CHECK(esp_event_loop_create_default()); +#ifdef ESP8266 + pinMode(LED, OUTPUT); +#endif Serial.begin(115200); - setup_wifi(); - xTaskCreate(intercept_task, "intercept", 4096, (void *)AF_INET, 5, NULL); + Serial.println(); + WiFi.begin(ssid, psk); + while (!WiFi.isConnected()) + { +#ifdef ESP8266 + toggleLED(); +#endif + Serial.print("."); + delay(200); + } + Serial.println(); + Serial.print("IP: "); + Serial.println(WiFi.localIP()); } void loop() { +#ifdef ESP8266 + digitalWrite(LED, LOW); + delay(DELAY); + digitalWrite(LED, HIGH); + delay(DELAY); +#endif } \ No newline at end of file