From 545ddee8de70ca71f183451be5a0b215db8cdeae Mon Sep 17 00:00:00 2001 From: Hendrik Langer Date: Fri, 21 Apr 2017 16:41:32 +0200 Subject: [PATCH] Add WiFi example code --- src/hardware.h | 3 ++ src/main.cpp | 8 ++-- src/wifi.cpp | 109 +++++++++++++++++++++++++++++++++++++++++++++++++ src/wifi.h | 26 ++++++++++++ 4 files changed, 142 insertions(+), 4 deletions(-) create mode 100644 src/wifi.cpp create mode 100644 src/wifi.h diff --git a/src/hardware.h b/src/hardware.h index a1ee216..6a459e9 100644 --- a/src/hardware.h +++ b/src/hardware.h @@ -30,4 +30,7 @@ #define BUF_LENGTH 1024 +#define WIFI_SSID "ssid" +#define WIFI_PASSWORD "password" + #endif /* _HARDWARE_H */ diff --git a/src/main.cpp b/src/main.cpp index 96c0175..0be0787 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -11,15 +11,14 @@ #include "keyboard.h" #include "sdcard.h" #include "sound.h" +#include "wifi.h" #define VERSION "0.0" -#define WIFI_SSID "ssid" -#define WIFI_PASSWORD "password" - Keyboard keyboard; SDCard sdcard; Sound sound; +Wifi wifi; TaskHandle_t xTaskSound = NULL; bool playing = false; @@ -42,6 +41,7 @@ void setup() keyboard.init(); sdcard.mount(); sound.init(); + wifi.init(); static const char *bootSound = "/boot.wav"; @@ -71,7 +71,7 @@ void loop() digitalWrite(LED_PIN, LOW); } - + wifi.loop(); // wait for a second delay(50); } diff --git a/src/wifi.cpp b/src/wifi.cpp new file mode 100644 index 0000000..270b18e --- /dev/null +++ b/src/wifi.cpp @@ -0,0 +1,109 @@ +/* WiFi + * + * Arduino ESP32 mDNS responder sample + */ + +#include "Arduino.h" + +#include +#include +#include + +#include "hardware.h" + +#include "wifi.h" + +using namespace std; + +Wifi::Wifi() { + this->server = WiFiServer(80); +} + +void Wifi::init() { + WiFi.begin(WIFI_SSID, WIFI_PASSWORD); + + // Wait for connection + while (WiFi.status() != WL_CONNECTED) { + delay(500); + Serial.print("."); + } + Serial.println(""); + Serial.print("Connected to "); + Serial.println(WIFI_SSID); + Serial.print("IP address: "); + Serial.println(WiFi.localIP()); + + // Set up mDNS responder: + // - first argument is the domain name, in this example + // the fully-qualified domain name is "esp8266.local" + // - second argument is the IP address to advertise + // we send our IP address on the WiFi network + if (!MDNS.begin("soundboard")) { + Serial.println("Error setting up MDNS responder!"); + while(1) { + delay(1000); + } + } + Serial.println("mDNS responder started"); + + // Start TCP (HTTP) server + server.begin(); + Serial.println("TCP server started"); + + // Add service to MDNS-SD + MDNS.addService("http", "tcp", 80); +} + +void Wifi::loop() { + // Check if a client has connected + WiFiClient client = server.available(); + if (!client) { + return; + } + Serial.println(""); + Serial.println("New client"); + + // Wait for data from client to become available + while(client.connected() && !client.available()){ + delay(1); + } + + // Read the first line of HTTP request + String req = client.readStringUntil('\r'); + + // First line of HTTP request looks like "GET /path HTTP/1.1" + // Retrieve the "/path" part by finding the spaces + int addr_start = req.indexOf(' '); + int addr_end = req.indexOf(' ', addr_start + 1); + if (addr_start == -1 || addr_end == -1) { + Serial.print("Invalid request: "); + Serial.println(req); + return; + } + req = req.substring(addr_start + 1, addr_end); + Serial.print("Request: "); + Serial.println(req); + client.flush(); + + String s; + if (req == "/") + { + IPAddress ip = WiFi.localIP(); + String ipStr = String(ip[0]) + '.' + String(ip[1]) + '.' + String(ip[2]) + '.' + String(ip[3]); + s = "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n\r\nHello from ESP32 at "; + s += ipStr; + s += "\r\n\r\n"; + Serial.println("Sending 200"); + } + else + { + s = "HTTP/1.1 404 Not Found\r\n\r\n"; + Serial.println("Sending 404"); + } + client.print(s); + + Serial.println("Done with client"); +} + +void Wifi::end() { +} diff --git a/src/wifi.h b/src/wifi.h new file mode 100644 index 0000000..26d1db2 --- /dev/null +++ b/src/wifi.h @@ -0,0 +1,26 @@ +#ifndef _WIFI_H +#define _WIFI_H + +#include "Arduino.h" + +#include +#include +#include + +#include "hardware.h" + + +class Wifi { + public: + Wifi(); + void init(); + void end(); + void loop(); +// esp_err_t event_handler(void *ctx, system_event_t *event); +// void wifi_init_sta(); +// void wifi_init_softap(); + private: + WiFiServer server; +}; + +#endif /* _WIFI_H */