반응형

[Arduino] - ESP32/NodeMcu Basic code for WiFi remote contol

 

ADUCON application

 

- Modify the HTTP response code in loop() function according to the protocol for "TEXT" set in the ADUCON app.

void loop() {
  client = server.available(); // Whether the server has data to receive and set now client
  String temp = "";
  while (client.connected()) { // Enter an infinite loop first to prevent re-execution of "client = server.available();"
    if(client.available()) {   // If the client has data
      temp = client.readStringUntil('\r'); 
      Serial.println(temp);
      if (temp.startsWith(F("GET"))) { // message for response
        Data  = "%%F4";                // protocol header of text for ADUCON
        Data += "Hello World ";
        Data += random(100,200);
        Data += "%%F1";                // protocol footer of text for ADUCON
        Data += '\n';                  // termination character of wifi for ADUCON
        Serial.println(Data);          // Output the message to the serial monitor.
        http_response(); 
        break;
      }
    } else break;
  }
  delay(10); // time for sending data at wifi component of ESP32 
}

 

ESP32_WIFI_BASIC_CONTROL.zip

ESP32_WIFI_BASIC_CONTROL.zip
0.00MB

 

ESP8266_WIFI_BASIC_CONTROL.zip

ESP8266_WIFI_BASIC_CONTROL.zip
0.00MB

 

- Output of Serial Monitor after upload scketch "ESP32_WIFI_BASIC_CONTROL.ino"

 

 

Connecting to the assigned IP while ESP01 is connected to the router

- Input the assigned IP address "192.168.1.6" on ADUCON app connected to network of "skynet".

received data of "%%F5" and "%%F8connect"
Respons of HTTP from ESP32

 

Connecting directly to the ESP32 and then connecting the access point

- Connect to ESP32 on the WiFi Setting of a mobile phone.

 

- Input the Password for the ACCESS POINT of ESP32.

 

- Select "Keep WiFi on" if the message output below.

 

- Input the address "192.168.4.1" on ADUCON app connected to ESP32.

received data of "%%F5" and "%%F8connect"
Respons of HTTP from ESP32

 

Code for remote control with WiFi of ADUCON applications

- wifi_read() function

1. Protocol header definition

ADUCON app's protocol header settings

Define the headers for Wifi in the Arduino sketch according to the protocol header settings in the ADUCON app.

#define pinHD F("%%F0")
#define pwmHD F("%%F3")
#define strHD F("%%F4")
#define footHD F("%%F1")
#define echoHD F("%%E1")
#define connectHD F("%%F8")
#define optionHD F("%%F9")
#define checkHD F("%%F5")
#define HDLen 4
#define addHD F("%%")
#define addHDLen 2

 

2. Code to check the connection attempt of ADUCON app

bool appConnection = false;

if (appConnection) { // If app connection is confirmed
  // Execute code after app connection is confirmed
} else { // Before app connection check
  if (income_wifi == checkHD) { // Check if the value is %%F5 and set the initial settings
    setFirstLogin(false); // Uncheck First Web Login (Do not use web login)
    detectLogout(false); // Uncheck Detect Logout (Do not use logout detection)
    showWebView(false); // Uncheck Web View (Do not display web view button)
    appConnection = true; http_response(); // Confirm the connection of ADUCOM
  }
}

 

3. Handling of values according to connection header "connectHD(%%F8)"

%%F8 + connect: Transmit the necessary app settings and current Arduino state values through the iniSet() and sendState() functions

%%F8 + disconnect: Disconnect and change value of appConnection to false

%%F8 + WiFi: change WiFi setting values

if (income_wifi == F("connect")) { iniSet(); sendState(); }
else if (income_wifi == F("disconnect")) appConnection = false;  
else if (income_wifi.startsWith(F("WiFi"))) { sendMessage(F("This funtion is not available."));  }
else Serial.println(income_wifi);

 

4. Handling of values according to option header "optionHD(%%F9)"

void CheckF9(String temp) {
  Serial.println(temp);
  if (temp.startsWith(F("RAD"))) { } // if it starts with "RAD"
  else if (temp.startsWith(F("CKB"))) { } // if it starts with "CKB"
  else if (temp.startsWith(F("CHD"))) { } // if it starts with "CHD"
  else if (temp.startsWith(F("LSD"))) { } // if it starts with "LSD"
  else if (temp.startsWith(F("STR"))) { } // if it starts with "STR"
  else { // if not
    if (temp.length() < 10) Serial.println(F("alarm")); // set the time if the length of the value is less than 10
    else Serial.println(F("date/time")); // current date/time if larger
  }
}

 

5. Handling of values according to button header "pinHD(%%F0)"

// income_wifi: Value + %%F1 (with protocol header %%F0 removed)
ed = income_wifi.lastIndexOf(footHD); // check the index number where footHD(%%F1) starts in the value
if (ed != -1) {                       // if the index number is found in the value
  temp = income_wifi.substring(0, ed);//Extract data up to index number
  uint8_t value = temp.toInt();       // convert to numeric value
  pin_control(value);                 // execute the code corresponding to the value
}

 

6. Handling of values according to SeekBar(slide bar) header "pwmHD(%%F3)"

// income_wifi: %%F3 + Count + %% + Slide Num + Value + %%F1
// Example of 3 values of COLOR PICKER: %%F31%%1255%%F1%%F31%%2133%%F1%%F31%%3123%%F1
ed = income_wifi.indexOf(addHD, HDLen); // Starting index of "%%" after HDLen 4(%%F3)
temp = income_wifi.substring(HDLen, ed); // Extract values from after HDLen 4(%%F3) to before "%%"
int count = temp.toInt(); // convert to numeric value
bool ok = true;
if (count == 0) pwmCount = 0; // If count is 0, reset counter for data verification
if (count < pwmCount) ok = false; // Verify if the data is continuous
else pwmCount = count;
if (ok) { // If data is continuous (unless data is missing due to Wi-Fi connection)
  uint8_t slide = 0; uint16_t value = 0;
  while(income_wifi.indexOf(footHD) != -1) { // Process 3 PWM values of COLOR PICKER
    ed = income_wifi.indexOf(addHD, HDLen)+addHDLen;
    income_wifi. remove(0, ed);
    temp = income_wifi.substring(0, 1);
    slide = temp. toInt();
    ed = income_wifi. indexOf(footHD);
    temp = income_wifi.substring(1, ed);
    value = temp. toInt();
    income_wifi.remove(0, ed+HDLen);
    pwm_control(slide, value);
  }
}

 

Protocol function to send data from Arduino to app

1. Change the state of the button.

void sendPinState(uint8_t pin_val){  
  Data += pinHD;
  Data += pin_val;
  Data += footHD;
  Data += '\n';
}

 

2. Transmits an echo for the received value of the button.

void sendPinEcho(uint8_t pin_val){  
  Data += pinHD;
  Data += pin_val;
  Data += echoHD;
  Data += '\n';
}

 

3. Change the slide state of the app.

void sendPwmSlide(uint8_t slide, uint16_t pwm_val) {                      
  Data += pwmHD;
  Data += slide;
  Data += pwm_val;
  Data += footHD;
  Data += '\n';
}

 

4. Transmits the echo for the received value of the PWM.

void sendPwmEcho(uint8_t slide, uint16_t pwm_val) {                          
  Data += pwmHD;
  Data += slide;
  Data += pwm_val;
  Data += echoHD;
  Data += '\n';
}

 

- A code example that reflects the status of the current button and PWM values of Arduino to the app when connected to the ADUCON app

// SEND LOCAL STATE AT PRESENT TO APP
void sendState() { 
  sendPinState(10);
  sendPinState(21);
  sendPinState(30);
  sendPinState(41);
  sendPinState(50);
  sendPinState(61);
  sendPinState(71);
  sendPinState(80);
  sendPinState(91);
  sendPinState(101);
  sendPinState(110);
  sendPinState(121);
  sendPinState(130);
  sendPinState(141);
  sendPinState(150);
  sendPwmSlide(1, 110);
  sendPwmSlide(2, 120);
  sendPwmSlide(3, 210);
}

Screen reflecting initial state and labels when connecting Arduino

 

5. Send text.

void sendText(String text) {                               
  Data += strHD;
  Data += text;
  Data += footHD;
  Data += '\n';
}

 

6. If web login is implemented using the HTML code in Arduino sketch, set to move to the login page when connecting for the first time.

void setFirstLogin(bool val){ 
  Data += connectHD;
  Data += F("FIR:");
  Data += val;
  Data += '\n';
}

 

7. If the control authority is released by a new web login, the Logout status is output as a message.

In case of connecting to the ACCESS POINT (ex. 192.168.4.1) - Administrator

When connecting via web login from the internal network through a router - General user

General user can be disengaged (pushed) by Administrator or another General user.

void detectLogout(bool val){ 
  Data += connectHD;
  Data += F("DET:");
  Data += val;
  Data += '\n';
}

 

8. Set to send the date/time of the Android system When connecting, or request the transmission of the date/time if necessary.

void requstDateTime(){ 
  Data += connectHD;
  Data += F("RDT:"); 
  Data += '\n';
}

 

9. Use when you want to acquire control authority by using the ACCESS POINT password of the module in the internal network connection through the router. (without using HTML web login)

It can be changed to any name through definition of the passName and passNameLen for processing the password in the sketch and the app send datas starting with passName as password value. This is to enhance security.

#define passName F("PAS:")
#define passNameLen 4

void requestPass(bool val){ 
  Data += connectHD;
  Data += val;
  Data += passName;
  Data += '\n';
}

Password dialog

 

10. If control authority is given through password confirmation, a cookie value is created and transmitted so that control authority for the data transmitted thereafter can be confirmed.

#define cooLength 5
String cooValue = "u7Dv0";

String makeCooValue() {
  cooValue = "";
  uint8_t temp = 0;
  for (int i = 0; i < cooLength; i++) {
    temp = random(39, 123); //random(39, 123); // 39 ~ 122 pick
    if (temp == 47 || temp == 59 || temp == 60 || temp == 62 || temp == 92 || temp == 94 || temp == 96) temp = 79;
    cooValue += char(temp);
  }
  Serial.print("coo: "); Serial.println(cooValue);
  return cooValue;
}

void responsePass(bool val){ 
  Data += connectHD;
  Data += F("RES:");
  if (val) Data += makeCooValue(); 
  else Data += val;
  Data += '\n';
}

 

11. Sends information about the current Wi-Fi connection so that the user can view and edit information in the app about the Wi-Fi connection.

void sendWifiInfo(){ 
  Data += connectHD;
  Data += F("INF:");
  Data += ssidAP;
  Data += ',';
  Data += passAP;
  Data += ',';
  Data += ap;
  Data += ',';
  Data += wifiRouter;
  Data += ',';
  Data += ssid;
  Data += ',';
  Data += pass;
  Data += ',';
  Data += ip;
  Data += '\n';
}

 

12. Outputs the message in the form of a pop-up.

void sendMessage(String str){ 
  Data += connectHD;
  Data += F("MES:");
  Data += str;
  Data += '\n';
}

 

13. Disable the feature, which is default setting, of automatically resend the value When a button or SeekBar (slide bar) is changed in the app and then if a response failure error occurs.

It is used when the Arduino intentionally does not respond due to the delay that occurs when sending data using the AT command to the ESP01.

void responseFailResend(bool val) {
  Data += connectHD;
  Data += F("RFR:");
  Data += val;
  Data += '\n';
}

 

14. Instruct the app to click the Disconnect Button.

void sendDisconnect(){ 
  Data += connectHD;
  Data += F("DIS");
  Data += '\n';
}

Disconnect Button for WiFi

 

15. Set the function to continuously transmit the changed value of the SeekBar (slide bar) according to the specified interval.

true: Active Slide Mode

false: Passive Slide Mode (transmits only values when the slide bar is touched up)

void setActiveSlide(bool val){ 
  Data += optionHD;
  Data += F("ACT:");
  Data += val;
  Data += '\n';
}

 

16. Set transmission interval of ACTIVE SLIDE MODE. (Unit: Millisecond)

void setSlideInterval(uint16_t val){ 
  Data += optionHD;
  Data += F("SLI:");
  Data += val;
  Data += '\n';
}

 

17. Activate a timer that sends checkHD(%%F5) at regular time intervals.

Regardless of the app, it is used to reflect independently changed values in the Arduino to the app.

void setRequest(bool val){ 
  Data += optionHD;
  Data += F("REQ:");
  Data += val;
  Data += '\n';
}

 

18. Set the operation interval of the timer sending checkHD(%%F5). (Unit: Millisecond)

void setRequestInterval(uint16_t val){ 
  Data += optionHD;
  Data += F("INT:");
  Data += val;
  Data += '\n';
}

 

19. Set the operation function when clicking the label displaying the current connection status.

0: No function.

1: Show only connection information when it clicked.

2: Show connection information and password when it clicked so that the connection and password can be set.

// 0: not use, 1: show only name, 2: show name & pass // defalult: 2
void setSSIDLabelMode(uint8_t val) { 
  Data += optionHD;
  Data += F("SLM:");
  Data += val;
  Data += '\n';
}

 

20. Display the button to go to the webview page where you can see the HTML Page coded in Arduino.

void showWebView(bool val){ 
  Data += optionHD;
  Data += F("SWV:"); 
  Data += val;
  Data += '\n';
}

 

21. Display additional push buttons in the main menu area. (it is possible to control only at Arduino)

void showDialogMenu(bool val){ 
  Data += optionHD;
  Data += F("SDM:"); 
  Data += val;
  Data += '\n';
}

 

22. Change the main screen to Color Picker / Pwm Nob screen from Arduino. (generally set in the options of the app)

uint8_t: list below

0: Display buttons and SeekBar (slide bar) on the main screen

1: Display Color_Picker on the main screen.

2: Display Color_Picker and Group 2 on the main screen.

3: Display PWM Nob on the main screen.

4: Display PWM Nob and Group 2 on the main screen.

String: name of the group

bool: Sets whether to display the SeekBar (slide bar) when the main screen buttons are displayed.

* true: SeekBar (slide bar) is also displayed when button is displayed or Pwm Nob is displayed

* false: SeekBar (slide bar) is not displayed when button is displayed and SeekBar (slide bar) is displayed only when Pwm Nob is displayed

//0: not use 1:color picke 2:color picker+add group 3: pwm nob 4: pwm nob+add group, group1 & group2 Label
void setColorNob(uint8_t val, String group1, String group2, bool pwmDPMain) { 
  Data += optionHD;
  Data += F("CNB:");
  Data += String(val); Data += ','; 
  Data += group1; Data += ','; 
  Data += group2; Data += ','; 
  Data += pwmDPMain;
  Data += '\n';
}

 

23. Set the main screen display of the app.

Set to 0 or 1 in order depending on whether buttons 1 to 15, SeekBar (slide bar) 1 to 6, text input, and floating buttons are displayed.

0: hidden 1: visible

///////////////////////// SET MAIN DISPLAY 0:hide 1:display
//F("111111111111111"  // button 1 ~ 15
//  "111111"           // slider 1 ~ 6
//  "11")              // Text Input/Floating Button
void setDisplay(String str){ 
  Data += optionHD;
  Data += F("SDP:");
  Data += str;
  Data += '\n';
}

 

24. Set labels for buttons and SeekBar.

Label buttons 1 to 15 and SeekBar (slide bar) 1 to 6 in order using ',' as a separator.

//F("BT1,BT2,BT3,BT4,BT5,BT6,BT7,BT8,BT9,BT10,BT11,BT12,BT13,BT14,BT15," // button 1 ~ 15
//  "PW1,PW2,PW3,PW4,PW5,PW6") // slider 1 ~ 6
void setButtonLabel(String str){ 
  Data += optionHD;
  Data += F("ABL:");
  Data += str;
  Data += '\n';
}

 

25. Set the button's toggle/push properties.

Set the button properties to 0 or 1 in order of buttons 1 to 15.

0: Push 1: Toggle

//F("111111111111111") // button 1 ~ 15, 0:push 1:toggle 
void setButtonToggle(String str){ 
  Data += optionHD;
  Data += F("BTG:");
  Data += str;  
  Data += '\n';
}

 

26. Set the maximum/minimum values of the SeekBar (slide bar).

SeekBar (slide bar) Specifies the maximum/minimum values from 1 to 6 in order using ',' as a separator.

//F("0,255,0,255,0,255,0,255,0,255,0,255")
void setMinMax(String str){ 
  Data += optionHD;
  Data += F("MNM:");
  Data += str;
  Data += '\n';
}

 

27. Set the title of the Arduino sketch.

//title: Test Mode
//text: Arduino<br/>- WiFi Controler<br/><br/>Manual<br/><a href='https://postpop.tistory.com/8'>https://postpop.tistory.com/8</a>
void setAppTitleMessage(String title, String text){
  Data += optionHD;
  Data += F("ATM:"); 
  Data += title; Data += ','; 
  Data += text; 
  Data += '\n';
}

 

28. Dialog Message: radio button

// option = "A,B,C" -> selection index = 1(A) ~ 3(C)
void dialogRadio(uint8_t id, String title, String option, uint8_t selection){ 
  Data += optionHD;
  Data += F("RAD:"); 
  Data += String(id); Data += ':';
  Data += title; Data += ':';
  Data += option; Data += ':';
  Data += String(selection);
  Data += '\n';
} // Return -> RAD:0:1(index) , RAD:id:option index

 

29. Dialog Message: Checkbox

// option = "A,B,C"
void dialogCheckbox(uint8_t id, String title, String option){ 
  Data += optionHD;
  Data += F("CKB:"); 
  Data += String(id); Data += ':';
  Data += title; Data += ':';
  Data += option; 
  Data += '\n';
} // Return -> CKB:0:1,3(index) , CKB:id:option index

 

30. Dialog Message: Select button

// option = "A,B"
void dialogChoose(uint8_t id, String message, String option){ 
  Data += optionHD;
  Data += F("CHD:"); 
  Data += String(id); Data += ':'; 
  Data += message; Data += ':'; 
  Data += option; 
  Data += '\n';
} // Return -> CHD:0:B , CHD:id:option str

 

31. Dialog Message: List Window

// option = "A,B,C,D,E,F,G,H"
void dialogList(uint8_t id, String title, String option){ 
  Data += optionHD;
  Data += F("LSD:"); 
  Data += String(id); Data += ':';
  Data += title; Data += ':'; 
  Data += option; 
  Data += '\n';
} // Return -> LSD:0:F , LSD:id:option str

 

32. Dialog Message: Input text

void dialogInput(uint8_t id, String title){ 
  Data += optionHD;
  Data += F("STR:"); 
  Data += String(id); Data += ':'; 
  Data += title; 
  Data += '\n';
} // Return -> STR:0:str , STR:id:str

 

1.ESP32_WIFI_HMI_CONTROL.zip

1.ESP32_WIFI_HMI_CONTROL.zip
0.00MB

 

1.ESP8266_WIFI_HMI_CONTROL.zip

1.ESP8266_WIFI_HMI_CONTROL.zip
0.00MB

 

https://youtu.be/2xkX0Wk-ePo

 

Main sketch

더보기
#include <WiFi.h>
#include <WiFiClient.h>
#include <WiFiAP.h>

WiFiServer server(80);
WiFiClient client;

const char ssidAP[] = "Esp32";   // your network SSID (name)
const char passAP[] = "12345678"; //"1234test";         // your network password
const char ssid[] = "skynet";    // "SK_WiFiGIGA40F7" your network SSID (name)
const char pass[] = "skynet00";  // "1712037218" your network password 

bool wifiRouter = true;          // Whether or not there is a router connection
String ap = "";
String ip = "";

String Data = "";
#include "protocol.h"

String toStringIp(IPAddress ip) {  // convert ip address to string
  String res = "";
  for (int i = 0; i < 3; i++) res += String((ip >> (8 * i)) & 0xFF) + ".";
  res += String(((ip >> 8 * 3)) & 0xFF);
  return res;
}

void setup() {
  Serial.begin(115200);
  if (wifiRouter) WiFi.mode(WIFI_AP_STA);  
  else WiFi.mode(WIFI_AP);
  WiFi.softAP(ssidAP, passAP);
  ap = toStringIp(WiFi.softAPIP());
  Serial.print(F("AP address: "));
  Serial.println(ap);
  if (wifiRouter) {
    if (pass == "") WiFi.begin(ssid);
    else WiFi.begin(ssid, pass);    // Connect to WPA/WPA2 network
    uint8_t wifi_count = 0;
    while (WiFi.status() != WL_CONNECTED) {
          delay(200); Serial.print(F("."));
          wifi_count++;
          if (wifi_count == 50) break;
    }
    delay(10);
    ip = toStringIp(WiFi.localIP());
    Serial.print(F("IP address: "));
    Serial.println(ip);
  }
  server.begin();
}

void pin_control(uint8_t value) {
  if (value != 0) {
    switch (value) {
      case 11: Serial.println(F("button 1 : on"));
               sendPinEcho(value);
               sendText(F("1: ON")); 
                  break;
      case 10: Serial.println(F("button 1 : off"));
               sendText(F("1: OFF")); 
               sendPinEcho(value);
                  break;
      case 21: Serial.println(F("button 2 : on"));
                  break;
      case 20: Serial.println(F("button 2 : off"));
                  break;
      case 31: Serial.println(F("button 3 : on"));
                  break;
      case 30: Serial.println(F("button 3 : off"));
                  break;
      case 41: Serial.println(F("button 4 : on"));
                  break;
      case 40: Serial.println(F("button 4 : off"));
                  break;
      case 51: Serial.println(F("button 5 : on"));
                  break;
      case 50: Serial.println(F("button 5 : off"));
                  break;
      case 61: Serial.println(F("button 6 : on"));
                  break;
      case 60: Serial.println(F("button 6 : off"));
                  break;
      case 71: Serial.println(F("button 7 : on"));
                  break;
      case 70: Serial.println(F("button 7 : off"));
                  break;
      case 81: Serial.println(F("button 8 : on"));
                  break;
      case 80: Serial.println(F("button 8 : off"));
                  break;
      case 91: Serial.println(F("button 9 : on"));
                  break;
      case 90: Serial.println(F("button 9 : off"));
                  break;  
      case 101: Serial.println(F("button 10 : on"));
                  break;
      case 100: Serial.println(F("button 10 : off"));
                  break;  
      case 111: Serial.println(F("button 11 : on"));
                  break;
      case 110: Serial.println(F("button 11 : off"));
                  break;  
      case 121: Serial.println(F("button 12 : on"));
                  break;
      case 120: Serial.println(F("button 12 : off"));
                  break;                  
      case 131: Serial.println(F("button 13 : on"));
                  break;  
      case 130: Serial.println(F("button 13 : off"));
                  break;  
      case 141: Serial.println(F("button 14 : on"));
                  break;  
      case 140: Serial.println(F("button 14 : off"));
                  break;  
      case 151: Serial.println(F("button 15 : on"));
                  break;  
      case 150: Serial.println(F("button 15 : off"));
                  break;         
      case 161: Serial.println(F("button 16 : click"));
                  break; 
      case 171: Serial.println(F("button 17 : click"));
                  break;
      case 181: Serial.println(F("button 18 : click"));
                  break;  
      case 191: Serial.println(F("button 19 : click"));
                  break;  
      case 201: Serial.println(F("button 20 : click"));
                  break;                       
    }
  }
}

void pwm_control(uint8_t num, uint16_t value) {
  if (num == 1) {  // SeekBar (slide bar) 1
    sendPwmEcho(num, value); 
    Serial.print(F("pwm1: ")); Serial.println(value);
  } else if (num == 2) { // SeekBar (slide bar) 2
    sendPwmEcho(num, value);
    Serial.print(F("pwm2: ")); Serial.println(value);
  } else if (num == 3) {  // SeekBar (slide bar) 3
    sendPwmEcho(num, value);
    Serial.print(F("pwm3: ")); Serial.println(value);
  } else if (num == 4) {  // SeekBar (slide bar) 4
    Serial.print(F("pwm4: ")); Serial.println(value);
  } else if (num == 5) {  // SeekBar (slide bar) 5
    Serial.print(F("pwm5: ")); Serial.println(value);
  } else if (num == 6) {  // SeekBar (slide bar) 6
    Serial.print(F("pwm6: ")); Serial.println(value);
  }
}

bool appConnection = false;
String income_wifi = ""; // storage variable for received string with WiFi
uint16_t pwmCount = 2;

void wifi_read() { 
  client = server.available(); 
  while (client.connected()) {
    if(client.available()) {     
      income_wifi = client.readStringUntil('\r');
      if (income_wifi.startsWith(F("GET"))) income_wifi.remove(0,5); // remove "GET /" 
      if (!income_wifi.startsWith(F("favi"))) {                      // favicon.ico pass
        int ed = income_wifi.lastIndexOf(F("HTTP/1.1"));
        income_wifi.remove(ed-1);
        if (appConnection) { 
          String temp = income_wifi.substring(0, HDLen);
          uint8_t order = 0;
          if (temp == checkHD) order = 1;
          else if (temp == pwmHD) order = 2;
          else if (temp == pinHD) order = 3;
          else if (temp == connectHD) order = 4;
          else if (temp == optionHD) order = 5; 
          if (order > 2) income_wifi.remove(0, HDLen);   // Remove protocol header
          if (order == 1) { // %%F5
            
          } else if (order == 2) { // %%F3
            ed = income_wifi.indexOf(addHD, HDLen);
            temp = income_wifi.substring(HDLen, ed);
            int count = temp.toInt();
            bool ok = true;
            if (count == 0) pwmCount = 0;
            if (count < pwmCount) ok = false;
            else pwmCount = count;
            if (ok) {
              uint8_t slide = 0; uint16_t value = 0;
              while(income_wifi.indexOf(footHD) != -1) {
                ed = income_wifi.indexOf(addHD, HDLen)+addHDLen;
                income_wifi.remove(0, ed);
                temp = income_wifi.substring(0, 1);
                slide = temp.toInt();
                ed = income_wifi.indexOf(footHD);
                temp = income_wifi.substring(1, ed);
                value = temp.toInt();
                income_wifi.remove(0, ed+HDLen);
                pwm_control(slide, value);
              }
            } 
          } else if (order == 3) { // %%F0
            ed = income_wifi.lastIndexOf(footHD);
            if (ed != -1) {
              temp = income_wifi.substring(0, ed);
              uint8_t value = temp.toInt();
              pin_control(value);
            }
          } else if (order == 4) { // %%F8
            if (income_wifi == F("connect")) { iniSet(); send_iniState(); }
            else if (income_wifi == F("disconnect")) appConnection = false;  
            else if (income_wifi.startsWith(F("WiFi"))) { sendMessage(F("This funtion is not available.")); } 
            else Serial.println(income_wifi);
          } else if (order == 5) { // %%F9
            income_wifi.replace(F("%20"), F(" ")); // URL Encoding space character change
            CheckF9(income_wifi);
          } else { // order == 0
            income_wifi.replace(F("%20"), F(" ")); // URL Encoding space character change
            Serial.println(income_wifi);
          }
          http_response();
        } else {
          if (income_wifi == checkHD) { 
            setFirstLogin(false); detectLogout(false); showWebView(false);
            appConnection = true; http_response(); 
          }
        }
      }
      income_wifi = "";
      break;
    } else break;
  }
}

unsigned long int one_millis = 0;

void wifi_delay() { // Check client connection and receive data every 10 milliseconds
  if (millis() - one_millis > 10) {
    one_millis = millis();
    wifi_read();
  }
}

void loop() {
  wifi_delay(); 
  if(Serial.available()) { // If there is data in the serial buffer
    String temp = Serial.readStringUntil('\n'); // Serial Monitor - NEW LINE option
    Serial.println(temp);
    if(temp == "on"){  
      Serial.println("ON");
      sendPinState(11);     
      sendText("ON");    
    } else if(temp == "off"){ 
      Serial.println("OFF");
      sendText("OFF");
      sendPinState(10);      
    } else if(temp == "1"){   
      
    } 
  }
}

 

Protocol sketch

더보기
//////////////////////////////////////////////////////////// COMMON
///////////////////////////////////////// ESSENTIAL 
#define pinHD F("%%F0")
#define pwmHD F("%%F3")
#define strHD F("%%F4")
#define footHD F("%%F1")
#define echoHD F("%%E1")
#define connectHD F("%%F8")
#define optionHD F("%%F9")
#define checkHD F("%%F5")
#define HDLen 4
#define addHD F("%%")
#define addHDLen 2

// SEND PIN VALUE
void sendPinState(uint8_t pin_val){  
  Data += pinHD;
  Data += pin_val;
  Data += footHD;
  Data += '\n';
}

// SEND PIN ECHO
void sendPinEcho(uint8_t pin_val){  
  Data += pinHD;
  Data += pin_val;
  Data += echoHD;
  Data += '\n';
}

// SEND SLIDE VALUE - USE AT PASSIVE SLIDE MODE
void sendPwmSlide(uint8_t slide, uint16_t pwm_val) {                      
  Data += pwmHD;
  Data += slide;
  Data += pwm_val;
  Data += footHD;
  Data += '\n';
}

// SEND SLIDE ECHO - ESP01: USE AT PASSIVE SLIDE MODE
void sendPwmEcho(uint8_t slide, uint16_t pwm_val) {                          
  Data += pwmHD;
  Data += slide;
  Data += pwm_val;
  Data += echoHD;
  Data += '\n';
}

// SEND TEXT
void sendText(String text) {                               
  Data += strHD;
  Data += text;
  Data += footHD;
  Data += '\n';
}

///////////////////////////////////////// CONNECTION 
void setFirstLogin(bool val){ 
  Data += connectHD;
  Data += F("FIR:");
  Data += val;
  Data += '\n';
}

void detectLogout(bool val){ 
  Data += connectHD;
  Data += F("DET:");
  Data += val;
  Data += '\n';
}

void requstDateTime(){ 
  Data += connectHD;
  Data += F("RDT:"); 
  Data += '\n';
}

//#define passName F("PAS:")
//#define passNameLen 4
//
//void requestPass(bool val){ 
//  Data += connectHD;
//  Data += val;
//  Data += passName;
//  Data += '\n';
//}
//
//#define cooLength 5
//String cooValue = "u7Dv0";
//
//String makeCooValue() {
//  cooValue = "";
//  uint8_t temp = 0;
//  for (int i = 0; i < cooLength; i++) {
//    temp = random(39, 123); //random(39, 123); // 39 ~ 122 pick
//    if (temp == 47 || temp == 59 || temp == 60 || temp == 62 || temp == 92 || temp == 94 || temp == 96) temp = 79;
//    cooValue += char(temp);
//  }
//  Serial.print("coo: "); Serial.println(cooValue);
//  return cooValue;
//}
//
//void responsePass(bool val){ 
//  Data += connectHD;
//  Data += F("RES:");
//  if (val) Data += makeCooValue(); 
//  else Data += val;
//  Data += '\n';
//}

void sendWifiInfo(){ 
  Data += connectHD;
  Data += F("INF:");
  Data += ssidAP;
  Data += ',';
  Data += passAP;
  Data += ',';
  Data += ap;
  Data += ',';
  Data += wifiRouter;
  Data += ',';
  Data += ssid;
  Data += ',';
  Data += pass;
  Data += ',';
  Data += ip;
  Data += '\n';
}

void sendMessage(String str){ 
  Data += connectHD;
  Data += F("MES:");
  Data += str;
  Data += '\n';
}

// defalt: true
void responseFailResend(bool val) {
  Data += connectHD;
  Data += F("RFR:");
  Data += val;
  Data += '\n';
}

void sendDisconnect(){ 
  Data += connectHD;
  Data += F("DIS");
  Data += '\n';
}

///////////////////////////////////////// OPTION
///////////////////////// SET FUNCION
void setActiveSlide(bool val){ 
  Data += optionHD;
  Data += F("ACT:");
  Data += val;
  Data += '\n';
}

void setSlideInterval(uint16_t val){ 
  Data += optionHD;
  Data += F("SLI:");
  Data += val;
  Data += '\n';
}

void setRequest(bool val){ 
  Data += optionHD;
  Data += F("REQ:");
  Data += val;
  Data += '\n';
}

void setRequestInterval(uint16_t val){ 
  Data += optionHD;
  Data += F("INT:");
  Data += val;
  Data += '\n';
}

void setSSIDLabelMode(uint8_t val) { // 0: not use, 1: show only name, 2: show name & pass // defalult: 2
  Data += optionHD;
  Data += F("SLM:");
  Data += val;
  Data += '\n';
}

void showWebView(bool val){ 
  Data += optionHD;
  Data += F("SWV:"); 
  Data += val;
  Data += '\n';
}

void showDialogMenu(bool val){ 
  Data += optionHD;
  Data += F("SDM:"); 
  Data += val;
  Data += '\n';
}

//0: not use 1:color picke 2:color picker+add group 3: pwm nob 4: pwm nob+add group, group1 & group2 Label
void setColorNob(uint8_t val, String group1, String group2, bool pwmDPSync) { 
  Data += optionHD;
  Data += F("CNB:");
  Data += String(val); Data += ','; 
  Data += group1; Data += ','; 
  Data += group2; Data += ','; 
  Data += pwmDPSync;
  Data += '\n';
}

///////////////////////// SET MAIN DISPLAY
//F("111111111111111"  // button 1 ~ 15, 0:hide 1:display
//  "111111"           // slider 1 ~ 6
//  "11")              // Text Input/Floating Button
void setDisplay(String str){ // must after setLeftPadMode(uint8_t val)
  Data += optionHD;
  Data += F("SDP:");
  Data += str;
  Data += '\n';
}

//F("BT1,BT2,BT3,BT4,BT5,BT6,BT7,BT8,BT9,BT10,BT11,BT12,BT13,BT14,BT15," // button 1 ~ 15
//  "PW1,PW2,PW3,PW4,PW5,PW6") // slider 1 ~ 6
void setButtonLabel(String str){ 
  Data += optionHD;
  Data += F("ABL:");
  Data += str;
  Data += '\n';
}

//F("111111111111111") // button 1 ~ 15, 0:push 1:toggle 
void setButtonToggle(String str){ 
  Data += optionHD;
  Data += F("BTG:");
  Data += str;  
  Data += '\n';
}

//F("0,255,0,255,0,255,0,255,0,255,0,255")
void setMinMax(String str){ 
  Data += optionHD;
  Data += F("MNM:");
  Data += str;
  Data += '\n';
}

void setAppTitleMessage(String title, String text){
  Data += optionHD;
  Data += F("ATM:"); 
  Data += title; Data += ','; 
  Data += text; 
  Data += '\n';
}

///////////////////////// PERFORM DIALOG
// option = "A,B,C" -> index = 1(A) ~ 3(C)
void dialogRadio(uint8_t id, String title, String option, uint8_t selection){ 
  Data += optionHD;
  Data += F("RAD:"); 
  Data += String(id); Data += ':';
  Data += title; Data += ':';
  Data += option; Data += ':';
  Data += String(selection);
  Data += '\n';
} // Return -> RAD:0:1(index) , RAD:id:option index

// option = "A,B,C"
void dialogCheckbox(uint8_t id, String title, String option){ 
  Data += optionHD;
  Data += F("CKB:"); 
  Data += String(id); Data += ':';
  Data += title; Data += ':';
  Data += option; 
  Data += '\n';
} // Return -> CKB:0:1,3(index) , CKB:id:option index

// option = "A,B"
void dialogChoose(uint8_t id, String message, String option){ 
  Data += optionHD;
  Data += F("CHD:"); 
  Data += String(id); Data += ':'; 
  Data += message; Data += ':'; 
  Data += option; 
  Data += '\n';
} // Return -> CHD:0:B , CHD:id:option str

// option = "A,B,C,D,E,F,G,H"
void dialogList(uint8_t id, String title, String option){ 
  Data += optionHD;
  Data += F("LSD:"); 
  Data += String(id); Data += ':';
  Data += title; Data += ':'; 
  Data += option; 
  Data += '\n';
} // Return -> LSD:0:F , LSD:id:option str 

void dialogInput(uint8_t id, String title){ 
  Data += optionHD;
  Data += F("STR:"); 
  Data += String(id); Data += ':'; 
  Data += title; 
  Data += '\n';
} // Return -> STR:0:str , STR:id:str

//////////////////////////////////////////////////////////// INDIVIDUAL
void iniSet() {
  sendWifiInfo(); 
  setDisplay(F("111111111111111" // button 1 ~ 15, 0:hide 1:display
               "111111"          // slider 1 ~ 6
               "11"));           // Text Input/Floating Button
  setColorNob(0, F("GR1"), F("GR2"), true);
  setButtonLabel(F("BT1,BT2,BT3,BT4,BT5,BT6,BT7,BT8,BT9,BT10,BT11,"
                   "BT12,BT13,BT14,BT15,PW1,PW2,PW3,PW4,PW5,PW6"));
  setButtonToggle(F("111111111111111"));  // button 1 ~ 15, 0:push 1:toggle 
  setMinMax(F("0,255,0,255,0,255,0,255,0,255,0,255")); // slider 1 ~ 6
  setAppTitleMessage(F("Test Mode"),                    //Title
  F("Arduino<br/>- WiFi Controler<br/><br/>매뉴얼<br/>" //Message
    "<a href='https://postpop.tistory.com/8'>"
    "https://postpop.tistory.com/8</a>"));
  setActiveSlide(true); setSlideInterval(100);
  setRequest(false); //setRequestInterval(1000);
}

// SEND LOCAL STATE AT PRESENT TO APP
void send_iniState() { // 현재 상태값
  sendPinState(10);
  sendPinState(21);
  sendPinState(30);
  sendPinState(41);
  sendPinState(50);
  sendPinState(61);
  sendPinState(71);
  sendPinState(80);
  sendPinState(91);
  sendPinState(101);
  sendPinState(110);
  sendPinState(121);
  sendPinState(130);
  sendPinState(141);
  sendPinState(150);
  sendPwmSlide(1, 110);
  sendPwmSlide(2, 120);
  sendPwmSlide(3, 210);
  sendPwmSlide(4, 0);
  sendPwmSlide(5, 0);
  sendPwmSlide(6, 0);
}

void http_response() {
  client.println(F("HTTP/1.1 200 OK\n"));
  client.print(Data);
  Data = "";
}

void CheckF9(String temp) {
  Serial.println(temp);
  if (temp.startsWith(F("RAD"))) {  }
  else if (temp.startsWith(F("CKB"))) {  }
  else if (temp.startsWith(F("CHD"))) {  }
  else if (temp.startsWith(F("LSD"))) {  }
  else if (temp.startsWith(F("STR"))) {  }
  else {  
    if (temp.length() < 10) Serial.println(F("alarm"));
    else Serial.println(F("date/time"));
  }
}

 

Example of using Protocol to set ADUCON app

- ADUCON app settings: label/toggle properties, use of the timer for checkHD(%%F5), Slide Mode settings, etc.

void iniSet() {
  sendWifiInfo();
  setDisplay(F("111111111111111" // button 1 ~ 15, 0:hide 1:display
               "111111"          // slider 1 ~ 6
               "11"));           // Text Input/Floating Button
  setColorNob(0, F("GR1"), F("GR2"), false); // Set Color Picker and Pwm Nob
  setButtonLabel(F("BT1,BT2,BT3,BT4,BT5,BT6,BT7,BT8,BT9,BT10,BT11," // set labels
                   "BT12,BT13,BT14,BT15,PW1,PW2,PW3,PW4,PW5,PW6"));
  setButtonToggle(F("111111111111111")); // button 1 ~ 15, toggle setting, 0:push 1:toggle
  setMinMax(F("0,255,0,255,0,255,0,255,0,255,0,255")); // slider 1 ~ 6
  setAppTitleMessage(F("Test Mode"), // Change title for Arduino code on the app
  F("Arduino<br/>- WiFi Controler<br/><br/>Manual<br/>" //Message
    "<a href='https://postpop.tistory.com/8'>"
    "https://postpop.tistory.com/8</a>"));
  setActiveSlide(true); setSlideInterval(100);   // Active Slide Mode
  setRequest(false); //setRequestInterval(1000); // Disable timer for check(%%F5)
  showDialogMenu(true); // show menu button
}

 

- Example of code processing for each function of a button that changes according to the screen number (pageId).

void pin_control(uint8_t value) {
  if (value != 0) {
    switch (value) {
      case 11: if (pageId == 4) Serial.println(F("전원 : on"));
               else Serial.println(F("button 1 : on"));
                  break;
      case 10: if (pageId == 4) Serial.println(F("전원 : off"));
               else Serial.println(F("button 1 : off"));
                  break;
      case 21: if (pageId == 4) { Serial.println(F("모터1 : on")); sendPinState(30); }
               else Serial.println(F("button 2 : on"));
                  break;
      case 20: if (pageId == 4) Serial.println(F("모터1 : off"));
               else Serial.println(F("button 2 : off"));
                  break;
      case 31: if (pageId == 4) { Serial.println(F("모터2 : on")); sendPinState(20); }
               else Serial.println(F("button 3 : on"));
                  break;
      case 30: if (pageId == 4) Serial.println(F("모터2 : off"));
               else Serial.println(F("button 3 : off"));
                  break;
      case 201: Serial.println(F("MENU : click"));
                dialogList(0, F("MENU"), F("select,Dial Nob,Color Picker,Key Pad,Arrow Pad,Main"));
                  break;                       
    }
  }
}

 

- Example of modifying the function "CheckF9()" that handles dialog messages.

uint8_t pageId = 0;  // screen number

void CheckF9(String temp) {
  Serial.println(temp);
  if (temp.startsWith(F("RAD"))) {  }         // radio button
  else if (temp.startsWith(F("CKB"))) {  }    // Checkbox
  else if (temp.startsWith(F("CHD"))) {  }    // Select button
  else if (temp.startsWith(F("LSD"))) {       // List Window
    int ed = temp.indexOf(':');
    temp.remove(0, ed+1);
    ed = temp.indexOf(':');
    String ID = temp.substring(0, ed);
    uint8_t id = ID.toInt();
    temp.remove(0, ed+1);
    if (id == 0) {
      if (temp.startsWith(F("Dial"))) {           // Dial Nob screen 
        setDisplay(F("11111111111111111111110")); // hide floating button
        setColorNob(4, F("GR1"), F("GR2"), false); pageId = 1; 
      } else if (temp.startsWith(F("Color"))) {   // Color Picker screen
        setDisplay(F("11111111111111111111110")); // hide floating button
        setColorNob(2, F("GR1"), F("GR2"), false); pageId = 2; 
      } else if (temp.startsWith(F("Key"))) {     // Key Pad screen
        if (pageId <= 2) setColorNob(0, F("GR1"), F("GR2"), false);
        setDisplay(F("11111111111100011111110")); 
        setButtonToggle(F("000000000000111"));
        setButtonLabel(F("1,2,3,4,5,6,7,8,9,*,0,#"));
        pageId = 3;
      } else if (temp.startsWith(F("Arrow"))) {   // Arrow Pad screen
        if (pageId <= 2) setColorNob(0, F("GR1"), F("GR2"), false);
        setDisplay(F("11111101010101011111110")); 
        setButtonToggle(F("111111000000000"));
        setButtonLabel(F("Power,Motor1,Moror2,BT4,BT5,BT6,7,Up,9,Left,0,Right,0,Down"));
        pageId = 4;
      } else {                                    // Main screen
        if (pageId <= 2) { setColorNob(0, F("GR1"), F("GR2"), false); pageId = 0; }
        setDisplay(F("11111111111111111111111"));
        setButtonToggle(F("111111111111111"));
        setButtonLabel(F("BT1,BT2,BT3,BT4,BT5,BT6,BT7,BT8,BT9,BT10,BT11,BT12,BT13,BT14,BT15"));
        send_State();
        pageId = 0;
      }
    }
  }
  else if (temp.startsWith(F("STR"))) {  }       // Input text
  else {  
    if (temp.length() < 6) Serial.println(F("alarm"));
    else Serial.println(F("date/time"));
  }
}

 

1-1.ESP32_WIFI_HMI_CONTROL_MENU.zip

1-1.ESP32_WIFI_HMI_CONTROL_MENU.zip
0.01MB

1-1.ESP8266_WIFI_HMI_CONTROL_MENU.zip

1-1.ESP8266_WIFI_HMI_CONTROL_MENU.zip
0.01MB

 

https://youtu.be/SNPHeavhEys

 

 

Web Manager

You can change the ID and password for access point and router connection using HTML coded in Arduino.

- Changed the variable type from const char to char so that the SSID and password for connecting to the router can be changed in the loop() function.

char ssid[21] = "skynet";    // "SK_WiFiGIGA40F7" your network SSID (name)
char pass[21] = "skynet00";  // "1712037218" your network password

- SPIFFS Setting

bool saveConfig() {  // Write ID and Password
  String value = ssid;
  value += ','; value += pass;
  value += ','; value += wifiRouter; 
  File configFile = SPIFFS.open(F("/config.txt"), "w");
  if (!configFile) {
    Serial.println(F("Failed to open config file for writing"));
    return false;
  } else {
    configFile.println(value); // save at config.txt including '\n'
    configFile.close();
    return true; 
  }
}

bool loadConfig() {  // Read ID and Password
  File configFile = SPIFFS.open(F("/config.txt"), "r");
  if (!configFile) {
    Serial.println(F("Failed to open config file"));
    return false;
  } else {
    String temp = configFile.readStringUntil(',');
    for (int i = 0; i < temp.length(); i++) ssid[i] = temp[i];
    ssid[temp.length()] = '\0';
    temp = configFile.readStringUntil(',');
    for (int i = 0; i < temp.length(); i++) pass[i] = temp[i];
    pass[temp.length()] = '\0';
    temp = configFile.readStringUntil('\r');
    wifiRouter = temp.toInt();
    configFile.close();
    Serial.println(ssid);
    Serial.println(pass);
    Serial.println(wifiRouter);
    return true;
  }
}

void setup() {
    if (SPIFFS.exists(F("/config.txt"))) loadConfig(); // if there is "/config.txt", read
    else saveConfig();  // if there is not, save a new config.txt 
}

// SPIFFS initialization code at serial monitor
void loop() {
  if(Serial.available()) { // Serial monitor
    String temp = Serial.readStringUntil('\n');
    Serial.println(temp);
    if(temp == "1"){   
      Serial.println(F("format..."));
      SPIFFS.format();
      Serial.println(F("done. can start upload."));
      while(1); // stanby for uploading
    } 
  }
}

 

2.ESP32_WIFI_HMI_WEB_MANAGER.zip

2.ESP32_WIFI_HMI_WEB_MANAGER.zip
0.01MB

2.ESP8266_WIFI_HMI_WEB_MANAGER.zip

2.ESP8266_WIFI_HMI_WEB_MANAGER.zip
0.01MB

 

SPIFFS_FORMAT_ESP32.zip

SPIFFS_FORMAT_ESP32.zip
0.00MB

SPIFFS_FORMAT_ESP8266.zip

SPIFFS_FORMAT_ESP8266.zip
0.00MB

 

https://youtu.be/qbofiZBYwAA

 

 

Web Login

When connecting through a router, obtain control authority using the name and password of the access point.

Login Page
Web Manager with Login

 

3.ESP32_WIFI_HMI_WEB_LOGIN.zip

3.ESP32_WIFI_HMI_WEB_LOGIN.zip
0.01MB

3.ESP8266_WIFI_HMI_WEB_LOGIN.zip

3.ESP8266_WIFI_HMI_WEB_LOGIN.zip
0.01MB

 

https://youtu.be/0hAbI_CerE0

 

 

App Wifi Manager

You can change the ID and password for connecting to access points and a router without using HTML code.

 

4.ESP32_WIFI_HMI_NAME.zip

4.ESP32_WIFI_HMI_NAME.zip
0.01MB

4.ESP8266_WIFI_HMI_NAME.zip

4.ESP8266_WIFI_HMI_NAME.zip
0.01MB

 

https://youtu.be/FdF1GXqSLC8

 

 

App Login

When connecting through a router, obtain control authority using password of the access point.

 

5.ESP32_WIFI_HMI_PASS.zip

5.ESP32_WIFI_HMI_PASS.zip
0.01MB

5.ESP8266_WIFI_HMI_PASS.zip

5.ESP8266_WIFI_HMI_PASS.zip
0.01MB

 

https://youtu.be/mtBl8fEYgyg

 

[Arduino/ADUCON] - ADUCON - Arduino wireless remote control application

 

 

 

 

 

+ Recent posts