Created
February 10, 2019 11:06
-
-
Save pfeerick/28a5aa0b00b98344f99443040b1752f9 to your computer and use it in GitHub Desktop.
ESP32 AP platformio forum code
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
#include <Arduino.h> | |
#include <WiFi.h> | |
#include <WiFiClient.h> | |
#include <IPAddress.h> | |
String inputString=""; | |
String inputStringWiFi=""; | |
String hello="hello\r"; | |
String fadd="fadd\r"; | |
String fdel="fdel\r"; | |
int isp=0; | |
uint8_t tmpraed; | |
uint32_t hsread; | |
const char* ssidDev="TEST"; | |
const char* pwdDev="test"; | |
WiFiServer wserver; | |
WiFiClient wsclient; | |
uint16_t trPort=567; | |
IPAddress ipDev (192,168,24,16); | |
IPAddress nmDev (255,255,255,0); | |
IPAddress gwDev (192,168,24,16); | |
char c; | |
void testWiFiConnect (void){ | |
Serial.println("Start test connecting...."); | |
//Serial.print("Connected, IP adress: "); Serial.println(WiFi.localIP()); | |
Serial.print("Mode = "); Serial.print(WiFi.getMode()); | |
Serial.print(" IP = "); Serial.println(WiFi.softAPIP().toString()); | |
Serial.println("...End test connected"); | |
} | |
void createWIFI (void){ | |
//create WIFI AP | |
Serial.println("Create WIFI AP"); | |
Serial.println("Parametr to create"); | |
Serial.print("SSID DEVICE="); Serial.println(ssidDev); | |
Serial.print("PASSWORD DEVICE="); Serial.println(pwdDev); | |
Serial.print("CONFIG IP="); Serial.println(ipDev); | |
Serial.print("GW="); Serial.println(gwDev); | |
Serial.print("NM="); Serial.println(nmDev); | |
//WiFi.mode(WIFI_AP); | |
//delay(10); | |
WiFi.softAPConfig(ipDev, gwDev, nmDev); | |
delay(10); | |
WiFi.softAP(ssidDev,pwdDev,1,0,4); | |
delay(10); | |
testWiFiConnect(); | |
Serial.println("WiFi AP is UP"); | |
} | |
void disconnectWIFI (void){ | |
//disconecting WIFI AP | |
WiFi.disconnect(); | |
WiFi.mode(WIFI_OFF); | |
} | |
void createServer (void){ | |
Serial.println("Created WIFI SERVER"); | |
//trPort=wifiAP.portAP.toInt(); | |
//WiFiServer wserver(trPort); | |
//WiFiClient wsclient(1); | |
wserver.begin(trPort); | |
//wserver.setNoDelay(true); | |
Serial.println("WiFi Server is cteate"); | |
} | |
void setup() { | |
// put your setup code here, to run once: | |
//WRITE_PERI_REG(RTC_CNTL_BROWN_OUT_REG,0); | |
Serial.begin(115200); | |
Serial.println("Hello Alex!!!"); | |
Serial.print("Temperatyre - "); | |
Serial.println(temperatureRead()); | |
Serial.print("HAll - "); | |
Serial.println(hallRead()); | |
Serial.println(); | |
//************************** | |
//**** Setup WIFI ********** | |
disconnectWIFI(); | |
delay(100); | |
createWIFI(); | |
delay(100); | |
createServer(); | |
delay(100); | |
} | |
void loop() { | |
//******************************** | |
//***** DEBUG UART port ********** | |
if (Serial.available()) { | |
char c = Serial.read(); | |
Serial.print(c); | |
//read line and parsing | |
if (c==10) { | |
Serial.print ("inputString ="); Serial.println(inputString); | |
if (inputString.equals(hello)) { | |
Serial.println ("Hello my friends!"); | |
} | |
Serial.println("Obnulili line string"); | |
inputString = ""; | |
} else { | |
inputString +=c; | |
} | |
} | |
//************ END DEBUG UART PORT *************** | |
delay(100); | |
//************************************************* | |
//*********** WIFI PORT ************************ | |
wsclient = wserver.available(); | |
if (wsclient) { | |
Serial.println ("new client"); | |
String currentLine = ""; | |
while (wsclient.connected()) { | |
if (wsclient.available()) { | |
c = wsclient.read(); | |
Serial.write(c); | |
if (c == 10) { | |
//Serial.println("end string"); | |
//wsclient.println("end command"); | |
if (currentLine.equals(hello)){ | |
Serial.print("Command - "); Serial.println(currentLine); | |
wsclient.println("Hello my WIFI friend!"); | |
} | |
if (currentLine.substring(0).equals(fadd)){ | |
Serial.print("Command-"); Serial.println(currentLine); | |
wsclient.print("Working - "); | |
wsclient.println(currentLine); | |
isp=1; | |
if (isp==1){ | |
wsclient.println("fadd:OK"); | |
} else { | |
wsclient.println("fadd:BAD"); | |
} | |
} | |
if (currentLine.substring(9).equals(fdel)){ | |
Serial.print("Command-"); Serial.println(currentLine); | |
isp=1; | |
if (isp==1){ | |
wsclient.println("fdel:OK"); | |
} else { | |
wsclient.println("fdel:BAD"); | |
} | |
} | |
currentLine = ""; | |
} else { | |
currentLine +=c; | |
} | |
} | |
} | |
wsclient.stop(); | |
Serial.println("Client disconnected."); | |
} | |
//************ END WIFI PORT ****************** | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment