Skip to content

Instantly share code, notes, and snippets.

@miceno
Created November 5, 2023 00:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save miceno/0e77e433fd06b68c3861dac62fce5d0f to your computer and use it in GitHub Desktop.
Save miceno/0e77e433fd06b68c3861dac62fce5d0f to your computer and use it in GitHub Desktop.
Example of a persistent TCP server using WiFiManager and ESP8266Wifi
/*
SocketServer.ino
Created on: 4.11.2023
Author: miceno.atreides@gmail.com
*/
#include <ESP8266WiFi.h>
#include <WiFiManager.h>
// If there is difficulty getting the INDI roof driver to connect, need to establish that the
// WiFi network is working and that the Arduino is connected. Running the Arduino IDE and
// using its serial monitor can be helpful to see any diagnostic messages.
// INIT_DELAY_SECS can be defined to wait a number of seconds after starting the Arduino
// to have time to get the serial monitor display started and see network startup messages.
#define INIT_DELAY_SECS 0
#define MAX_BAUD_RATE 115200
String strData = ""; // a string to hold incoming data
uint16_t port = 8888; // Port number
WiFiServer server(port);
//Static IP address configuration
IPAddress staticIP(192, 168, 1, 61); // Board static ip
IPAddress gateway(192, 168, 1, 1); // WiFi Router Gateway IP Address
IPAddress subnet(255, 255, 255, 0); // Subnet mask
IPAddress dns(8, 8, 8, 8); // DNS
WiFiClient client;
bool setup_wifi() {
delay(INIT_DELAY_SECS * 1000); // diagnostic, allow time to get serial monitor displayed
WiFi.mode(WIFI_STA); // explicitly set mode, esp defaults to STA+AP
// it is a good practice to make sure your code sets wifi mode how you want it.
// put your setup code here, to run once:
Serial.begin(MAX_BAUD_RATE);
//WiFiManager, Local intialization. Once its business is done, there is no need to keep it around
WiFiManager wm;
// reset settings - wipe stored credentials for testing
// these are stored by the esp library
// wm.resetSettings();
// Automatically connect using saved credentials,
// if connection fails, it starts an access point with the specified name ( "AutoConnectAP"),
// if empty will auto generate SSID, if password is blank it will be anonymous AP (wm.autoConnect())
// then goes into a blocking loop awaiting configuration and will return success result
bool res;
// res = wm.autoConnect(); // auto generated AP name from chipid
// res = wm.autoConnect("AutoConnectAP"); // anonymous ap
res = wm.autoConnect("AutoConnectAP", "password"); // password protected ap
if (!res) {
Serial.println("Failed to connect");
// ESP.restart();
} else {
//if you get here you have connected to the WiFi
Serial.println("connected...yeey :)");
}
return res;
}
void setup() {
bool res = false;
Serial.begin(MAX_BAUD_RATE);
while (Serial.available() > 0)
;
WiFi.disconnect(); //Prevent connecting to wifi based on previous configuration
ESP.eraseConfig();
res = setup_wifi();
if(!res){
Serial.println("Unable to start wifi... aborting");
exit(-1);
}
Serial.println("\nWiFi connected");
Serial.print("START MIRRORING SERIAL: ");
Serial.println(WiFi.localIP());
// Start server
server.begin();
}
void loop() {
// Serial.println("looping..."); // DEBUG
if (!client){
// Only request a client in case there is no valid one.
// The client should persist on the loop so it is a global variable.
client = server.available();
}
if (client) {
// Serial.println("client...");
while (client.available() > 0) {
char c;
// Serial.println("data is available...");
// read data from the connected client
c = client.read();
strData += c;
}
// Serial.println("\nNo more data available");
if( strData.length()>0){
Serial.printf("Message received is %s\n", strData.c_str());
client.write("Response received");
client.flush();
strData = "";
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment