Skip to content

Commit

Permalink
Add MQTT notifications
Browse files Browse the repository at this point in the history
  • Loading branch information
jbaudoux committed Aug 27, 2019
1 parent 9f6bea7 commit 971e9b5
Show file tree
Hide file tree
Showing 5 changed files with 150 additions and 75 deletions.
37 changes: 35 additions & 2 deletions OpenSprinkler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,11 @@ extern char ether_buffer[];
byte OpenSprinkler::pin_sr_data = PIN_SR_DATA;
#endif

#if defined(MQTT) && defined(OSPI)
struct mosquitto *mqtt_client = NULL;
#endif


/** Option json names (stored in progmem) */
// IMPORTANT: each json name is strictly 5 characters
// with 0 fillings if less
Expand Down Expand Up @@ -142,6 +147,8 @@ const char op_json_names[] PROGMEM =
"dns4\0"
"sar\0\0"
"ife\0\0"
"mqtt\0"
"notif"
"sn2t\0"
"sn2o\0"
"reset";
Expand Down Expand Up @@ -199,7 +206,9 @@ const char op_prompts[] PROGMEM =
"DNS server.ip3: "
"DNS server.ip4: "
"Special Refresh?"
"IFTTT Enable: "
"IFTTT enabled? "
"MQTT enabled? "
"Notify events: "
"Sensor 2 type: "
"Normally open? "
"Factory reset? ";
Expand Down Expand Up @@ -255,6 +264,8 @@ const byte op_max[] PROGMEM = {
255,
255,
1,
1,
1,
255,
255,
1,
Expand Down Expand Up @@ -317,7 +328,9 @@ byte OpenSprinkler::options[] = {
8,
8,
0, // special station auto refresh
0, // ifttt enable bits
0, // ifttt enabled
0, // mqtt enabled
255, // notify messages bits
0, // sensor 2 type
0, // sensor 2 option. 0: normally closed; 1: normally open.
0 // reset
Expand Down Expand Up @@ -466,6 +479,7 @@ byte OpenSprinkler::start_ether() {
}
#endif


/** Reboot controller */
void OpenSprinkler::reboot_dev() {
lcd_print_line_clear_pgm(PSTR("Rebooting..."), 0);
Expand All @@ -489,6 +503,17 @@ extern char ether_buffer[];

/** Initialize network with the given mac address and http port */
byte OpenSprinkler::start_network() {
#ifdef MQTT
if (options[OPTION_MQTT_ENABLE]) {
mosquitto_lib_init();
int keepalive = 60;
bool clean_session = true;
mqtt_client = mosquitto_new(NULL, clean_session, NULL);
if (!mqtt_client) return 1;
if (mosquitto_connect(mqtt_client, DEFAULT_MQTT_HOST, DEFAULT_MQTT_PORT, keepalive)) return 3;
}
#endif

unsigned int port = (unsigned int)(options[OPTION_HTTPPORT_1]<<8) + (unsigned int)options[OPTION_HTTPPORT_0];
#if defined(DEMO)
port = 80;
Expand Down Expand Up @@ -520,6 +545,14 @@ void OpenSprinkler::update_dev() {
}
#endif // end network init functions


void OpenSprinkler::mqtt_publish(const char *topic, const char *payload) {
#if defined(MQTT) && defined(OSPI)
mosquitto_publish(mqtt_client, NULL, topic, strlen(payload), payload, 0, true);
// mqtt_client.publish(topic, payload, true);
#endif
}

#if defined(ARDUINO)
/** Initialize LCD */
void OpenSprinkler::lcd_start() {
Expand Down
5 changes: 5 additions & 0 deletions OpenSprinkler.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@
#include <unistd.h>
#include <netdb.h>
#include "etherport.h"

#ifdef MQTT
#include <mosquitto.h>
#endif
#endif // end of headers

/** Non-volatile data */
Expand Down Expand Up @@ -175,6 +179,7 @@ class OpenSprinkler {
static void begin(); // initialization, must call this function before calling other functions
static byte start_network(); // initialize network with the given mac and port
static byte start_ether(); // initialize ethernet with the given mac and port
static void mqtt_publish(const char *topic, const char *payload); // publish mqtt message
#if defined(ARDUINO)
static bool load_hardware_mac(uint8_t*, bool wired=false); // read hardware mac address
#endif
Expand Down
2 changes: 1 addition & 1 deletion build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ if [ "$1" == "demo" ]; then
elif [ "$1" == "osbo" ]; then
g++ -o OpenSprinkler -DOSBO main.cpp OpenSprinkler.cpp program.cpp server.cpp utils.cpp weather.cpp gpio.cpp etherport.cpp -lpthread
else
g++ -o OpenSprinkler -DOSPI main.cpp OpenSprinkler.cpp program.cpp server.cpp utils.cpp weather.cpp gpio.cpp etherport.cpp -lpthread
g++ -o OpenSprinkler -DOSPI -DMQTT main.cpp OpenSprinkler.cpp program.cpp server.cpp utils.cpp weather.cpp gpio.cpp etherport.cpp -lpthread
fi

if [ ! "$SILENT" = true ] && [ -f OpenSprinkler.launch ] && [ ! -f /etc/init.d/OpenSprinkler.sh ]; then
Expand Down
18 changes: 12 additions & 6 deletions defines.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,13 @@ typedef unsigned long ulong;
#define STN_TYPE_HTTP 0x04 // Support for HTTP Get connection
#define STN_TYPE_OTHER 0xFF

#define IFTTT_PROGRAM_SCHED 0x01
#define IFTTT_RAINSENSOR 0x02
#define IFTTT_FLOWSENSOR 0x04
#define IFTTT_WEATHER_UPDATE 0x08
#define IFTTT_REBOOT 0x10
#define IFTTT_STATION_RUN 0x20
#define NOTIFY_PROGRAM_SCHED 0x01
#define NOTIFY_RAINSENSOR 0x02
#define NOTIFY_FLOWSENSOR 0x04
#define NOTIFY_WEATHER_UPDATE 0x08
#define NOTIFY_REBOOT 0x10
#define NOTIFY_STATION_OFF 0x20
#define NOTIFY_STATION_ON 0x40

/** Sensor type macro defines */
#define SENSOR_TYPE_NONE 0x00
Expand Down Expand Up @@ -207,6 +208,8 @@ typedef unsigned long ulong;
#define DEFAULT_JAVASCRIPT_URL "https://ui.opensprinkler.com/js"
#define DEFAULT_WEATHER_URL "weather.opensprinkler.com"
#define DEFAULT_IFTTT_URL "maker.ifttt.com"
#define DEFAULT_MQTT_HOST "localhost"
#define DEFAULT_MQTT_PORT 1883

/** Macro define of each option
* Refer to OpenSprinkler.cpp for details on each option
Expand Down Expand Up @@ -262,6 +265,8 @@ typedef enum {
OPTION_DNS_IP4,
OPTION_SPE_AUTO_REFRESH,
OPTION_IFTTT_ENABLE,
OPTION_MQTT_ENABLE,
OPTION_NOTIFY_TOPIC,
OPTION_SENSOR2_TYPE,
OPTION_SENSOR2_OPTION,
OPTION_RESET,
Expand Down Expand Up @@ -527,6 +532,7 @@ typedef enum {
#define PSTR(x) x
#define strcat_P strcat
#define strcpy_P strcpy
#define sprintf_P sprintf
#define PROGMEM
typedef const char* PGM_P;
typedef unsigned char uint8_t;
Expand Down
Loading

0 comments on commit 971e9b5

Please sign in to comment.