Skip to content

Commit

Permalink
Updated to latest EOR
Browse files Browse the repository at this point in the history
EOR now a submodule
RFM69 driver now a submodule
Removed legacy SPI driver
  • Loading branch information
kanflo committed Jan 10, 2020
1 parent 6a4caf0 commit 1b36560
Show file tree
Hide file tree
Showing 7 changed files with 147 additions and 54 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,7 @@
# Debug files
*.dSYM/
*.su

.DS_Store
build/
firmware/
6 changes: 6 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[submodule "mqtt_sniffer/esp-open-rtos"]
path = mqtt_sniffer/esp-open-rtos
url = https://github.com/superhouse/esp-open-rtos.git
[submodule "mqtt_sniffer/rfm69"]
path = mqtt_sniffer/rfm69
url = https://github.com/kanflo/eor-rfm69.git
16 changes: 11 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,17 @@ Look at [hardware/v2](https://github.com/kanflo/espism/tree/master/hardware/v2)
#### Building

```
git clone --recursive https://github.com/SuperHouse/esp-open-rtos.git
vi esp-open-rtos/include/ssid_config.h # Fix wifi credentials
git clone https://github.com/kanflo/eor-spi.git esp-open-rtos/extras/spi
git clone https://github.com/kanflo/eor-rfm69 esp-open-rtos/extras/rfm69
export EOR_ROOT=$PWD/esp-open-rtos
git clone https://github.com/kanflo/espism.git
cd espism
git submodule init && git submodule update
cd mqtt_sniffer
vi esp-open-rtos/include/private_ssid_config.h # Fix wifi credentials
make && make flash
```

Wifi credentials, in case you did not know, are written to `esp-open-rtos/include/private_ssid_config.h` as

```
#define WIFI_SSID "your ssid"
#define WIFI_PASS "your key"
```
32 changes: 30 additions & 2 deletions mqtt_sniffer/Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,32 @@
PROGRAM=mqtt_sniffer
EXTRA_COMPONENTS = extras/spi extras/rfm69 extras/rboot-ota extras/paho_mqtt_c
EXTRA_COMPONENTS = extras/rboot-ota extras/paho_mqtt_c
OTA=1
include $(EOR_ROOT)/common.mk

# MQTT host config. Override with one, more or all of:
# make MQTT_HOST=172.16.3.100 MQTT_PORT=123345 MQTT_USER=mrrobot MQTT_PASS=secret
MQTT_HOST ?= iot.eclipse.org
MQTT_PORT ?= 1883
MQTT_USER ?= NULL
MQTT_PASS ?= NULL
HW_REV ?= 4

# You should change this ;)
RFM_KEY ?= sampleEncryptKey

# Things will go south without this, flags are not inherited by EOR
PROGRAM_CFLAGS = -Wall -Wl,-EL -nostdlib -ffunction-sections -fdata-sections -g -O2 -std=gnu99

# Add our config
PROGRAM_CFLAGS += -DCONFIG_HW_REV=$(HW_REV) -DCONFIG_RFM_KEY=\"$(RFM_KEY)\"
PROGRAM_CFLAGS += -DCONFIG_MQTT_HOST=\"$(MQTT_HOST)\" -DCONFIG_MQTT_PORT=$(MQTT_PORT)
ifneq ($(MQTT_USER), NULL)
PROGRAM_CFLAGS += -DCONFIG_MQTT_USER=\"$(MQTT_USER)\"
endif
ifneq ($(MQTT_PASS), NULL)
PROGRAM_CFLAGS += -DCONFIG_MQTT_PASS=\"$(MQTT_PASS)\"
endif

PROGRAM_INC_DIR = . ./rfm69
PROGRAM_SRC_DIR = . ./rfm69

include esp-open-rtos/common.mk
1 change: 1 addition & 0 deletions mqtt_sniffer/esp-open-rtos
Submodule esp-open-rtos added at 53fa63
141 changes: 94 additions & 47 deletions mqtt_sniffer/mqtt_sniffer.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <spi.h>
#include <esp/spi.h>
#include <rfm69.h>
#include <ssid_config.h>
#include <FreeRTOS.h>
Expand All @@ -50,12 +50,14 @@ typedef enum {
led_off = 0,
led_flash_once,
led_flash_double,
led_flash_slow,
led_flash_fast
} led_state_t;


#define HW_REV (2) // 1..3
#define HW_REV CONFIG_HW_REV

/** The various espism boards I have build */
#if HW_REV==1
#define LED_GPIO (15)
#define RFM69_INT (5)
Expand All @@ -69,27 +71,47 @@ typedef enum {
#define RFM69_INT (5)
#define RFM69_CS (4)
#define RFM69_RESET (16) // Only on v3
#elif HW_REV==4
#define LED_GPIO (15)
#define RFM69_INT (5)
#define RFM69_CS (4)
#define RFM69_RESET (16) // Only on v4
#else
#error "Unsupported HW revision"
#endif


#define MAX_ISM_PKT_SIZE (64)
#define PUB_QUEUE_DEPTH (10)
#define PUB_QUEUE_DEPTH (10)

#define MQTT_HOST CONFIG_MQTT_HOST
#define MQTT_PORT CONFIG_MQTT_PORT

#ifdef CONFIG_MQTT_USER
#define MQTT_USER CONFIG_MQTT_USER
#else // CONFIG_MQTT_USER
#define MQTT_USER NULL
#endif // CONFIG_MQTT_USER

#ifdef CONFIG_MQTT_PASS
#define MQTT_PASS CONFIG_MQTT_PASS
#else // CONFIG_MQTT_PASS
#define MQTT_PASS NULL
#endif // CONFIG_MQTT_PASS

#define MQTT_HOST ("iot.eclipse.org")
#define MQTT_PORT 1883
#define MQTT_USER NULL
#define MQTT_PASS NULL
#define RFM_KEY CONFIG_RFM_KEY

SemaphoreHandle_t wifi_alive;
SemaphoreHandle_t sniffer_alive;
QueueHandle_t publish_queue;

led_state_t g_led_state = led_off;
volatile led_state_t g_led_state = led_off;

#define delay_ms(ms) vTaskDelay(ms / portTICK_PERIOD_MS)

static const char* get_station_mac(void);
static const char* get_station_ip(void);

static void set_led(bool on)
{
gpio_write(LED_GPIO, on?0:1);
Expand All @@ -100,6 +122,27 @@ static void set_led_state(led_state_t state)
g_led_state = state;
}

static void heartbeat_task(void *param)
{
(void) param;
char msg[MAX_ISM_PKT_SIZE];
while(1) {
delay_ms(60000);
printf("Publishing heartbeat\n");
#if 0
/** Currently cannot do this as the entries in the queue are RFM
* packets and not arbitrary strings
*/
snprintf(msg, sizeof(msg)-1, "heartbeat:%s", get_station_ip());
#else
memset(msg, 0, sizeof(msg));
#endif
if (xQueueSend(publish_queue, (void*) msg, 0) == pdFALSE) {
printf("Publish queue overflow.\n");
}
}
}

static void led_task(void *pvParameters)
{
while(1) {
Expand All @@ -108,26 +151,23 @@ static void led_task(void *pvParameters)
delay_ms(100); // Do not while(1). Todo: Use semaphore
break;
case led_flash_once:
set_led(true);
delay_ms(50);
set_led(false);
set_led(1); delay_ms(50);
set_led(0);
g_led_state = led_off;
break;
case led_flash_double:
set_led(true);
delay_ms(10);
set_led(false);
delay_ms(100);
set_led(true);
delay_ms(10);
set_led(false);
delay_ms(950);
set_led(1); delay_ms(50);
set_led(0); delay_ms(250);
set_led(1); delay_ms(50);
set_led(0); delay_ms(950);
break;
case led_flash_fast:
set_led(true);
delay_ms(10);
set_led(false);
delay_ms(450);
set_led(1); delay_ms(10);
set_led(0); delay_ms(450);
break;
case led_flash_slow:
set_led(1); delay_ms(500);
set_led(0); delay_ms(500);
break;
}
}
Expand All @@ -145,28 +185,32 @@ static bool ism_init()
rfm69_reset();
#endif // RFM69_RESET

spi_init(iHSPI);
bool ok = rfm69_init(RFM69_CS, false); // false = RFM69W, true = RFM69HW
spi_init(1, SPI_MODE0, SPI_FREQ_DIV_1M, 1, SPI_LITTLE_ENDIAN, true);
/** true = RFM69HW, false = RFM69W */
bool ok = rfm69_init(RFM69_CS, false);
if (!ok) {
printf("RFM69 init failed!\n");
set_led_state(led_flash_slow);
return false;
} else {
printf("RFM69 init ok\n");
gpio_enable(RFM69_INT, GPIO_INPUT);
rfm69_sleep(); // init RF module and put it to sleep
rfm69_setPowerDBm(10); // // set output power, +10 dBm
rfm69_setCSMA(true); // enable CSMA/CA algorithm
rfm69_sleep();
/** set output power, +10 dBm */
rfm69_setPowerDBm(10);
/** enable CSMA/CA algorithm */
rfm69_setCSMA(true);
rfm69_setAutoReadRSSI(true);
(void) rfm69_setAESEncryption((void*) "sampleEncryptKey", 16);
(void) rfm69_setAESEncryption((void*) RFM_KEY, 16);
rfm69_setPowerLevel(0); // max 31
}
return true;
}

static void rx_task(void *pvParameters)
static void ism_task(void *pvParameters)
{
if (!ism_init()) {
taskYIELD();
return;
}
while(1) {
xSemaphoreTake(sniffer_alive, portMAX_DELAY);
Expand All @@ -179,7 +223,7 @@ static void rx_task(void *pvParameters)
data[0] = len+1; // [len data[0] data[1] ... data[len-1] rssi]
data[len] = (int8_t) rssi;
// rfm69_sleep();
if (xQueueSend(publish_queue, (void *)data, 0) == pdFALSE) {
if (xQueueSend(publish_queue, (void*) data, 0) == pdFALSE) {
printf("Publish queue overflow.\n");
}
}
Expand All @@ -188,7 +232,7 @@ static void rx_task(void *pvParameters)
}

// Return our IP address as a string "a.b.c.d" or "0.0.0.0" if we cannot determine it
static const char* get_my_ip(void)
static const char* get_station_ip(void)
{
static char ip[16] = "0.0.0.0";
ip[0] = 0;
Expand All @@ -199,7 +243,7 @@ static const char* get_my_ip(void)
}

// Use MAC address for Station as unique ID
static const char* get_my_id(void)
static const char* get_station_mac(void)
{
static char my_id[32];
static bool my_id_done = false;
Expand Down Expand Up @@ -241,13 +285,12 @@ static void mqtt_task(void *pvParameters)
mqtt_packet_connect_data_t data = mqtt_packet_connect_data_initializer;

mqtt_network_new(&network);
snprintf(mqtt_client_id, sizeof(mqtt_client_id)-1, "espism-%s", get_my_id());
snprintf(mqtt_client_id, sizeof(mqtt_client_id)-1, "espism-%s", get_station_mac());

while(1) {
char msg[3*MAX_ISM_PKT_SIZE];
xSemaphoreTake(wifi_alive, portMAX_DELAY);
printf("%s: started\n", __func__);
printf("%s: (Re)connecting to MQTT server %s ... ",__func__, MQTT_HOST);
printf("Connecting to MQTT server %s ... ", MQTT_HOST);
ret = mqtt_network_connect(&network, MQTT_HOST, MQTT_PORT);
if(ret) {
printf("error: %d\n", ret);
Expand Down Expand Up @@ -278,13 +321,13 @@ static void mqtt_task(void *pvParameters)
set_led_state(led_off);
xQueueReset(publish_queue);

snprintf(msg, sizeof(msg)-1, "Hello from %s", get_my_ip());
snprintf(msg, sizeof(msg)-1, "boot:%s", get_station_ip());
printf("Publishing %s/%s'\n", mqtt_client_id, msg);
(void) mqtt_publish_message(&client, (char*) mqtt_client_id, (char*) msg);

while(1) {
uint8_t pkt[MAX_ISM_PKT_SIZE];
while(xQueueReceive(publish_queue, (void *)pkt, 0) == pdTRUE) {
while(xQueueReceive(publish_queue, (void*) pkt, 0) == pdTRUE) {
uint8_t msg_len = sizeof(msg);
uint8_t len = pkt[0];
int8_t rssi = (int8_t) pkt[len-1];
Expand Down Expand Up @@ -329,7 +372,7 @@ static void wifi_task(void *pvParameters)
while(1) {
while ((status != STATION_GOT_IP) && (retries)) {
status = sdk_wifi_station_get_connect_status();
printf("%s: status = %d\n", __func__, status);
printf("Wifi status : %d\n", status);
if(status == STATION_WRONG_PASSWORD) {
printf("WiFi: wrong password\n");
break;
Expand Down Expand Up @@ -365,23 +408,27 @@ void user_init(void)
set_led(false);

uart_set_baud(0, 115200);
printf("SDK version:%s\n", sdk_system_get_sdk_version());
rboot_config conf = rboot_get_config();
printf("\n\nEspism Sniffer.\nCurrently running on flash slot %d / %d.\n\n", conf.current_rom, conf.count);
printf("\n\nEspism Sniffer\n");
printf("SDK version:%s\n", sdk_system_get_sdk_version());
printf("HW rev %d\n", HW_REV);

printf("Running on flash slot %d / %d\n", conf.current_rom, conf.count);
printf("Image addresses in flash:\n");
for(int i = 0; i <conf.count; i++) {
printf("%c%d: offset 0x%08x\n", i == conf.current_rom ? '*':' ', i, conf.roms[i]);
}
ota_tftp_init_server(TFTP_PORT);
set_led_state(led_flash_double);
xTaskCreate(&led_task, "led_task", 256, NULL, 2, NULL);
delay_ms(5000); /** not sure why I added this ;) */

vSemaphoreCreateBinary(wifi_alive);
vSemaphoreCreateBinary(sniffer_alive);
xSemaphoreTake(sniffer_alive, portMAX_DELAY);
publish_queue = xQueueCreate(PUB_QUEUE_DEPTH, MAX_ISM_PKT_SIZE);
xTaskCreate(&wifi_task, (int8_t *) "wifi_task", 256, NULL, 2, NULL);
xTaskCreate(&mqtt_task, (int8_t *) "mqtt_task", 1024, NULL, 4, NULL);
xTaskCreate(&rx_task, (int8_t *) "rx_task", 256, NULL, 2, NULL);
xTaskCreate(&led_task, (int8_t *) "led_task", 256, NULL, 2, NULL);
set_led_state(led_flash_double);
xTaskCreate(&wifi_task, "wifi_task", 256, NULL, 2, NULL);
xTaskCreate(&mqtt_task, "mqtt_task", 1024, NULL, 4, NULL);
xTaskCreate(&ism_task, "ism_task", 256, NULL, 2, NULL);
xTaskCreate(&heartbeat_task, "heartbeat_task", 256, NULL, 2, NULL);
}
1 change: 1 addition & 0 deletions mqtt_sniffer/rfm69
Submodule rfm69 added at 306721

0 comments on commit 1b36560

Please sign in to comment.