Created
January 21, 2017 08:44
-
-
Save bleader/76a0c5fef8bf0937bbec24a1255c85d3 to your computer and use it in GitHub Desktop.
simple esp32 AP/client code for testing range
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <Arduino.h> | |
#include <WiFi.h> | |
#define AP 1 | |
#ifdef AP | |
void setup() | |
{ | |
IPAddress ip; | |
Serial.begin(115200); | |
Serial.println(); | |
delay(10); | |
Serial.print("Setting soft-AP ... "); | |
boolean result = WiFi.softAP("Chrono", "chrono-slalom"); | |
if(result == true) | |
{ | |
Serial.println("Ready"); | |
} | |
else | |
{ | |
Serial.println("Failed!"); | |
} | |
pinMode(2, OUTPUT); | |
digitalWrite(2, LOW); | |
ip = WiFi.softAPIP(); | |
Serial.println(ip); | |
} | |
void loop() | |
{ | |
int nb = WiFi.softAPgetStationNum(); | |
if (nb > 0) | |
digitalWrite(2, HIGH); | |
else | |
digitalWrite(2, LOW); | |
Serial.print("Stations connected = "); | |
Serial.println(WiFi.softAPgetStationNum()); | |
delay(3000); | |
} | |
#else | |
void setup() | |
{ | |
Serial.begin(115200); | |
Serial.println(); | |
delay(10); | |
Serial.println("Connecting to AP..."); | |
WiFi.mode(WIFI_STA); | |
WiFi.begin("Chrono", "chrono-slalom"); | |
while (WiFi.status() != WL_CONNECTED) { | |
delay(500); | |
Serial.print("."); | |
} | |
Serial.println(" Connected"); | |
WiFi.setAutoConnect(true); | |
pinMode(2, OUTPUT); | |
digitalWrite(2, LOW); | |
} | |
void loop() | |
{ | |
uint32_t m = millis(); | |
Serial.print(m); | |
if (WiFi.isConnected()) { | |
Serial.println(" - connected"); | |
digitalWrite(2, HIGH); | |
} | |
else { | |
Serial.println(" - not connected"); | |
digitalWrite(2, LOW); | |
Serial.println(" Trying to reconnec..."); | |
WiFi.reconnect(); | |
} | |
delay(1000); | |
} | |
#endif |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment