Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add MQTT notifications #113

Merged
merged 4 commits into from
Jun 6, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,5 @@ wtopts.txt
*.sh
testmode.h
build-1284/*
.vscode
.pio
27 changes: 23 additions & 4 deletions OpenSprinkler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#include "testmode.h"

/** Declare static data members */
OSMqtt OpenSprinkler::mqtt;
NVConData OpenSprinkler::nvdata;
ConStatus OpenSprinkler::status;
ConStatus OpenSprinkler::old_status;
Expand Down Expand Up @@ -65,7 +66,7 @@ byte OpenSprinkler::attrib_igrd[MAX_NUM_BOARDS];
byte OpenSprinkler::attrib_dis[MAX_NUM_BOARDS];
byte OpenSprinkler::attrib_seq[MAX_NUM_BOARDS];
byte OpenSprinkler::attrib_spe[MAX_NUM_BOARDS];

extern char tmp_buffer[];
extern char ether_buffer[];

Expand Down Expand Up @@ -175,6 +176,7 @@ const char sopt_json_names[] PROGMEM =
"ifkey"
"ssid\0"
"pass\0"
"mqtt\0"
"apass";
*/

Expand Down Expand Up @@ -405,6 +407,7 @@ const char *OpenSprinkler::sopts[] = {
DEFAULT_EMPTY_STRING,
DEFAULT_EMPTY_STRING,
DEFAULT_EMPTY_STRING,
DEFAULT_EMPTY_STRING,
DEFAULT_EMPTY_STRING
};

Expand Down Expand Up @@ -483,14 +486,14 @@ byte OpenSprinkler::start_network() {
wifi_server = new ESP8266WebServer(httpport);
}
}

return 1;
#else

if(start_ether()) {
m_server = new EthernetServer(httpport);
m_server->begin();

Udp = new EthernetUDP();
// Start UDP service for NTP. Avoid the same port with http
if(httpport==8888)
Expand All @@ -499,7 +502,7 @@ byte OpenSprinkler::start_network() {
Udp->begin(8888);
return 1;
}

return 0;

#endif
Expand Down Expand Up @@ -539,6 +542,18 @@ byte OpenSprinkler::start_ether() {
return 1;
}

bool OpenSprinkler::network_connected(void) {
#if defined (ESP8266)
if(m_server) {
return (Ethernet.linkStatus()==LinkON);
} else {
return (get_wifi_mode()==WIFI_MODE_STA && WiFi.status()==WL_CONNECTED && state==OS_STATE_CONNECTED);
}
#else
return (Ethernet.linkStatus()==LinkON);
#endif
}

/** Reboot controller */
void OpenSprinkler::reboot_dev(uint8_t cause) {
lcd_print_line_clear_pgm(PSTR("Rebooting..."), 0);
Expand Down Expand Up @@ -574,6 +589,10 @@ byte OpenSprinkler::start_network() {
return m_server->begin();
}

bool OpenSprinkler::network_connected(void) {
return true;
}

/** Reboot controller */
void OpenSprinkler::reboot_dev(uint8_t cause) {
nvdata.reboot_cause = cause;
Expand Down
6 changes: 6 additions & 0 deletions OpenSprinkler.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#include "utils.h"
#include "gpio.h"
#include "images.h"
#include "mqtt.h"

#if defined(ARDUINO) // headers for ESP8266
#include <Arduino.h>
Expand Down Expand Up @@ -130,6 +131,7 @@ struct ConStatus {
byte sensor2:1; // sensor2 status bit (when set, sensor2 on is detected)
byte sensor1_active:1; // sensor1 active bit (when set, sensor1 is activated)
byte sensor2_active:1; // sensor2 active bit (when set, sensor2 is activated)
byte req_mqtt_restart:1; // request mqtt restart
};

extern const char iopt_json_names[];
Expand All @@ -152,6 +154,8 @@ class OpenSprinkler {
// to handle RPi rev. 1
#endif

static OSMqtt mqtt;

static NVConData nvdata;
static ConStatus status;
static ConStatus old_status;
Expand Down Expand Up @@ -197,6 +201,8 @@ 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 bool network_connected(); // check if the network is up

#if defined(ARDUINO)
static bool load_hardware_mac(byte* buffer, bool wired=false); // read hardware mac address
#endif
Expand Down
6 changes: 3 additions & 3 deletions build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ done
echo "Building OpenSprinkler..."

if [ "$1" == "demo" ]; then
g++ -o OpenSprinkler -DDEMO -m32 main.cpp OpenSprinkler.cpp program.cpp server.cpp utils.cpp weather.cpp gpio.cpp etherport.cpp -lpthread
g++ -o OpenSprinkler -DDEMO -m32 main.cpp OpenSprinkler.cpp program.cpp server.cpp utils.cpp weather.cpp gpio.cpp etherport.cpp mqtt.cpp -lpthread -lmosquitto
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
g++ -o OpenSprinkler -DOSBO main.cpp OpenSprinkler.cpp program.cpp server.cpp utils.cpp weather.cpp gpio.cpp etherport.cpp mqtt.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 main.cpp OpenSprinkler.cpp program.cpp server.cpp utils.cpp weather.cpp gpio.cpp etherport.cpp mqtt.cpp -lpthread -lmosquitto
fi

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

/** IFTTT macro defines */
#define IFTTT_PROGRAM_SCHED 0x01
#define IFTTT_SENSOR1 0x02
#define IFTTT_FLOWSENSOR 0x04
#define IFTTT_WEATHER_UPDATE 0x08
#define IFTTT_REBOOT 0x10
#define IFTTT_STATION_RUN 0x20
#define IFTTT_SENSOR2 0x40
#define IFTTT_RAINDELAY 0x80
/** Notification macro defines */
#define NOTIFY_PROGRAM_SCHED 0x0001
#define NOTIFY_SENSOR1 0x0002
#define NOTIFY_FLOWSENSOR 0x0004
#define NOTIFY_WEATHER_UPDATE 0x0008
#define NOTIFY_REBOOT 0x0010
#define NOTIFY_STATION_OFF 0x0020
#define NOTIFY_SENSOR2 0x0040
#define NOTIFY_RAINDELAY 0x0080
#define NOTIFY_STATION_ON 0x0100

/** HTTP request macro defines */
#define HTTP_RQT_SUCCESS 0
Expand Down Expand Up @@ -222,9 +223,9 @@ enum {
SOPT_IFTTT_KEY,
SOPT_STA_SSID,
SOPT_STA_PASS,
SOPT_MQTT_OPTS,
//SOPT_WEATHER_KEY,
//SOPT_AP_PASS,
//SOPT_MQTT_IP,
NUM_SOPTS // total number of string options
};

Expand Down Expand Up @@ -448,6 +449,7 @@ enum {
#define F(x) x
#define strcat_P strcat
#define strcpy_P strcpy
#define sprintf_P sprintf
#include<string>
#define String string
using namespace std;
Expand Down Expand Up @@ -487,5 +489,3 @@ enum {
#define DISPLAY_MSG_MS 2000 // message display time (milliseconds)

#endif // _DEFINES_H


Loading