Skip to content

Instantly share code, notes, and snippets.

@yaqwsx
Last active February 26, 2024 09:34
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yaqwsx/ac662c9b600ef39a802da0be1b25d32d to your computer and use it in GitHub Desktop.
Save yaqwsx/ac662c9b600ef39a802da0be1b25d32d to your computer and use it in GitHub Desktop.
Simple test for ESP32 WiFi range
#include <Arduino.h>
#include <WiFi.h>
#include <WiFiUdp.h>
#include <esp_wifi.h>
#include "peripherals.h"
#include "credentials.h"
Led redLed( GPIO_NUM_19 );
Led greenLed( GPIO_NUM_21 );
WiFiUDP udp;
const char *toStr( wl_status_t status ) {
switch( status ) {
case WL_NO_SHIELD: return "No shield";
case WL_IDLE_STATUS: return "Idle status";
case WL_NO_SSID_AVAIL: return "No SSID avail";
case WL_SCAN_COMPLETED: return "Scan compleded";
case WL_CONNECTED: return "Connected";
case WL_CONNECT_FAILED: return "Failed";
case WL_CONNECTION_LOST: return "Connection lost";
case WL_DISCONNECTED: return "Disconnected";
}
return "Unknown";
}
void setup() {
Serial.begin( 115200 );
Serial.println( "Slave" );
WiFi.begin();
delay( 500 );
ESP_ERROR_CHECK( esp_wifi_set_protocol( WIFI_IF_STA, WIFI_PROTOCOL_LR ) );
WiFi.mode( WIFI_STA );
udp.begin( 8888 );
}
void loop() {
if ( WiFi.status() != WL_CONNECTED ) {
Serial.println( "|" );
int tries = 0;
WiFi.begin( ssid, password );
while( WiFi.status() != WL_CONNECTED ) {
tries++;
if ( tries == 5 )
return;
Serial.println( toStr( WiFi.status() ) );
greenLed.set();
delay( 10 );
greenLed.reset();
delay( 500 );
}
Serial.print( "Connected " );
Serial.println( WiFi.localIP() );
}
int size = udp.parsePacket();
if ( size == 0 )
return;
char c = udp.read();
if ( c == 'b' )
greenLed.invert();
udp.flush();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment