ESP8266 Wi-Fi

Photo







Sketch
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Adafruit ESP8266 WiFi | |
// Copyright (C) 2019 https://www.roboticboat.uk | |
// fa032de3-9b62-4b21-a867-ae14a5200c2a | |
// | |
// This program is free software: you can redistribute it and/or modify | |
// it under the terms of the GNU General Public License as published by | |
// the Free Software Foundation, either version 3 of the License, or | |
// (at your option) any later version. | |
// | |
// This program 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 General Public License for more details. | |
// | |
// You should have received a copy of the GNU General Public License | |
// along with this program. If not, see <https://www.gnu.org/licenses/>. | |
// These Terms shall be governed and construed in accordance with the laws of | |
// England and Wales, without regard to its conflict of law provisions. | |
#include <ESP8266WiFi.h> | |
#include <WiFiClient.h> | |
// Press down the GPIO0 button | |
// Press down the Reset button | |
// Release the Reset button | |
// Release the GPIO0 button | |
// The board should display a faint red light | |
// Now upload the script | |
// Network name and password | |
const char *ssid = "Yardstick Robotic Boat"; | |
const char *password = "password1"; | |
WiFiServer server(80); | |
void setup() { | |
// Open connection to the User Serial Monitor | |
Serial.begin(9600); | |
// Setup a local WiFi hotspot | |
WiFi.mode(WIFI_AP); | |
// Wait one second | |
delay(1000); | |
// Update the User | |
Serial.println(); | |
Serial.print("Configuring WiFi"); | |
// Login to the local WiFi hotspot | |
WiFi.softAP(ssid, password); | |
// Obtain the IP address on the WiFi network | |
IPAddress myIP = WiFi.softAPIP(); | |
// Start the WiFi Server | |
server.begin(); | |
// Update the User | |
Serial.println("HTTP server started"); | |
} | |
void loop() { | |
// Is there a client on the WiFi server | |
WiFiClient client = server.available(); | |
// Check if a client connection exists | |
if (client) { | |
// We have a connected client | |
// Read the http request | |
String reg = client.readStringUntil('\r'); | |
// Update the User with the request seen | |
Serial.println(reg); | |
// The webserver sends a request to the client | |
client.print("Received message "); | |
client.print(reg); | |
client.flush(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Adafruit ESP8266 WiFi | |
// Copyright (C) 2019 https://www.roboticboat.uk | |
// 1473b8dd-8dcf-4a2c-a6a5-44e9aa50aaa8 | |
// | |
// This program is free software: you can redistribute it and/or modify | |
// it under the terms of the GNU General Public License as published by | |
// the Free Software Foundation, either version 3 of the License, or | |
// (at your option) any later version. | |
// | |
// This program 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 General Public License for more details. | |
// | |
// You should have received a copy of the GNU General Public License | |
// along with this program. If not, see <https://www.gnu.org/licenses/>. | |
// These Terms shall be governed and construed in accordance with the laws of | |
// England and Wales, without regard to its conflict of law provisions. | |
#include <ESP8266WiFi.h> | |
#include <WiFiClient.h> | |
// Press down the GPIO0 button | |
// Press down the Reset button | |
// Release the Reset button | |
// Release the GPIO0 button | |
// The board should display a faint red light | |
// Now upload the script | |
// Network name and password | |
const char *ssid = "Yardstick Robotic Boat"; | |
const char *password = "password1"; | |
WiFiServer server(80); | |
void setup() { | |
// Open connection to the User Serial Monitor | |
Serial.begin(9600); | |
// Setup a local WiFi hotspot | |
WiFi.mode(WIFI_AP); | |
// Wait one second | |
delay(1000); | |
// Update the User | |
Serial.println("\nConfiguring WiFi"); | |
// Login to the local WiFi hotspot | |
WiFi.softAP(ssid, password); | |
// Obtain the IP address on the WiFi network | |
IPAddress myIP = WiFi.softAPIP(); | |
// Start the WiFi Server | |
server.begin(); | |
// Update the User | |
Serial.println("HTTP server started"); | |
} | |
void loop() { | |
// Is there a client on the WiFi server | |
WiFiClient client = server.available(); | |
// Check if a client connection exists | |
if (client) { | |
// We have a connected client | |
// Read the http request | |
String reg = client.readStringUntil('\r'); | |
// Update the User with the request seen | |
//Serial.println(reg); | |
// Parse the string | |
extractMessage(reg); | |
//parseString(reg); | |
// The webserver sends a request to the client | |
client.print("Received message "); | |
client.print(reg); | |
client.flush(); | |
} | |
} | |
void extractMessage(String msg){ | |
// Below is an example of a message a,b,c,d,e,f,g | |
// Notice GET / and HTTP/1.1 are added to the string | |
// GET /a,b,c,d,e,f,g HTTP/1.1 | |
// Can ignore this information as not required | |
if (msg == "GET /favicon.ico HTTP/1.1") return; | |
int i = msg.indexOf('/') + 1; | |
int j = msg.indexOf(' ',i); | |
// Update the User on the Serial monitor | |
//Serial.print('['); | |
Serial.println(msg.substring(i,j)); | |
//Serial.println(']'); | |
} | |
void parseString(String msg) { | |
// Below is an example of a message a,b,c,d,e,f,g | |
// Notice GET / and HTTP/1.1 are added to the string | |
// GET /a,b,c,d,e,f,g HTTP/1.1 | |
// Length of the Url message | |
int commaLocation = 0; | |
int i = msg.indexOf('/') + 1; | |
String cmd = ""; | |
// Return if there are no information | |
if (i <= 0) return; | |
// Loop over the String message | |
while (i < msg.length() && msg.indexOf(',',i) >= 0){ | |
// Find the location of the comma in the string | |
commaLocation = msg.indexOf(',', i); | |
// Extract the String between the commas | |
cmd = msg.substring(i, commaLocation); | |
// Update the User on the Serial monitor | |
Serial.println(cmd); | |
// Move on to the next comma | |
i = commaLocation + 1; | |
} | |
// Now find the last string | |
commaLocation = msg.indexOf(' ', i); | |
// Extract the String between the commas | |
cmd = msg.substring(i, commaLocation); | |
// Update the User on the Serial monitor | |
Serial.println(cmd); | |
} |