Skip to content

Commit

Permalink
added mqtt example, gpio handles from mqtt remote by default etc.
Browse files Browse the repository at this point in the history
  • Loading branch information
suraji committed May 6, 2020
1 parent 9c74e1d commit 5f59886
Show file tree
Hide file tree
Showing 16 changed files with 400 additions and 150 deletions.
2 changes: 0 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,6 @@ Restart the arduino ide and navigate to File->Examples->esp8266-framework->EwSta

**Note** that installed version of esp8266 should be > 2.6.x. 2.6.2 or greater is recommended. you can check your installed esp8266 version in tools->boards->board manager (type "esp" in top search bar).

**for now** : if you are uploading it first time in device then wait for 5 seconds(default station connect timeout on start) and then press flash key on nodemcu board for upto 6-7 seconds to load first default settings. device will get reset to default settings and restart.

* after initializing device completely, check in pc/mobile wifi list if **esp8266Stack** name appear.
* select it and enter default password **espStack@8266**.
* finally after succesful connectinon to device open browser, type **192.168.0.1** in address bar and press enter
Expand Down
119 changes: 119 additions & 0 deletions examples/MqttExample/MqttExample.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
/* Demo example about how to use mqtt service provider
*/

#include <EwingsEsp8266Stack.h>

// mqtt grneral configuration
#define MQTT_HOST "---mqtt host---"
#define MQTT_PORT 1883
#define MQTT_CLIENT_ID "---client id---"
#define MQTT_USERNAME ""
#define MQTT_PASSWORD ""
#define MQTT_KEEP_ALIVE 60

// mqtt publish / subscribe configuration
#define MQTT_PUBLISH_TOPIC "test_publish"
#define MQTT_SUBSCRIBE_TOPIC "test_subscribe"
#define MQTT_PUBLISH_FREQ 5 // publish after every 5 second
#define MQTT_PUBLISH_QOS 0
#define MQTT_SUBSCRIBE_QOS 0

// mqtt lwt configuration
#define MQTT_WILL_TOPIC "disconnect"
#define MQTT_WILL_MESSAGE "[mac] is disconnected" // [mac] will get replaced internally with actual mac id
#define MQTT_WILL_QOS 0

#if defined(ENABLE_EWING_HTTP_SERVER)


// mqtt service will call this function whenever it initiate publish process to set user data for publish in payload
// sending demo data in json format
void publish_callback( char* _payload, uint16_t _length ){

memset( _payload, 0, _length );

string _data_to_publish = "";
_data_to_publish += "{\"device_id\":[mac], \"value\":";
_data_to_publish += random(0, 100);
_data_to_publish += "}";

_data_to_publish.toCharArray( _payload, _length );
}


// mqtt service will call this function whenever it receive data on subscribed topic
void subscribe_callback( uint32_t *args, const char* topic, uint32_t topic_len, const char *data, uint32_t data_len ){

char *topicBuf = new char[topic_len+1], *dataBuf = new char[data_len+1];

memcpy(topicBuf, topic, topic_len);
topicBuf[topic_len] = 0;

memcpy(dataBuf, data, data_len);
dataBuf[data_len] = 0;

#ifdef EW_SERIAL_LOG
Logln(F("\n\nMQTT: user data callback"));
Serial.printf("MQTT: user Receive topic: %s, data: %s \n\n", topicBuf, dataBuf);
#endif

delete[] topicBuf; delete[] dataBuf;
}

void configure_mqtt(){

// take mqtt tables from database
mqtt_general_config_table _mqtt_general_configs = __database_service.get_mqtt_general_config_table();
mqtt_pubsub_config_table _mqtt_pubsub_configs = __database_service.get_mqtt_pubsub_config_table();
mqtt_lwt_config_table _mqtt_lwt_configs = __database_service.get_mqtt_lwt_config_table();

// copy general configs in mqtt general table
memcpy( _mqtt_general_configs.host, MQTT_HOST, strlen( MQTT_HOST ) );
_mqtt_general_configs.port = MQTT_PORT;
memcpy( _mqtt_general_configs.client_id, MQTT_CLIENT_ID, strlen( MQTT_CLIENT_ID ) );
memcpy( _mqtt_general_configs.username, MQTT_USERNAME, strlen( MQTT_USERNAME ) );
memcpy( _mqtt_general_configs.password, MQTT_PASSWORD, strlen( MQTT_PASSWORD ) );
_mqtt_general_configs.keepalive = MQTT_KEEP_ALIVE;

// copy publish / subscribe configs in mqtt pubsub table
// by default 2 publish and subscribe topics are suported you can change it in mqtt configuration file
memcpy( _mqtt_pubsub_configs.publish_topics[0].topic, MQTT_PUBLISH_TOPIC, strlen( MQTT_PUBLISH_TOPIC ) );
_mqtt_pubsub_configs.publish_topics[0].qos = MQTT_PUBLISH_QOS;
memcpy( _mqtt_pubsub_configs.subscribe_topics[0].topic, MQTT_SUBSCRIBE_TOPIC, strlen( MQTT_SUBSCRIBE_TOPIC ) );
_mqtt_pubsub_configs.subscribe_topics[0].qos = MQTT_SUBSCRIBE_QOS;
_mqtt_pubsub_configs.publish_frequency = MQTT_PUBLISH_FREQ;

// copy lwt configs in mqtt lwt table
memcpy( _mqtt_lwt_configs.will_topic, MQTT_WILL_TOPIC, strlen( MQTT_WILL_TOPIC ) );
memcpy( _mqtt_lwt_configs.will_message, MQTT_WILL_MESSAGE, strlen( MQTT_WILL_MESSAGE ) );
_mqtt_lwt_configs.will_qos = MQTT_WILL_QOS;

// set config tables back in database
__database_service.set_mqtt_general_config_table( &_mqtt_general_configs );
__database_service.set_mqtt_lwt_config_table( &_mqtt_lwt_configs );
__database_service.set_mqtt_pubsub_config_table( &_mqtt_pubsub_configs );

// set publish subscribe callbacks
__mqtt_service.setMqttPublishDataCallback( publish_callback );
__mqtt_service.setMqttSubscribeDataCallback( subscribe_callback );

// start mqtt service with new configuration immediate after this call. e.g. here after 10 ms
__task_scheduler.setTimeout( [&]() { __mqtt_service.handleMqttConfigChange(); }, 10 );
}


#else
#error "Mqtt service is disabled ( in config/Common.h of framework library ). please enable(uncomment) it for this example"
#endif

void setup() {
EwStack.initialize();

// call it only after framework initialization
configure_mqtt();
}

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.1
version=1.1.2
author=Suraj I.
maintainer=Suraj I. <[email protected]>
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 @@ -38,6 +38,11 @@ extern "C" {
*/
#define EW_SERIAL_LOG

/**
* enable/disable auto factory reset on invalid database config found
*/
#define AUTO_FACTORY_RESET_ON_INVALID_CONFIGS

/**
* enable/disable exception notifier
*/
Expand Down
4 changes: 4 additions & 0 deletions src/config/MqttConfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ created Date : 1st June 2019

#define MQTT_INITIALIZE_DURATION MILLISECOND_DURATION_5000

/**
* enable/disable mqtt default payload for publish if user not assigned explicitely
*/
#define ENABLE_MQTT_DEFAULT_PAYLOAD
/**
* enable/disable mqtt config modification here
*/
Expand Down
16 changes: 16 additions & 0 deletions src/database/Database.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,22 @@ void Database::init_database( uint16_t _size ){
for (size_t i = 0; i < DatabaseTableAbstractLayer::_total_instances; i++) {
DatabaseTableAbstractLayer::_instances[i]->boot();
}

#ifdef AUTO_FACTORY_RESET_ON_INVALID_CONFIGS

__task_scheduler.setInterval( [&]() {

if ( !isValidConfigs() ){

#ifdef EW_SERIAL_LOG
Log( F("\n\nFound invalid configs.. starting factory reset..!\n\n") );
#endif
__factory_reset.factory_reset();
}

}, MILLISECOND_DURATION_5000 );

#endif
}

/**
Expand Down
11 changes: 11 additions & 0 deletions src/database/EepromDatabase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,14 @@ void cleanAllConfigs( void ){
}
EEPROM.end();
}

/**
* check whether database configs are valid
*
* @return bool
*/
bool isValidConfigs( void ){
return (EEPROM.read(CONFIG_START + 0) == CONFIG_VERSION[0] &&
EEPROM.read(CONFIG_START + 1) == CONFIG_VERSION[1] &&
EEPROM.read(CONFIG_START + 2) == CONFIG_VERSION[2]);
}
5 changes: 2 additions & 3 deletions src/database/EepromDatabase.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ created Date : 1st June 2019

void beginConfigs( uint16_t _size );
void cleanAllConfigs( void );
bool isValidConfigs( void );

/**
* template to save table in database by their address from table object
Expand Down Expand Up @@ -62,9 +63,7 @@ template <typename T> void clearConfigs( const T * _object, uint16_t _address )
* @param uint16_t configStart
*/
template <typename T> void loadConfig ( T * _object, uint16_t configStart ) {
if (EEPROM.read(CONFIG_START + 0) == CONFIG_VERSION[0] &&
EEPROM.read(CONFIG_START + 1) == CONFIG_VERSION[1] &&
EEPROM.read(CONFIG_START + 2) == CONFIG_VERSION[2])
if ( isValidConfigs() )
for (unsigned int i = 0; i < sizeof((*_object)); i++)
*((char*) & (*_object) + i) = EEPROM.read(configStart + i);
}
Expand Down
19 changes: 1 addition & 18 deletions src/service_provider/EmailServiceProvider.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,24 +64,7 @@ void EmailServiceProvider::handleEmail(){

#ifdef ENABLE_GPIO_SERVICE

for (uint8_t _pin = 0; _pin < MAX_NO_OF_GPIO_PINS; _pin++) {

if( !__gpio_service.is_exceptional_gpio_pin(_pin) ){

_payload += "D";
_payload += _pin;
_payload += " ( mode : ";
_payload += __gpio_service.virtual_gpio_configs.gpio_mode[_pin];
_payload += ", val : ";
_payload += __gpio_service.virtual_gpio_configs.gpio_readings[_pin];
_payload += ")\n";
}
}
_payload += "A0 ( mode : ";
_payload += __gpio_service.virtual_gpio_configs.gpio_mode[MAX_NO_OF_GPIO_PINS];
_payload += ", val : ";
_payload += __gpio_service.virtual_gpio_configs.gpio_readings[MAX_NO_OF_GPIO_PINS];
_payload += ")";
__gpio_service.appendGpioJsonPayload( _payload );
#endif

_payload += "\n\nHello from Esp\n";
Expand Down
Loading

0 comments on commit 5f59886

Please sign in to comment.