#include <EEPROM.h>
#include <Arduino.h>
#include <WiFiClient.h>
const int buttonPin = 21; // the number of the pushbutton pin
// Pressbutton
int Press_Time = 2;
const int LONG_PRESS_TIME = 1000 * Press_Time; // 1000 milliseconds
int lastState = LOW; // the previous state from the input pin
int currentState = HIGH; // the current reading from the input pin
unsigned long pressedTime = 0;
unsigned long releasedTime = 0;
unsigned long pressDuration = 0;
bool isPressing = false;
bool isLongDetected = false;
int debounceDelay = 50; // May be replaced by a larger value for heavy bouncing buttons
const int CONF_ADDRESS = 0;
const int SSID_ADDRESS = 10;
const int PASS_ADDRESS = 30;
const int HOST_ADDRESS = 50;
const int PORT_ADDRESS = 100;
const int TXID_ADDRESS = 110;
const int RXID_ADDRESS = 120;
const int LAT_ADDRESS = 130;
const int LONG_ADDRESS = 150;
// Structure to hold configuration data
struct Config {
String isconfig;
String ssid;
String password;
String host;
String port;
String txid;
String rxid;
String lat;
String lng;
};
Config config;
void saveStringToEEPROM(int address, const String& data) {
// Get the length of the string
int length = data.length();
// Write the length of the string to the EEPROM
EEPROM.write(address, length);
// Write each character of the string to the EEPROM
for (int i = 0; i < length; i++) {
EEPROM.write(address + 1 + i, data[i]); // Skip 1 byte for the length
}
// Add a null terminator at the end of the string
EEPROM.write(address + 1 + length, '\0');
EEPROM.commit();
}
String loadStringFromEEPROM(int address) {
// Read the length of the string from the EEPROM
int length = EEPROM.read(address);
// Read each character of the string from the EEPROM
String loadedString = "";
for (int i = 0; i < length; i++) {
char character = EEPROM.read(address + 1 + i);
loadedString += character;
}
return loadedString;
}
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
EEPROM.begin(512);
// Setup pin for push button
pinMode(buttonPin, INPUT);
digitalWrite(buttonPin, HIGH);
// Load configuration
config.isconfig = loadStringFromEEPROM(CONF_ADDRESS);
if ((config.isconfig) == "Y") {
config.host = loadStringFromEEPROM(HOST_ADDRESS);
config.port = loadStringFromEEPROM(PORT_ADDRESS);
config.ssid = loadStringFromEEPROM(SSID_ADDRESS);
config.password = loadStringFromEEPROM(PASS_ADDRESS);
config.txid = loadStringFromEEPROM(TXID_ADDRESS);
config.rxid = loadStringFromEEPROM(RXID_ADDRESS);
config.lat = loadStringFromEEPROM(LAT_ADDRESS);
config.lng = loadStringFromEEPROM(LONG_ADDRESS);
}
else
{
config.host = "ecsserv.rast.or.th/postmcu";
config.port = "80";
config.ssid = "HS2JFW";
config.password = "0123456789";
config.txid = "9A";
config.rxid = "9A";
config.lat = "13.7581492";
config.lng = "100.5549858";
saveStringToEEPROM(CONF_ADDRESS,"Y");
saveStringToEEPROM(HOST_ADDRESS,config.host);
saveStringToEEPROM(PORT_ADDRESS,config.port);
saveStringToEEPROM(SSID_ADDRESS,config.ssid);
saveStringToEEPROM(PASS_ADDRESS,config.password);
saveStringToEEPROM(TXID_ADDRESS,config.txid);
saveStringToEEPROM(RXID_ADDRESS,config.rxid);
saveStringToEEPROM(LAT_ADDRESS,config.lat);
saveStringToEEPROM(LONG_ADDRESS,config.lng);
}
}
void PushButton()
{
isLongDetected = false;
pressDuration = 0;
pressedTime = 0;
releasedTime = 0;
currentState = digitalRead(buttonPin); // read the state of the button
if (currentState == LOW) { // button is pressed
pressedTime = millis();
static int lastTimeStamp = 0;
if (pressedTime - lastTimeStamp > debounceDelay) {
while (currentState == LOW) {
releasedTime = millis();
currentState = digitalRead(buttonPin);
}
isPressing = true;
Serial.println("Press is detected");
}
lastTimeStamp = pressedTime;
pressDuration = releasedTime - pressedTime;
Serial.println("Press is released");
Serial.println(pressedTime);
Serial.println(releasedTime);
Serial.println(pressDuration);
if( pressDuration > LONG_PRESS_TIME ) {
Serial.println("A long press is detected");
isLongDetected = true;
}
}
if (isLongDetected) {
Serial.println("Post API...");
// save the the last state
lastState = currentState;
}
}
void loop() {
// put your main code here, to run repeatedly:
delay(10); // this speeds up the simulation
PushButton();
}