Basic wifi setup for esp32 and esp8266
This commit is contained in:
parent
798f0dec03
commit
5067438b9d
2 changed files with 42 additions and 58 deletions
|
@ -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
|
||||
|
||||
|
|
|
@ -1,72 +1,49 @@
|
|||
#include <Arduino.h>
|
||||
#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 <lwip/netdb.h>
|
||||
#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
|
||||
}
|
Reference in a new issue