#include <WiFi.h>
#include <Preferences.h> // this library is used to get access to Non-volatile storage (NVS) of ESP32
//Digital INs/OUTs
#define BTN_AP_en 12 // digital input
#define PIN_LED_YELLOW 25 // digital input
#define PIN_LED_GREEN 26 // digital output
#define PIN_LED_RED 27 // digital input
//Version-Date
#define Date 2
#define Month 4
#define Year 19
#define NetworkConnection_successfully 20
#define NetworkConnection_unsuccessfully 21
#define AP_modus_on 22
#define hfm_connected_to_server 23
//****************************************************************************//
//* global variables
//****************************************************************************//
int status = WL_IDLE_STATUS;
int connection_status=0;
//****************************************************************************//
//* global objects of the classes
//****************************************************************************//
WiFiClient SCCclient; // generate object of the class WiFiClient for SCC-Server
Preferences preferences; // generate object of the class preference (Non-volatile storage)
//****************************************************************************//
//* FUNCTION int WiFi_NetworkConnect(char* ch_SSID, char* ch_Password)
//* Connect to network and return 1 (success) or -1 (no success)
//****************************************************************************//
int WiFi_NetworkConnect(char* ch_SSID, char* ch_Password)
{
Serial.println("Function WiFi_NetworkConnect()");
int success;
//connect to WiFi network see https://www.arduino.cc/en/Reference/WiFiBegin
WiFi.begin(ch_SSID, ch_Password);
//wait until connection is established or 10 seconds are gone
char WiFiConnectTimeOut = 0;
while ((WiFi.status() != WL_CONNECTED) && (WiFiConnectTimeOut < 100))
{
if(WiFiConnectTimeOut & 1)
{
digitalWrite(PIN_LED_RED, LOW);
digitalWrite(PIN_LED_GREEN, HIGH);
}
else
{
digitalWrite(PIN_LED_RED, HIGH);
digitalWrite(PIN_LED_GREEN, LOW);
}
if(WiFiConnectTimeOut & 2)Serial.print(".");
delay(100);
WiFiConnectTimeOut++;
}
// not connected
if (WiFi.status() != WL_CONNECTED)
{
success = -1;
}
else
{
success = 1;
}
return success;
}
//****************************************************************************//
//* FUNCTION void SwitchLED(void)
// Switch LEDs
// LED Color must be LED_GREEN, LED_RED, LED_YELLOW or LED_OFF
//****************************************************************************//
void SwitchLED(void)
{
if(connection_status==hfm_connected_to_server)
{
digitalWrite(PIN_LED_RED, HIGH);
digitalWrite(PIN_LED_GREEN, LOW);
digitalWrite(PIN_LED_YELLOW, HIGH);
}
else if(connection_status==NetworkConnection_successfully)
{
digitalWrite(PIN_LED_RED, HIGH);
digitalWrite(PIN_LED_GREEN, HIGH);
digitalWrite(PIN_LED_YELLOW, LOW);
}
else
{
digitalWrite(PIN_LED_RED, HIGH);
digitalWrite(PIN_LED_GREEN, HIGH);
digitalWrite(PIN_LED_YELLOW, HIGH);
}
}
//****************************************************************************//
//* FUNCTION void connect_to_server()
//****************************************************************************//
void connect_to_server(void)
{
// Open Preferences with WLAN_Config namespace. Each application module, library, etc
// has to use a namespace name to prevent key name collisions. We will open storage in
// RW-mode (second parameter has to be false). Note: Namespace name is limited to 15 chars.
// see https://github.com/espressif/arduino-esp32/blob/master/libraries/Preferences/examples/StartCounter/StartCounter.ino
preferences.begin("WLAN_Config", false);
// takeout WLAN_Config Strings out of the Non-volatile storage
String eeprom_SSID = preferences.getString("SSID", "");
String eeprom_Password = preferences.getString("Password", "");
String eeprom_Server_IP = preferences.getString("Server_IP", "");
String eeprom_Server_Port = preferences.getString("Server_Port", "");
// convert String to char*, see https://coderwall.com/p/zfmwsg/arduino-string-to-char
char* ch_SSID = const_cast<char*>(eeprom_SSID.c_str());
char* ch_Password = const_cast<char*>(eeprom_Password.c_str());
char* ch_Server_IP = const_cast<char*>(eeprom_Server_IP.c_str());
Serial.println("Attempting to connect to WPA network..."); //
Serial.print("SSID: ");
Serial.println(eeprom_SSID);
Serial.print("Pass: ");
Serial.println(eeprom_Password);
Serial.print("Server_IP: ");
Serial.println(eeprom_Server_IP);
Serial.print("Server_Port: ");
Serial.println(eeprom_Server_Port);
// Connect to network
if(WiFi_NetworkConnect(ch_SSID, ch_Password)==1) //NetworkConnection is successfully
{
connection_status=NetworkConnection_successfully;
SwitchLED();
Serial.println("Connected to wifi");
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
// Connect to Server
Serial.println("Starting connection to server...");
SCCclient.connect(ch_Server_IP,eeprom_Server_Port.toInt());
Serial.print("Status Connection: ");
if (SCCclient.connected()) //Connection to Server open
{
connection_status=hfm_connected_to_server;
Serial.println("connected to server");
}
else // Connection to Server closed
{
Serial.println("Couldn't get a Server connection");
Serial.println("Restarting");
ESP.restart();
}
}
else //NetworkConnection is unsuccessfully
{
connection_status=NetworkConnection_unsuccessfully;
Serial.println("Couldn't get a wifi connection");
Serial.println("Restarting");
ESP.restart();
}
}
//****************************************************************************//
//* FUNCTION void setup()
//****************************************************************************//
void setup()
{
Serial.begin(115200);
Serial.println("Version 0.0.0");
//Digital INs/OUTs config
pinMode(PIN_LED_YELLOW, OUTPUT);
pinMode(PIN_LED_RED, OUTPUT);
pinMode(PIN_LED_GREEN, OUTPUT);
pinMode(BTN_AP_en, INPUT_PULLUP);
//attachInterrupt(digitalPinToInterrupt(BTN_AP_en), set_flag_check_btn_AP, FALLING);
SwitchLED();
connect_to_server();
}
//****************************************************************************//
//* FUNCTION void loop()
//****************************************************************************//
void loop()
{
//WiFi to UART Bridge
if(connection_status==hfm_connected_to_server)
{
while (SCCclient.available() || Serial.available())
{
if(SCCclient.available())
{
char rx = SCCclient.read();
Serial.write(rx);
}
if(Serial.available())
{
char tx = Serial.read();
SCCclient.write(tx);
}
}
}
}