Skip to content

Commit

Permalink
Merge pull request #76 from ivanalayan15/v2.4
Browse files Browse the repository at this point in the history
V2.4
  • Loading branch information
ivanalayan15 authored Oct 2, 2021
2 parents 7ccec53 + 6f52d3f commit fbcf40b
Show file tree
Hide file tree
Showing 6 changed files with 244 additions and 13 deletions.
165 changes: 156 additions & 9 deletions JuanFi-nodemcu/JuanFi-nodemcu.ino
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,14 @@
*
*/

//increase always when publishing a new version for tracking
#define CURRENT_VERSION "2.4"

#ifdef ESP32
#include <TelnetClient.h>
#include "lan_definition.h"
#include <SPIFFS.h>
#include <Update.h>
#include <WiFi.h>
#else
#include <ESP8266TelnetClient.h>
Expand All @@ -36,6 +40,8 @@
#include <ESP8266HTTPClient.h>
#include <ESP8266mDNS.h>
#include <DNSServer.h>
#include <Arduino.h>
#include <flash_hal.h>
#endif


Expand Down Expand Up @@ -94,12 +100,15 @@ int ratesCount = 0;
int currentValidity = 0;
int currentDataLimit = 0;
String currentRateProfile = "";
String ADMIN_USER = "";
String ADMIN_PW = "";


const int LIFETIME_COIN_COUNT_ADDRESS = 0;
const int COIN_COUNT_ADDRESS = 5;
const int CUSTOMER_COUNT_ADDRESS = 10;
const int RANDOM_MAC_ADDRESS = 15;
const int BACKUP_CONFIG_LENGTH_INDEX = 20;

void ICACHE_RAM_ATTR coinInserted()
{
Expand Down Expand Up @@ -163,8 +172,6 @@ IPAddress apIP(172, 217, 28, 1);
DNSServer dnsServer;
#endif



const int WIFI_CONNECT_TIMEOUT = 180000;
const int WIFI_CONNECT_DELAY = 500;

Expand All @@ -182,7 +189,7 @@ String MARQUEE_MESSAGE = "This is marquee";
void setup () {

Serial.begin (115200);
EEPROM.begin(32);
EEPROM.begin(512);
if(!SPIFFS.begin()){
Serial.println("An Error has occurred while mounting SPIFFS");
return;
Expand Down Expand Up @@ -335,7 +342,8 @@ void setup () {
server.on("/admin/api/generateVouchers", handleGenerateVouchers);
server.on("/admin", handleAdminPage);
server.on("/admin/viewGeneratedVouchers", handleAdminGeneratedVoucherPage);

server.on("/admin/updateMainBin", HTTP_POST, handleFileUploadRequest, handleFileUploadStream);

populateRates();

server.begin();
Expand All @@ -346,6 +354,98 @@ void setup () {

}


boolean hasUploadError = false;
boolean isFileSystem = true;

void handleFileUploadRequest(){
if (Update.hasError()) {
//when esp32 has sometimes error of not enough space, but actually its uploaded some part succesfully so we will just return success
if(isFileSystem && HARDWARE_TYPE == "ESP32"){
server.send(200, F("text/html"), "Upload done, with warnings");
server.client().stop();
ESP.restart();
}else{
server.send(200, F("text/html"), "Upload has error");
}
}
else {
#ifdef ESP32
//nothing not avaiable at esp32
#else
server.client().setNoDelay(true);
#endif
server.send_P(200, PSTR("text/html"), "Upload done");
delay(100);
server.client().stop();
ESP.restart();
}
}

//get from https://github.com/esp8266/Arduino/blob/master/libraries/ESP8266HTTPUpdateServer/src/ESP8266HTTPUpdateServer-impl.h
void handleFileUploadStream(){
HTTPUpload& upload = server.upload();
if (upload.status == UPLOAD_FILE_START) {
if(!isAuthorized()){
handleNotAuthorize();
return;
}
if (upload.name == "filesystem") {
isFileSystem = true;
backupSystemConfig();
#ifdef ESP32
if (!Update.begin(SPIFFS.totalBytes(), U_SPIFFS)) {
Serial.println("Upload filesystem start failed");
hasUploadError = true;
}
#else
size_t fsSize = ((size_t) &_FS_end - (size_t) &_FS_start);
close_all_fs();
if (!Update.begin(fsSize, U_FS)){//start with max available size
Serial.println("Upload filesystem start failed");
hasUploadError = true;
}
#endif
}
else {
uint32_t maxSketchSpace = (ESP.getFreeSketchSpace() - 0x1000) & 0xFFFFF000;
if (!Update.begin(maxSketchSpace, U_FLASH)) {//start with max available size
Serial.println("Upload sketch start failed");
hasUploadError = true;
}
}
}
else if (upload.status == UPLOAD_FILE_WRITE && !hasUploadError) {
Serial.printf(".");
if (Update.write(upload.buf, upload.currentSize) != upload.currentSize) {
Serial.println("Upload write failed");
hasUploadError = true;
}
}
else if (upload.status == UPLOAD_FILE_END && !hasUploadError) {
if (Update.end(true)) { //true to set the size to the current progress
Serial.printf("Update Success: %u\nRebooting...\n", upload.totalSize);
}
else {
Serial.printf("Update Success: %u\nRebooting...\n", upload.totalSize);
}
}
else if (upload.status == UPLOAD_FILE_ABORTED) {
Update.end();
hasUploadError = true;
Serial.println("Upload aborted");
}
delay(0);
}

void backupSystemConfig(){
Serial.println("Starting to backup system.data");
String data = readFile("/admin/config/system.data");
int len = data.length();
eeWriteInt(BACKUP_CONFIG_LENGTH_INDEX, len);
eeWriteString(BACKUP_CONFIG_LENGTH_INDEX+5, data);
}

#ifdef ESP32
void initializeLANSetup(){
delay(3000);
Expand Down Expand Up @@ -520,6 +620,25 @@ void eeWriteInt(int pos, int val) {
EEPROM.commit();
}

void eeWriteString(int addr, String val) {
int str_len = val.length() + 1;
for (int i = addr; i < str_len + addr; ++i)
{
EEPROM.write(i, val.charAt(i - addr));
}
EEPROM.write(str_len + addr, '\0');
EEPROM.commit();
}

String eeReadString(int addr, int str_len) {
String val = "";
for (int i = addr; i < str_len + addr; ++i)
{
val += String(char(EEPROM.read(i)));
}
return val;
}

int eeGetInt(int pos) {
int val;
byte* p = (byte*) &val;
Expand Down Expand Up @@ -642,6 +761,8 @@ void handleAdminDashboard(){
data += currentIpAddress;
data += String("|");
data += HARDWARE_TYPE;
data += String("|");
data += CURRENT_VERSION;

server.send(200, "text/plain", data);
}
Expand Down Expand Up @@ -1215,6 +1336,20 @@ const char * ROW_DELIMETER = "|";

void populateSystemConfiguration(){

//detect if backup is exists
int backupLength = eeGetInt(BACKUP_CONFIG_LENGTH_INDEX);
if(backupLength > 0){
Serial.print("Backup data found ");
Serial.println(backupLength);
String backupData = eeReadString(BACKUP_CONFIG_LENGTH_INDEX+5, backupLength);
Serial.println(backupData);
handleFileWrite("/admin/config/system.data", backupData);
eeWriteInt(BACKUP_CONFIG_LENGTH_INDEX, 0);
Serial.print("Backup data restored!, restarting....");
ESP.restart();
return;
}

Serial.println("Loading system configuration");
String data = readFile("/admin/config/system.data");
Serial.print("Data: ");
Expand All @@ -1235,7 +1370,9 @@ void populateSystemConfiguration(){
user = rows[4];
pwd = rows[5];
MAX_WAIT_COIN_SEC = rows[6].toInt() * 1000;
adminAuth = base64::encode(rows[7]+":"+rows[8]);
ADMIN_USER = rows[7];
ADMIN_PW = rows[8];
adminAuth = base64::encode(ADMIN_USER+":"+ADMIN_PW);
COINSLOT_BAN_COUNT = rows[9].toInt();
COINSLOT_BAN_MINUTES = rows[10].toInt();
COIN_SELECTOR_PIN = rows[11].toInt();
Expand Down Expand Up @@ -1378,10 +1515,12 @@ void loop () {
return;
}
}else{
//clear thank you message after button press
thankyou_cooldown = 0;
targetMilis = currentMilis;
delay(1000);
if(timeToAdd == 0){
//clear thank you message after button press
thankyou_cooldown = 0;
targetMilis = currentMilis;
delay(1000);
}
}
}else{
//make coinslot expired when button is pressed
Expand Down Expand Up @@ -1457,6 +1596,14 @@ void loop () {
}
}
}else{
unsigned long currentMilis = millis();
if(SETUP_FINISH == 1){
//when setup is already finish and cannnot connect, wait for 10 mins to setup and will auto restart after that
//this is to cater slow boot AP
if(currentMilis >= 600000){
ESP.restart();
}
}
#ifdef ESP32
//nothing
#else
Expand Down
Loading

0 comments on commit fbcf40b

Please sign in to comment.