Skip to content

Commit

Permalink
added some examples for reference
Browse files Browse the repository at this point in the history
  • Loading branch information
suraji committed Apr 30, 2020
1 parent 58a15f5 commit c399e24
Show file tree
Hide file tree
Showing 35 changed files with 1,495 additions and 386 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ Network address translation (NAT) is a method of remapping one IP address space
from v2.6.^ arduino has provided initial support example of NAT with lwip v2 variant (IPv4 only).

before that lwip 1.4 is used to enable napt ( network address & port transform ) feature but with some customizations in lwip1.4.

**you can test lwip 1.4** just rename "...esp8266/tools/sdk/lwip" with "...esp8266/tools/sdk/lwip.org" and copy lwip folder ( in this repo ) there. do not forget to select lwip 1.4 compile from source variant in arduino tools option while building.

By default this feature is active based on what lwip variant from ide tool option is selected.
Expand Down Expand Up @@ -185,5 +185,5 @@ As name clears the purpose of this utility. It just used to convert the data typ
Logger enables log on uart0 pins at 115200 baud rate. This is useful in case of debugging application flow.


#### Detailed Documentation
Detailed documentations are ongoing in wiki page of repo....
## Detailed Documentation
Detailed documentation is ongoing at wiki page, please visit [wiki page](https://github.com/Suraj151/esp8266-framework/wiki)....
95 changes: 95 additions & 0 deletions examples/AddingController/AddingController.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
/* Adding controller (available from third release)
*
* This example illustrate adding controller to framework to handle specific url path
* framework recommends this design of request response model to implement for future use
*
*/

#include <EwingsEsp8266Stack.h>


#if defined(ENABLE_EWING_HTTP_SERVER)

/**
* TestController class must be derived from Controller class
*/
class TestController : public Controller {

public:

/**
* TestController constructor.
* construct with unique controller name to differentiate it from others
*/
TestController():Controller("test"){}

/**
* register test controller
*
* argument includes url path, route handler method, middleware option.
*
* AUTH_MIDDLEWARE is middlware which enable auth check service to this route so that
* handler method will only call if user is authenticated before with login credentials.
*/
void boot( void ){
this->route_handler->register_route( "/test-route", [&]() { this->handleTestRoute(); }, AUTH_MIDDLEWARE );
}

/**
* build and send test page to client
*/
void handleTestRoute( void ) {

#ifdef EW_SERIAL_LOG
Logln(F("Handling Test route"));
#endif

/**
* take new dynamic array to build html response page
* EW_HTML_MAX_SIZE defined in framework as 5000
*/
char* _page = new char[EW_HTML_MAX_SIZE];
memset( _page, 0, EW_HTML_MAX_SIZE );

/**
* first append header part of html to reponse
*/
strcat_P( _page, EW_SERVER_HEADER_HTML );

/**
* then append body part of html to response
* for demo purpose, dashboard card added with the help of html helpers available in framework
*/
strcat_P( _page, EW_SERVER_MENU_CARD_PAGE_WRAP_TOP );
concat_svg_menu_card( _page, EW_SERVER_HOME_MENU_TITLE_DASHBOARD, SVG_ICON48_PATH_DASHBOARD, EW_SERVER_DASHBOARD_ROUTE );
strcat_P( _page, EW_SERVER_MENU_CARD_PAGE_WRAP_BOTTOM );

/**
* lastely append footer part of html to response
*/
strcat_P( _page, EW_SERVER_FOOTER_HTML );

/**
* finally send response and deallocate page
*/
this->web_resource->server->send( HTTP_OK, EW_HTML_CONTENT, _page );
delete[] _page;
}
};

/**
* this should be defined before framework initialization
*/
TestController test_controller;
#else
#error "Ewings HTTP server is disabled ( in config/Common.h of framework library ). please enable(uncomment) it for this example"
#endif


void setup() {
EwStack.initialize();
}

void loop() {
EwStack.serve();
}
160 changes: 160 additions & 0 deletions examples/AddingDatabaseTable/AddingDatabaseTable.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
/* Adding table in database (available from third release)
*
* This example illustrate adding table to database
*/

#include <EwingsEsp8266Stack.h>


#if defined(ENABLE_EWING_HTTP_SERVER)

// be carefull about table address or else table will not register.
// it should be on minimum distant from last added table address + last added table size
// keep safe distance between table addresses accordings their size
// addresses upto 2499 are reserved for default framework tables so choose addresses from 2500 onwards
// Eeprom is using only one spi flash sector so we have max size of 4096 bytes only.
// hence put only importanat configs in tables

#define MAX_STUDENTS 5
#define STUDENT_NAME_MAX_SIZE 20
#define STUDENT_TABLE_ADDRESS 2500

enum sex{ MALE, FEMALE };

typedef struct {
char _name[STUDENT_NAME_MAX_SIZE];
uint8_t _age;
sex _sex;
} student_t;

struct student_table {
student_t students[MAX_STUDENTS];
int student_count;
};

const student_table PROGMEM _student_table_defaults = {NULL, 0};

/**
* StudentTable should class extends public DatabaseTable as its base/parent class
*/
class StudentTable : public DatabaseTable<student_table> {

public:
/**
* StudentTable constructor.
*/
StudentTable(){
}

/**
* register tables to database
*/
void boot(){
this->register_table( STUDENT_TABLE_ADDRESS, &_student_table_defaults );
}

/**
* get/fetch table from database.
*
* @return student_table
*/
student_table get(){
return this->get_table(STUDENT_TABLE_ADDRESS);
}

/**
* set table in database.
*
* @param student_table* _table
*/
void set( student_table* _table ){
this->set_table( _table, STUDENT_TABLE_ADDRESS );
}

/**
* clear table in database.
*/
void clear(){
this->clear_table( STUDENT_TABLE_ADDRESS );
}
};

/**
* this should be defined before framework initialization
*/
StudentTable __student_table;
#else
#error "Ewings HTTP server is disabled ( in config/Common.h of framework library ). please enable(uncomment) it for this example"
#endif


void setup() {

Serial.begin(115200);
Serial.printf("Hold on!!!, Stack will initialize and begin within next %d seconds !\n", WIFI_STATION_CONNECT_ATTEMPT_TIMEOUT);

EwStack.initialize();

auto _students_data = __student_table.get();

// Adding sample records to table
memset(_students_data.students[0]._name, 0, STUDENT_NAME_MAX_SIZE);
strcpy_P(_students_data.students[0]._name, "suraj inamdar");
_students_data.students[0]._age = 20;
_students_data.students[0]._sex = MALE;
_students_data.student_count = 1;

memset(_students_data.students[1]._name, 0, STUDENT_NAME_MAX_SIZE);
strcpy_P(_students_data.students[1]._name, "sajeev reddy");
_students_data.students[1]._age = 21;
_students_data.students[1]._sex = MALE;
_students_data.student_count++;


memset(_students_data.students[2]._name, 0, STUDENT_NAME_MAX_SIZE);
strcpy_P(_students_data.students[2]._name, "sakshi mehta");
_students_data.students[2]._age = 19;
_students_data.students[2]._sex = FEMALE;
_students_data.student_count++;

memset(_students_data.students[3]._name, 0, STUDENT_NAME_MAX_SIZE);
strcpy_P(_students_data.students[3]._name, "john stephens");
_students_data.students[3]._age = 22;
_students_data.students[3]._sex = MALE;
_students_data.student_count++;

memset(_students_data.students[4]._name, 0, STUDENT_NAME_MAX_SIZE);
strcpy_P(_students_data.students[4]._name, "angelina jolie");
_students_data.students[4]._age = 18;
_students_data.students[4]._sex = FEMALE;
_students_data.student_count++;

// now set table in database
__student_table.set(&_students_data);

// add task of print stdudent table every 5000 milliseconds
__task_scheduler.setInterval( printStudents, MILLISECOND_DURATION_5000 );
}

void loop() {
EwStack.serve();
}

/**
* prints log as per defined duration
*/
void printStudents(){

//get student table from database
auto _students_data = __student_table.get();

Serial.println(F("\n**************************Adding Database Example Log**************************"));
Serial.println( F("Student table : \nname\t\tage\tsex\n") );
for (int i = 0; i < _students_data.student_count; i++) {

Serial.print( _students_data.students[i]._name ); Serial.print(F("\t"));
Serial.print( _students_data.students[i]._age ); Serial.print(F("\t"));
Serial.println( _students_data.students[i]._sex==MALE? F("male"):F("female"));
}
Serial.println(F("******************************************************************************"));
}
68 changes: 68 additions & 0 deletions examples/TaskScheduling/TaskScheduling.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/* Task Scheduling in framework
*
* This example illustrate adding tasks in framework
*/

#include <EwingsEsp8266Stack.h>

int variable_time_task_id = 0;

// should run only one time at specified duration after EwStack initialization
void timeout_task(){
Serial.print(millis());
Serial.println(F(" : running timeout task once"));
}

// should run continueously at specific intervals after EwStack initialization
void interval_task(){
Serial.print(millis());
Serial.println(F(" : running interval task"));
}

// update interval task after some time to run it at rate 1000 milliseconds
void update_interval_task(){
Serial.print(millis());
Serial.println(F(" : ******* updating interval task @1000 milliseconds ************"));
variable_time_task_id = __task_scheduler.updateInterval(
variable_time_task_id,
interval_task,
MILLISECOND_DURATION_1000
);
}

// clear interval task after specified duration
void clear_interval_task(){
Serial.print(millis());
Serial.println(F(" : ******* clearing interval task ********"));
__task_scheduler.clearInterval( variable_time_task_id );
}


void setup() {

// NOTE : Please disable framework serial log for this demo or framework log will get printed alongwith this demo log
// Disable it by commenting ==> #define EW_SERIAL_LOG line in src/config/Common.h file of this framework library

Serial.begin(115200);
Serial.printf("Hold on!!!, Stack will initialize and begin within next %d seconds !\n", WIFI_STATION_CONNECT_ATTEMPT_TIMEOUT);

EwStack.initialize();

// run timeout task one time at 1000 milliseconds after EwStack initialization
// dont worry about clearing timeout tasks, they will die automatically after their one time execution
__task_scheduler.setTimeout( timeout_task, MILLISECOND_DURATION_1000 );

// run interval task every 3000 milliseconds after EwStack initialization
// shcedular provide unique id for each task to update task later in runtime if we want
variable_time_task_id = __task_scheduler.setInterval( interval_task, MILLISECOND_DURATION_1000*3 );

// adding timeout task which will update above interval task duration after 15000 milliseconds
__task_scheduler.setTimeout( update_interval_task, MILLISECOND_DURATION_5000*3 );

// adding timeout task which will clear above interval task duration after 30000 milliseconds
__task_scheduler.setTimeout( clear_interval_task, MILLISECOND_DURATION_5000*6 );
}

void loop() {
EwStack.serve();
}
2 changes: 1 addition & 1 deletion library.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name=esp8266-framework
version=1.1
version=1.1.1
author=Suraj I.
maintainer=Suraj I. <surajinamdar151@gmail.com>
sentence=esp8266 framework stack for easy configurable applications
Expand Down
5 changes: 5 additions & 0 deletions src/config/Common.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ extern "C" {
*/
#define ENABLE_EMAIL_SERVICE

/**
* enable/disable serial log
*/
#define EW_SERIAL_LOG

/**
* enable/disable exception notifier
*/
Expand Down
Loading

0 comments on commit c399e24

Please sign in to comment.