From 10f292f27ec0cef259e3635c4a074763706113f4 Mon Sep 17 00:00:00 2001 From: Martin Ger Date: Wed, 7 Jun 2017 22:21:26 +0200 Subject: [PATCH] Added basic sample at user_basic/ --- Makefile | 2 +- README.md | 6 ++++-- {user => include}/ringbuf.h | 0 user/user_config.h | 6 ------ user_basic/user_config.h | 25 +++++++++++++++++++++++++ user_basic/user_main.c | 28 ++++++++++++++++++++++++++++ 6 files changed, 58 insertions(+), 9 deletions(-) rename {user => include}/ringbuf.h (100%) create mode 100644 user_basic/user_config.h create mode 100644 user_basic/user_main.c diff --git a/Makefile b/Makefile index ab84c1c..c96ce35 100644 --- a/Makefile +++ b/Makefile @@ -43,7 +43,7 @@ LIB_MODULES = mqtt LIBS = c gcc hal pp phy net80211 lwip wpa main # compiler flags using during compilation of source files -CFLAGS = -Os -g -O2 -Wpointer-arith -Wundef -Werror -Wl,-EL -fno-inline-functions -nostdlib -mlongcalls -mtext-section-literals -D__ets__ -DICACHE_FLASH -DUSE_OPTIMIZE_PRINTF +CFLAGS = -Os -g -O2 -Wpointer-arith -Wundef -Werror -Wl,-EL -fno-inline-functions -nostdlib -mlongcalls -mtext-section-literals -D__ets__ -DICACHE_FLASH -DUSE_OPTIMIZE_PRINTF #-DMQTT_DEBUG_ON # linker flags used to generate the main object file diff --git a/README.md b/README.md index 51974ec..88cb31b 100644 --- a/README.md +++ b/README.md @@ -31,16 +31,18 @@ in the user_init() function. # Building and Flashing The code can be used in any project that is compiled using the NONOS_SDK or the esp-open-sdk. Also the sample code in the user directory can be build using the standard SDKs after adapting the variables in the Makefile. +If you don't need the full demo program, you can find a minimal demo in the directory "user_basic". Rename it to "user", adapt "user_config.h", and do the "make" to build a small demo that just starts an MQTT broker. + Build the esp_uMQTT_broker firmware with "make". "make flash" flashes it onto an esp8266. -If you want to use the precompiled binaries from the firmware directory you can flash them on an ESP12 with +If you want to use the precompiled binaries of the bigger demo (see below) from the firmware directory you can flash them on an ESP12 with ```bash esptool.py --port /dev/ttyUSB0 write_flash -fs 32m 0x00000 firmware/0x00000.bin 0x10000 firmware/0x10000.bin ``` # Usage -In the user directory there is a demo program that serves as a stand-alone MQTT broker. The program starts with the following default configuration: +In the user directory there is a demo program that serves as a stand-alone MQTT broker and bridge. The program starts with the following default configuration: - ssid: ssid, password: password - ap_ssid: MyAP, ap_password: none, ap_on: 1, ap_open: 1 - network: 192.168.4.0/24 diff --git a/user/ringbuf.h b/include/ringbuf.h similarity index 100% rename from user/ringbuf.h rename to include/ringbuf.h diff --git a/user/user_config.h b/user/user_config.h index 8dd54ce..e691c8d 100644 --- a/user/user_config.h +++ b/user/user_config.h @@ -10,7 +10,6 @@ typedef enum {SIG_DO_NOTHING=0, SIG_START_SERVER=1, SIG_SEND_DATA, SIG_UART0, SI #define WIFI_AP_PASSWORD "none" #define MAX_CLIENTS 8 -#define MAX_DHCP 8 // // Here the MQTT stuff @@ -33,11 +32,6 @@ typedef enum {SIG_DO_NOTHING=0, SIG_START_SERVER=1, SIG_SEND_DATA, SIG_UART0, SI #define MAX_CON_SEND_SIZE 1024 #define MAX_CON_CMD_SIZE 80 -// -// Define this if you have a status LED connected to a GPIO pin -// -#define STATUS_LED_GIPO 2 - // // Define this to support the "scan" command for AP search // diff --git a/user_basic/user_config.h b/user_basic/user_config.h new file mode 100644 index 0000000..146eb15 --- /dev/null +++ b/user_basic/user_config.h @@ -0,0 +1,25 @@ +#ifndef __MQTT_CONFIG_H__ +#define __MQTT_CONFIG_H__ + +/*DEFAULT CONFIGURATIONS*/ + +#define MQTT_PORT 1883 +#define MQTT_BUF_SIZE 1024 +#define MQTT_KEEPALIVE 120 /*second*/ +#define MQTT_RECONNECT_TIMEOUT 5 /*seconds*/ + +#define MQTT_MAX_SUBSCRIPTIONS 30 +#define MQTT_MAX_RETAINED_TOPICS 30 + + +#define TCP_MAX_CONNECTIONS 10 +#define STA_SSID "SSID" +#define STA_PASS "PASSWD" + + +#define PROTOCOL_NAMEv31 /*MQTT version 3.1 compatible with Mosquitto v0.15*/ +//PROTOCOL_NAMEv311 /*MQTT version 3.11 compatible with https://eclipse.org/paho/clients/testing/*/ + +typedef enum {SIG_DO_NOTHING=0, SIG_UART0, SIG_CONSOLE_RX, SIG_CONSOLE_TX} USER_SIGNALS; + +#endif // __MQTT_CONFIG_H__ diff --git a/user_basic/user_main.c b/user_basic/user_main.c new file mode 100644 index 0000000..155b73e --- /dev/null +++ b/user_basic/user_main.c @@ -0,0 +1,28 @@ +#include "user_interface.h" +#include "mqtt_server.h" +#include "user_config.h" + +void ICACHE_FLASH_ATTR user_init() +{ + struct station_config stationConf; + + // Initialize the UART + uart_div_modify(0, UART_CLK_FREQ / 115200); + + os_printf("\r\n\r\nMQTT Broker starting\r\n", espconn_tcp_get_max_con()); + + // Setup STA + wifi_set_opmode(STATIONAP_MODE); + stationConf.bssid_set = 0; + os_strcpy(&stationConf.ssid, STA_SSID); + os_strcpy(&stationConf.password, STA_PASS); + wifi_station_set_config(&stationConf); + wifi_station_set_auto_connect(1); + + // Allow larger number of TCP (=MQTT) clients + espconn_tcp_set_max_con(TCP_MAX_CONNECTIONS); + os_printf("Max number of TCP clients: %d\r\n", espconn_tcp_get_max_con()); + + //Start MQTT broker + MQTT_server_start(MQTT_PORT, MQTT_MAX_SUBSCRIPTIONS, MQTT_MAX_RETAINED_TOPICS); +}