mirror of
https://gitlab.com/wirelos/sprocket-plugin-gpio.git
synced 2025-12-16 14:05:05 +01:00
pot plugin w. example
This commit is contained in:
@@ -0,0 +1,116 @@
|
||||
/*
|
||||
WiFi Web Server LED Blink
|
||||
|
||||
A simple web server that lets you blink an LED via the web.
|
||||
This sketch will print the IP address of your WiFi Shield (once connected)
|
||||
to the Serial monitor. From there, you can open that address in a web browser
|
||||
to turn on and off the LED on pin 5.
|
||||
|
||||
If the IP address of your shield is yourAddress:
|
||||
http://yourAddress/H turns the LED on
|
||||
http://yourAddress/L turns it off
|
||||
|
||||
This example is written for a network using WPA encryption. For
|
||||
WEP or WPA, change the Wifi.begin() call accordingly.
|
||||
|
||||
Circuit:
|
||||
* WiFi shield attached
|
||||
* LED attached to pin 5
|
||||
|
||||
created for arduino 25 Nov 2012
|
||||
by Tom Igoe
|
||||
|
||||
ported for sparkfun esp32
|
||||
31.01.2017 by Jan Hendrik Berlin
|
||||
|
||||
*/
|
||||
|
||||
#include <WiFi.h>
|
||||
|
||||
const char* ssid = "yourssid";
|
||||
const char* password = "yourpasswd";
|
||||
|
||||
WiFiServer server(80);
|
||||
|
||||
void setup()
|
||||
{
|
||||
Serial.begin(115200);
|
||||
pinMode(5, OUTPUT); // set the LED pin mode
|
||||
|
||||
delay(10);
|
||||
|
||||
// We start by connecting to a WiFi network
|
||||
|
||||
Serial.println();
|
||||
Serial.println();
|
||||
Serial.print("Connecting to ");
|
||||
Serial.println(ssid);
|
||||
|
||||
WiFi.begin(ssid, password);
|
||||
|
||||
while (WiFi.status() != WL_CONNECTED) {
|
||||
delay(500);
|
||||
Serial.print(".");
|
||||
}
|
||||
|
||||
Serial.println("");
|
||||
Serial.println("WiFi connected");
|
||||
Serial.println("IP address: ");
|
||||
Serial.println(WiFi.localIP());
|
||||
|
||||
server.begin();
|
||||
|
||||
}
|
||||
|
||||
int value = 0;
|
||||
|
||||
void loop(){
|
||||
WiFiClient client = server.available(); // listen for incoming clients
|
||||
|
||||
if (client) { // if you get a client,
|
||||
Serial.println("new client"); // print a message out the serial port
|
||||
String currentLine = ""; // make a String to hold incoming data from the client
|
||||
while (client.connected()) { // loop while the client's connected
|
||||
if (client.available()) { // if there's bytes to read from the client,
|
||||
char c = client.read(); // read a byte, then
|
||||
Serial.write(c); // print it out the serial monitor
|
||||
if (c == '\n') { // if the byte is a newline character
|
||||
|
||||
// if the current line is blank, you got two newline characters in a row.
|
||||
// that's the end of the client HTTP request, so send a response:
|
||||
if (currentLine.length() == 0) {
|
||||
// HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK)
|
||||
// and a content-type so the client knows what's coming, then a blank line:
|
||||
client.println("HTTP/1.1 200 OK");
|
||||
client.println("Content-type:text/html");
|
||||
client.println();
|
||||
|
||||
// the content of the HTTP response follows the header:
|
||||
client.print("Click <a href=\"/H\">here</a> turn the LED on pin 5 on<br>");
|
||||
client.print("Click <a href=\"/L\">here</a> turn the LED on pin 5 off<br>");
|
||||
|
||||
// The HTTP response ends with another blank line:
|
||||
client.println();
|
||||
// break out of the while loop:
|
||||
break;
|
||||
} else { // if you got a newline, then clear currentLine:
|
||||
currentLine = "";
|
||||
}
|
||||
} else if (c != '\r') { // if you got anything else but a carriage return character,
|
||||
currentLine += c; // add it to the end of the currentLine
|
||||
}
|
||||
|
||||
// Check to see if the client request was "GET /H" or "GET /L":
|
||||
if (currentLine.endsWith("GET /H")) {
|
||||
digitalWrite(5, HIGH); // GET /H turns the LED on
|
||||
}
|
||||
if (currentLine.endsWith("GET /L")) {
|
||||
digitalWrite(5, LOW); // GET /L turns the LED off
|
||||
}
|
||||
}
|
||||
}
|
||||
// close the connection:
|
||||
client.stop();
|
||||
Serial.println("client disonnected");
|
||||
}
|
||||
}
|
||||
94
lib/esp32_lora_wifi/WiFi/examples/WiFiClient/WiFiClient.ino
Normal file
94
lib/esp32_lora_wifi/WiFi/examples/WiFiClient/WiFiClient.ino
Normal file
@@ -0,0 +1,94 @@
|
||||
/*
|
||||
* This sketch sends data via HTTP GET requests to data.sparkfun.com service.
|
||||
*
|
||||
* You need to get streamId and privateKey at data.sparkfun.com and paste them
|
||||
* below. Or just customize this script to talk to other HTTP servers.
|
||||
*
|
||||
*/
|
||||
|
||||
#include <WiFi.h>
|
||||
|
||||
const char* ssid = "your-ssid";
|
||||
const char* password = "your-password";
|
||||
|
||||
const char* host = "data.sparkfun.com";
|
||||
const char* streamId = "....................";
|
||||
const char* privateKey = "....................";
|
||||
|
||||
void setup()
|
||||
{
|
||||
Serial.begin(115200);
|
||||
delay(10);
|
||||
|
||||
// We start by connecting to a WiFi network
|
||||
|
||||
Serial.println();
|
||||
Serial.println();
|
||||
Serial.print("Connecting to ");
|
||||
Serial.println(ssid);
|
||||
|
||||
WiFi.begin(ssid, password);
|
||||
|
||||
while (WiFi.status() != WL_CONNECTED) {
|
||||
delay(500);
|
||||
Serial.print(".");
|
||||
}
|
||||
|
||||
Serial.println("");
|
||||
Serial.println("WiFi connected");
|
||||
Serial.println("IP address: ");
|
||||
Serial.println(WiFi.localIP());
|
||||
}
|
||||
|
||||
int value = 0;
|
||||
|
||||
void loop()
|
||||
{
|
||||
delay(5000);
|
||||
++value;
|
||||
|
||||
Serial.print("connecting to ");
|
||||
Serial.println(host);
|
||||
|
||||
// Use WiFiClient class to create TCP connections
|
||||
WiFiClient client;
|
||||
const int httpPort = 80;
|
||||
if (!client.connect(host, httpPort)) {
|
||||
Serial.println("connection failed");
|
||||
return;
|
||||
}
|
||||
|
||||
// We now create a URI for the request
|
||||
String url = "/input/";
|
||||
url += streamId;
|
||||
url += "?private_key=";
|
||||
url += privateKey;
|
||||
url += "&value=";
|
||||
url += value;
|
||||
|
||||
Serial.print("Requesting URL: ");
|
||||
Serial.println(url);
|
||||
|
||||
// This will send the request to the server
|
||||
client.print(String("GET ") + url + " HTTP/1.1\r\n" +
|
||||
"Host: " + host + "\r\n" +
|
||||
"Connection: close\r\n\r\n");
|
||||
unsigned long timeout = millis();
|
||||
while (client.available() == 0) {
|
||||
if (millis() - timeout > 5000) {
|
||||
Serial.println(">>> Client Timeout !");
|
||||
client.stop();
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
// Read all the lines of the reply from server and print them to Serial
|
||||
while(client.available()) {
|
||||
String line = client.readStringUntil('\r');
|
||||
Serial.print(line);
|
||||
}
|
||||
|
||||
Serial.println();
|
||||
Serial.println("closing connection");
|
||||
}
|
||||
|
||||
@@ -0,0 +1,70 @@
|
||||
/*
|
||||
* This sketch sends a message to a TCP server
|
||||
*
|
||||
*/
|
||||
|
||||
#include <WiFi.h>
|
||||
#include <WiFiMulti.h>
|
||||
|
||||
WiFiMulti WiFiMulti;
|
||||
|
||||
void setup()
|
||||
{
|
||||
Serial.begin(115200);
|
||||
delay(10);
|
||||
|
||||
// We start by connecting to a WiFi network
|
||||
WiFiMulti.addAP("SSID", "passpasspass");
|
||||
|
||||
Serial.println();
|
||||
Serial.println();
|
||||
Serial.print("Wait for WiFi... ");
|
||||
|
||||
while(WiFiMulti.run() != WL_CONNECTED) {
|
||||
Serial.print(".");
|
||||
delay(500);
|
||||
}
|
||||
|
||||
Serial.println("");
|
||||
Serial.println("WiFi connected");
|
||||
Serial.println("IP address: ");
|
||||
Serial.println(WiFi.localIP());
|
||||
|
||||
delay(500);
|
||||
}
|
||||
|
||||
|
||||
void loop()
|
||||
{
|
||||
const uint16_t port = 80;
|
||||
const char * host = "192.168.1.1"; // ip or dns
|
||||
|
||||
|
||||
|
||||
Serial.print("connecting to ");
|
||||
Serial.println(host);
|
||||
|
||||
// Use WiFiClient class to create TCP connections
|
||||
WiFiClient client;
|
||||
|
||||
if (!client.connect(host, port)) {
|
||||
Serial.println("connection failed");
|
||||
Serial.println("wait 5 sec...");
|
||||
delay(5000);
|
||||
return;
|
||||
}
|
||||
|
||||
// This will send the request to the server
|
||||
client.print("Send this data to server");
|
||||
|
||||
//read back one line from server
|
||||
String line = client.readStringUntil('\r');
|
||||
client.println(line);
|
||||
|
||||
Serial.println("closing connection");
|
||||
client.stop();
|
||||
|
||||
Serial.println("wait 5 sec...");
|
||||
delay(5000);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,51 @@
|
||||
/*
|
||||
* This sketch shows the WiFi event usage
|
||||
*
|
||||
*/
|
||||
|
||||
#include <WiFi.h>
|
||||
|
||||
const char* ssid = "your-ssid";
|
||||
const char* password = "your-password";
|
||||
|
||||
|
||||
void WiFiEvent(WiFiEvent_t event)
|
||||
{
|
||||
Serial.printf("[WiFi-event] event: %d\n", event);
|
||||
|
||||
switch(event) {
|
||||
case SYSTEM_EVENT_STA_GOT_IP:
|
||||
Serial.println("WiFi connected");
|
||||
Serial.println("IP address: ");
|
||||
Serial.println(WiFi.localIP());
|
||||
break;
|
||||
case SYSTEM_EVENT_STA_DISCONNECTED:
|
||||
Serial.println("WiFi lost connection");
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void setup()
|
||||
{
|
||||
Serial.begin(115200);
|
||||
|
||||
// delete old config
|
||||
WiFi.disconnect(true);
|
||||
|
||||
delay(1000);
|
||||
|
||||
WiFi.onEvent(WiFiEvent);
|
||||
|
||||
WiFi.begin(ssid, password);
|
||||
|
||||
Serial.println();
|
||||
Serial.println();
|
||||
Serial.println("Wait for WiFi... ");
|
||||
}
|
||||
|
||||
|
||||
void loop()
|
||||
{
|
||||
delay(1000);
|
||||
}
|
||||
|
||||
119
lib/esp32_lora_wifi/WiFi/examples/WiFiIPv6/WiFiIPv6.ino
Normal file
119
lib/esp32_lora_wifi/WiFi/examples/WiFiIPv6/WiFiIPv6.ino
Normal file
@@ -0,0 +1,119 @@
|
||||
#include "WiFi.h"
|
||||
|
||||
#define STA_SSID "**********"
|
||||
#define STA_PASS "**********"
|
||||
#define AP_SSID "esp32-v6"
|
||||
|
||||
static volatile bool wifi_connected = false;
|
||||
|
||||
WiFiUDP ntpClient;
|
||||
|
||||
void wifiOnConnect(){
|
||||
Serial.println("STA Connected");
|
||||
Serial.print("STA IPv4: ");
|
||||
Serial.println(WiFi.localIP());
|
||||
|
||||
ntpClient.begin(2390);
|
||||
}
|
||||
|
||||
void wifiOnDisconnect(){
|
||||
Serial.println("STA Disconnected");
|
||||
delay(1000);
|
||||
WiFi.begin(STA_SSID, STA_PASS);
|
||||
}
|
||||
|
||||
void wifiConnectedLoop(){
|
||||
//lets check the time
|
||||
const int NTP_PACKET_SIZE = 48;
|
||||
byte ntpPacketBuffer[NTP_PACKET_SIZE];
|
||||
|
||||
IPAddress address;
|
||||
WiFi.hostByName("time.nist.gov", address);
|
||||
memset(ntpPacketBuffer, 0, NTP_PACKET_SIZE);
|
||||
ntpPacketBuffer[0] = 0b11100011; // LI, Version, Mode
|
||||
ntpPacketBuffer[1] = 0; // Stratum, or type of clock
|
||||
ntpPacketBuffer[2] = 6; // Polling Interval
|
||||
ntpPacketBuffer[3] = 0xEC; // Peer Clock Precision
|
||||
// 8 bytes of zero for Root Delay & Root Dispersion
|
||||
ntpPacketBuffer[12] = 49;
|
||||
ntpPacketBuffer[13] = 0x4E;
|
||||
ntpPacketBuffer[14] = 49;
|
||||
ntpPacketBuffer[15] = 52;
|
||||
ntpClient.beginPacket(address, 123); //NTP requests are to port 123
|
||||
ntpClient.write(ntpPacketBuffer, NTP_PACKET_SIZE);
|
||||
ntpClient.endPacket();
|
||||
|
||||
delay(1000);
|
||||
|
||||
int packetLength = ntpClient.parsePacket();
|
||||
if (packetLength){
|
||||
if(packetLength >= NTP_PACKET_SIZE){
|
||||
ntpClient.read(ntpPacketBuffer, NTP_PACKET_SIZE);
|
||||
}
|
||||
ntpClient.flush();
|
||||
uint32_t secsSince1900 = (uint32_t)ntpPacketBuffer[40] << 24 | (uint32_t)ntpPacketBuffer[41] << 16 | (uint32_t)ntpPacketBuffer[42] << 8 | ntpPacketBuffer[43];
|
||||
//Serial.printf("Seconds since Jan 1 1900: %u\n", secsSince1900);
|
||||
uint32_t epoch = secsSince1900 - 2208988800UL;
|
||||
//Serial.printf("EPOCH: %u\n", epoch);
|
||||
uint8_t h = (epoch % 86400L) / 3600;
|
||||
uint8_t m = (epoch % 3600) / 60;
|
||||
uint8_t s = (epoch % 60);
|
||||
Serial.printf("UTC: %02u:%02u:%02u (GMT)\n", h, m, s);
|
||||
}
|
||||
|
||||
delay(9000);
|
||||
}
|
||||
|
||||
void WiFiEvent(WiFiEvent_t event){
|
||||
switch(event) {
|
||||
|
||||
case SYSTEM_EVENT_AP_START:
|
||||
//can set ap hostname here
|
||||
WiFi.softAPsetHostname(AP_SSID);
|
||||
//enable ap ipv6 here
|
||||
WiFi.softAPenableIpV6();
|
||||
break;
|
||||
|
||||
case SYSTEM_EVENT_STA_START:
|
||||
//set sta hostname here
|
||||
WiFi.setHostname(AP_SSID);
|
||||
break;
|
||||
case SYSTEM_EVENT_STA_CONNECTED:
|
||||
//enable sta ipv6 here
|
||||
WiFi.enableIpV6();
|
||||
break;
|
||||
case SYSTEM_EVENT_AP_STA_GOT_IP6:
|
||||
//both interfaces get the same event
|
||||
Serial.print("STA IPv6: ");
|
||||
Serial.println(WiFi.localIPv6());
|
||||
Serial.print("AP IPv6: ");
|
||||
Serial.println(WiFi.softAPIPv6());
|
||||
break;
|
||||
case SYSTEM_EVENT_STA_GOT_IP:
|
||||
wifiOnConnect();
|
||||
wifi_connected = true;
|
||||
break;
|
||||
case SYSTEM_EVENT_STA_DISCONNECTED:
|
||||
wifi_connected = false;
|
||||
wifiOnDisconnect();
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void setup(){
|
||||
Serial.begin(115200);
|
||||
WiFi.disconnect(true);
|
||||
WiFi.onEvent(WiFiEvent);
|
||||
WiFi.mode(WIFI_MODE_APSTA);
|
||||
WiFi.softAP(AP_SSID);
|
||||
WiFi.begin(STA_SSID, STA_PASS);
|
||||
}
|
||||
|
||||
void loop(){
|
||||
if(wifi_connected){
|
||||
wifiConnectedLoop();
|
||||
}
|
||||
while(Serial.available()) Serial.write(Serial.read());
|
||||
}
|
||||
35
lib/esp32_lora_wifi/WiFi/examples/WiFiMulti/WiFiMulti.ino
Normal file
35
lib/esp32_lora_wifi/WiFi/examples/WiFiMulti/WiFiMulti.ino
Normal file
@@ -0,0 +1,35 @@
|
||||
/*
|
||||
* This sketch trys to Connect to the best AP based on a given list
|
||||
*
|
||||
*/
|
||||
|
||||
#include <WiFi.h>
|
||||
#include <WiFiMulti.h>
|
||||
|
||||
WiFiMulti wifiMulti;
|
||||
|
||||
void setup()
|
||||
{
|
||||
Serial.begin(115200);
|
||||
delay(10);
|
||||
|
||||
wifiMulti.addAP("ssid_from_AP_1", "your_password_for_AP_1");
|
||||
wifiMulti.addAP("ssid_from_AP_2", "your_password_for_AP_2");
|
||||
wifiMulti.addAP("ssid_from_AP_3", "your_password_for_AP_3");
|
||||
|
||||
Serial.println("Connecting Wifi...");
|
||||
if(wifiMulti.run() == WL_CONNECTED) {
|
||||
Serial.println("");
|
||||
Serial.println("WiFi connected");
|
||||
Serial.println("IP address: ");
|
||||
Serial.println(WiFi.localIP());
|
||||
}
|
||||
}
|
||||
|
||||
void loop()
|
||||
{
|
||||
if(wifiMulti.run() != WL_CONNECTED) {
|
||||
Serial.println("WiFi not connected!");
|
||||
delay(1000);
|
||||
}
|
||||
}
|
||||
48
lib/esp32_lora_wifi/WiFi/examples/WiFiScan/WiFiScan.ino
Normal file
48
lib/esp32_lora_wifi/WiFi/examples/WiFiScan/WiFiScan.ino
Normal file
@@ -0,0 +1,48 @@
|
||||
/*
|
||||
* This sketch demonstrates how to scan WiFi networks.
|
||||
* The API is almost the same as with the WiFi Shield library,
|
||||
* the most obvious difference being the different file you need to include:
|
||||
*/
|
||||
#include "WiFi.h"
|
||||
|
||||
void setup()
|
||||
{
|
||||
Serial.begin(115200);
|
||||
|
||||
// Set WiFi to station mode and disconnect from an AP if it was previously connected
|
||||
WiFi.mode(WIFI_STA);
|
||||
WiFi.disconnect();
|
||||
delay(100);
|
||||
|
||||
Serial.println("Setup done");
|
||||
}
|
||||
|
||||
void loop()
|
||||
{
|
||||
Serial.println("scan start");
|
||||
|
||||
// WiFi.scanNetworks will return the number of networks found
|
||||
int n = WiFi.scanNetworks();
|
||||
Serial.println("scan done");
|
||||
if (n == 0) {
|
||||
Serial.println("no networks found");
|
||||
} else {
|
||||
Serial.print(n);
|
||||
Serial.println(" networks found");
|
||||
for (int i = 0; i < n; ++i) {
|
||||
// Print SSID and RSSI for each network found
|
||||
Serial.print(i + 1);
|
||||
Serial.print(": ");
|
||||
Serial.print(WiFi.SSID(i));
|
||||
Serial.print(" (");
|
||||
Serial.print(WiFi.RSSI(i));
|
||||
Serial.print(")");
|
||||
Serial.println((WiFi.encryptionType(i) == WIFI_AUTH_OPEN)?" ":"*");
|
||||
delay(10);
|
||||
}
|
||||
}
|
||||
Serial.println("");
|
||||
|
||||
// Wait a bit before scanning again
|
||||
delay(5000);
|
||||
}
|
||||
28
lib/esp32_lora_wifi/WiFi/examples/WiFiScan/images.h
Normal file
28
lib/esp32_lora_wifi/WiFi/examples/WiFiScan/images.h
Normal file
@@ -0,0 +1,28 @@
|
||||
#define WiFi_Logo_width 60
|
||||
#define WiFi_Logo_height 36
|
||||
const char WiFi_Logo_bits[] PROGMEM = {
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xF8,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0xFF, 0x07, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0xE0, 0xFF, 0x1F, 0x00, 0x00, 0x00, 0x00, 0x00, 0xF8, 0xFF,
|
||||
0x7F, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFC, 0xFF, 0xFF, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0xFE, 0xFF, 0xFF, 0x01, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF,
|
||||
0xFF, 0x03, 0x00, 0x00, 0x00, 0xFC, 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00,
|
||||
0x00, 0xFF, 0xFF, 0xFF, 0x07, 0xC0, 0x83, 0x01, 0x80, 0xFF, 0xFF, 0xFF,
|
||||
0x01, 0x00, 0x07, 0x00, 0xC0, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x0C, 0x00,
|
||||
0xC0, 0xFF, 0xFF, 0x7C, 0x00, 0x60, 0x0C, 0x00, 0xC0, 0x31, 0x46, 0x7C,
|
||||
0xFC, 0x77, 0x08, 0x00, 0xE0, 0x23, 0xC6, 0x3C, 0xFC, 0x67, 0x18, 0x00,
|
||||
0xE0, 0x23, 0xE4, 0x3F, 0x1C, 0x00, 0x18, 0x00, 0xE0, 0x23, 0x60, 0x3C,
|
||||
0x1C, 0x70, 0x18, 0x00, 0xE0, 0x03, 0x60, 0x3C, 0x1C, 0x70, 0x18, 0x00,
|
||||
0xE0, 0x07, 0x60, 0x3C, 0xFC, 0x73, 0x18, 0x00, 0xE0, 0x87, 0x70, 0x3C,
|
||||
0xFC, 0x73, 0x18, 0x00, 0xE0, 0x87, 0x70, 0x3C, 0x1C, 0x70, 0x18, 0x00,
|
||||
0xE0, 0x87, 0x70, 0x3C, 0x1C, 0x70, 0x18, 0x00, 0xE0, 0x8F, 0x71, 0x3C,
|
||||
0x1C, 0x70, 0x18, 0x00, 0xC0, 0xFF, 0xFF, 0x3F, 0x00, 0x00, 0x08, 0x00,
|
||||
0xC0, 0xFF, 0xFF, 0x1F, 0x00, 0x00, 0x0C, 0x00, 0x80, 0xFF, 0xFF, 0x1F,
|
||||
0x00, 0x00, 0x06, 0x00, 0x80, 0xFF, 0xFF, 0x0F, 0x00, 0x00, 0x07, 0x00,
|
||||
0x00, 0xFE, 0xFF, 0xFF, 0xFF, 0xFF, 0x01, 0x00, 0x00, 0xF8, 0xFF, 0xFF,
|
||||
0xFF, 0x7F, 0x00, 0x00, 0x00, 0x00, 0xFE, 0xFF, 0xFF, 0x01, 0x00, 0x00,
|
||||
0x00, 0x00, 0xFC, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00, 0x00, 0xF8, 0xFF,
|
||||
0x7F, 0x00, 0x00, 0x00, 0x00, 0x00, 0xE0, 0xFF, 0x1F, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x80, 0xFF, 0x07, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFC,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
};
|
||||
@@ -0,0 +1,36 @@
|
||||
#include "WiFi.h"
|
||||
|
||||
void setup() {
|
||||
Serial.begin(115200);
|
||||
|
||||
//Init WiFi as Station, start SmartConfig
|
||||
WiFi.mode(WIFI_AP_STA);
|
||||
WiFi.beginSmartConfig();
|
||||
|
||||
//Wait for SmartConfig packet from mobile
|
||||
Serial.println("Waiting for SmartConfig.");
|
||||
while (!WiFi.smartConfigDone()) {
|
||||
delay(500);
|
||||
Serial.print(".");
|
||||
}
|
||||
|
||||
Serial.println("");
|
||||
Serial.println("SmartConfig received.");
|
||||
|
||||
//Wait for WiFi to connect to AP
|
||||
Serial.println("Waiting for WiFi");
|
||||
while (WiFi.status() != WL_CONNECTED) {
|
||||
delay(500);
|
||||
Serial.print(".");
|
||||
}
|
||||
|
||||
Serial.println("WiFi Connected.");
|
||||
|
||||
Serial.print("IP Address: ");
|
||||
Serial.println(WiFi.localIP());
|
||||
}
|
||||
|
||||
void loop() {
|
||||
// put your main code here, to run repeatedly:
|
||||
|
||||
}
|
||||
@@ -0,0 +1,75 @@
|
||||
/*
|
||||
* This sketch sends random data over UDP on a ESP32 device
|
||||
*
|
||||
*/
|
||||
#include <WiFi.h>
|
||||
#include <WiFiUdp.h>
|
||||
|
||||
// WiFi network name and password:
|
||||
const char * networkName = "your-ssid";
|
||||
const char * networkPswd = "your-password";
|
||||
|
||||
//IP address to send UDP data to:
|
||||
// either use the ip address of the server or
|
||||
// a network broadcast address
|
||||
const char * udpAddress = "192.168.0.255";
|
||||
const int udpPort = 3333;
|
||||
|
||||
//Are we currently connected?
|
||||
boolean connected = false;
|
||||
|
||||
//The udp library class
|
||||
WiFiUDP udp;
|
||||
|
||||
void setup(){
|
||||
// Initilize hardware serial:
|
||||
Serial.begin(115200);
|
||||
|
||||
//Connect to the WiFi network
|
||||
connectToWiFi(networkName, networkPswd);
|
||||
}
|
||||
|
||||
void loop(){
|
||||
//only send data when connected
|
||||
if(connected){
|
||||
//Send a packet
|
||||
udp.beginPacket(udpAddress,udpPort);
|
||||
udp.printf("Seconds since boot: %u", millis()/1000);
|
||||
udp.endPacket();
|
||||
}
|
||||
//Wait for 1 second
|
||||
delay(1000);
|
||||
}
|
||||
|
||||
void connectToWiFi(const char * ssid, const char * pwd){
|
||||
Serial.println("Connecting to WiFi network: " + String(ssid));
|
||||
|
||||
// delete old config
|
||||
WiFi.disconnect(true);
|
||||
//register event handler
|
||||
WiFi.onEvent(WiFiEvent);
|
||||
|
||||
//Initiate connection
|
||||
WiFi.begin(ssid, pwd);
|
||||
|
||||
Serial.println("Waiting for WIFI connection...");
|
||||
}
|
||||
|
||||
//wifi event handler
|
||||
void WiFiEvent(WiFiEvent_t event){
|
||||
switch(event) {
|
||||
case SYSTEM_EVENT_STA_GOT_IP:
|
||||
//When connected set
|
||||
Serial.print("WiFi connected! IP address: ");
|
||||
Serial.println(WiFi.localIP());
|
||||
//initializes the UDP state
|
||||
//This initializes the transfer buffer
|
||||
udp.begin(WiFi.localIP(),udpPort);
|
||||
connected = true;
|
||||
break;
|
||||
case SYSTEM_EVENT_STA_DISCONNECTED:
|
||||
Serial.println("WiFi lost connection");
|
||||
connected = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
# This python script listens on UDP port 3333
|
||||
# for messages from the ESP32 board and prints them
|
||||
import socket
|
||||
import sys
|
||||
|
||||
try :
|
||||
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
|
||||
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
|
||||
except socket.error, msg :
|
||||
print 'Failed to create socket. Error Code : ' + str(msg[0]) + ' Message ' + msg[1]
|
||||
sys.exit()
|
||||
|
||||
try:
|
||||
s.bind(('', 3333))
|
||||
except socket.error , msg:
|
||||
print 'Bind failed. Error: ' + str(msg[0]) + ': ' + msg[1]
|
||||
sys.exit()
|
||||
|
||||
print 'Server listening'
|
||||
|
||||
while 1:
|
||||
d = s.recvfrom(1024)
|
||||
data = d[0]
|
||||
|
||||
if not data:
|
||||
break
|
||||
|
||||
print data.strip()
|
||||
|
||||
s.close()
|
||||
@@ -0,0 +1,16 @@
|
||||
# This ruby script listens on UDP port 3333
|
||||
# for messages from the ESP32 board and prints them
|
||||
|
||||
require 'socket'
|
||||
include Socket::Constants
|
||||
|
||||
udp_socket = UDPSocket.new(AF_INET)
|
||||
|
||||
#bind
|
||||
udp_socket.bind("", 3333)
|
||||
puts 'Server listening'
|
||||
|
||||
while true do
|
||||
message, sender = udp_socket.recvfrom(1024)
|
||||
puts message
|
||||
end
|
||||
68
lib/esp32_lora_wifi/WiFi/keywords.txt
Normal file
68
lib/esp32_lora_wifi/WiFi/keywords.txt
Normal file
@@ -0,0 +1,68 @@
|
||||
#######################################
|
||||
# Syntax Coloring Map For WiFi
|
||||
#######################################
|
||||
|
||||
#######################################
|
||||
# Library (KEYWORD3)
|
||||
#######################################
|
||||
|
||||
WiFi KEYWORD3
|
||||
|
||||
#######################################
|
||||
# Datatypes (KEYWORD1)
|
||||
#######################################
|
||||
|
||||
WiFi KEYWORD1
|
||||
WiFiClient KEYWORD1
|
||||
WiFiServer KEYWORD1
|
||||
WiFiUDP KEYWORD1
|
||||
WiFiClientSecure KEYWORD1
|
||||
|
||||
#######################################
|
||||
# Methods and Functions (KEYWORD2)
|
||||
#######################################
|
||||
|
||||
status KEYWORD2
|
||||
mode KEYWORD2
|
||||
connect KEYWORD2
|
||||
write KEYWORD2
|
||||
available KEYWORD2
|
||||
config KEYWORD2
|
||||
setDNS KEYWORD2
|
||||
read KEYWORD2
|
||||
flush KEYWORD2
|
||||
stop KEYWORD2
|
||||
connected KEYWORD2
|
||||
begin KEYWORD2
|
||||
beginMulticast KEYWORD2
|
||||
disconnect KEYWORD2
|
||||
macAddress KEYWORD2
|
||||
localIP KEYWORD2
|
||||
subnetMask KEYWORD2
|
||||
gatewayIP KEYWORD2
|
||||
SSID KEYWORD2
|
||||
psk KEYWORD2
|
||||
BSSID KEYWORD2
|
||||
RSSI KEYWORD2
|
||||
encryptionType KEYWORD2
|
||||
beginPacket KEYWORD2
|
||||
beginPacketMulticast KEYWORD2
|
||||
endPacket KEYWORD2
|
||||
parsePacket KEYWORD2
|
||||
destinationIP KEYWORD2
|
||||
remoteIP KEYWORD2
|
||||
remotePort KEYWORD2
|
||||
softAP KEYWORD2
|
||||
softAPIP KEYWORD2
|
||||
softAPmacAddress KEYWORD2
|
||||
softAPConfig KEYWORD2
|
||||
printDiag KEYWORD2
|
||||
hostByName KEYWORD2
|
||||
scanNetworks KEYWORD2
|
||||
|
||||
#######################################
|
||||
# Constants (LITERAL1)
|
||||
#######################################
|
||||
WIFI_AP LITERAL1
|
||||
WIFI_STA LITERAL1
|
||||
WIFI_AP_STA LITERAL1
|
||||
9
lib/esp32_lora_wifi/WiFi/library.properties
Normal file
9
lib/esp32_lora_wifi/WiFi/library.properties
Normal file
@@ -0,0 +1,9 @@
|
||||
name=WiFi
|
||||
version=1.0
|
||||
author=Hristo Gochkov
|
||||
maintainer=Hristo Gochkov <hristo@espressif.com>
|
||||
sentence=Enables network connection (local and Internet) using the ESP32 built-in WiFi.
|
||||
paragraph=With this library you can instantiate Servers, Clients and send/receive UDP packets through WiFi. The shield can connect either to open or encrypted networks (WEP, WPA). The IP address can be assigned statically or through a DHCP. The library can also manage DNS.
|
||||
category=Communication
|
||||
url=
|
||||
architectures=esp32
|
||||
96
lib/esp32_lora_wifi/WiFi/src/WiFi.cpp
Normal file
96
lib/esp32_lora_wifi/WiFi/src/WiFi.cpp
Normal file
@@ -0,0 +1,96 @@
|
||||
/*
|
||||
ESP8266WiFi.cpp - WiFi library for esp8266
|
||||
|
||||
Copyright (c) 2014 Ivan Grokhotkov. All rights reserved.
|
||||
This file is part of the esp8266 core for Arduino environment.
|
||||
|
||||
This library is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU Lesser General Public
|
||||
License as published by the Free Software Foundation; either
|
||||
version 2.1 of the License, or (at your option) any later version.
|
||||
|
||||
This library is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
Lesser General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public
|
||||
License along with this library; if not, write to the Free Software
|
||||
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
|
||||
Reworked on 28 Dec 2015 by Markus Sattler
|
||||
|
||||
*/
|
||||
#include "WiFi.h"
|
||||
|
||||
extern "C" {
|
||||
#include <stdint.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <inttypes.h>
|
||||
#include <string.h>
|
||||
#include <esp_err.h>
|
||||
#include <esp_wifi.h>
|
||||
#include <esp_event_loop.h>
|
||||
}
|
||||
|
||||
|
||||
// -----------------------------------------------------------------------------------------------------------------------
|
||||
// ---------------------------------------------------------- Debug ------------------------------------------------------
|
||||
// -----------------------------------------------------------------------------------------------------------------------
|
||||
|
||||
|
||||
/**
|
||||
* Output WiFi settings to an object derived from Print interface (like Serial).
|
||||
* @param p Print interface
|
||||
*/
|
||||
void WiFiClass::printDiag(Print& p)
|
||||
{
|
||||
const char* modes[] = { "NULL", "STA", "AP", "STA+AP" };
|
||||
|
||||
wifi_mode_t mode;
|
||||
esp_wifi_get_mode(&mode);
|
||||
|
||||
uint8_t primaryChan;
|
||||
wifi_second_chan_t secondChan;
|
||||
esp_wifi_get_channel(&primaryChan, &secondChan);
|
||||
|
||||
bool autoConnect;
|
||||
esp_wifi_get_auto_connect(&autoConnect);
|
||||
|
||||
p.print("Mode: ");
|
||||
p.println(modes[mode]);
|
||||
|
||||
p.print("Channel: ");
|
||||
p.println(primaryChan);
|
||||
/*
|
||||
p.print("AP id: ");
|
||||
p.println(wifi_station_get_current_ap_id());
|
||||
|
||||
p.print("Status: ");
|
||||
p.println(wifi_station_get_connect_status());
|
||||
*/
|
||||
p.print("Auto connect: ");
|
||||
p.println(autoConnect);
|
||||
|
||||
wifi_config_t conf;
|
||||
esp_wifi_get_config(WIFI_IF_STA, &conf);
|
||||
|
||||
const char* ssid = reinterpret_cast<const char*>(conf.sta.ssid);
|
||||
p.print("SSID (");
|
||||
p.print(strlen(ssid));
|
||||
p.print("): ");
|
||||
p.println(ssid);
|
||||
|
||||
const char* passphrase = reinterpret_cast<const char*>(conf.sta.password);
|
||||
p.print("Passphrase (");
|
||||
p.print(strlen(passphrase));
|
||||
p.print("): ");
|
||||
p.println(passphrase);
|
||||
|
||||
p.print("BSSID set: ");
|
||||
p.println(conf.sta.bssid_set);
|
||||
}
|
||||
|
||||
WiFiClass WiFi;
|
||||
67
lib/esp32_lora_wifi/WiFi/src/WiFi.h
Normal file
67
lib/esp32_lora_wifi/WiFi/src/WiFi.h
Normal file
@@ -0,0 +1,67 @@
|
||||
/*
|
||||
ESP8266WiFi.h - esp8266 Wifi support.
|
||||
Based on WiFi.h from Arduino WiFi shield library.
|
||||
Copyright (c) 2011-2014 Arduino. All right reserved.
|
||||
Modified by Ivan Grokhotkov, December 2014
|
||||
|
||||
This library is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU Lesser General Public
|
||||
License as published by the Free Software Foundation; either
|
||||
version 2.1 of the License, or (at your option) any later version.
|
||||
|
||||
This library is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
Lesser General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public
|
||||
License along with this library; if not, write to the Free Software
|
||||
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
*/
|
||||
|
||||
#ifndef WiFi_h
|
||||
#define WiFi_h
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
#include "Print.h"
|
||||
#include "IPAddress.h"
|
||||
#include "IPv6Address.h"
|
||||
|
||||
#include "WiFiType.h"
|
||||
#include "WiFiSTA.h"
|
||||
#include "WiFiAP.h"
|
||||
#include "WiFiScan.h"
|
||||
#include "WiFiGeneric.h"
|
||||
|
||||
#include "WiFiClient.h"
|
||||
#include "WiFiServer.h"
|
||||
#include "WiFiUdp.h"
|
||||
|
||||
class WiFiClass : public WiFiGenericClass, public WiFiSTAClass, public WiFiScanClass, public WiFiAPClass
|
||||
{
|
||||
public:
|
||||
using WiFiGenericClass::channel;
|
||||
|
||||
using WiFiSTAClass::SSID;
|
||||
using WiFiSTAClass::RSSI;
|
||||
using WiFiSTAClass::BSSID;
|
||||
using WiFiSTAClass::BSSIDstr;
|
||||
|
||||
using WiFiScanClass::SSID;
|
||||
using WiFiScanClass::encryptionType;
|
||||
using WiFiScanClass::RSSI;
|
||||
using WiFiScanClass::BSSID;
|
||||
using WiFiScanClass::BSSIDstr;
|
||||
using WiFiScanClass::channel;
|
||||
|
||||
public:
|
||||
void printDiag(Print& dest);
|
||||
friend class WiFiClient;
|
||||
friend class WiFiServer;
|
||||
friend class WiFiUDP;
|
||||
};
|
||||
|
||||
extern WiFiClass WiFi;
|
||||
|
||||
#endif
|
||||
279
lib/esp32_lora_wifi/WiFi/src/WiFiAP.cpp
Normal file
279
lib/esp32_lora_wifi/WiFi/src/WiFiAP.cpp
Normal file
@@ -0,0 +1,279 @@
|
||||
/*
|
||||
ESP8266WiFiSTA.cpp - WiFi library for esp8266
|
||||
|
||||
Copyright (c) 2014 Ivan Grokhotkov. All rights reserved.
|
||||
This file is part of the esp8266 core for Arduino environment.
|
||||
|
||||
This library is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU Lesser General Public
|
||||
License as published by the Free Software Foundation; either
|
||||
version 2.1 of the License, or (at your option) any later version.
|
||||
|
||||
This library is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
Lesser General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public
|
||||
License along with this library; if not, write to the Free Software
|
||||
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
|
||||
Reworked on 28 Dec 2015 by Markus Sattler
|
||||
|
||||
*/
|
||||
|
||||
#include "WiFi.h"
|
||||
#include "WiFiGeneric.h"
|
||||
#include "WiFiAP.h"
|
||||
|
||||
extern "C" {
|
||||
#include <stdint.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <inttypes.h>
|
||||
#include <string.h>
|
||||
#include <esp_err.h>
|
||||
#include <esp_wifi.h>
|
||||
#include <esp_event_loop.h>
|
||||
#include <lwip/ip_addr.h>
|
||||
}
|
||||
|
||||
|
||||
|
||||
// -----------------------------------------------------------------------------------------------------------------------
|
||||
// ---------------------------------------------------- Private functions ------------------------------------------------
|
||||
// -----------------------------------------------------------------------------------------------------------------------
|
||||
|
||||
static bool softap_config_equal(const wifi_config_t& lhs, const wifi_config_t& rhs);
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* compare two AP configurations
|
||||
* @param lhs softap_config
|
||||
* @param rhs softap_config
|
||||
* @return equal
|
||||
*/
|
||||
static bool softap_config_equal(const wifi_config_t& lhs, const wifi_config_t& rhs)
|
||||
{
|
||||
if(strcmp(reinterpret_cast<const char*>(lhs.ap.ssid), reinterpret_cast<const char*>(rhs.ap.ssid)) != 0) {
|
||||
return false;
|
||||
}
|
||||
if(strcmp(reinterpret_cast<const char*>(lhs.ap.password), reinterpret_cast<const char*>(rhs.ap.password)) != 0) {
|
||||
return false;
|
||||
}
|
||||
if(lhs.ap.channel != rhs.ap.channel) {
|
||||
return false;
|
||||
}
|
||||
if(lhs.ap.ssid_hidden != rhs.ap.ssid_hidden) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
// -----------------------------------------------------------------------------------------------------------------------
|
||||
// ----------------------------------------------------- AP function -----------------------------------------------------
|
||||
// -----------------------------------------------------------------------------------------------------------------------
|
||||
|
||||
|
||||
/**
|
||||
* Set up an access point
|
||||
* @param ssid Pointer to the SSID (max 63 char).
|
||||
* @param passphrase (for WPA2 min 8 char, for open use NULL)
|
||||
* @param channel WiFi channel number, 1 - 13.
|
||||
* @param ssid_hidden Network cloaking (0 = broadcast SSID, 1 = hide SSID)
|
||||
*/
|
||||
bool WiFiAPClass::softAP(const char* ssid, const char* passphrase, int channel, int ssid_hidden)
|
||||
{
|
||||
|
||||
if(!WiFi.enableAP(true)) {
|
||||
// enable AP failed
|
||||
return false;
|
||||
}
|
||||
|
||||
if(!ssid || *ssid == 0 || strlen(ssid) > 31) {
|
||||
// fail SSID too long or missing!
|
||||
return false;
|
||||
}
|
||||
|
||||
if(passphrase && (strlen(passphrase) > 63 || strlen(passphrase) < 8)) {
|
||||
// fail passphrase to long or short!
|
||||
return false;
|
||||
}
|
||||
|
||||
esp_wifi_start();
|
||||
|
||||
wifi_config_t conf;
|
||||
strcpy(reinterpret_cast<char*>(conf.ap.ssid), ssid);
|
||||
conf.ap.channel = channel;
|
||||
conf.ap.ssid_len = strlen(ssid);
|
||||
conf.ap.ssid_hidden = ssid_hidden;
|
||||
conf.ap.max_connection = 4;
|
||||
conf.ap.beacon_interval = 100;
|
||||
|
||||
if(!passphrase || strlen(passphrase) == 0) {
|
||||
conf.ap.authmode = WIFI_AUTH_OPEN;
|
||||
*conf.ap.password = 0;
|
||||
} else {
|
||||
conf.ap.authmode = WIFI_AUTH_WPA2_PSK;
|
||||
strcpy(reinterpret_cast<char*>(conf.ap.password), passphrase);
|
||||
}
|
||||
|
||||
wifi_config_t conf_current;
|
||||
esp_wifi_get_config(WIFI_IF_AP, &conf_current);
|
||||
if(softap_config_equal(conf, conf_current)) {
|
||||
//DEBUGV("softap config unchanged");
|
||||
return true;
|
||||
}
|
||||
|
||||
bool ret;
|
||||
|
||||
ret = esp_wifi_set_config(WIFI_IF_AP, &conf) == ESP_OK;
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Configure access point
|
||||
* @param local_ip access point IP
|
||||
* @param gateway gateway IP
|
||||
* @param subnet subnet mask
|
||||
*/
|
||||
bool WiFiAPClass::softAPConfig(IPAddress local_ip, IPAddress gateway, IPAddress subnet)
|
||||
{
|
||||
|
||||
if(!WiFi.enableAP(true)) {
|
||||
// enable AP failed
|
||||
return false;
|
||||
}
|
||||
|
||||
tcpip_adapter_ip_info_t info;
|
||||
info.ip.addr = static_cast<uint32_t>(local_ip);
|
||||
info.gw.addr = static_cast<uint32_t>(gateway);
|
||||
info.netmask.addr = static_cast<uint32_t>(subnet);
|
||||
tcpip_adapter_dhcps_stop(TCPIP_ADAPTER_IF_AP);
|
||||
if(tcpip_adapter_set_ip_info(TCPIP_ADAPTER_IF_AP, &info) == ESP_OK) {
|
||||
return tcpip_adapter_dhcps_start(TCPIP_ADAPTER_IF_AP) == ESP_OK;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Disconnect from the network (close AP)
|
||||
* @param wifioff disable mode?
|
||||
* @return one value of wl_status_t enum
|
||||
*/
|
||||
bool WiFiAPClass::softAPdisconnect(bool wifioff)
|
||||
{
|
||||
bool ret;
|
||||
wifi_config_t conf;
|
||||
*conf.ap.ssid = 0;
|
||||
*conf.ap.password = 0;
|
||||
ret = esp_wifi_set_config(WIFI_IF_AP, &conf) == ESP_OK;
|
||||
|
||||
if(wifioff) {
|
||||
ret = WiFi.enableAP(false) == ESP_OK;
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Get the count of the Station / client that are connected to the softAP interface
|
||||
* @return Stations count
|
||||
*/
|
||||
uint8_t WiFiAPClass::softAPgetStationNum()
|
||||
{
|
||||
wifi_sta_list_t clients;
|
||||
if(esp_wifi_ap_get_sta_list(&clients) == ESP_OK) {
|
||||
return clients.num;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the softAP interface IP address.
|
||||
* @return IPAddress softAP IP
|
||||
*/
|
||||
IPAddress WiFiAPClass::softAPIP()
|
||||
{
|
||||
tcpip_adapter_ip_info_t ip;
|
||||
tcpip_adapter_get_ip_info(TCPIP_ADAPTER_IF_AP, &ip);
|
||||
return IPAddress(ip.ip.addr);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Get the softAP interface MAC address.
|
||||
* @param mac pointer to uint8_t array with length WL_MAC_ADDR_LENGTH
|
||||
* @return pointer to uint8_t*
|
||||
*/
|
||||
uint8_t* WiFiAPClass::softAPmacAddress(uint8_t* mac)
|
||||
{
|
||||
esp_wifi_get_mac(WIFI_IF_AP, mac);
|
||||
return mac;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the softAP interface MAC address.
|
||||
* @return String mac
|
||||
*/
|
||||
String WiFiAPClass::softAPmacAddress(void)
|
||||
{
|
||||
uint8_t mac[6];
|
||||
char macStr[18] = { 0 };
|
||||
esp_wifi_get_mac(WIFI_IF_AP, mac);
|
||||
|
||||
sprintf(macStr, "%02X:%02X:%02X:%02X:%02X:%02X", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
|
||||
return String(macStr);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the softAP interface Host name.
|
||||
* @return char array hostname
|
||||
*/
|
||||
const char * WiFiAPClass::softAPgetHostname()
|
||||
{
|
||||
const char * hostname;
|
||||
if(tcpip_adapter_get_hostname(TCPIP_ADAPTER_IF_AP, &hostname)) {
|
||||
return NULL;
|
||||
}
|
||||
return hostname;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the softAP interface Host name.
|
||||
* @param hostname pointer to const string
|
||||
* @return true on success
|
||||
*/
|
||||
bool WiFiAPClass::softAPsetHostname(const char * hostname)
|
||||
{
|
||||
return tcpip_adapter_set_hostname(TCPIP_ADAPTER_IF_AP, hostname) == ESP_OK;
|
||||
}
|
||||
|
||||
/**
|
||||
* Enable IPv6 on the softAP interface.
|
||||
* @return true on success
|
||||
*/
|
||||
bool WiFiAPClass::softAPenableIpV6()
|
||||
{
|
||||
return tcpip_adapter_create_ip6_linklocal(TCPIP_ADAPTER_IF_AP) == ESP_OK;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the softAP interface IPv6 address.
|
||||
* @return IPv6Address softAP IPv6
|
||||
*/
|
||||
IPv6Address WiFiAPClass::softAPIPv6()
|
||||
{
|
||||
static ip6_addr_t addr;
|
||||
if(tcpip_adapter_get_ip6_linklocal(TCPIP_ADAPTER_IF_AP, &addr)) {
|
||||
return IPv6Address();
|
||||
}
|
||||
return IPv6Address(addr.addr);
|
||||
}
|
||||
61
lib/esp32_lora_wifi/WiFi/src/WiFiAP.h
Normal file
61
lib/esp32_lora_wifi/WiFi/src/WiFiAP.h
Normal file
@@ -0,0 +1,61 @@
|
||||
/*
|
||||
ESP8266WiFiAP.h - esp8266 Wifi support.
|
||||
Based on WiFi.h from Arduino WiFi shield library.
|
||||
Copyright (c) 2011-2014 Arduino. All right reserved.
|
||||
Modified by Ivan Grokhotkov, December 2014
|
||||
Reworked by Markus Sattler, December 2015
|
||||
|
||||
This library is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU Lesser General Public
|
||||
License as published by the Free Software Foundation; either
|
||||
version 2.1 of the License, or (at your option) any later version.
|
||||
|
||||
This library is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
Lesser General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public
|
||||
License along with this library; if not, write to the Free Software
|
||||
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
*/
|
||||
|
||||
#ifndef ESP32WIFIAP_H_
|
||||
#define ESP32WIFIAP_H_
|
||||
|
||||
|
||||
#include "WiFiType.h"
|
||||
#include "WiFiGeneric.h"
|
||||
|
||||
|
||||
class WiFiAPClass
|
||||
{
|
||||
|
||||
// ----------------------------------------------------------------------------------------------
|
||||
// ----------------------------------------- AP function ----------------------------------------
|
||||
// ----------------------------------------------------------------------------------------------
|
||||
|
||||
public:
|
||||
|
||||
bool softAP(const char* ssid, const char* passphrase = NULL, int channel = 1, int ssid_hidden = 0);
|
||||
bool softAPConfig(IPAddress local_ip, IPAddress gateway, IPAddress subnet);
|
||||
bool softAPdisconnect(bool wifioff = false);
|
||||
|
||||
uint8_t softAPgetStationNum();
|
||||
|
||||
IPAddress softAPIP();
|
||||
|
||||
bool softAPenableIpV6();
|
||||
IPv6Address softAPIPv6();
|
||||
|
||||
const char * softAPgetHostname();
|
||||
bool softAPsetHostname(const char * hostname);
|
||||
|
||||
uint8_t* softAPmacAddress(uint8_t* mac);
|
||||
String softAPmacAddress(void);
|
||||
|
||||
protected:
|
||||
|
||||
};
|
||||
|
||||
#endif /* ESP32WIFIAP_H_*/
|
||||
241
lib/esp32_lora_wifi/WiFi/src/WiFiClient.cpp
Normal file
241
lib/esp32_lora_wifi/WiFi/src/WiFiClient.cpp
Normal file
@@ -0,0 +1,241 @@
|
||||
/*
|
||||
Client.h - Client class for Raspberry Pi
|
||||
Copyright (c) 2016 Hristo Gochkov All right reserved.
|
||||
|
||||
This library is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU Lesser General Public
|
||||
License as published by the Free Software Foundation; either
|
||||
version 2.1 of the License, or (at your option) any later version.
|
||||
|
||||
This library is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
Lesser General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public
|
||||
License along with this library; if not, write to the Free Software
|
||||
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
*/
|
||||
|
||||
#include "WiFiClient.h"
|
||||
#include <lwip/sockets.h>
|
||||
#include <lwip/netdb.h>
|
||||
#include <errno.h>
|
||||
|
||||
#undef connect
|
||||
#undef write
|
||||
#undef read
|
||||
|
||||
WiFiClient::WiFiClient():sockfd(-1),_connected(false),next(NULL)
|
||||
{
|
||||
}
|
||||
|
||||
WiFiClient::WiFiClient(int fd):sockfd(fd),_connected(true),next(NULL)
|
||||
{
|
||||
}
|
||||
|
||||
WiFiClient::~WiFiClient()
|
||||
{
|
||||
stop();
|
||||
}
|
||||
|
||||
WiFiClient & WiFiClient::operator=(const WiFiClient &other)
|
||||
{
|
||||
stop();
|
||||
sockfd = other.sockfd;
|
||||
_connected = other._connected;
|
||||
return *this;
|
||||
}
|
||||
|
||||
void WiFiClient::stop()
|
||||
{
|
||||
if(_connected && sockfd >= 0) {
|
||||
close(sockfd);
|
||||
sockfd = -1;
|
||||
_connected = false;
|
||||
}
|
||||
}
|
||||
|
||||
int WiFiClient::connect(IPAddress ip, uint16_t port)
|
||||
{
|
||||
sockfd = socket(AF_INET, SOCK_STREAM, 0);
|
||||
if (sockfd < 0) {
|
||||
log_e("socket: %d", errno);
|
||||
return 0;
|
||||
}
|
||||
uint32_t ip_addr = ip;
|
||||
struct sockaddr_in serveraddr;
|
||||
bzero((char *) &serveraddr, sizeof(serveraddr));
|
||||
serveraddr.sin_family = AF_INET;
|
||||
bcopy((const void *)(&ip_addr), (void *)&serveraddr.sin_addr.s_addr, 4);
|
||||
serveraddr.sin_port = htons(port);
|
||||
int res = lwip_connect_r(sockfd, (struct sockaddr*)&serveraddr, sizeof(serveraddr));
|
||||
if (res < 0) {
|
||||
log_e("lwip_connect_r: %d", errno);
|
||||
close(sockfd);
|
||||
sockfd = -1;
|
||||
return 0;
|
||||
}
|
||||
_connected = true;
|
||||
return 1;
|
||||
}
|
||||
|
||||
int WiFiClient::connect(const char *host, uint16_t port)
|
||||
{
|
||||
struct hostent *server;
|
||||
server = gethostbyname(host);
|
||||
if (server == NULL) {
|
||||
return 0;
|
||||
}
|
||||
IPAddress srv((const uint8_t *)(server->h_addr));
|
||||
return connect(srv, port);
|
||||
}
|
||||
|
||||
int WiFiClient::setSocketOption(int option, char* value, size_t len)
|
||||
{
|
||||
int res = setsockopt(sockfd, SOL_SOCKET, option, value, len);
|
||||
if(res < 0) {
|
||||
log_e("%d", errno);
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
int WiFiClient::setTimeout(uint32_t seconds)
|
||||
{
|
||||
struct timeval tv;
|
||||
tv.tv_sec = seconds;
|
||||
tv.tv_usec = 0;
|
||||
if(setSocketOption(SO_RCVTIMEO, (char *)&tv, sizeof(struct timeval)) < 0) {
|
||||
return -1;
|
||||
}
|
||||
return setSocketOption(SO_SNDTIMEO, (char *)&tv, sizeof(struct timeval));
|
||||
}
|
||||
|
||||
int WiFiClient::setOption(int option, int *value)
|
||||
{
|
||||
int res = setsockopt(sockfd, IPPROTO_TCP, option, (char *)value, sizeof(int));
|
||||
if(res < 0) {
|
||||
log_e("%d", errno);
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
int WiFiClient::getOption(int option, int *value)
|
||||
{
|
||||
size_t size = sizeof(int);
|
||||
int res = getsockopt(sockfd, IPPROTO_TCP, option, (char *)value, &size);
|
||||
if(res < 0) {
|
||||
log_e("%d", errno);
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
int WiFiClient::setNoDelay(bool nodelay)
|
||||
{
|
||||
int flag = nodelay;
|
||||
return setOption(TCP_NODELAY, &flag);
|
||||
}
|
||||
|
||||
bool WiFiClient::getNoDelay()
|
||||
{
|
||||
int flag = 0;
|
||||
getOption(TCP_NODELAY, &flag);
|
||||
return flag;
|
||||
}
|
||||
|
||||
size_t WiFiClient::write(uint8_t data)
|
||||
{
|
||||
return write(&data, 1);
|
||||
}
|
||||
|
||||
int WiFiClient::read()
|
||||
{
|
||||
uint8_t data = 0;
|
||||
int res = read(&data, 1);
|
||||
if(res < 0) {
|
||||
return res;
|
||||
}
|
||||
return data;
|
||||
}
|
||||
|
||||
size_t WiFiClient::write(const uint8_t *buf, size_t size)
|
||||
{
|
||||
if(!_connected) {
|
||||
return 0;
|
||||
}
|
||||
int res = send(sockfd, (void*)buf, size, MSG_DONTWAIT);
|
||||
if(res < 0) {
|
||||
log_e("%d", errno);
|
||||
stop();
|
||||
res = 0;
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
int WiFiClient::read(uint8_t *buf, size_t size)
|
||||
{
|
||||
if(!available()) {
|
||||
return -1;
|
||||
}
|
||||
int res = recv(sockfd, buf, size, MSG_DONTWAIT);
|
||||
if(res < 0 && errno != EWOULDBLOCK) {
|
||||
log_e("%d", errno);
|
||||
stop();
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
int WiFiClient::available()
|
||||
{
|
||||
if(!_connected) {
|
||||
return 0;
|
||||
}
|
||||
int count;
|
||||
int res = ioctl(sockfd, FIONREAD, &count);
|
||||
if(res < 0) {
|
||||
log_e("%d", errno);
|
||||
stop();
|
||||
return 0;
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
uint8_t WiFiClient::connected()
|
||||
{
|
||||
uint8_t dummy = 0;
|
||||
read(&dummy, 0);
|
||||
return _connected;
|
||||
}
|
||||
|
||||
IPAddress WiFiClient::remoteIP(int fd)
|
||||
{
|
||||
struct sockaddr_storage addr;
|
||||
socklen_t len = sizeof addr;
|
||||
getpeername(fd, (struct sockaddr*)&addr, &len);
|
||||
struct sockaddr_in *s = (struct sockaddr_in *)&addr;
|
||||
return IPAddress((uint32_t)(s->sin_addr.s_addr));
|
||||
}
|
||||
|
||||
uint16_t WiFiClient::remotePort(int fd)
|
||||
{
|
||||
struct sockaddr_storage addr;
|
||||
socklen_t len = sizeof addr;
|
||||
getpeername(fd, (struct sockaddr*)&addr, &len);
|
||||
struct sockaddr_in *s = (struct sockaddr_in *)&addr;
|
||||
return ntohs(s->sin_port);
|
||||
}
|
||||
|
||||
IPAddress WiFiClient::remoteIP()
|
||||
{
|
||||
return remoteIP(sockfd);
|
||||
}
|
||||
|
||||
uint16_t WiFiClient::remotePort()
|
||||
{
|
||||
return remotePort(sockfd);
|
||||
}
|
||||
|
||||
bool WiFiClient::operator==(const WiFiClient& rhs)
|
||||
{
|
||||
return sockfd == rhs.sockfd && remotePort(sockfd) == remotePort(rhs.sockfd) && remoteIP(sockfd) == remoteIP(rhs.sockfd);
|
||||
}
|
||||
92
lib/esp32_lora_wifi/WiFi/src/WiFiClient.h
Normal file
92
lib/esp32_lora_wifi/WiFi/src/WiFiClient.h
Normal file
@@ -0,0 +1,92 @@
|
||||
/*
|
||||
Client.h - Base class that provides Client
|
||||
Copyright (c) 2011 Adrian McEwen. All right reserved.
|
||||
|
||||
This library is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU Lesser General Public
|
||||
License as published by the Free Software Foundation; either
|
||||
version 2.1 of the License, or (at your option) any later version.
|
||||
|
||||
This library is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
Lesser General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public
|
||||
License along with this library; if not, write to the Free Software
|
||||
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
*/
|
||||
|
||||
#ifndef _WIFICLIENT_H_
|
||||
#define _WIFICLIENT_H_
|
||||
|
||||
|
||||
#include "Arduino.h"
|
||||
#include "Client.h"
|
||||
|
||||
class WiFiClient : public Client
|
||||
{
|
||||
protected:
|
||||
int sockfd;
|
||||
bool _connected;
|
||||
|
||||
public:
|
||||
WiFiClient *next;
|
||||
WiFiClient();
|
||||
WiFiClient(int fd);
|
||||
~WiFiClient();
|
||||
int connect(IPAddress ip, uint16_t port);
|
||||
int connect(const char *host, uint16_t port);
|
||||
size_t write(uint8_t data);
|
||||
size_t write(const uint8_t *buf, size_t size);
|
||||
int available();
|
||||
int read();
|
||||
int read(uint8_t *buf, size_t size);
|
||||
int peek()
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
void flush() {}
|
||||
void stop();
|
||||
uint8_t connected();
|
||||
|
||||
operator bool()
|
||||
{
|
||||
return connected();
|
||||
}
|
||||
WiFiClient & operator=(const WiFiClient &other);
|
||||
bool operator==(const bool value)
|
||||
{
|
||||
return bool() == value;
|
||||
}
|
||||
bool operator!=(const bool value)
|
||||
{
|
||||
return bool() != value;
|
||||
}
|
||||
bool operator==(const WiFiClient&);
|
||||
bool operator!=(const WiFiClient& rhs)
|
||||
{
|
||||
return !this->operator==(rhs);
|
||||
};
|
||||
|
||||
int fd()
|
||||
{
|
||||
return sockfd;
|
||||
}
|
||||
IPAddress remoteIP();
|
||||
uint16_t remotePort();
|
||||
int setSocketOption(int option, char* value, size_t len);
|
||||
int setOption(int option, int *value);
|
||||
int getOption(int option, int *value);
|
||||
int setTimeout(uint32_t seconds);
|
||||
int setNoDelay(bool nodelay);
|
||||
bool getNoDelay();
|
||||
|
||||
IPAddress remoteIP(int fd);
|
||||
uint16_t remotePort(int fd);
|
||||
|
||||
//friend class WiFiServer;
|
||||
using Print::write;
|
||||
};
|
||||
|
||||
#endif /* _WIFICLIENT_H_ */
|
||||
358
lib/esp32_lora_wifi/WiFi/src/WiFiGeneric.cpp
Normal file
358
lib/esp32_lora_wifi/WiFi/src/WiFiGeneric.cpp
Normal file
@@ -0,0 +1,358 @@
|
||||
/*
|
||||
ESP8266WiFiGeneric.cpp - WiFi library for esp8266
|
||||
|
||||
Copyright (c) 2014 Ivan Grokhotkov. All rights reserved.
|
||||
This file is part of the esp8266 core for Arduino environment.
|
||||
|
||||
This library is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU Lesser General Public
|
||||
License as published by the Free Software Foundation; either
|
||||
version 2.1 of the License, or (at your option) any later version.
|
||||
|
||||
This library is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
Lesser General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public
|
||||
License along with this library; if not, write to the Free Software
|
||||
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
|
||||
Reworked on 28 Dec 2015 by Markus Sattler
|
||||
|
||||
*/
|
||||
|
||||
#include "WiFi.h"
|
||||
#include "WiFiGeneric.h"
|
||||
|
||||
extern "C" {
|
||||
#include <stdint.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <inttypes.h>
|
||||
#include <string.h>
|
||||
|
||||
#include <esp_err.h>
|
||||
#include <esp_wifi.h>
|
||||
#include <esp_event_loop.h>
|
||||
#include "lwip/ip_addr.h"
|
||||
#include "lwip/opt.h"
|
||||
#include "lwip/err.h"
|
||||
#include "lwip/dns.h"
|
||||
#include "esp_ipc.h"
|
||||
|
||||
#include "esp32-hal-log.h"
|
||||
|
||||
/**
|
||||
* Boot and start WiFi
|
||||
* This method get's called on boot if you use any of the WiFi methods.
|
||||
* If you do not link to this library, WiFi will not be started.
|
||||
* */
|
||||
static bool _esp_wifi_initalized = false;
|
||||
extern void initWiFi()
|
||||
{
|
||||
#if !CONFIG_ESP32_PHY_AUTO_INIT
|
||||
arduino_phy_init();
|
||||
#endif
|
||||
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
|
||||
tcpip_adapter_init();
|
||||
esp_event_loop_init(&WiFiGenericClass::_eventCallback, NULL);
|
||||
esp_wifi_init(&cfg);
|
||||
esp_wifi_set_storage(WIFI_STORAGE_FLASH);
|
||||
_esp_wifi_initalized = true;
|
||||
}
|
||||
|
||||
} //extern "C"
|
||||
|
||||
#undef min
|
||||
#undef max
|
||||
#include <vector>
|
||||
|
||||
static bool _esp_wifi_start()
|
||||
{
|
||||
static bool started = false;
|
||||
esp_err_t err;
|
||||
|
||||
if(!_esp_wifi_initalized){
|
||||
initWiFi();
|
||||
if(!_esp_wifi_initalized){
|
||||
log_w("not initialized");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
if(started){
|
||||
return true;
|
||||
}
|
||||
started = true;
|
||||
err = esp_wifi_start();
|
||||
if (err != ESP_OK) {
|
||||
log_e("%d", err);
|
||||
return false;
|
||||
}
|
||||
#if CONFIG_AUTOCONNECT_WIFI
|
||||
wifi_mode_t mode = WIFI_MODE_NULL;
|
||||
bool auto_connect = false;
|
||||
|
||||
err = esp_wifi_get_mode(&mode);
|
||||
if (err != ESP_OK) {
|
||||
log_e("esp_wifi_get_mode: %d", err);
|
||||
return false;
|
||||
}
|
||||
|
||||
err = esp_wifi_get_auto_connect(&auto_connect);
|
||||
if ((mode == WIFI_MODE_STA || mode == WIFI_MODE_APSTA) && auto_connect) {
|
||||
err = esp_wifi_connect();
|
||||
if (err != ESP_OK) {
|
||||
log_e("esp_wifi_connect: %d", err);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
return true;
|
||||
}
|
||||
|
||||
// -----------------------------------------------------------------------------------------------------------------------
|
||||
// ------------------------------------------------- Generic WiFi function -----------------------------------------------
|
||||
// -----------------------------------------------------------------------------------------------------------------------
|
||||
|
||||
// arduino dont like std::vectors move static here
|
||||
static std::vector<WiFiEventCbList_t> cbEventList;
|
||||
|
||||
bool WiFiGenericClass::_persistent = true;
|
||||
wifi_mode_t WiFiGenericClass::_forceSleepLastMode = WIFI_MODE_NULL;
|
||||
|
||||
WiFiGenericClass::WiFiGenericClass()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* set callback function
|
||||
* @param cbEvent WiFiEventCb
|
||||
* @param event optional filter (WIFI_EVENT_MAX is all events)
|
||||
*/
|
||||
void WiFiGenericClass::onEvent(WiFiEventCb cbEvent, system_event_id_t event)
|
||||
{
|
||||
if(!cbEvent) {
|
||||
return;
|
||||
}
|
||||
WiFiEventCbList_t newEventHandler;
|
||||
newEventHandler.cb = cbEvent;
|
||||
newEventHandler.event = event;
|
||||
cbEventList.push_back(newEventHandler);
|
||||
}
|
||||
|
||||
/**
|
||||
* removes a callback form event handler
|
||||
* @param cbEvent WiFiEventCb
|
||||
* @param event optional filter (WIFI_EVENT_MAX is all events)
|
||||
*/
|
||||
void WiFiGenericClass::removeEvent(WiFiEventCb cbEvent, system_event_id_t event)
|
||||
{
|
||||
if(!cbEvent) {
|
||||
return;
|
||||
}
|
||||
|
||||
for(uint32_t i = 0; i < cbEventList.size(); i++) {
|
||||
WiFiEventCbList_t entry = cbEventList[i];
|
||||
if(entry.cb == cbEvent && entry.event == event) {
|
||||
cbEventList.erase(cbEventList.begin() + i);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* callback for WiFi events
|
||||
* @param arg
|
||||
*/
|
||||
#if ARDUHAL_LOG_LEVEL >= ARDUHAL_LOG_LEVEL_DEBUG
|
||||
const char * system_event_names[] = { "WIFI_READY", "SCAN_DONE", "STA_START", "STA_STOP", "STA_CONNECTED", "STA_DISCONNECTED", "STA_AUTHMODE_CHANGE", "STA_GOT_IP", "STA_WPS_ER_SUCCESS", "STA_WPS_ER_FAILED", "STA_WPS_ER_TIMEOUT", "STA_WPS_ER_PIN", "AP_START", "AP_STOP", "AP_STACONNECTED", "AP_STADISCONNECTED", "AP_PROBEREQRECVED", "AP_STA_GOT_IP6", "ETH_START", "ETH_STOP", "ETH_CONNECTED", "ETH_DISCONNECTED", "ETH_GOT_IP", "MAX"};
|
||||
#endif
|
||||
#if ARDUHAL_LOG_LEVEL >= ARDUHAL_LOG_LEVEL_WARN
|
||||
const char * system_event_reasons[] = { "UNSPECIFIED", "AUTH_EXPIRE", "AUTH_LEAVE", "ASSOC_EXPIRE", "ASSOC_TOOMANY", "NOT_AUTHED", "NOT_ASSOCED", "ASSOC_LEAVE", "ASSOC_NOT_AUTHED", "DISASSOC_PWRCAP_BAD", "DISASSOC_SUPCHAN_BAD", "IE_INVALID", "MIC_FAILURE", "4WAY_HANDSHAKE_TIMEOUT", "GROUP_KEY_UPDATE_TIMEOUT", "IE_IN_4WAY_DIFFERS", "GROUP_CIPHER_INVALID", "PAIRWISE_CIPHER_INVALID", "AKMP_INVALID", "UNSUPP_RSN_IE_VERSION", "INVALID_RSN_IE_CAP", "802_1X_AUTH_FAILED", "CIPHER_SUITE_REJECTED", "BEACON_TIMEOUT", "NO_AP_FOUND", "AUTH_FAIL", "ASSOC_FAIL", "HANDSHAKE_TIMEOUT" };
|
||||
#define reason2str(r) ((r>176)?system_event_reasons[r-176]:system_event_reasons[r-1])
|
||||
#endif
|
||||
esp_err_t WiFiGenericClass::_eventCallback(void *arg, system_event_t *event)
|
||||
{
|
||||
log_d("Event: %d - %s", event->event_id, system_event_names[event->event_id]);
|
||||
|
||||
if(event->event_id == SYSTEM_EVENT_SCAN_DONE) {
|
||||
WiFiScanClass::_scanDone();
|
||||
} else if(event->event_id == SYSTEM_EVENT_STA_DISCONNECTED) {
|
||||
uint8_t reason = event->event_info.disconnected.reason;
|
||||
log_w("Reason: %u - %s", reason, reason2str(reason));
|
||||
if(reason == WIFI_REASON_NO_AP_FOUND) {
|
||||
WiFiSTAClass::_setStatus(WL_NO_SSID_AVAIL);
|
||||
} else if(reason == WIFI_REASON_AUTH_FAIL || reason == WIFI_REASON_ASSOC_FAIL) {
|
||||
WiFiSTAClass::_setStatus(WL_CONNECT_FAILED);
|
||||
} else if(reason == WIFI_REASON_BEACON_TIMEOUT || reason == WIFI_REASON_HANDSHAKE_TIMEOUT) {
|
||||
WiFiSTAClass::_setStatus(WL_CONNECTION_LOST);
|
||||
} else {
|
||||
WiFiSTAClass::_setStatus(WL_DISCONNECTED);
|
||||
}
|
||||
} else if(event->event_id == SYSTEM_EVENT_STA_START) {
|
||||
WiFiSTAClass::_setStatus(WL_DISCONNECTED);
|
||||
} else if(event->event_id == SYSTEM_EVENT_STA_STOP) {
|
||||
WiFiSTAClass::_setStatus(WL_NO_SHIELD);
|
||||
} else if(event->event_id == SYSTEM_EVENT_STA_GOT_IP) {
|
||||
WiFiSTAClass::_setStatus(WL_CONNECTED);
|
||||
}
|
||||
|
||||
for(uint32_t i = 0; i < cbEventList.size(); i++) {
|
||||
WiFiEventCbList_t entry = cbEventList[i];
|
||||
if(entry.cb) {
|
||||
if(entry.event == (system_event_id_t) event->event_id || entry.event == SYSTEM_EVENT_MAX) {
|
||||
entry.cb((system_event_id_t) event->event_id);
|
||||
}
|
||||
}
|
||||
}
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the current channel associated with the network
|
||||
* @return channel (1-13)
|
||||
*/
|
||||
int32_t WiFiGenericClass::channel(void)
|
||||
{
|
||||
uint8_t primaryChan;
|
||||
wifi_second_chan_t secondChan;
|
||||
esp_wifi_get_channel(&primaryChan, &secondChan);
|
||||
return primaryChan;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* store WiFi config in SDK flash area
|
||||
* @param persistent
|
||||
*/
|
||||
void WiFiGenericClass::persistent(bool persistent)
|
||||
{
|
||||
_persistent = persistent;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* set new mode
|
||||
* @param m WiFiMode_t
|
||||
*/
|
||||
bool WiFiGenericClass::mode(wifi_mode_t m)
|
||||
{
|
||||
wifi_mode_t cm = getMode();
|
||||
if(cm == WIFI_MODE_MAX){
|
||||
return false;
|
||||
}
|
||||
if(cm == m) {
|
||||
return true;
|
||||
}
|
||||
return esp_wifi_set_mode(m) == ESP_OK;
|
||||
}
|
||||
|
||||
/**
|
||||
* get WiFi mode
|
||||
* @return WiFiMode
|
||||
*/
|
||||
wifi_mode_t WiFiGenericClass::getMode()
|
||||
{
|
||||
uint8_t mode;
|
||||
if(!_esp_wifi_start()){
|
||||
return WIFI_MODE_MAX;
|
||||
}
|
||||
esp_wifi_get_mode((wifi_mode_t*)&mode);
|
||||
return (wifi_mode_t)mode;
|
||||
}
|
||||
|
||||
/**
|
||||
* control STA mode
|
||||
* @param enable bool
|
||||
* @return ok
|
||||
*/
|
||||
bool WiFiGenericClass::enableSTA(bool enable)
|
||||
{
|
||||
|
||||
wifi_mode_t currentMode = getMode();
|
||||
bool isEnabled = ((currentMode & WIFI_MODE_STA) != 0);
|
||||
|
||||
if(isEnabled != enable) {
|
||||
if(enable) {
|
||||
return mode((wifi_mode_t)(currentMode | WIFI_MODE_STA));
|
||||
} else {
|
||||
return mode((wifi_mode_t)(currentMode & (~WIFI_MODE_STA)));
|
||||
}
|
||||
} else {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* control AP mode
|
||||
* @param enable bool
|
||||
* @return ok
|
||||
*/
|
||||
bool WiFiGenericClass::enableAP(bool enable)
|
||||
{
|
||||
|
||||
wifi_mode_t currentMode = getMode();
|
||||
bool isEnabled = ((currentMode & WIFI_MODE_AP) != 0);
|
||||
|
||||
if(isEnabled != enable) {
|
||||
if(enable) {
|
||||
return mode((wifi_mode_t)(currentMode | WIFI_MODE_AP));
|
||||
} else {
|
||||
return mode((wifi_mode_t)(currentMode & (~WIFI_MODE_AP)));
|
||||
}
|
||||
} else {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// -----------------------------------------------------------------------------------------------------------------------
|
||||
// ------------------------------------------------ Generic Network function ---------------------------------------------
|
||||
// -----------------------------------------------------------------------------------------------------------------------
|
||||
|
||||
static bool _dns_busy = false;
|
||||
|
||||
/**
|
||||
* DNS callback
|
||||
* @param name
|
||||
* @param ipaddr
|
||||
* @param callback_arg
|
||||
*/
|
||||
static void wifi_dns_found_callback(const char *name, const ip_addr_t *ipaddr, void *callback_arg)
|
||||
{
|
||||
if(ipaddr) {
|
||||
(*reinterpret_cast<IPAddress*>(callback_arg)) = ipaddr->u_addr.ip4.addr;
|
||||
}
|
||||
_dns_busy = false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Resolve the given hostname to an IP address.
|
||||
* @param aHostname Name to be resolved
|
||||
* @param aResult IPAddress structure to store the returned IP address
|
||||
* @return 1 if aIPAddrString was successfully converted to an IP address,
|
||||
* else error code
|
||||
*/
|
||||
int WiFiGenericClass::hostByName(const char* aHostname, IPAddress& aResult)
|
||||
{
|
||||
ip_addr_t addr;
|
||||
aResult = static_cast<uint32_t>(0);
|
||||
|
||||
_dns_busy = true;
|
||||
err_t err = dns_gethostbyname(aHostname, &addr, &wifi_dns_found_callback, &aResult);
|
||||
if(err == ERR_OK && addr.u_addr.ip4.addr) {
|
||||
aResult = addr.u_addr.ip4.addr;
|
||||
_dns_busy = false;
|
||||
} else if(err == ERR_INPROGRESS) {
|
||||
while(_dns_busy){
|
||||
delay(1);
|
||||
}
|
||||
} else {
|
||||
_dns_busy = false;
|
||||
return 0;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
73
lib/esp32_lora_wifi/WiFi/src/WiFiGeneric.h
Normal file
73
lib/esp32_lora_wifi/WiFi/src/WiFiGeneric.h
Normal file
@@ -0,0 +1,73 @@
|
||||
/*
|
||||
ESP8266WiFiGeneric.h - esp8266 Wifi support.
|
||||
Based on WiFi.h from Ardiono WiFi shield library.
|
||||
Copyright (c) 2011-2014 Arduino. All right reserved.
|
||||
Modified by Ivan Grokhotkov, December 2014
|
||||
Reworked by Markus Sattler, December 2015
|
||||
|
||||
This library is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU Lesser General Public
|
||||
License as published by the Free Software Foundation; either
|
||||
version 2.1 of the License, or (at your option) any later version.
|
||||
|
||||
This library is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
Lesser General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public
|
||||
License along with this library; if not, write to the Free Software
|
||||
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
*/
|
||||
|
||||
#ifndef ESP32WIFIGENERIC_H_
|
||||
#define ESP32WIFIGENERIC_H_
|
||||
|
||||
#include "WiFiType.h"
|
||||
#include <esp_err.h>
|
||||
#include <esp_event_loop.h>
|
||||
|
||||
typedef void (*WiFiEventCb)(system_event_id_t event);
|
||||
|
||||
typedef struct {
|
||||
WiFiEventCb cb;
|
||||
system_event_id_t event;
|
||||
} WiFiEventCbList_t;
|
||||
|
||||
class WiFiGenericClass
|
||||
{
|
||||
public:
|
||||
|
||||
WiFiGenericClass();
|
||||
|
||||
void onEvent(WiFiEventCb cbEvent, system_event_id_t event = SYSTEM_EVENT_MAX);
|
||||
void removeEvent(WiFiEventCb cbEvent, system_event_id_t event = SYSTEM_EVENT_MAX);
|
||||
|
||||
int32_t channel(void);
|
||||
|
||||
void persistent(bool persistent);
|
||||
|
||||
static bool mode(wifi_mode_t);
|
||||
static wifi_mode_t getMode();
|
||||
|
||||
bool enableSTA(bool enable);
|
||||
bool enableAP(bool enable);
|
||||
|
||||
static esp_err_t _eventCallback(void *arg, system_event_t *event);
|
||||
|
||||
protected:
|
||||
static bool _persistent;
|
||||
static wifi_mode_t _forceSleepLastMode;
|
||||
|
||||
public:
|
||||
|
||||
int hostByName(const char* aHostname, IPAddress& aResult);
|
||||
|
||||
protected:
|
||||
|
||||
friend class WiFiSTAClass;
|
||||
friend class WiFiScanClass;
|
||||
friend class WiFiAPClass;
|
||||
};
|
||||
|
||||
#endif /* ESP32WIFIGENERIC_H_ */
|
||||
218
lib/esp32_lora_wifi/WiFi/src/WiFiMulti.cpp
Normal file
218
lib/esp32_lora_wifi/WiFi/src/WiFiMulti.cpp
Normal file
@@ -0,0 +1,218 @@
|
||||
/**
|
||||
*
|
||||
* @file ESP8266WiFiMulti.cpp
|
||||
* @date 16.05.2015
|
||||
* @author Markus Sattler
|
||||
*
|
||||
* Copyright (c) 2015 Markus Sattler. All rights reserved.
|
||||
* This file is part of the esp8266 core for Arduino environment.
|
||||
*
|
||||
* This library is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU Lesser General Public
|
||||
* License as published by the Free Software Foundation; either
|
||||
* version 2.1 of the License, or (at your option) any later version.
|
||||
*
|
||||
* This library is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public
|
||||
* License along with this library; if not, write to the Free Software
|
||||
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
*
|
||||
*/
|
||||
|
||||
#include "WiFiMulti.h"
|
||||
#include <limits.h>
|
||||
#include <string.h>
|
||||
#include <esp32-hal.h>
|
||||
|
||||
WiFiMulti::WiFiMulti()
|
||||
{
|
||||
}
|
||||
|
||||
WiFiMulti::~WiFiMulti()
|
||||
{
|
||||
APlistClean();
|
||||
}
|
||||
|
||||
bool WiFiMulti::addAP(const char* ssid, const char *passphrase)
|
||||
{
|
||||
return APlistAdd(ssid, passphrase);
|
||||
}
|
||||
|
||||
uint8_t WiFiMulti::run(void)
|
||||
{
|
||||
|
||||
int8_t scanResult;
|
||||
uint8_t status = WiFi.status();
|
||||
if(status == WL_DISCONNECTED || status == WL_NO_SSID_AVAIL || status == WL_IDLE_STATUS || status == WL_CONNECT_FAILED) {
|
||||
|
||||
scanResult = WiFi.scanComplete();
|
||||
if(scanResult == WIFI_SCAN_RUNNING) {
|
||||
// scan is running
|
||||
return WL_NO_SSID_AVAIL;
|
||||
} else if(scanResult > 0) {
|
||||
// scan done analyze
|
||||
WifiAPlist_t bestNetwork { NULL, NULL };
|
||||
int bestNetworkDb = INT_MIN;
|
||||
uint8_t bestBSSID[6];
|
||||
int32_t bestChannel = 0;
|
||||
|
||||
DEBUG_WIFI_MULTI("[WIFI] scan done\n");
|
||||
delay(0);
|
||||
|
||||
if(scanResult <= 0) {
|
||||
DEBUG_WIFI_MULTI("[WIFI] no networks found\n");
|
||||
} else {
|
||||
DEBUG_WIFI_MULTI("[WIFI] %d networks found\n", scanResult);
|
||||
for(int8_t i = 0; i < scanResult; ++i) {
|
||||
|
||||
String ssid_scan;
|
||||
int32_t rssi_scan;
|
||||
uint8_t sec_scan;
|
||||
uint8_t* BSSID_scan;
|
||||
int32_t chan_scan;
|
||||
|
||||
WiFi.getNetworkInfo(i, ssid_scan, sec_scan, rssi_scan, BSSID_scan, chan_scan);
|
||||
|
||||
bool known = false;
|
||||
for(uint32_t x = 0; x < APlist.size(); x++) {
|
||||
WifiAPlist_t entry = APlist[x];
|
||||
|
||||
if(ssid_scan == entry.ssid) { // SSID match
|
||||
known = true;
|
||||
if(rssi_scan > bestNetworkDb) { // best network
|
||||
if(sec_scan == WIFI_AUTH_OPEN || entry.passphrase) { // check for passphrase if not open wlan
|
||||
bestNetworkDb = rssi_scan;
|
||||
bestChannel = chan_scan;
|
||||
memcpy((void*) &bestNetwork, (void*) &entry, sizeof(bestNetwork));
|
||||
memcpy((void*) &bestBSSID, (void*) BSSID_scan, sizeof(bestBSSID));
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if(known) {
|
||||
DEBUG_WIFI_MULTI(" ---> ");
|
||||
} else {
|
||||
DEBUG_WIFI_MULTI(" ");
|
||||
}
|
||||
|
||||
DEBUG_WIFI_MULTI(" %d: [%d][%02X:%02X:%02X:%02X:%02X:%02X] %s (%d) %c\n", i, chan_scan, BSSID_scan[0], BSSID_scan[1], BSSID_scan[2], BSSID_scan[3], BSSID_scan[4], BSSID_scan[5], ssid_scan.c_str(), rssi_scan, (sec_scan == WIFI_AUTH_OPEN) ? ' ' : '*');
|
||||
delay(0);
|
||||
}
|
||||
}
|
||||
|
||||
// clean up ram
|
||||
WiFi.scanDelete();
|
||||
|
||||
DEBUG_WIFI_MULTI("\n\n");
|
||||
delay(0);
|
||||
|
||||
if(bestNetwork.ssid) {
|
||||
DEBUG_WIFI_MULTI("[WIFI] Connecting BSSID: %02X:%02X:%02X:%02X:%02X:%02X SSID: %s Channal: %d (%d)\n", bestBSSID[0], bestBSSID[1], bestBSSID[2], bestBSSID[3], bestBSSID[4], bestBSSID[5], bestNetwork.ssid, bestChannel, bestNetworkDb);
|
||||
|
||||
WiFi.begin(bestNetwork.ssid, bestNetwork.passphrase, bestChannel, bestBSSID);
|
||||
status = WiFi.status();
|
||||
|
||||
// wait for connection or fail
|
||||
while(status != WL_CONNECTED && status != WL_NO_SSID_AVAIL && status != WL_CONNECT_FAILED) {
|
||||
delay(10);
|
||||
status = WiFi.status();
|
||||
}
|
||||
|
||||
IPAddress ip;
|
||||
uint8_t * mac;
|
||||
switch(status) {
|
||||
case 3:
|
||||
ip = WiFi.localIP();
|
||||
mac = WiFi.BSSID();
|
||||
DEBUG_WIFI_MULTI("[WIFI] Connecting done.\n");
|
||||
DEBUG_WIFI_MULTI("[WIFI] SSID: %s\n", WiFi.SSID());
|
||||
DEBUG_WIFI_MULTI("[WIFI] IP: %d.%d.%d.%d\n", ip[0], ip[1], ip[2], ip[3]);
|
||||
DEBUG_WIFI_MULTI("[WIFI] MAC: %02X:%02X:%02X:%02X:%02X:%02X\n", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
|
||||
DEBUG_WIFI_MULTI("[WIFI] Channel: %d\n", WiFi.channel());
|
||||
break;
|
||||
case 1:
|
||||
DEBUG_WIFI_MULTI("[WIFI] Connecting Failed AP not found.\n");
|
||||
break;
|
||||
case 4:
|
||||
DEBUG_WIFI_MULTI("[WIFI] Connecting Failed.\n");
|
||||
break;
|
||||
default:
|
||||
DEBUG_WIFI_MULTI("[WIFI] Connecting Failed (%d).\n", status);
|
||||
break;
|
||||
}
|
||||
} else {
|
||||
DEBUG_WIFI_MULTI("[WIFI] no matching wifi found!\n");
|
||||
}
|
||||
} else {
|
||||
// start scan
|
||||
DEBUG_WIFI_MULTI("[WIFI] delete old wifi config...\n");
|
||||
WiFi.disconnect();
|
||||
|
||||
DEBUG_WIFI_MULTI("[WIFI] start scan\n");
|
||||
// scan wifi async mode
|
||||
WiFi.scanNetworks(true);
|
||||
}
|
||||
}
|
||||
return status;
|
||||
}
|
||||
|
||||
// ##################################################################################
|
||||
|
||||
bool WiFiMulti::APlistAdd(const char* ssid, const char *passphrase)
|
||||
{
|
||||
|
||||
WifiAPlist_t newAP;
|
||||
|
||||
if(!ssid || *ssid == 0x00 || strlen(ssid) > 31) {
|
||||
// fail SSID to long or missing!
|
||||
DEBUG_WIFI_MULTI("[WIFI][APlistAdd] no ssid or ssid to long\n");
|
||||
return false;
|
||||
}
|
||||
|
||||
if(passphrase && strlen(passphrase) > 63) {
|
||||
// fail passphrase to long!
|
||||
DEBUG_WIFI_MULTI("[WIFI][APlistAdd] passphrase to long\n");
|
||||
return false;
|
||||
}
|
||||
|
||||
newAP.ssid = strdup(ssid);
|
||||
|
||||
if(!newAP.ssid) {
|
||||
DEBUG_WIFI_MULTI("[WIFI][APlistAdd] fail newAP.ssid == 0\n");
|
||||
return false;
|
||||
}
|
||||
|
||||
if(passphrase && *passphrase != 0x00) {
|
||||
newAP.passphrase = strdup(passphrase);
|
||||
if(!newAP.passphrase) {
|
||||
DEBUG_WIFI_MULTI("[WIFI][APlistAdd] fail newAP.passphrase == 0\n");
|
||||
free(newAP.ssid);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
APlist.push_back(newAP);
|
||||
DEBUG_WIFI_MULTI("[WIFI][APlistAdd] add SSID: %s\n", newAP.ssid);
|
||||
return true;
|
||||
}
|
||||
|
||||
void WiFiMulti::APlistClean(void)
|
||||
{
|
||||
for(uint32_t i = 0; i < APlist.size(); i++) {
|
||||
WifiAPlist_t entry = APlist[i];
|
||||
if(entry.ssid) {
|
||||
free(entry.ssid);
|
||||
}
|
||||
if(entry.passphrase) {
|
||||
free(entry.passphrase);
|
||||
}
|
||||
}
|
||||
APlist.clear();
|
||||
}
|
||||
|
||||
66
lib/esp32_lora_wifi/WiFi/src/WiFiMulti.h
Normal file
66
lib/esp32_lora_wifi/WiFi/src/WiFiMulti.h
Normal file
@@ -0,0 +1,66 @@
|
||||
/**
|
||||
*
|
||||
* @file ESP8266WiFiMulti.h
|
||||
* @date 16.05.2015
|
||||
* @author Markus Sattler
|
||||
*
|
||||
* Copyright (c) 2015 Markus Sattler. All rights reserved.
|
||||
* This file is part of the esp8266 core for Arduino environment.
|
||||
*
|
||||
* This library is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU Lesser General Public
|
||||
* License as published by the Free Software Foundation; either
|
||||
* version 2.1 of the License, or (at your option) any later version.
|
||||
*
|
||||
* This library is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public
|
||||
* License along with this library; if not, write to the Free Software
|
||||
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef WIFICLIENTMULTI_H_
|
||||
#define WIFICLIENTMULTI_H_
|
||||
|
||||
#include "WiFi.h"
|
||||
#undef min
|
||||
#undef max
|
||||
#include <vector>
|
||||
|
||||
#ifdef DEBUG_ESP_WIFI
|
||||
#ifdef DEBUG_ESP_PORT
|
||||
#define DEBUG_WIFI_MULTI(...) DEBUG_ESP_PORT.printf( __VA_ARGS__ )
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#ifndef DEBUG_WIFI_MULTI
|
||||
#define DEBUG_WIFI_MULTI(...)
|
||||
#endif
|
||||
|
||||
typedef struct {
|
||||
char * ssid;
|
||||
char * passphrase;
|
||||
} WifiAPlist_t;
|
||||
|
||||
class WiFiMulti
|
||||
{
|
||||
public:
|
||||
WiFiMulti();
|
||||
~WiFiMulti();
|
||||
|
||||
bool addAP(const char* ssid, const char *passphrase = NULL);
|
||||
|
||||
uint8_t run(void);
|
||||
|
||||
private:
|
||||
std::vector<WifiAPlist_t> APlist;
|
||||
bool APlistAdd(const char* ssid, const char *passphrase = NULL);
|
||||
void APlistClean(void);
|
||||
|
||||
};
|
||||
|
||||
#endif /* WIFICLIENTMULTI_H_ */
|
||||
572
lib/esp32_lora_wifi/WiFi/src/WiFiSTA.cpp
Normal file
572
lib/esp32_lora_wifi/WiFi/src/WiFiSTA.cpp
Normal file
@@ -0,0 +1,572 @@
|
||||
/*
|
||||
ESP8266WiFiSTA.cpp - WiFi library for esp8266
|
||||
|
||||
Copyright (c) 2014 Ivan Grokhotkov. All rights reserved.
|
||||
This file is part of the esp8266 core for Arduino environment.
|
||||
|
||||
This library is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU Lesser General Public
|
||||
License as published by the Free Software Foundation; either
|
||||
version 2.1 of the License, or (at your option) any later version.
|
||||
|
||||
This library is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
Lesser General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public
|
||||
License along with this library; if not, write to the Free Software
|
||||
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
|
||||
Reworked on 28 Dec 2015 by Markus Sattler
|
||||
|
||||
*/
|
||||
|
||||
#include "WiFi.h"
|
||||
#include "WiFiGeneric.h"
|
||||
#include "WiFiSTA.h"
|
||||
|
||||
extern "C" {
|
||||
#include <stdint.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <inttypes.h>
|
||||
#include <string.h>
|
||||
#include <esp_err.h>
|
||||
#include <esp_wifi.h>
|
||||
#include <esp_event_loop.h>
|
||||
#include <esp32-hal.h>
|
||||
#include <lwip/ip_addr.h>
|
||||
#include "lwip/err.h"
|
||||
#include "lwip/dns.h"
|
||||
#include <esp_smartconfig.h>
|
||||
}
|
||||
|
||||
// -----------------------------------------------------------------------------------------------------------------------
|
||||
// ---------------------------------------------------- Private functions ------------------------------------------------
|
||||
// -----------------------------------------------------------------------------------------------------------------------
|
||||
|
||||
static bool sta_config_equal(const wifi_config_t& lhs, const wifi_config_t& rhs);
|
||||
|
||||
|
||||
/**
|
||||
* compare two STA configurations
|
||||
* @param lhs station_config
|
||||
* @param rhs station_config
|
||||
* @return equal
|
||||
*/
|
||||
static bool sta_config_equal(const wifi_config_t& lhs, const wifi_config_t& rhs)
|
||||
{
|
||||
if(strcmp(reinterpret_cast<const char*>(lhs.sta.ssid), reinterpret_cast<const char*>(rhs.sta.ssid)) != 0) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if(strcmp(reinterpret_cast<const char*>(lhs.sta.password), reinterpret_cast<const char*>(rhs.sta.password)) != 0) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if(lhs.sta.bssid_set != rhs.sta.bssid_set) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if(lhs.sta.bssid_set) {
|
||||
if(memcmp(lhs.sta.bssid, rhs.sta.bssid, 6) != 0) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
// -----------------------------------------------------------------------------------------------------------------------
|
||||
// ---------------------------------------------------- STA function -----------------------------------------------------
|
||||
// -----------------------------------------------------------------------------------------------------------------------
|
||||
|
||||
bool WiFiSTAClass::_useStaticIp = false;
|
||||
wl_status_t WiFiSTAClass::_status = WL_NO_SHIELD;
|
||||
/**
|
||||
* Start Wifi connection
|
||||
* if passphrase is set the most secure supported mode will be automatically selected
|
||||
* @param ssid const char* Pointer to the SSID string.
|
||||
* @param passphrase const char * Optional. Passphrase. Valid characters in a passphrase must be between ASCII 32-126 (decimal).
|
||||
* @param bssid uint8_t[6] Optional. BSSID / MAC of AP
|
||||
* @param channel Optional. Channel of AP
|
||||
* @param connect Optional. call connect
|
||||
* @return
|
||||
*/
|
||||
wl_status_t WiFiSTAClass::begin(const char* ssid, const char *passphrase, int32_t channel, const uint8_t* bssid, bool connect)
|
||||
{
|
||||
|
||||
if(!WiFi.enableSTA(true)) {
|
||||
// enable STA failed
|
||||
return WL_CONNECT_FAILED;
|
||||
}
|
||||
|
||||
if(!ssid || *ssid == 0x00 || strlen(ssid) > 31) {
|
||||
// fail SSID too long or missing!
|
||||
return WL_CONNECT_FAILED;
|
||||
}
|
||||
|
||||
if(passphrase && strlen(passphrase) > 63) {
|
||||
// fail passphrase too long!
|
||||
return WL_CONNECT_FAILED;
|
||||
}
|
||||
|
||||
wifi_config_t conf;
|
||||
strcpy(reinterpret_cast<char*>(conf.sta.ssid), ssid);
|
||||
|
||||
if(passphrase) {
|
||||
strcpy(reinterpret_cast<char*>(conf.sta.password), passphrase);
|
||||
} else {
|
||||
*conf.sta.password = 0;
|
||||
}
|
||||
|
||||
if(bssid) {
|
||||
conf.sta.bssid_set = 1;
|
||||
memcpy((void *) &conf.sta.bssid[0], (void *) bssid, 6);
|
||||
} else {
|
||||
conf.sta.bssid_set = 0;
|
||||
}
|
||||
|
||||
wifi_config_t current_conf;
|
||||
esp_wifi_get_config(WIFI_IF_STA, ¤t_conf);
|
||||
if(!sta_config_equal(current_conf, conf)) {
|
||||
esp_wifi_set_config(WIFI_IF_STA, &conf);
|
||||
}
|
||||
|
||||
if(channel > 0 && channel <= 13) {
|
||||
esp_wifi_set_channel(channel, WIFI_SECOND_CHAN_NONE);
|
||||
}
|
||||
|
||||
if(connect) {
|
||||
esp_wifi_connect();
|
||||
}
|
||||
|
||||
if(!_useStaticIp) {
|
||||
tcpip_adapter_dhcpc_start(TCPIP_ADAPTER_IF_STA);
|
||||
} else {
|
||||
tcpip_adapter_dhcpc_stop(TCPIP_ADAPTER_IF_STA);
|
||||
}
|
||||
|
||||
return status();
|
||||
}
|
||||
|
||||
wl_status_t WiFiSTAClass::begin(char* ssid, char *passphrase, int32_t channel, const uint8_t* bssid, bool connect)
|
||||
{
|
||||
return begin((const char*) ssid, (const char*) passphrase, channel, bssid, connect);
|
||||
}
|
||||
|
||||
/**
|
||||
* Use to connect to SDK config.
|
||||
* @return wl_status_t
|
||||
*/
|
||||
wl_status_t WiFiSTAClass::begin()
|
||||
{
|
||||
|
||||
if(!WiFi.enableSTA(true)) {
|
||||
// enable STA failed
|
||||
return WL_CONNECT_FAILED;
|
||||
}
|
||||
esp_wifi_start();
|
||||
esp_wifi_connect();
|
||||
|
||||
if(!_useStaticIp) {
|
||||
tcpip_adapter_dhcpc_start(TCPIP_ADAPTER_IF_STA);
|
||||
} else {
|
||||
tcpip_adapter_dhcpc_stop(TCPIP_ADAPTER_IF_STA);
|
||||
}
|
||||
|
||||
return status();
|
||||
}
|
||||
|
||||
void WiFiSTAClass::_setStatus(wl_status_t status)
|
||||
{
|
||||
_status = status;
|
||||
//log_i("wifi status: %d", status);
|
||||
}
|
||||
|
||||
/**
|
||||
* Change IP configuration settings disabling the dhcp client
|
||||
* @param local_ip Static ip configuration
|
||||
* @param gateway Static gateway configuration
|
||||
* @param subnet Static Subnet mask
|
||||
* @param dns1 Static DNS server 1
|
||||
* @param dns2 Static DNS server 2
|
||||
*/
|
||||
bool WiFiSTAClass::config(IPAddress local_ip, IPAddress gateway, IPAddress subnet, IPAddress dns1, IPAddress dns2)
|
||||
{
|
||||
|
||||
if(!WiFi.enableSTA(true)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
tcpip_adapter_ip_info_t info;
|
||||
info.ip.addr = static_cast<uint32_t>(local_ip);
|
||||
info.gw.addr = static_cast<uint32_t>(gateway);
|
||||
info.netmask.addr = static_cast<uint32_t>(subnet);
|
||||
|
||||
tcpip_adapter_dhcpc_stop(TCPIP_ADAPTER_IF_STA);
|
||||
if(tcpip_adapter_set_ip_info(TCPIP_ADAPTER_IF_STA, &info) == ESP_OK) {
|
||||
_useStaticIp = true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
ip_addr_t d;
|
||||
|
||||
if(dns1 != (uint32_t)0x00000000) {
|
||||
// Set DNS1-Server
|
||||
d.u_addr.ip4.addr = static_cast<uint32_t>(dns1);
|
||||
dns_setserver(0, &d);
|
||||
}
|
||||
|
||||
if(dns2 != (uint32_t)0x00000000) {
|
||||
// Set DNS2-Server
|
||||
d.u_addr.ip4.addr = static_cast<uint32_t>(dns2);
|
||||
dns_setserver(1, &d);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* will force a disconnect an then start reconnecting to AP
|
||||
* @return ok
|
||||
*/
|
||||
bool WiFiSTAClass::reconnect()
|
||||
{
|
||||
if((WiFi.getMode() & WIFI_MODE_STA) != 0) {
|
||||
if(esp_wifi_disconnect() == ESP_OK) {
|
||||
return esp_wifi_connect() == ESP_OK;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Disconnect from the network
|
||||
* @param wifioff
|
||||
* @return one value of wl_status_t enum
|
||||
*/
|
||||
bool WiFiSTAClass::disconnect(bool wifioff)
|
||||
{
|
||||
bool ret;
|
||||
wifi_config_t conf;
|
||||
*conf.sta.ssid = 0;
|
||||
*conf.sta.password = 0;
|
||||
|
||||
esp_wifi_set_config(WIFI_IF_STA, &conf);
|
||||
ret = esp_wifi_set_config(WIFI_IF_STA, &conf) == ESP_OK;
|
||||
|
||||
if(wifioff) {
|
||||
WiFi.enableSTA(false);
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
/**
|
||||
* is STA interface connected?
|
||||
* @return true if STA is connected to an AD
|
||||
*/
|
||||
bool WiFiSTAClass::isConnected()
|
||||
{
|
||||
return (status() == WL_CONNECTED);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Setting the ESP32 station to connect to the AP (which is recorded)
|
||||
* automatically or not when powered on. Enable auto-connect by default.
|
||||
* @param autoConnect bool
|
||||
* @return if saved
|
||||
*/
|
||||
bool WiFiSTAClass::setAutoConnect(bool autoConnect)
|
||||
{
|
||||
bool ret;
|
||||
ret = esp_wifi_set_auto_connect(autoConnect);
|
||||
return ret;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if ESP32 station mode will connect to AP
|
||||
* automatically or not when it is powered on.
|
||||
* @return auto connect
|
||||
*/
|
||||
bool WiFiSTAClass::getAutoConnect()
|
||||
{
|
||||
bool autoConnect;
|
||||
esp_wifi_get_auto_connect(&autoConnect);
|
||||
return autoConnect;
|
||||
}
|
||||
|
||||
/**
|
||||
* Wait for WiFi connection to reach a result
|
||||
* returns the status reached or disconnect if STA is off
|
||||
* @return wl_status_t
|
||||
*/
|
||||
uint8_t WiFiSTAClass::waitForConnectResult()
|
||||
{
|
||||
//1 and 3 have STA enabled
|
||||
if((WiFiGenericClass::getMode() & WIFI_MODE_STA) == 0) {
|
||||
return WL_DISCONNECTED;
|
||||
}
|
||||
int i = 0;
|
||||
while(status() >= WL_DISCONNECTED && i++ < 100) {
|
||||
delay(100);
|
||||
}
|
||||
return status();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the station interface IP address.
|
||||
* @return IPAddress station IP
|
||||
*/
|
||||
IPAddress WiFiSTAClass::localIP()
|
||||
{
|
||||
tcpip_adapter_ip_info_t ip;
|
||||
tcpip_adapter_get_ip_info(TCPIP_ADAPTER_IF_STA, &ip);
|
||||
return IPAddress(ip.ip.addr);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Get the station interface MAC address.
|
||||
* @param mac pointer to uint8_t array with length WL_MAC_ADDR_LENGTH
|
||||
* @return pointer to uint8_t *
|
||||
*/
|
||||
uint8_t* WiFiSTAClass::macAddress(uint8_t* mac)
|
||||
{
|
||||
esp_wifi_get_mac(WIFI_IF_STA, mac);
|
||||
return mac;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the station interface MAC address.
|
||||
* @return String mac
|
||||
*/
|
||||
String WiFiSTAClass::macAddress(void)
|
||||
{
|
||||
uint8_t mac[6];
|
||||
char macStr[18] = { 0 };
|
||||
esp_wifi_get_mac(WIFI_IF_STA, mac);
|
||||
|
||||
sprintf(macStr, "%02X:%02X:%02X:%02X:%02X:%02X", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
|
||||
return String(macStr);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the interface subnet mask address.
|
||||
* @return IPAddress subnetMask
|
||||
*/
|
||||
IPAddress WiFiSTAClass::subnetMask()
|
||||
{
|
||||
tcpip_adapter_ip_info_t ip;
|
||||
tcpip_adapter_get_ip_info(TCPIP_ADAPTER_IF_STA, &ip);
|
||||
return IPAddress(ip.netmask.addr);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the gateway ip address.
|
||||
* @return IPAddress gatewayIP
|
||||
*/
|
||||
IPAddress WiFiSTAClass::gatewayIP()
|
||||
{
|
||||
tcpip_adapter_ip_info_t ip;
|
||||
tcpip_adapter_get_ip_info(TCPIP_ADAPTER_IF_STA, &ip);
|
||||
return IPAddress(ip.gw.addr);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the DNS ip address.
|
||||
* @param dns_no
|
||||
* @return IPAddress DNS Server IP
|
||||
*/
|
||||
IPAddress WiFiSTAClass::dnsIP(uint8_t dns_no)
|
||||
{
|
||||
ip_addr_t dns_ip = dns_getserver(dns_no);
|
||||
return IPAddress(dns_ip.u_addr.ip4.addr);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return Connection status.
|
||||
* @return one of the value defined in wl_status_t
|
||||
*
|
||||
*/
|
||||
wl_status_t WiFiSTAClass::status()
|
||||
{
|
||||
return WiFiSTAClass::_status;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the current SSID associated with the network
|
||||
* @return SSID
|
||||
*/
|
||||
String WiFiSTAClass::SSID() const
|
||||
{
|
||||
wifi_ap_record_t info;
|
||||
if(!esp_wifi_sta_get_ap_info(&info)) {
|
||||
return String(reinterpret_cast<char*>(info.ssid));
|
||||
}
|
||||
return String();
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the current pre shared key associated with the network
|
||||
* @return psk string
|
||||
*/
|
||||
String WiFiSTAClass::psk() const
|
||||
{
|
||||
wifi_config_t conf;
|
||||
esp_wifi_get_config(WIFI_IF_STA, &conf);
|
||||
return String(reinterpret_cast<char*>(conf.sta.password));
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the current bssid / mac associated with the network if configured
|
||||
* @return bssid uint8_t *
|
||||
*/
|
||||
uint8_t* WiFiSTAClass::BSSID(void)
|
||||
{
|
||||
static uint8_t bssid[6];
|
||||
wifi_ap_record_t info;
|
||||
if(!esp_wifi_sta_get_ap_info(&info)) {
|
||||
memcpy(bssid, info.bssid, 6);
|
||||
return reinterpret_cast<uint8_t*>(bssid);
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the current bssid / mac associated with the network if configured
|
||||
* @return String bssid mac
|
||||
*/
|
||||
String WiFiSTAClass::BSSIDstr(void)
|
||||
{
|
||||
uint8_t* bssid = BSSID();
|
||||
if(!bssid){
|
||||
return String();
|
||||
}
|
||||
char mac[18] = { 0 };
|
||||
sprintf(mac, "%02X:%02X:%02X:%02X:%02X:%02X", bssid[0], bssid[1], bssid[2], bssid[3], bssid[4], bssid[5]);
|
||||
return String(mac);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the current network RSSI.
|
||||
* @return RSSI value
|
||||
*/
|
||||
int8_t WiFiSTAClass::RSSI(void)
|
||||
{
|
||||
wifi_ap_record_t info;
|
||||
if(!esp_wifi_sta_get_ap_info(&info)) {
|
||||
return info.rssi;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the station interface Host name.
|
||||
* @return char array hostname
|
||||
*/
|
||||
const char * WiFiSTAClass::getHostname()
|
||||
{
|
||||
const char * hostname;
|
||||
if(tcpip_adapter_get_hostname(TCPIP_ADAPTER_IF_STA, &hostname)){
|
||||
return NULL;
|
||||
}
|
||||
return hostname;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the station interface Host name.
|
||||
* @param hostname pointer to const string
|
||||
* @return true on success
|
||||
*/
|
||||
bool WiFiSTAClass::setHostname(const char * hostname)
|
||||
{
|
||||
return tcpip_adapter_set_hostname(TCPIP_ADAPTER_IF_STA, hostname) == 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Enable IPv6 on the station interface.
|
||||
* @return true on success
|
||||
*/
|
||||
bool WiFiSTAClass::enableIpV6()
|
||||
{
|
||||
return tcpip_adapter_create_ip6_linklocal(TCPIP_ADAPTER_IF_STA) == 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the station interface IPv6 address.
|
||||
* @return IPv6Address
|
||||
*/
|
||||
IPv6Address WiFiSTAClass::localIPv6()
|
||||
{
|
||||
static ip6_addr_t addr;
|
||||
if(tcpip_adapter_get_ip6_linklocal(TCPIP_ADAPTER_IF_STA, &addr)){
|
||||
return IPv6Address();
|
||||
}
|
||||
return IPv6Address(addr.addr);
|
||||
}
|
||||
|
||||
|
||||
bool WiFiSTAClass::_smartConfigStarted = false;
|
||||
bool WiFiSTAClass::_smartConfigDone = false;
|
||||
|
||||
|
||||
bool WiFiSTAClass::beginSmartConfig() {
|
||||
if (_smartConfigStarted) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!WiFi.mode(WIFI_STA)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
esp_err_t err;
|
||||
err = esp_smartconfig_start(reinterpret_cast<sc_callback_t>(&WiFiSTAClass::_smartConfigCallback), 1);
|
||||
if (err == ESP_OK) {
|
||||
_smartConfigStarted = true;
|
||||
_smartConfigDone = false;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
bool WiFiSTAClass::stopSmartConfig() {
|
||||
if (!_smartConfigStarted) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (esp_smartconfig_stop() == ESP_OK) {
|
||||
_smartConfigStarted = false;
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
bool WiFiSTAClass::smartConfigDone() {
|
||||
if (!_smartConfigStarted) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return _smartConfigDone;
|
||||
}
|
||||
|
||||
void WiFiSTAClass::_smartConfigCallback(uint32_t st, void* result) {
|
||||
smartconfig_status_t status = (smartconfig_status_t) st;
|
||||
if (status == SC_STATUS_LINK) {
|
||||
wifi_sta_config_t *sta_conf = reinterpret_cast<wifi_sta_config_t *>(result);
|
||||
|
||||
esp_wifi_set_config(WIFI_IF_AP, (wifi_config_t *)sta_conf);
|
||||
esp_wifi_disconnect();
|
||||
esp_wifi_connect();
|
||||
|
||||
_smartConfigDone = true;
|
||||
} else if (status == SC_STATUS_LINK_OVER) {
|
||||
WiFi.stopSmartConfig();
|
||||
}
|
||||
}
|
||||
101
lib/esp32_lora_wifi/WiFi/src/WiFiSTA.h
Normal file
101
lib/esp32_lora_wifi/WiFi/src/WiFiSTA.h
Normal file
@@ -0,0 +1,101 @@
|
||||
/*
|
||||
ESP8266WiFiSTA.h - esp8266 Wifi support.
|
||||
Based on WiFi.h from Ardiono WiFi shield library.
|
||||
Copyright (c) 2011-2014 Arduino. All right reserved.
|
||||
Modified by Ivan Grokhotkov, December 2014
|
||||
Reworked by Markus Sattler, December 2015
|
||||
|
||||
This library is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU Lesser General Public
|
||||
License as published by the Free Software Foundation; either
|
||||
version 2.1 of the License, or (at your option) any later version.
|
||||
|
||||
This library is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
Lesser General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public
|
||||
License along with this library; if not, write to the Free Software
|
||||
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
*/
|
||||
|
||||
#ifndef ESP32WIFISTA_H_
|
||||
#define ESP32WIFISTA_H_
|
||||
|
||||
|
||||
#include "WiFiType.h"
|
||||
#include "WiFiGeneric.h"
|
||||
|
||||
|
||||
class WiFiSTAClass
|
||||
{
|
||||
// ----------------------------------------------------------------------------------------------
|
||||
// ---------------------------------------- STA function ----------------------------------------
|
||||
// ----------------------------------------------------------------------------------------------
|
||||
|
||||
public:
|
||||
|
||||
wl_status_t begin(const char* ssid, const char *passphrase = NULL, int32_t channel = 0, const uint8_t* bssid = NULL, bool connect = true);
|
||||
wl_status_t begin(char* ssid, char *passphrase = NULL, int32_t channel = 0, const uint8_t* bssid = NULL, bool connect = true);
|
||||
wl_status_t begin();
|
||||
|
||||
bool config(IPAddress local_ip, IPAddress gateway, IPAddress subnet, IPAddress dns1 = (uint32_t)0x00000000, IPAddress dns2 = (uint32_t)0x00000000);
|
||||
|
||||
bool reconnect();
|
||||
bool disconnect(bool wifioff = false);
|
||||
|
||||
bool isConnected();
|
||||
|
||||
bool setAutoConnect(bool autoConnect);
|
||||
bool getAutoConnect();
|
||||
|
||||
bool setAutoReconnect(bool autoReconnect);
|
||||
|
||||
uint8_t waitForConnectResult();
|
||||
|
||||
// STA network info
|
||||
IPAddress localIP();
|
||||
|
||||
uint8_t * macAddress(uint8_t* mac);
|
||||
String macAddress();
|
||||
|
||||
IPAddress subnetMask();
|
||||
IPAddress gatewayIP();
|
||||
IPAddress dnsIP(uint8_t dns_no = 0);
|
||||
|
||||
bool enableIpV6();
|
||||
IPv6Address localIPv6();
|
||||
|
||||
const char * getHostname();
|
||||
bool setHostname(const char * hostname);
|
||||
|
||||
// STA WiFi info
|
||||
wl_status_t status();
|
||||
String SSID() const;
|
||||
String psk() const;
|
||||
|
||||
uint8_t * BSSID();
|
||||
String BSSIDstr();
|
||||
|
||||
int8_t RSSI();
|
||||
|
||||
static void _setStatus(wl_status_t status);
|
||||
protected:
|
||||
static wl_status_t _status;
|
||||
static bool _useStaticIp;
|
||||
|
||||
public:
|
||||
bool beginSmartConfig();
|
||||
bool stopSmartConfig();
|
||||
bool smartConfigDone();
|
||||
|
||||
protected:
|
||||
static bool _smartConfigStarted;
|
||||
static bool _smartConfigDone;
|
||||
static void _smartConfigCallback(uint32_t status, void* result);
|
||||
|
||||
};
|
||||
|
||||
|
||||
#endif /* ESP32WIFISTA_H_ */
|
||||
268
lib/esp32_lora_wifi/WiFi/src/WiFiScan.cpp
Normal file
268
lib/esp32_lora_wifi/WiFi/src/WiFiScan.cpp
Normal file
@@ -0,0 +1,268 @@
|
||||
/*
|
||||
ESP8266WiFiScan.cpp - WiFi library for esp8266
|
||||
|
||||
Copyright (c) 2014 Ivan Grokhotkov. All rights reserved.
|
||||
This file is part of the esp8266 core for Arduino environment.
|
||||
|
||||
This library is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU Lesser General Public
|
||||
License as published by the Free Software Foundation; either
|
||||
version 2.1 of the License, or (at your option) any later version.
|
||||
|
||||
This library is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
Lesser General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public
|
||||
License along with this library; if not, write to the Free Software
|
||||
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
|
||||
Reworked on 28 Dec 2015 by Markus Sattler
|
||||
|
||||
*/
|
||||
|
||||
#include "WiFi.h"
|
||||
#include "WiFiGeneric.h"
|
||||
#include "WiFiScan.h"
|
||||
|
||||
extern "C" {
|
||||
#include <stdint.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <inttypes.h>
|
||||
#include <string.h>
|
||||
#include <esp_err.h>
|
||||
#include <esp_wifi.h>
|
||||
#include <esp_event_loop.h>
|
||||
#include <esp32-hal.h>
|
||||
#include <lwip/ip_addr.h>
|
||||
#include "lwip/err.h"
|
||||
}
|
||||
|
||||
bool WiFiScanClass::_scanAsync = false;
|
||||
bool WiFiScanClass::_scanStarted = false;
|
||||
bool WiFiScanClass::_scanComplete = false;
|
||||
|
||||
uint16_t WiFiScanClass::_scanCount = 0;
|
||||
void* WiFiScanClass::_scanResult = 0;
|
||||
|
||||
/**
|
||||
* Start scan WiFi networks available
|
||||
* @param async run in async mode
|
||||
* @param show_hidden show hidden networks
|
||||
* @return Number of discovered networks
|
||||
*/
|
||||
int8_t WiFiScanClass::scanNetworks(bool async, bool show_hidden)
|
||||
{
|
||||
if(WiFiScanClass::_scanStarted) {
|
||||
return WIFI_SCAN_RUNNING;
|
||||
}
|
||||
|
||||
WiFiScanClass::_scanAsync = async;
|
||||
|
||||
WiFi.enableSTA(true);
|
||||
|
||||
scanDelete();
|
||||
|
||||
wifi_scan_config_t config;
|
||||
config.ssid = 0;
|
||||
config.bssid = 0;
|
||||
config.channel = 0;
|
||||
config.show_hidden = show_hidden;
|
||||
if(esp_wifi_scan_start(&config, WiFiScanClass::_scanAsync) == ESP_OK) {
|
||||
WiFiScanClass::_scanComplete = false;
|
||||
WiFiScanClass::_scanStarted = true;
|
||||
|
||||
if(WiFiScanClass::_scanAsync) {
|
||||
return WIFI_SCAN_RUNNING;
|
||||
}
|
||||
while(!(WiFiScanClass::_scanComplete)) {
|
||||
delay(10);
|
||||
}
|
||||
return WiFiScanClass::_scanCount;
|
||||
} else {
|
||||
return WIFI_SCAN_FAILED;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* private
|
||||
* scan callback
|
||||
* @param result void *arg
|
||||
* @param status STATUS
|
||||
*/
|
||||
void WiFiScanClass::_scanDone()
|
||||
{
|
||||
WiFiScanClass::_scanComplete = true;
|
||||
WiFiScanClass::_scanStarted = false;
|
||||
esp_wifi_scan_get_ap_num(&(WiFiScanClass::_scanCount));
|
||||
if(WiFiScanClass::_scanCount) {
|
||||
WiFiScanClass::_scanResult = new wifi_ap_record_t[WiFiScanClass::_scanCount];
|
||||
if(WiFiScanClass::_scanResult) {
|
||||
esp_wifi_scan_get_ap_records(&(WiFiScanClass::_scanCount), (wifi_ap_record_t*)_scanResult);
|
||||
} else {
|
||||
//no memory
|
||||
WiFiScanClass::_scanCount = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param i specify from which network item want to get the information
|
||||
* @return bss_info *
|
||||
*/
|
||||
void * WiFiScanClass::_getScanInfoByIndex(int i)
|
||||
{
|
||||
if(!WiFiScanClass::_scanResult || (size_t) i > WiFiScanClass::_scanCount) {
|
||||
return 0;
|
||||
}
|
||||
return reinterpret_cast<wifi_ap_record_t*>(WiFiScanClass::_scanResult) + i;
|
||||
}
|
||||
|
||||
/**
|
||||
* called to get the scan state in Async mode
|
||||
* @return scan result or status
|
||||
* -1 if scan not fin
|
||||
* -2 if scan not triggered
|
||||
*/
|
||||
int8_t WiFiScanClass::scanComplete()
|
||||
{
|
||||
|
||||
if(_scanStarted) {
|
||||
return WIFI_SCAN_RUNNING;
|
||||
}
|
||||
|
||||
if(_scanComplete) {
|
||||
return WiFiScanClass::_scanCount;
|
||||
}
|
||||
|
||||
return WIFI_SCAN_FAILED;
|
||||
}
|
||||
|
||||
/**
|
||||
* delete last scan result from RAM
|
||||
*/
|
||||
void WiFiScanClass::scanDelete()
|
||||
{
|
||||
if(WiFiScanClass::_scanResult) {
|
||||
delete[] reinterpret_cast<wifi_ap_record_t*>(WiFiScanClass::_scanResult);
|
||||
WiFiScanClass::_scanResult = 0;
|
||||
WiFiScanClass::_scanCount = 0;
|
||||
}
|
||||
_scanComplete = false;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* loads all infos from a scanned wifi in to the ptr parameters
|
||||
* @param networkItem uint8_t
|
||||
* @param ssid const char**
|
||||
* @param encryptionType uint8_t *
|
||||
* @param RSSI int32_t *
|
||||
* @param BSSID uint8_t **
|
||||
* @param channel int32_t *
|
||||
* @return (true if ok)
|
||||
*/
|
||||
bool WiFiScanClass::getNetworkInfo(uint8_t i, String &ssid, uint8_t &encType, int32_t &rssi, uint8_t* &bssid, int32_t &channel)
|
||||
{
|
||||
wifi_ap_record_t* it = reinterpret_cast<wifi_ap_record_t*>(_getScanInfoByIndex(i));
|
||||
if(!it) {
|
||||
return false;
|
||||
}
|
||||
ssid = (const char*) it->ssid;
|
||||
encType = it->authmode;
|
||||
rssi = it->rssi;
|
||||
bssid = it->bssid;
|
||||
channel = it->primary;
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Return the SSID discovered during the network scan.
|
||||
* @param i specify from which network item want to get the information
|
||||
* @return ssid string of the specified item on the networks scanned list
|
||||
*/
|
||||
String WiFiScanClass::SSID(uint8_t i)
|
||||
{
|
||||
wifi_ap_record_t* it = reinterpret_cast<wifi_ap_record_t*>(_getScanInfoByIndex(i));
|
||||
if(!it) {
|
||||
return String();
|
||||
}
|
||||
return String(reinterpret_cast<const char*>(it->ssid));
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Return the encryption type of the networks discovered during the scanNetworks
|
||||
* @param i specify from which network item want to get the information
|
||||
* @return encryption type (enum wl_enc_type) of the specified item on the networks scanned list
|
||||
*/
|
||||
wifi_auth_mode_t WiFiScanClass::encryptionType(uint8_t i)
|
||||
{
|
||||
wifi_ap_record_t* it = reinterpret_cast<wifi_ap_record_t*>(_getScanInfoByIndex(i));
|
||||
if(!it) {
|
||||
return WIFI_AUTH_OPEN;
|
||||
}
|
||||
return it->authmode;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the RSSI of the networks discovered during the scanNetworks
|
||||
* @param i specify from which network item want to get the information
|
||||
* @return signed value of RSSI of the specified item on the networks scanned list
|
||||
*/
|
||||
int32_t WiFiScanClass::RSSI(uint8_t i)
|
||||
{
|
||||
wifi_ap_record_t* it = reinterpret_cast<wifi_ap_record_t*>(_getScanInfoByIndex(i));
|
||||
if(!it) {
|
||||
return 0;
|
||||
}
|
||||
return it->rssi;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* return MAC / BSSID of scanned wifi
|
||||
* @param i specify from which network item want to get the information
|
||||
* @return uint8_t * MAC / BSSID of scanned wifi
|
||||
*/
|
||||
uint8_t * WiFiScanClass::BSSID(uint8_t i)
|
||||
{
|
||||
wifi_ap_record_t* it = reinterpret_cast<wifi_ap_record_t*>(_getScanInfoByIndex(i));
|
||||
if(!it) {
|
||||
return 0;
|
||||
}
|
||||
return it->bssid;
|
||||
}
|
||||
|
||||
/**
|
||||
* return MAC / BSSID of scanned wifi
|
||||
* @param i specify from which network item want to get the information
|
||||
* @return String MAC / BSSID of scanned wifi
|
||||
*/
|
||||
String WiFiScanClass::BSSIDstr(uint8_t i)
|
||||
{
|
||||
char mac[18] = { 0 };
|
||||
wifi_ap_record_t* it = reinterpret_cast<wifi_ap_record_t*>(_getScanInfoByIndex(i));
|
||||
if(!it) {
|
||||
return String();
|
||||
}
|
||||
sprintf(mac, "%02X:%02X:%02X:%02X:%02X:%02X", it->bssid[0], it->bssid[1], it->bssid[2], it->bssid[3], it->bssid[4], it->bssid[5]);
|
||||
return String(mac);
|
||||
}
|
||||
|
||||
int32_t WiFiScanClass::channel(uint8_t i)
|
||||
{
|
||||
wifi_ap_record_t* it = reinterpret_cast<wifi_ap_record_t*>(_getScanInfoByIndex(i));
|
||||
if(!it) {
|
||||
return 0;
|
||||
}
|
||||
return it->primary;
|
||||
}
|
||||
|
||||
64
lib/esp32_lora_wifi/WiFi/src/WiFiScan.h
Normal file
64
lib/esp32_lora_wifi/WiFi/src/WiFiScan.h
Normal file
@@ -0,0 +1,64 @@
|
||||
/*
|
||||
ESP8266WiFiScan.h - esp8266 Wifi support.
|
||||
Based on WiFi.h from Ardiono WiFi shield library.
|
||||
Copyright (c) 2011-2014 Arduino. All right reserved.
|
||||
Modified by Ivan Grokhotkov, December 2014
|
||||
Reworked by Markus Sattler, December 2015
|
||||
|
||||
This library is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU Lesser General Public
|
||||
License as published by the Free Software Foundation; either
|
||||
version 2.1 of the License, or (at your option) any later version.
|
||||
|
||||
This library is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
Lesser General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public
|
||||
License along with this library; if not, write to the Free Software
|
||||
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
*/
|
||||
|
||||
#ifndef ESP32WIFISCAN_H_
|
||||
#define ESP32WIFISCAN_H_
|
||||
|
||||
#include "WiFiType.h"
|
||||
#include "WiFiGeneric.h"
|
||||
|
||||
class WiFiScanClass
|
||||
{
|
||||
|
||||
public:
|
||||
|
||||
int8_t scanNetworks(bool async = false, bool show_hidden = false);
|
||||
|
||||
int8_t scanComplete();
|
||||
void scanDelete();
|
||||
|
||||
// scan result
|
||||
bool getNetworkInfo(uint8_t networkItem, String &ssid, uint8_t &encryptionType, int32_t &RSSI, uint8_t* &BSSID, int32_t &channel);
|
||||
|
||||
String SSID(uint8_t networkItem);
|
||||
wifi_auth_mode_t encryptionType(uint8_t networkItem);
|
||||
int32_t RSSI(uint8_t networkItem);
|
||||
uint8_t * BSSID(uint8_t networkItem);
|
||||
String BSSIDstr(uint8_t networkItem);
|
||||
int32_t channel(uint8_t networkItem);
|
||||
|
||||
static void _scanDone();
|
||||
protected:
|
||||
|
||||
static bool _scanAsync;
|
||||
static bool _scanStarted;
|
||||
static bool _scanComplete;
|
||||
|
||||
static uint16_t _scanCount;
|
||||
static void* _scanResult;
|
||||
|
||||
static void * _getScanInfoByIndex(int i);
|
||||
|
||||
};
|
||||
|
||||
|
||||
#endif /* ESP32WIFISCAN_H_ */
|
||||
77
lib/esp32_lora_wifi/WiFi/src/WiFiServer.cpp
Normal file
77
lib/esp32_lora_wifi/WiFi/src/WiFiServer.cpp
Normal file
@@ -0,0 +1,77 @@
|
||||
/*
|
||||
Server.cpp - Server class for Raspberry Pi
|
||||
Copyright (c) 2016 Hristo Gochkov All right reserved.
|
||||
|
||||
This library is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU Lesser General Public
|
||||
License as published by the Free Software Foundation; either
|
||||
version 2.1 of the License, or (at your option) any later version.
|
||||
|
||||
This library is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
Lesser General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public
|
||||
License along with this library; if not, write to the Free Software
|
||||
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
*/
|
||||
#include "WiFiServer.h"
|
||||
#include <lwip/sockets.h>
|
||||
#include <lwip/netdb.h>
|
||||
|
||||
#undef write
|
||||
|
||||
int WiFiServer::setTimeout(uint32_t seconds){
|
||||
struct timeval tv;
|
||||
tv.tv_sec = seconds;
|
||||
tv.tv_usec = 0;
|
||||
if(setsockopt(sockfd, SOL_SOCKET, SO_RCVTIMEO, (char *)&tv, sizeof(struct timeval)) < 0)
|
||||
return -1;
|
||||
return setsockopt(sockfd, SOL_SOCKET, SO_SNDTIMEO, (char *)&tv, sizeof(struct timeval));
|
||||
}
|
||||
|
||||
size_t WiFiServer::write(const uint8_t *data, size_t len){
|
||||
return 0;
|
||||
}
|
||||
|
||||
void WiFiServer::stopAll(){}
|
||||
|
||||
WiFiClient WiFiServer::available(){
|
||||
if(!_listening)
|
||||
return WiFiClient();
|
||||
struct sockaddr_in _client;
|
||||
int cs = sizeof(struct sockaddr_in);
|
||||
int client_sock = accept(sockfd, (struct sockaddr *)&_client, (socklen_t*)&cs);
|
||||
if(client_sock >= 0){
|
||||
int val = 1;
|
||||
if(setsockopt(client_sock, SOL_SOCKET, SO_KEEPALIVE, (char*)&val, sizeof(int)) == ESP_OK)
|
||||
return WiFiClient(client_sock);
|
||||
}
|
||||
return WiFiClient();
|
||||
}
|
||||
|
||||
void WiFiServer::begin(){
|
||||
if(_listening)
|
||||
return;
|
||||
struct sockaddr_in server;
|
||||
sockfd = socket(AF_INET , SOCK_STREAM, 0);
|
||||
if (sockfd < 0)
|
||||
return;
|
||||
server.sin_family = AF_INET;
|
||||
server.sin_addr.s_addr = INADDR_ANY;
|
||||
server.sin_port = htons(_port);
|
||||
if(bind(sockfd, (struct sockaddr *)&server, sizeof(server)) < 0)
|
||||
return;
|
||||
if(listen(sockfd , _max_clients) < 0)
|
||||
return;
|
||||
fcntl(sockfd, F_SETFL, O_NONBLOCK);
|
||||
_listening = true;
|
||||
}
|
||||
|
||||
void WiFiServer::end(){
|
||||
close(sockfd);
|
||||
sockfd = -1;
|
||||
_listening = false;
|
||||
}
|
||||
|
||||
53
lib/esp32_lora_wifi/WiFi/src/WiFiServer.h
Normal file
53
lib/esp32_lora_wifi/WiFi/src/WiFiServer.h
Normal file
@@ -0,0 +1,53 @@
|
||||
/*
|
||||
Server.h - Server class for Raspberry Pi
|
||||
Copyright (c) 2016 Hristo Gochkov All right reserved.
|
||||
|
||||
This library is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU Lesser General Public
|
||||
License as published by the Free Software Foundation; either
|
||||
version 2.1 of the License, or (at your option) any later version.
|
||||
|
||||
This library is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
Lesser General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public
|
||||
License along with this library; if not, write to the Free Software
|
||||
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
*/
|
||||
#ifndef _WIFISERVER_H_
|
||||
#define _WIFISERVER_H_
|
||||
|
||||
#include "Arduino.h"
|
||||
#include "Server.h"
|
||||
#include "WiFiClient.h"
|
||||
|
||||
class WiFiServer : public Server {
|
||||
private:
|
||||
int sockfd;
|
||||
uint16_t _port;
|
||||
uint8_t _max_clients;
|
||||
bool _listening;
|
||||
|
||||
public:
|
||||
void listenOnLocalhost(){}
|
||||
|
||||
WiFiServer(uint16_t port=80, uint8_t max_clients=4):sockfd(-1),_port(port),_max_clients(max_clients),_listening(false){}
|
||||
~WiFiServer(){ end();}
|
||||
WiFiClient available();
|
||||
WiFiClient accept(){return available();}
|
||||
void begin();
|
||||
size_t write(const uint8_t *data, size_t len);
|
||||
size_t write(uint8_t data){
|
||||
return write(&data, 1);
|
||||
}
|
||||
using Print::write;
|
||||
|
||||
void end();
|
||||
operator bool(){return _listening;}
|
||||
int setTimeout(uint32_t seconds);
|
||||
void stopAll();
|
||||
};
|
||||
|
||||
#endif /* _WIFISERVER_H_ */
|
||||
48
lib/esp32_lora_wifi/WiFi/src/WiFiType.h
Normal file
48
lib/esp32_lora_wifi/WiFi/src/WiFiType.h
Normal file
@@ -0,0 +1,48 @@
|
||||
/*
|
||||
ESP8266WiFiType.h - esp8266 Wifi support.
|
||||
Copyright (c) 2011-2014 Arduino. All right reserved.
|
||||
Modified by Ivan Grokhotkov, December 2014
|
||||
Reworked by Markus Sattler, December 2015
|
||||
|
||||
This library is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU Lesser General Public
|
||||
License as published by the Free Software Foundation; either
|
||||
version 2.1 of the License, or (at your option) any later version.
|
||||
|
||||
This library is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
Lesser General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public
|
||||
License along with this library; if not, write to the Free Software
|
||||
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
*/
|
||||
|
||||
|
||||
#ifndef ESP32WIFITYPE_H_
|
||||
#define ESP32WIFITYPE_H_
|
||||
|
||||
#define WIFI_SCAN_RUNNING (-1)
|
||||
#define WIFI_SCAN_FAILED (-2)
|
||||
|
||||
#define WiFiMode_t wifi_mode_t
|
||||
#define WIFI_OFF WIFI_MODE_NULL
|
||||
#define WIFI_STA WIFI_MODE_STA
|
||||
#define WIFI_AP WIFI_MODE_AP
|
||||
#define WIFI_AP_STA WIFI_MODE_APSTA
|
||||
|
||||
#define WiFiEvent_t system_event_id_t
|
||||
|
||||
typedef enum {
|
||||
WL_NO_SHIELD = 255, // for compatibility with WiFi Shield library
|
||||
WL_IDLE_STATUS = 0,
|
||||
WL_NO_SSID_AVAIL = 1,
|
||||
WL_SCAN_COMPLETED = 2,
|
||||
WL_CONNECTED = 3,
|
||||
WL_CONNECT_FAILED = 4,
|
||||
WL_CONNECTION_LOST = 5,
|
||||
WL_DISCONNECTED = 6
|
||||
} wl_status_t;
|
||||
|
||||
#endif /* ESP32WIFITYPE_H_ */
|
||||
277
lib/esp32_lora_wifi/WiFi/src/WiFiUdp.cpp
Normal file
277
lib/esp32_lora_wifi/WiFi/src/WiFiUdp.cpp
Normal file
@@ -0,0 +1,277 @@
|
||||
/*
|
||||
Udp.cpp - UDP class for Raspberry Pi
|
||||
Copyright (c) 2016 Hristo Gochkov All right reserved.
|
||||
|
||||
This library is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU Lesser General Public
|
||||
License as published by the Free Software Foundation; either
|
||||
version 2.1 of the License, or (at your option) any later version.
|
||||
|
||||
This library is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
Lesser General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public
|
||||
License along with this library; if not, write to the Free Software
|
||||
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
*/
|
||||
#include "WiFiUdp.h"
|
||||
#include <lwip/sockets.h>
|
||||
#include <lwip/netdb.h>
|
||||
#include <errno.h>
|
||||
|
||||
#undef write
|
||||
#undef read
|
||||
|
||||
WiFiUDP::WiFiUDP()
|
||||
: udp_server(-1)
|
||||
, server_port(0)
|
||||
, remote_port(0)
|
||||
, tx_buffer(0)
|
||||
, tx_buffer_len(0)
|
||||
, rx_buffer(0)
|
||||
{}
|
||||
|
||||
WiFiUDP::~WiFiUDP(){
|
||||
stop();
|
||||
}
|
||||
|
||||
uint8_t WiFiUDP::begin(IPAddress address, uint16_t port){
|
||||
stop();
|
||||
|
||||
server_port = port;
|
||||
|
||||
tx_buffer = new char[1460];
|
||||
if(!tx_buffer){
|
||||
log_e("could not create tx buffer: %d", errno);
|
||||
return 0;
|
||||
}
|
||||
|
||||
if ((udp_server=socket(AF_INET, SOCK_DGRAM, 0)) == -1){
|
||||
log_e("could not create socket: %d", errno);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int yes = 1;
|
||||
if (setsockopt(udp_server,SOL_SOCKET,SO_REUSEADDR,&yes,sizeof(yes)) < 0) {
|
||||
log_e("could not set socket option: %d", errno);
|
||||
stop();
|
||||
return 0;
|
||||
}
|
||||
|
||||
struct sockaddr_in addr;
|
||||
memset((char *) &addr, 0, sizeof(addr));
|
||||
addr.sin_family = AF_INET;
|
||||
addr.sin_port = htons(server_port);
|
||||
addr.sin_addr.s_addr = (in_addr_t)address;
|
||||
if(bind(udp_server , (struct sockaddr*)&addr, sizeof(addr)) == -1){
|
||||
log_e("could not bind socket: %d", errno);
|
||||
stop();
|
||||
return 0;
|
||||
}
|
||||
fcntl(udp_server, F_SETFL, O_NONBLOCK);
|
||||
return 1;
|
||||
}
|
||||
|
||||
uint8_t WiFiUDP::begin(uint16_t p){
|
||||
return begin(IPAddress(INADDR_ANY), p);
|
||||
}
|
||||
|
||||
uint8_t WiFiUDP::beginMulticast(IPAddress a, uint16_t p){
|
||||
if(begin(IPAddress(INADDR_ANY), p)){
|
||||
if(a != 0){
|
||||
struct ip_mreq mreq;
|
||||
mreq.imr_multiaddr.s_addr = (in_addr_t)a;
|
||||
mreq.imr_interface.s_addr = INADDR_ANY;
|
||||
if (setsockopt(udp_server, IPPROTO_IP, IP_ADD_MEMBERSHIP, &mreq, sizeof(mreq)) < 0) {
|
||||
log_e("could not join igmp: %d", errno);
|
||||
stop();
|
||||
return 0;
|
||||
}
|
||||
multicast_ip = a;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
void WiFiUDP::stop(){
|
||||
if(tx_buffer){
|
||||
delete[] tx_buffer;
|
||||
}
|
||||
tx_buffer_len = 0;
|
||||
if(rx_buffer){
|
||||
cbuf *b = rx_buffer;
|
||||
rx_buffer = NULL;
|
||||
delete b;
|
||||
}
|
||||
if(udp_server == -1)
|
||||
return;
|
||||
if(multicast_ip != 0){
|
||||
struct ip_mreq mreq;
|
||||
mreq.imr_multiaddr.s_addr = (in_addr_t)multicast_ip;
|
||||
mreq.imr_interface.s_addr = (in_addr_t)0;
|
||||
setsockopt(udp_server, IPPROTO_IP, IP_DROP_MEMBERSHIP, &mreq, sizeof(mreq));
|
||||
multicast_ip = IPAddress(INADDR_ANY);
|
||||
}
|
||||
close(udp_server);
|
||||
udp_server = -1;
|
||||
}
|
||||
|
||||
int WiFiUDP::beginMulticastPacket(){
|
||||
if(!server_port || multicast_ip == IPAddress(INADDR_ANY))
|
||||
return 0;
|
||||
remote_ip = server_port;
|
||||
remote_port = multicast_ip;
|
||||
return beginPacket();
|
||||
}
|
||||
|
||||
int WiFiUDP::beginPacket(){
|
||||
if(!remote_port)
|
||||
return 0;
|
||||
|
||||
// allocate tx_buffer if is necessary
|
||||
if(!tx_buffer){
|
||||
tx_buffer = new char[1460];
|
||||
if(!tx_buffer){
|
||||
log_e("could not create tx buffer: %d", errno);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
tx_buffer_len = 0;
|
||||
|
||||
// check whereas socket is already open
|
||||
if (udp_server != -1)
|
||||
return 1;
|
||||
|
||||
if ((udp_server=socket(AF_INET, SOCK_DGRAM, 0)) == -1){
|
||||
log_e("could not create socket: %d", errno);
|
||||
return 0;
|
||||
}
|
||||
|
||||
fcntl(udp_server, F_SETFL, O_NONBLOCK);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
int WiFiUDP::beginPacket(IPAddress ip, uint16_t port){
|
||||
remote_ip = ip;
|
||||
remote_port = port;
|
||||
return beginPacket();
|
||||
}
|
||||
|
||||
int WiFiUDP::beginPacket(const char *host, uint16_t port){
|
||||
struct hostent *server;
|
||||
server = gethostbyname(host);
|
||||
if (server == NULL){
|
||||
log_e("could not get host from dns: %d", errno);
|
||||
return 0;
|
||||
}
|
||||
return beginPacket(IPAddress((const uint8_t *)(server->h_addr_list[0])), port);
|
||||
}
|
||||
|
||||
int WiFiUDP::endPacket(){
|
||||
struct sockaddr_in recipient;
|
||||
recipient.sin_addr.s_addr = (uint32_t)remote_ip;
|
||||
recipient.sin_family = AF_INET;
|
||||
recipient.sin_port = htons(remote_port);
|
||||
int sent = sendto(udp_server, tx_buffer, tx_buffer_len, 0, (struct sockaddr*) &recipient, sizeof(recipient));
|
||||
if(sent < 0){
|
||||
log_e("could not send data: %d", errno);
|
||||
return 0;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
size_t WiFiUDP::write(uint8_t data){
|
||||
if(tx_buffer_len == 1460){
|
||||
endPacket();
|
||||
tx_buffer_len = 0;
|
||||
}
|
||||
tx_buffer[tx_buffer_len++] = data;
|
||||
return 1;
|
||||
}
|
||||
|
||||
size_t WiFiUDP::write(const uint8_t *buffer, size_t size){
|
||||
size_t i;
|
||||
for(i=0;i<size;i++)
|
||||
write(buffer[i]);
|
||||
return i;
|
||||
}
|
||||
|
||||
int WiFiUDP::parsePacket(){
|
||||
if(rx_buffer)
|
||||
return 0;
|
||||
struct sockaddr_in si_other;
|
||||
int slen = sizeof(si_other) , len;
|
||||
char * buf = new char[1460];
|
||||
if(!buf){
|
||||
return 0;
|
||||
}
|
||||
if ((len = recvfrom(udp_server, buf, 1460, MSG_DONTWAIT, (struct sockaddr *) &si_other, (socklen_t *)&slen)) == -1){
|
||||
delete[] buf;
|
||||
if(errno == EWOULDBLOCK){
|
||||
return 0;
|
||||
}
|
||||
log_e("could not receive data: %d", errno);
|
||||
return 0;
|
||||
}
|
||||
remote_ip = IPAddress(si_other.sin_addr.s_addr);
|
||||
remote_port = ntohs(si_other.sin_port);
|
||||
rx_buffer = new cbuf(len);
|
||||
rx_buffer->write(buf, len);
|
||||
delete[] buf;
|
||||
return len;
|
||||
}
|
||||
|
||||
int WiFiUDP::available(){
|
||||
if(!rx_buffer) return 0;
|
||||
return rx_buffer->available();
|
||||
}
|
||||
|
||||
int WiFiUDP::read(){
|
||||
if(!rx_buffer) return -1;
|
||||
int out = rx_buffer->read();
|
||||
if(!rx_buffer->available()){
|
||||
cbuf *b = rx_buffer;
|
||||
rx_buffer = 0;
|
||||
delete b;
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
int WiFiUDP::read(unsigned char* buffer, size_t len){
|
||||
return read((char *)buffer, len);
|
||||
}
|
||||
|
||||
int WiFiUDP::read(char* buffer, size_t len){
|
||||
if(!rx_buffer) return 0;
|
||||
int out = rx_buffer->read(buffer, len);
|
||||
if(!rx_buffer->available()){
|
||||
cbuf *b = rx_buffer;
|
||||
rx_buffer = 0;
|
||||
delete b;
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
int WiFiUDP::peek(){
|
||||
if(!rx_buffer) return -1;
|
||||
return rx_buffer->peek();
|
||||
}
|
||||
|
||||
void WiFiUDP::flush(){
|
||||
cbuf *b = rx_buffer;
|
||||
rx_buffer = 0;
|
||||
delete b;
|
||||
}
|
||||
|
||||
IPAddress WiFiUDP::remoteIP(){
|
||||
return remote_ip;
|
||||
}
|
||||
|
||||
uint16_t WiFiUDP::remotePort(){
|
||||
return remote_port;
|
||||
}
|
||||
77
lib/esp32_lora_wifi/WiFi/src/WiFiUdp.h
Normal file
77
lib/esp32_lora_wifi/WiFi/src/WiFiUdp.h
Normal file
@@ -0,0 +1,77 @@
|
||||
/*
|
||||
* Udp.cpp: Library to send/receive UDP packets.
|
||||
*
|
||||
* NOTE: UDP is fast, but has some important limitations (thanks to Warren Gray for mentioning these)
|
||||
* 1) UDP does not guarantee the order in which assembled UDP packets are received. This
|
||||
* might not happen often in practice, but in larger network topologies, a UDP
|
||||
* packet can be received out of sequence.
|
||||
* 2) UDP does not guard against lost packets - so packets *can* disappear without the sender being
|
||||
* aware of it. Again, this may not be a concern in practice on small local networks.
|
||||
* For more information, see http://www.cafeaulait.org/course/week12/35.html
|
||||
*
|
||||
* MIT License:
|
||||
* Copyright (c) 2008 Bjoern Hartmann
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*
|
||||
* bjoern@cs.stanford.edu 12/30/2008
|
||||
*/
|
||||
|
||||
#ifndef _WIFIUDP_H_
|
||||
#define _WIFIUDP_H_
|
||||
|
||||
#include <Arduino.h>
|
||||
#include <Udp.h>
|
||||
#include <cbuf.h>
|
||||
|
||||
class WiFiUDP : public UDP {
|
||||
private:
|
||||
int udp_server;
|
||||
IPAddress multicast_ip;
|
||||
IPAddress remote_ip;
|
||||
uint16_t server_port;
|
||||
uint16_t remote_port;
|
||||
char * tx_buffer;
|
||||
size_t tx_buffer_len;
|
||||
cbuf * rx_buffer;
|
||||
public:
|
||||
WiFiUDP();
|
||||
~WiFiUDP();
|
||||
uint8_t begin(IPAddress a, uint16_t p);
|
||||
uint8_t begin(uint16_t p);
|
||||
uint8_t beginMulticast(IPAddress a, uint16_t p);
|
||||
void stop();
|
||||
int beginMulticastPacket();
|
||||
int beginPacket();
|
||||
int beginPacket(IPAddress ip, uint16_t port);
|
||||
int beginPacket(const char *host, uint16_t port);
|
||||
int endPacket();
|
||||
size_t write(uint8_t);
|
||||
size_t write(const uint8_t *buffer, size_t size);
|
||||
int parsePacket();
|
||||
int available();
|
||||
int read();
|
||||
int read(unsigned char* buffer, size_t len);
|
||||
int read(char* buffer, size_t len);
|
||||
int peek();
|
||||
void flush();
|
||||
IPAddress remoteIP();
|
||||
uint16_t remotePort();
|
||||
};
|
||||
|
||||
#endif /* _WIFIUDP_H_ */
|
||||
Reference in New Issue
Block a user