diff --git a/samples/Basic_Blink_RTOS/.cproject b/samples/Basic_Blink_RTOS/.cproject new file mode 100644 index 0000000..8e68134 --- /dev/null +++ b/samples/Basic_Blink_RTOS/.cproject @@ -0,0 +1,152 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + make + -f ${ProjDirPath}/Makefile + all + true + true + true + + + make + -f ${ProjDirPath}/Makefile + clean + true + true + true + + + make + -f ${ProjDirPath}/Makefile + flash + true + true + true + + + make + -f ${ProjDirPath}/Makefile + flashonefile + true + true + true + + + make + -f ${ProjDirPath}/Makefile + flashinit + true + true + true + + + make + -f ${ProjDirPath}/Makefile + flashboot + true + true + true + + + make + -f ${ProjDirPath}/Makefile + rebuild + true + true + true + + + + + + + + + + + + + + + + + + + + diff --git a/samples/Basic_Blink_RTOS/.project b/samples/Basic_Blink_RTOS/.project new file mode 100644 index 0000000..ac56616 --- /dev/null +++ b/samples/Basic_Blink_RTOS/.project @@ -0,0 +1,28 @@ + + + Basic_Blink_RTOS + + + SmingFramework + + + + org.eclipse.cdt.managedbuilder.core.genmakebuilder + clean,full,incremental, + + + + + org.eclipse.cdt.managedbuilder.core.ScannerConfigBuilder + full,incremental, + + + + + + org.eclipse.cdt.core.cnature + org.eclipse.cdt.core.ccnature + org.eclipse.cdt.managedbuilder.core.managedBuildNature + org.eclipse.cdt.managedbuilder.core.ScannerConfigNature + + diff --git a/samples/Basic_Blink_RTOS/Makefile b/samples/Basic_Blink_RTOS/Makefile new file mode 100644 index 0000000..16d76cd --- /dev/null +++ b/samples/Basic_Blink_RTOS/Makefile @@ -0,0 +1,24 @@ +##################################################################### +#### Please don't change this file. Use Makefile-user.mk instead #### +##################################################################### +# Including user Makefile. +# Should be used to set project-specific parameters +include ./Makefile-user.mk + +# Important parameters check. +# We need to make sure SMING_HOME and ESP_HOME variables are set. +# You can use Makefile-user.mk in each project or use enviromental variables to set it globally. + +ifndef SMING_HOME +$(error SMING_HOME is not set. Please configure it in Makefile-user.mk) +endif +ifndef ESP_HOME +$(error ESP_HOME is not set. Please configure it in Makefile-user.mk) +endif + +# Include main Sming Makefile +ifeq ($(RBOOT_ENABLED), 1) +include $(SMING_HOME)/Makefile-rboot.mk +else +include $(SMING_HOME)/Makefile-project.mk +endif diff --git a/samples/Basic_Blink_RTOS/Makefile-user.mk b/samples/Basic_Blink_RTOS/Makefile-user.mk new file mode 100644 index 0000000..a564a12 --- /dev/null +++ b/samples/Basic_Blink_RTOS/Makefile-user.mk @@ -0,0 +1,41 @@ +## Local build configuration +## Parameters configured here will override default and ENV values. +## Uncomment and change examples: + +## Add your source directories here separated by space +# MODULES = app +# EXTRA_INCDIR = include + +## ESP_HOME sets the path where ESP tools and SDK are located. +## Windows: +# ESP_HOME = c:/espressif + +## MacOS / Linux: +# ESP_HOME = /opt/esp-open-sdk + +## SMING_HOME sets the path where Sming framework is located. +## Windows: +# SMING_HOME = c:/tools/SmingRTOS/sming + +## MacOS / Linux +# SMING_HOME = /opt/sming/Sming + +## COM port parameter is reqruied to flash firmware correctly. +## Windows: +# COM_PORT = COM6 + +## MacOS / Linux: +# COM_PORT = /dev/tty.usbserial + +## Com port speed +# COM_SPEED = 115200 + +## Configure flash parameters (for ESP12-E and other new boards): +# SPI_MODE = dio + +## SPIFFS options +DISABLE_SPIFFS = 1 +# SPIFF_FILES = files + +#ESPTOOL2 ?= c:/tools/sming/esptool2 + diff --git a/samples/Basic_Blink_RTOS/app/application.cpp b/samples/Basic_Blink_RTOS/app/application.cpp new file mode 100644 index 0000000..8fb8b5f --- /dev/null +++ b/samples/Basic_Blink_RTOS/app/application.cpp @@ -0,0 +1,64 @@ +#include +#include + + +#define LEDB_PIN 2 // GPIO2 +#define LEDR_PIN 15 // GPIO15 + + +bool state = true; +bool stateR = true; + +// +// actualy "portTICK_RATE_MS" (base RTOS tick for ESP8266) is set to 10 ms +// + +// ****************************** +// ledB task (our first "program") +// ****************************** +void ledB_task(void *pvParameters) +{ + + pinMode(LEDB_PIN, OUTPUT); + for(;;) + { + digitalWrite(LEDB_PIN, state); + state = !state; + // + // Perform other action here. + // + + vTaskDelay(500/portTICK_RATE_MS); //Pause operation for 50 ticks (500 ms) + //create opportunities to launch another task with the same priority + } + +} +// ******************************* +// ledR task (our second "program") +// ******************************* +void ledR_task(void *pvParameters) +{ + + pinMode(LEDR_PIN, OUTPUT); + while(1) + { + digitalWrite(LEDR_PIN, stateR); + stateR = !stateR; + // + // Perform other action here. + // + + vTaskDelay(250/portTICK_RATE_MS); //Pause operation for 25 ticks (250 ms) + //create opportunities to launch another task with the same priority + } + +} + + +void init() +{ + + // The creation of two task with the same priority + xTaskCreate(ledB_task, (const signed char*)"ledB_task", 256, NULL, 2, NULL); + xTaskCreate(ledR_task, (const signed char*)"ledR_task", 256, NULL, 2, NULL); +} diff --git a/samples/Basic_Blink_RTOS/include/user_config.h b/samples/Basic_Blink_RTOS/include/user_config.h new file mode 100644 index 0000000..f2a508f --- /dev/null +++ b/samples/Basic_Blink_RTOS/include/user_config.h @@ -0,0 +1,7 @@ +#ifndef __USER_CONFIG_H__ +#define __USER_CONFIG_H__ + +// In this file you can define Sming Runtime parameters +// For possible options see : .... + +#endif diff --git a/sming/sming/network/MqttClient.cpp b/sming/sming/network/MqttClient.cpp index 4ee2148..1701a2a 100644 --- a/sming/sming/network/MqttClient.cpp +++ b/sming/sming/network/MqttClient.cpp @@ -41,6 +41,17 @@ void MqttClient::setKeepAlive(int seconds) keepAlive = seconds; } +void MqttClient::setPingRepeatTime(int seconds) + { + if (seconds > keepAlive) + PingRepeatTime = keepAlive; + else + PingRepeatTime = seconds; + } + + + + bool MqttClient::setWill(String topic, String message, int QoS, bool retained /* = false*/) { return mqtt_set_will(&broker, topic.c_str(), message.c_str(), QoS, retained); @@ -107,6 +118,7 @@ int MqttClient::staticSendPacket(void* userInfo, const void* buf, unsigned int c { MqttClient* client = (MqttClient*)userInfo; bool sent = client->send((const char*)buf, count); + client->lastMessage = millis(); return sent ? count : 0; } @@ -298,10 +310,13 @@ err_t MqttClient::onReceive(pbuf *buf) void MqttClient::onReadyToSendData(TcpConnectionEvent sourceEvent) { - if (sleep >= 10) - { - mqtt_ping(&broker); - sleep = 0; - } - TcpClient::onReadyToSendData(sourceEvent); + + // Send PINGREQ every PingRepeatTime time, if there is no outgoing traffic + // PingRepeatTime should be <= keepAlive + if (lastMessage && (millis() - lastMessage >= PingRepeatTime*1000)) + { + mqtt_ping(&broker); + } + TcpClient::onReadyToSendData(sourceEvent); + } diff --git a/sming/sming/network/MqttClient.h b/sming/sming/network/MqttClient.h index 61f167c..d4730b2 100644 --- a/sming/sming/network/MqttClient.h +++ b/sming/sming/network/MqttClient.h @@ -30,7 +30,10 @@ class MqttClient: protected TcpClient MqttClient(IPAddress serverIp, int serverPort, MqttStringSubscriptionCallback callback = NULL); virtual ~MqttClient(); - void setKeepAlive(int seconds); + + void setKeepAlive(int seconds); //send to broker + void setPingRepeatTime(int seconds); //used by client + // Sets Last Will and Testament bool setWill(String topic, String message, int QoS, bool retained = false); @@ -68,7 +71,9 @@ class MqttClient: protected TcpClient uint8_t *current; int posHeader; MqttStringSubscriptionCallback callback; - int keepAlive = 20; + int keepAlive = 60; + int PingRepeatTime = 20; + unsigned long lastMessage; String mqttCommandEnabled = ""; String mqttCommandTopic = "";