Library build, Arduino instructions

pull/16/head
Martin Ger 2017-09-17 10:34:08 +02:00
rodzic 6908a299d4
commit 284d553234
9 zmienionych plików z 300 dodań i 17 usunięć

246
Makefile.orig 100644
Wyświetl plik

@ -0,0 +1,246 @@
# none sdkota espboot rboot
OTA ?= none
OTA_APP_ADDR = 0x2000
OTA_BOOTLOADER_PATH = ../esp-bootloader/firmware/espboot.bin
THISDIR:=$(dir $(abspath $(lastword $(MAKEFILE_LIST))))
# Base directory for the compiler. Needs a / at the end; if not set it'll use the tools that are in
# the PATH.
XTENSA_TOOLS_ROOT ?=
# base directory of the ESP8266 SDK package, absolute
SDK_BASE ?= /home/martin/github/esp-open-sdk/sdk/
#Esptool.py path and port
ESPTOOL ?= /home/martin/github/esp-open-sdk/esptool/esptool.py
ESPPORT ?= /dev/ttyUSB0
#ESPPORT ?= /dev/tty.wchusbserial1410
#ESPDELAY indicates seconds to wait between flashing the two binary images
ESPDELAY ?= 3
#ESPBAUD ?= 115200
ESPBAUD ?= 460800
# 40m 26m 20m 80m
ESP_FREQ = 40m
# qio qout dio dout
ESP_MODE = dio
#4m 2m 8m 16m 32m
ESP_SIZE = 32m
VERBOSE = yes
FLAVOR = debug
# name for the target project
TARGET ?= esp_mqtt
# name for the target when compiling as library
TARGET_LIB ?= libmqtt.a
# which modules (subdirectories) of the project to include in compiling
USER_MODULES = user driver mqtt modules
USER_INC = include
USER_LIB =
# which modules (subdirectories) of the project to include when compiling as library
LIB_MODULES = mqtt
SDK_LIBDIR = lib
SDK_LIBS = c gcc phy pp net80211 wpa main lwip
# crypto ssl json driver
SDK_INC = include include/json
# Output directors to store intermediate compiled files
# relative to the project directory
BUILD_BASE = build
FIRMWARE_BASE = firmware
# Opensdk patches stdint.h when compiled with an internal SDK. If you run into compile problems pertaining to
# redefinition of int types, try setting this to 'yes'.
USE_OPENSDK ?= yes
DATETIME := $(shell date "+%Y-%b-%d_%H:%M:%S_%Z")
# select which tools to use as compiler, librarian and linker
CC := $(XTENSA_TOOLS_ROOT)xtensa-lx106-elf-gcc
AR := $(XTENSA_TOOLS_ROOT)xtensa-lx106-elf-ar
LD := $(XTENSA_TOOLS_ROOT)xtensa-lx106-elf-gcc
OBJCOPY := $(XTENSA_TOOLS_ROOT)xtensa-lx106-elf-objcopy
####
#### no user configurable options below here
####
SRC_DIR := $(USER_MODULES)
SRC_DIR_LIB := $(LIB_MODULES)
BUILD_DIR := $(addprefix $(BUILD_BASE)/,$(USER_MODULES))
INCDIR := $(addprefix -I,$(SRC_DIR))
EXTRA_INCDIR := $(addprefix -I,$(USER_INC))
MODULE_INCDIR := $(addsuffix /include,$(INCDIR))
SDK_LIBDIR := $(addprefix $(SDK_BASE)/,$(SDK_LIBDIR))
SDK_LIBS := $(addprefix -l,$(SDK_LIBS))
SDK_INCDIR := $(addprefix -I$(SDK_BASE)/,$(SDK_INC))
SRC := $(foreach sdir,$(SRC_DIR),$(wildcard $(sdir)/*.c))
SRC_LIB := $(foreach sdir,$(SRC_DIR_LIB),$(wildcard $(sdir)/*.c))
ASMSRC = $(foreach sdir,$(SRC_DIR),$(wildcard $(sdir)/*.S))
ASMSRC_LIB = $(foreach sdir,$(SRC_DIR_LIB),$(wildcard $(sdir)/*.S))
OBJ = $(patsubst %.c,$(BUILD_BASE)/%.o,$(SRC))
OBJ_LIB = $(patsubst %.c,$(BUILD_BASE)/%.o,$(SRC_LIB))
OBJ += $(patsubst %.S,$(BUILD_BASE)/%.o,$(ASMSRC))
OBJ_LIB += $(patsubst %.c,$(BUILD_BASE)/%.o,$(ASMSRC_LIB))
APP_AR := $(addprefix $(BUILD_BASE)/,$(TARGET).a)
TARGET_OUT := $(addprefix $(BUILD_BASE)/,$(TARGET).out)
# compiler flags using during compilation of source files
CFLAGS = -g \
-Wpointer-arith \
-Wundef \
-Wl,-EL \
-Wno-implicit-function-declaration \
-fno-inline-functions \
-nostdlib \
-mlongcalls \
-mtext-section-literals \
-ffunction-sections \
-fdata-sections \
-fno-builtin-printf\
-DICACHE_FLASH \
-DBUID_TIME=\"$(DATETIME)\"
# linker flags used to generate the main object file
LDFLAGS = -nostdlib -Wl,--no-check-sections -u call_user_start -Wl,-static
ifeq ($(FLAVOR),debug)
LDFLAGS += -g -O2
CFLAGS += -DMQTT_DEBUG_ON -DDEBUG_ON
endif
ifeq ($(FLAVOR),release)
LDFLAGS += -g -O0
endif
V ?= $(VERBOSE)
ifeq ("$(V)","yes")
Q :=
vecho := @true
else
Q := @
vecho := @echo
endif
ifeq ("$(USE_OPENSDK)","yes")
CFLAGS += -DUSE_OPENSDK
else
CFLAGS += -D_STDINT_H
endif
ifneq ("$(wildcard $(THISDIR)/include/user_config.local.h)","")
CFLAGS += -DLOCAL_CONFIG_AVAILABLE
endif
ESPTOOL_OPTS=--port $(ESPPORT) --baud $(ESPBAUD)
#32m
ESP_INIT_DATA_DEFAULT_ADDR = 0xfc000
ifeq ("$(ESP_SIZE)","16m")
ESP_INIT_DATA_DEFAULT_ADDR = 0x1fc000
else ifeq ("$(ESP_SIZE)","32m")
ESP_INIT_DATA_DEFAULT_ADDR = 0x3fc000
endif
ifeq ("$(OTA)","espboot")
OUTPUT := $(addprefix $(FIRMWARE_BASE)/,$(TARGET)-0x2000.bin)
ESPTOOL_WRITE = write_flash --flash_freq $(ESP_FREQ) --flash_mode $(ESP_MODE) --flash_size $(ESP_SIZE) \
0x00000 $(OTA_BOOTLOADER_PATH) \
$(OTA_APP_ADDR) $(OUTPUT) \
$(ESP_INIT_DATA_DEFAULT_ADDR) $(SDK_BASE)/bin/esp_init_data_default.bin
ESPTOOL_FLASHDEF=--version=2
LD_SCRIPT = -Tld/with-espboot-flash-at-0x2000-size-1M.ld
else
OUTPUT := $(addprefix $(FIRMWARE_BASE)/,$(TARGET))
ESPTOOL_WRITE = write_flash --flash_freq $(ESP_FREQ) --flash_mode $(ESP_MODE) --flash_size $(ESP_SIZE) \
0x00000 $(OUTPUT)0x00000.bin \
0x10000 $(OUTPUT)0x10000.bin \
$(ESP_INIT_DATA_DEFAULT_ADDR) $(SDK_BASE)/bin/esp_init_data_default.bin
ESPTOOL_FLASHDEF=
LD_SCRIPT = -T$(SDK_BASE)/ld/eagle.app.v6.ld
endif
OUTPUT_LIB := $(addprefix $(FIRMWARE_BASE)/,$(TARGET_LIB))
vpath %.c $(SRC_DIR)
define compile-objects
$1/%.o: %.c
$(vecho) "CC $$<"
$(Q) $(CC) $(INCDIR) $(MODULE_INCDIR) $(EXTRA_INCDIR) $(SDK_INCDIR) $(CFLAGS) -c $$< -o $$@
endef
.PHONY: all lib checkdirs clean
all: touch checkdirs $(OUTPUT)
lib: checkdirs $(OUTPUT_LIB)
touch:
$(vecho) "-------------------------------------------\n"
$(vecho) "BUID TIME $(DATETIME)"
$(vecho) "-------------------------------------------\n"
$(Q) touch user/user_main.c
checkdirs: $(BUILD_DIR) $(FIRMWARE_BASE)
$(OUTPUT): $(TARGET_OUT)
$(vecho) "FW $@"
$(Q) $(ESPTOOL) elf2image $(ESPTOOL_FLASHDEF) $< -o $(OUTPUT)
$(OUTPUT_LIB): $(OBJ_LIB)
$(vecho) "AR $@"
$(Q) $(AR) cru $@ $^
$(BUILD_DIR):
$(Q) mkdir -p $@
$(FIRMWARE_BASE):
$(Q) mkdir -p $@
$(TARGET_OUT): $(APP_AR)
$(vecho) "LD $@"
$(Q) $(LD) -L$(SDK_LIBDIR) $(LD_SCRIPT) $(LDFLAGS) -Wl,--start-group $(SDK_LIBS) $(APP_AR) -Wl,--end-group -o $@
$(APP_AR): $(OBJ)
$(vecho) "AR $@"
$(Q) $(AR) cru $@ $^
flash:
$(ESPTOOL) $(ESPTOOL_OPTS) $(ESPTOOL_WRITE)
fast: all flash openport
openport:
$(vecho) "After flash, terminal will enter serial port screen"
$(vecho) "Please exit with command:"
$(vecho) "\033[0;31m" "Ctrl + A + k" "\033[0m"
#@read -p "Press any key to continue... " -n1 -s
@screen $(ESPPORT) 115200
clean:
$(Q) rm -rf $(BUILD_DIR)
$(Q) rm -rf $(FIRMWARE_BASE)
$(foreach bdir,$(BUILD_DIR),$(eval $(call compile-objects,$(bdir))))

Wyświetl plik

@ -123,7 +123,6 @@ do
setvar $status_topic="/martinshome/switch/" | $device_number | "/status"
publish local $status_topic $relay_status retained
publish remote $status_topic $relay_status retained
% local subscriptions once in 'init'
subscribe local $command_topic
@ -134,6 +133,8 @@ do
% remote subscriptions for each connection in 'mqttconnect'
subscribe remote $command_topic
publish remote $status_topic $relay_status retained
% Now the events, checked whenever something happens
% Is there a remote command?
@ -313,16 +314,41 @@ The broker does not yet support:
- many TCP(=MQTT) clients
- non-clear sessions
- TLS
"
# Using the esp_uMQTT_broker in an Arduino project
There is a fast-and-dirty hack to add the broker functionality to any ESP Arduino project:
- Go to the install directory of the ESP8266 support package (something like: "<yourArduinoDir>/hardware/esp8266com/esp8266)
- Look for the file "platform.txt"
- Search for the line with "compiler.c.elf.libs"
- Add "-lmqtt" to the libs. Now it should look like:
```
compiler.c.elf.libs=-lm -lgcc -lhal -lphy -lpp -lnet80211 -lwpa -lcrypto -lmain -lwps -laxtls -lsmartconfig -lmesh -lwpa2 -lmqtt {build.lwip_lib} -lstdc++
```
- For this directory go to "cd tools/sdk/lib".
- Copy "libmqtt.a" from the "firmware" directory of this repository into that location (where the other C-libs of the SDK are).
- Now you can use it in your sketch. Just set up the WiFi connection (client or SoftAP, whatever you need) and add these lines:
```c
extern "C" {
bool MQTT_server_start(uint16_t portno, uint16_t max_subscriptions, uint16_t max_retained_topics);
}
```
and at the end of setup() do e.g.:
```c
MQTT_server_start(1883, 30, 30);
```
The MQTT server will now run in the background and you can connect with any MQTT client.
# Using the Source Code
The complete functionality is included in the mqtt directory and can be integrated into any NONOS SDK program. The broker is started by simply including:
The complete functionality is included in the mqtt directory and can be integrated into any NONOS SDK program ("make -f Makefile.orig lib" will build the mqtt code as a C library). The broker is started by simply including:
```c
#include "mqtt_server.h"
bool MQTT_server_start(uint16_t portno, uint16_t max_subscriptions, uint16_t max_retained_topics);
```
in the user_init() function. Now it is ready for MQTT connections on all activated interfaces (STA and/or AP). Please note, that the lib uses two tasks (with prio 1 and 2) for client and broker. Thus, only task with prio 0 is left for a user application.
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 without any additional logic.

Plik binarny nie jest wyświetlany.

Plik binarny nie jest wyświetlany.

BIN
firmware/libmqtt.a 100644

Plik binarny nie jest wyświetlany.

Wyświetl plik

@ -33,7 +33,6 @@ do
setvar $status_topic="/martinshome/switch/" | $device_number | "/status"
publish local $status_topic $relay_status retained
publish remote $status_topic $relay_status retained
% local subscriptions once in 'init'
subscribe local $command_topic
@ -44,6 +43,8 @@ do
% remote subscriptions for each connection in 'mqttconnect'
subscribe remote $command_topic
publish remote $status_topic $relay_status retained
% Now the events, checked whenever something happens
% Is there a remote command?

Wyświetl plik

@ -25,15 +25,6 @@ if (interpreter_status==SYNTAX_CHECK && next_token+(x) >= max_token) \
return syntax_error(next_token+(x), EOT)
#define syn_chk (interpreter_status==SYNTAX_CHECK)
typedef struct _var_entry_t {
uint8_t name[15];
uint8_t free;
uint32_t buffer_len;
uint8_t *data;
uint32_t data_len;
Value_Type data_type;
} var_entry_t;
typedef struct _timestamp_entry_t {
uint8_t *ts;
bool happened;
@ -66,7 +57,7 @@ int ts_counter;
int gpio_counter;
static os_timer_t timers[MAX_TIMERS];
static var_entry_t vars[MAX_VARS];
var_entry_t vars[MAX_VARS];
static timestamp_entry_t timestamps[MAX_TIMESTAMPS];
var_entry_t ICACHE_FLASH_ATTR *find_var(const uint8_t *name, var_entry_t **free_var) {

Wyświetl plik

@ -3,6 +3,20 @@
#include "mqtt_server.h"
typedef enum {SYNTAX_CHECK, CONFIG, INIT, MQTT_CLIENT_CONNECT, TOPIC_LOCAL, TOPIC_REMOTE, TIMER, GPIO_INT, CLOCK} Interpreter_Status;
typedef enum {STRING_T, DATA_T} Value_Type;
typedef struct _var_entry_t {
uint8_t name[15];
uint8_t free;
uint32_t buffer_len;
uint8_t *data;
uint32_t data_len;
Value_Type data_type;
} var_entry_t;
extern var_entry_t vars[];
extern MQTT_Client mqttClient;
extern bool mqtt_enabled, mqtt_connected;
extern bool lang_logging;
@ -10,9 +24,6 @@ extern bool lang_logging;
uint8_t tmp_buffer[128];
uint32_t loop_time;
typedef enum {SYNTAX_CHECK, CONFIG, INIT, MQTT_CLIENT_CONNECT, TOPIC_LOCAL, TOPIC_REMOTE, TIMER, GPIO_INT, CLOCK} Interpreter_Status;
typedef enum {STRING_T, DATA_T} Value_Type;
int text_into_tokens(char *str);
void free_tokens(void);
bool is_token(int i, char *s);

Wyświetl plik

@ -623,6 +623,14 @@ void ICACHE_FLASH_ATTR console_handle_command(struct espconn *pespconn) {
goto command_handled;
}
int i;
for (i = 0; i < MAX_VARS; i++) {
if (!vars[i].free) {
os_sprintf(response, "%s: %s\r\n", vars[i].name, vars[i].data);
to_console(response);
}
}
uint8_t slots[MAX_FLASH_SLOTS*FLASH_SLOT_LEN];
blob_load(1, (uint32_t *)slots, sizeof(slots));