Skip to content

Commit

Permalink
Create Pio project
Browse files Browse the repository at this point in the history
  • Loading branch information
Gavinin committed Jul 30, 2023
1 parent 963c054 commit 09bd36c
Show file tree
Hide file tree
Showing 11 changed files with 644 additions and 2 deletions.
7 changes: 7 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
.pio
.vscode/.browse.c_cpp.db*
.vscode/c_cpp_properties.json
.vscode/launch.json
.vscode/ipch
# Prerequisites
*.d

Expand Down Expand Up @@ -50,3 +55,5 @@ modules.order
Module.symvers
Mkfile.old
dkms.conf
cmake-build-debug/
cmake-build-esp8285/
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
# esp8266_ddns
DDNS for Esp8266
# DDNS for Esp8266

##
17 changes: 17 additions & 0 deletions platformio.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
; PlatformIO Project Configuration File
;
; Build options: build flags, source filter
; Upload options: custom upload port, speed and extra flags
; Library options: dependencies, extra library storages
; Advanced options: extra scripting
;
; Please visit documentation for the other options and examples
; https://docs.platformio.org/page/projectconf.html

[env:esp8285]
platform = espressif8266
board = esp01_1m
framework = arduino
monitor_speed = 115200
upload_resetmethod = nodemcu
lib_deps = bblanchon/ArduinoJson@^6.21.3
129 changes: 129 additions & 0 deletions src/config/user_config.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
//
// Created by Gavin Wang on 6/23/23.
//

#include <LittleFS.h>
#include "user_config.h"
#include <ArduinoJson.h>

const String CONFIG_FILE = "/config.txt";


/**
* Save param userConfig to local file system.
* @param userConfig user configuration
*/
void UserConfigService::saveConfigToFS(UserConfig *userConfig) {
File configFile = LittleFS.open(CONFIG_FILE, "w");
if (!configFile) {
Serial.println("Failed to open userConfig file for writing");
return;
}
DynamicJsonDocument doc(1024);
doc["wifi_ssid"] = userConfig->wifi_ssid.c_str();
doc["wifi_password"] = userConfig->wifi_password.c_str();
doc["ddns_hostname"] = userConfig->ddns_hostname.c_str();
doc["ddns_domain"] = userConfig->ddns_domain.c_str();
doc["ddns_password"] = userConfig->ddns_password.c_str();
doc["ddns_url"] = userConfig->ddns_url.c_str();
doc["ip_service_url"] = userConfig->ip_service_url.c_str();
doc["refresh_timeout"] = userConfig->refresh_timeout.c_str();

String jsonStr;
serializeJson(doc, jsonStr);
configFile.println(jsonStr);

configFile.close();
}

/**
* Delete config from file system.
*/
void UserConfigService::deleteConfigFromFS() {
bool result = LittleFS.remove(CONFIG_FILE);
if (!result) {
Serial.println("Failed to open config file for writing");
return;
}
}

/**
* Check UserConfig correctly.
* @param config user input
* @return is correctly
*/
bool UserConfigService::checkConfigProperties(UserConfig *config) {

if (config->wifi_ssid == "") {
Serial.println("wifi_ssid:" + config->wifi_ssid);
return false;
}
if (config->wifi_password == "") {
Serial.println("wifi_password:" + config->wifi_password);
return false;
}
if (config->ddns_domain == "") {
Serial.println("ddns_domain:" + config->ddns_domain);
return false;
}
if (config->ddns_url == "") {
Serial.println("ddns_url:" + config->ddns_url);
return false;
}
if (config->ip_service_url == "") {
Serial.println("ip_service_url:" + config->ip_service_url);
return false;
}

if (config->refresh_timeout == "0") {
Serial.println("refresh_timeout:" + config->refresh_timeout);
return false;
}

return true;
}

/**
* Reading user setting from file system.
* @return
*/
bool UserConfigService::readConfig() {
bool isFileExists = LittleFS.exists(CONFIG_FILE);
Serial.printf("readConfig: %d", isFileExists);
if (isFileExists) {
File configFile = LittleFS.open(CONFIG_FILE, "r");
if (configFile) {

DynamicJsonDocument doc(1024);
deserializeJson(doc, configFile.readStringUntil('\n').c_str());
Serial.println("configFile: exist");

this->config.wifi_ssid = doc["wifi_ssid"].as<String>();
this->config.wifi_password = doc["wifi_password"].as<String>();
this->config.ddns_hostname = doc["ddns_hostname"].as<String>();
this->config.ddns_domain = doc["ddns_domain"].as<String>();
this->config.ddns_password = doc["ddns_password"].as<String>();
this->config.ddns_url = doc["ddns_url"].as<String>();
this->config.ip_service_url = doc["ip_service_url"].as<String>();
this->config.refresh_timeout = doc["refresh_timeout"].as<String>();

Serial.printf("local configFile: %s \n%s \n%s \n%s \n%s \n%s",
this->config.wifi_ssid.c_str(),
this->config.wifi_password.c_str(), this->config.ddns_hostname.c_str(), this->config.ddns_domain.c_str(),
this->config.ddns_password.c_str(), this->config.ddns_url.c_str());

configFile.close();

return true;
} else {
Serial.println("configFile: isn't exist");
}
}
return false;
}

UserConfig UserConfigService::getUserConfig() {
return this->config;
}


31 changes: 31 additions & 0 deletions src/config/user_config.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
//
// Created by Gavin Wang on 6/23/23.
//

#ifndef ESP8266_DDNS_USER_CONFIG_H
#define ESP8266_DDNS_USER_CONFIG_H

#include <Arduino.h>
#include "entity/config.h"

class UserConfigService {
private:
UserConfig config;

public:
UserConfigService() = default;

UserConfig getUserConfig();

bool readConfig();

static void deleteConfigFromFS();

static void saveConfigToFS(UserConfig *userConfig);

static bool checkConfigProperties(UserConfig *config);

};


#endif //ESP8266_DDNS_USER_CONFIG_H
23 changes: 23 additions & 0 deletions src/entity/config.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
//
// Created by Gavin Wang on 30/6/23.
//

#ifndef ESP8266_DDNS_CONFIG_H
#define ESP8266_DDNS_CONFIG_H

#include <Arduino.h>

class UserConfig {
public:
String wifi_ssid;
String wifi_password;
String ddns_hostname;
String ddns_domain;
String ddns_password;
String ddns_url;
String ip_service_url;
String refresh_timeout;
};


#endif //ESP8266_DDNS_CONFIG_H
17 changes: 17 additions & 0 deletions src/entity/index.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
//
// Created by Gavin Wang on 6/23/23.
//

#ifndef ESP8266_DDNS_INDEX_H
#define ESP8266_DDNS_INDEX_H

#include <Arduino.h>

class Index {

public:
const String PAGE = R"(<!DOCTYPE html><html lang="en"><head><meta charset="utf-8"><meta name="viewport"content="width=device-width, initial-scale=1.0"><title>Wifi Settings</title></head><style>*,*:before,*:after{-moz-box-sizing:border-box;-webkit-box-sizing:border-box;box-sizing:border-box}body{font-family:'Nunito',sans-serif;color:#384047}form{max-width:300px;margin:10px auto;padding:10px 20px;background:#f4f7f8;border-radius:8px}h1{margin:0 0 30px 0;text-align:center}input[type="text"],input[type="password"],input[type="url"],textarea,select{background:rgba(255,255,255,0.1);border:none;font-size:16px;height:auto;margin:0;outline:0;padding:15px;width:100%;background-color:#e8eeef;color:#8a97a0;box-shadow:0 1px 0 rgba(0,0,0,0.03)inset;margin-bottom:30px}button{padding:3vh 8vh 3vh 8vh;color:#FFF;background-color:#2590e3;font-size:1.5vh;text-align:center;font-style:normal;border-radius:5px;width:100%;border:1px solid#2590e3;border-width:1px 1px 3px;box-shadow:0-1px 0 rgba(255,255,255,0.1)inset;margin-bottom:10px}fieldset{margin-bottom:30px;border:none}legend{font-size:1.4em;margin-bottom:10px}label{display:block;margin-bottom:8px}@media screen and(min-width:480px){form{max-width:720px}}</style><body><div class="row"><div class="col-md-12"><form method='post'action='/set_info'><h1>Settings</h1><fieldset><legend>WiFi and DDNS Configuration</legend><label for="wifi_ssid">WiFi SSID:</label><input type='text'id="wifi_ssid"name='wifi_ssid'><br><label for="wifi_password">WiFi Password:</label><input type='text'id="wifi_password"name='wifi_password'><br><label for="ddns_hostname">DDNS Hostname:</label><input type='text'id="ddns_hostname"name='ddns_hostname'><br><label for="ddns_domain">DDNS Domain:</label><input type='text'id="ddns_domain"name='ddns_domain'><br><label for="ddns_password">DDNS Password:</label><input type='text'id="ddns_password"name='ddns_password'><br><label for="ddns_url">DDNS URL:</label><input type='url'id="ddns_url"name='ddns_url'value="http://dynamicdns.park-your-domain.com/update?host=$DDNS_HOSTNAME&domain=$DDNS_DOMAIN&password=$DDNS_PASSWORD&ip=$PUBLIC_IP"><br><label for="ip_service_url">IP Server:</label><input type='url'id="ip_service_url"name='ip_service_url'value="http://members.3322.org/dyndns/getip"><br><label for="refresh_timeout">Delay:</label><select id="refresh_timeout"name="refresh_timeout"><option value="5">5 Minutes</option><option value="10">10 Minutes</option><option value="30">30 Minutes</option><option value="60">1 Hour</option><option value="180">3 Hours</option><option value="360">6 Hours</option><option value="720">12 Hours</option><option value="1440">24 Hours</option></select></fieldset><button type="submit">Save</button></form></div></div></body></html>)";
};


#endif //ESP8266_DDNS_INDEX_H
Loading

0 comments on commit 09bd36c

Please sign in to comment.