Skip to content

Commit

Permalink
Minor updates
Browse files Browse the repository at this point in the history
  • Loading branch information
shaun feakes authored and shaun feakes committed Jul 21, 2019
1 parent b6b96e1 commit 9648433
Show file tree
Hide file tree
Showing 17 changed files with 415 additions and 191 deletions.
13 changes: 5 additions & 8 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ CFLAGS = $(GCCFLAGS) -I. -I./minIni $(DBG) $(LIBS) -D MG_DISABLE_MD5 -D MG_DISAB

# define the C source files
SRCS = sprinkler.c utils.c config.c net_services.c json_messages.c zone_ctrl.c sd_cron.c mongoose.c minIni/minIni.c $(sd_GPIO_C)
TSRC = test.c config.c utils.c minIni/minIni.c

# define the C object files
#
Expand All @@ -38,11 +37,11 @@ TSRC = test.c config.c utils.c minIni/minIni.c
# with the .o suffix
#
OBJS = $(SRCS:.c=.o)
TOBJ = $(TSRC:.c=.o)

# define the executable file
MAIN = ./release/sprinklerd
TEST = ./release/testing
GMON = ./release/gpio_monitor
GPIO = ./release/gpio

#
# The following part of the makefile is generic; it can be used to
Expand All @@ -58,11 +57,9 @@ all: $(MAIN)
$(MAIN): $(OBJS)
$(CC) $(CFLAGS) $(INCLUDES) -o $(MAIN) $(OBJS) $(LFLAGS) $(LIBS)

test: $(TEST)
@echo: $(TEST) have been compiled

$(TEST): $(TOBJ)
$(CC) $(CFLAGS) $(INCLUDES) -o $(TEST) $(TOBJ) $(LFLAGS) $(LIBS)
gpio_tools:
$(CC) -o $(GMON) sd_GPIO.c -lm -lpthread -D GPIO_MONITOR
$(CC) -o $(GPIO) sd_GPIO.c -lm -lpthread -D GPIO_RW

# this is a suffix replacement rule for building .o's from .c's
# it uses automatic variables $<: the name of the prerequisite of
Expand Down
67 changes: 61 additions & 6 deletions config.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@
#include <wiringPi.h>
#define PIN_CFG_NAME "WPI_PIN"
#else
#include "sd_GPIO.h"
//#include "sd_GPIO.h"
#define PIN_CFG_NAME "GPIO_PIN"
#endif

#include "sd_GPIO.h"
#include "minIni.h"
#include "utils.h"
#include "config.h"
Expand Down Expand Up @@ -308,13 +309,13 @@ void readCfg(char *inifile)
_sdconfig_.zonecfg[i].default_runtime = ini_getl(str, "DEFAULT_RUNTIME", 10, inifile);
//ini_gets(str, "NAME", NULL, _sdconfig_.zonecfg[idx].name, sizearray(_sdconfig_.zonecfg[idx].name), inifile);
ini_gets(str, "NAME", NULL, _sdconfig_.zonecfg[i].name, sizearray(_sdconfig_.zonecfg[i].name), inifile);
#ifndef USE_WIRINGPI
//#ifndef USE_WIRINGPI
if ( ! validGPIO(pin) ) {
logMessage (LOG_ERR, "GPIO %d is not valid, found in ZONE:%d of configuration file %s \n",pin, i, inifile);
pin = GPIO_MAX; // Set pin to MAX so we can continue to run if error is not fixed.
sprintf(_sdconfig_.zonecfg[i].name, "ERROR in cfg");
logMessage (LOG_ERR, "GPIO pin %d is not valid, found in ZONE:%d of configuration file %s \n",pin, i, inifile);
pin = -1;
sprintf(_sdconfig_.zonecfg[i].name, "ERROR");
}
#endif
//#endif
/*
logMessage (LOG_DEBUG,"Zone Config : %s\n%25s : %d\n%25s : %d\n%25s : %d\n%25s : %d\n%25s : %d\n",
_sdconfig_.zonecfg[i].name,
Expand Down Expand Up @@ -354,6 +355,60 @@ void readCfg(char *inifile)
logMessage (LOG_ERR," no config zones set\n");
exit (EXIT_FAILURE);
}

// Caculate how many inputs we have
for (i=1; i <= 24; i++) // 24 = Just some arbutary number (max GPIO without expansion board)
{
sprintf(str, "INPUT:%d", i);
pin = ini_getl(str, PIN_CFG_NAME, -1, inifile);
if (pin == -1)
break;
else
_sdconfig_.inputs = i;
}

logMessage (LOG_DEBUG, "Found %d INPUTS\n", _sdconfig_.inputs);

if ( _sdconfig_.inputs != 0) {
// n= _sdconfig_.zones+1;
_sdconfig_.inputcfg = malloc((_sdconfig_.inputs + 1) * sizeof(struct GPIOcfg));
for (i=0; i < _sdconfig_.inputs; i++)
{
sprintf(str, "INPUT:%d", i+1);
pin = ini_getl(str, PIN_CFG_NAME, -1, inifile);
if (! validGPIO(pin) ) {
logMessage (LOG_ERR, "GPIO pin %d is not valid, found in INPUT:%d of configuration file %s \n",pin, i+1, inifile);
continue;
}
logMessage (LOG_DEBUG, "INPUT = %d\n", i+1);

_sdconfig_.inputcfg[i].input_output = INPUT; // Zone is always input
_sdconfig_.inputcfg[i].receive_mode = BOTH; // Zone always needs trigger on both (high or low)
_sdconfig_.inputcfg[i].zone = i+1;
_sdconfig_.inputcfg[i].pin = pin;
_sdconfig_.inputcfg[i].on_state = ini_getl(str, "GPIO_ON_STATE", NO, inifile);
//_sdconfig_.inputcfg[i].startup_state = !_sdconfig_.inputcfg[i].on_state;
//_sdconfig_.inputcfg[i].shutdown_state = !_sdconfig_.inputcfg[i].on_state;

_sdconfig_.inputcfg[i].set_pull_updown = ini_getl(str, "GPIO_PULL_UPDN", -1, inifile);
_sdconfig_.inputcfg[i].dz_idx = ini_getl(str, "DOMOTICZ_IDX", -1, inifile); // Not used at the moment.
ini_gets(str, "NAME", NULL, _sdconfig_.inputcfg[i].name, sizearray(_sdconfig_.inputcfg[i].name), inifile);

_sdconfig_.inputcfg[i].command_on = malloc(COMMAND_SIZE * sizeof(char));
_sdconfig_.inputcfg[i].command_off = malloc(COMMAND_SIZE * sizeof(char));
ini_gets(str, "COMMAND_ON", NULL, _sdconfig_.inputcfg[i].command_on, COMMAND_SIZE, inifile);
ini_gets(str, "COMMAND_OFF", NULL, _sdconfig_.inputcfg[i].command_off, COMMAND_SIZE, inifile);

logMessage (LOG_DEBUG,"Input Config : %s\n%25s : %d\n%25s : %d\n%25s : %d\n%25s : %d\n",
_sdconfig_.inputcfg[i].name,
"PIN",_sdconfig_.inputcfg[i].pin,
"Set pull up/down", _sdconfig_.inputcfg[i].set_pull_updown,
"ON state", _sdconfig_.inputcfg[i].on_state,
"Domoticz IDX", _sdconfig_.inputcfg[i].dz_idx);
}
}


/*
idx=0;
pin=-1;
Expand Down
4 changes: 4 additions & 0 deletions config.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ struct GPIOcfg {
//int ignore_requests;
int zone;
int default_runtime;
char *command_on;
char *command_off;
//bool master_valve;
//struct GPIOextra *extra;
};
Expand Down Expand Up @@ -74,6 +76,7 @@ struct sprinklerdcfg {
bool enableMQTTdz;
bool enableMQTTaq;
int zones;
int inputs;
//int pincfgs;
bool calendar;
bool delay24h;
Expand All @@ -84,6 +87,7 @@ struct sprinklerdcfg {
float precipInchDelay2day;
struct DZcache *dz_cache;
struct GPIOcfg *zonecfg;
struct GPIOcfg *inputcfg;
//struct GPIOcfg *gpiocfg;
struct CALENDARday cron[7];
//time_t cron_update;
Expand Down
11 changes: 8 additions & 3 deletions json_messages.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ int build_sprinkler_cal_JSON(char* buffer, int size)
for (day=0; day <= 6; day++) {
if (_sdconfig_.cron[day].hour >= 0 && _sdconfig_.cron[day].minute >= 0) {
length += sprintf(buffer+length, ", \"d%d-starttime\" : \"%.2d:%.2d\" ",day,_sdconfig_.cron[day].hour,_sdconfig_.cron[day].minute);
for (zone=0; zone < _sdconfig_.zones; zone ++) {
for (zone=1; zone < _sdconfig_.zones; zone ++) {
if (_sdconfig_.cron[day].zruntimes[zone] >= 0) {
length += sprintf(buffer+length, ", \"d%dz%d-runtime\" : %d",day,zone+1,_sdconfig_.cron[day].zruntimes[zone]);
//logMessage(LOG_DEBUG, "Zone %d, length %d limit %d\n",zone,length,size);
Expand Down Expand Up @@ -105,6 +105,7 @@ int build_advanced_sprinkler_JSON(char* buffer, int size)
int i, day;
memset(&buffer[0], 0, size);
int length = 0;
bool cal = false;
/*
length += sprintf(buffer+length, "{ \"title\" : \"%s\",\"calendar\" : \"%s\", \"24hdelay\" : \"%s\", \"allz\" : \"%s\", \"#zones\" : %d, \"24hdelay-offtime\" : %li",
_sdconfig_.name,
Expand Down Expand Up @@ -146,8 +147,9 @@ int build_advanced_sprinkler_JSON(char* buffer, int size)

for (day=0; day <= 6; day++) {
if (_sdconfig_.cron[day].hour >= 0 && _sdconfig_.cron[day].minute >= 0) {
cal = true;
length += sprintf(buffer+length, "\"day %d\" : { \"start time\" : \"%.2d:%.2d\", ",day,_sdconfig_.cron[day].hour,_sdconfig_.cron[day].minute);
for (i=0; i < _sdconfig_.zones; i ++) {
for (i=1; i < _sdconfig_.zones; i ++) {
if (_sdconfig_.cron[day].zruntimes[i] >= 0) {
length += sprintf(buffer+length, "\"Zone %d\" : %d,",i+1,_sdconfig_.cron[day].zruntimes[i]);
//logMessage(LOG_DEBUG, "Zone %d, length %d limit %d\n",i+1,length,size);
Expand All @@ -157,8 +159,11 @@ int build_advanced_sprinkler_JSON(char* buffer, int size)
length += sprintf(buffer+length, "},");
}
}
length -= 1;
if (cal) {
length -= 1;
}
length += sprintf(buffer+length, "}}");


buffer[length] = '\0';
return strlen(buffer);
Expand Down
2 changes: 0 additions & 2 deletions net_services.c
Original file line number Diff line number Diff line change
Expand Up @@ -197,8 +197,6 @@ void publish_zone_mqtt(struct mg_connection *nc, struct GPIOcfg *gpiopin) {
static char mqtt_topic[250];
static char mqtt_msg[50];

printf("PUBLISH pin %d\n",gpiopin->pin);

if (_sdconfig_.enableMQTTaq == true) {
// sprintf(mqtt_topic, "%s/%s", _sdconfig_.mqtt_topic, gpiopin->name);
sprintf(mqtt_topic, "%s/zone%d", _sdconfig_.mqtt_topic, gpiopin->zone);
Expand Down
Binary file added release/gpio
Binary file not shown.
Binary file added release/gpio_monitor
Binary file not shown.
Binary file modified release/sprinklerd
Binary file not shown.
55 changes: 33 additions & 22 deletions release/sprinklerd.test.conf
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,17 @@ NAME=My Sprinklers
DOCUMENTROOT = /nas/data/Development/Raspberry/SprinklerD/web/
CACHE = /var/cache/sprinklerd.cache
# The log level. [DEBUG, INFO, NOTICE, WARNING, ERROR]
LOG_LEVEL = DEBUG
#LOG_LEVEL = NOTICE
#LOG_LEVEL = DEBUG
#LOG_LEVEL = NOTICE
LOG_LEVEL = INFO

# mqtt stuff
MQTT_ADDRESS = trident:1883
#MQTT_USER = someusername
#MQTT_PASSWD = somepassword
MQT_TOPIC = sd_test
#MQTT_DZ_PUB_TOPIC = domoticz/in
#MQTT_DZ_SUB_TOPIC = domoticz/out
MQTT_DZ_PUB_TOPIC = domoticz/in
MQTT_DZ_SUB_TOPIC = domoticz/out

DZIDX_CALENDAR = 197
DZIDX_24HDELAY = 198
Expand All @@ -39,51 +40,61 @@ DZIDX_RAINSENSOR = 48

# Don't use ZONE:0 for anything other than master valve, if you don't have a master valve simply delete it and start from ZONE:1
[ZONE]
#[ZONE:0]
#NAME=Master Valve
#MASTER_VALVE=1
#GPIO_PIN=17
#WPI_PIN=0
#GPIO_PULL_UPDN=1
#GPIO_ON_STATE=0
[ZONE:0]
NAME=Master Valve
MASTER_VALVE=1
GPIO_PIN=2
WPI_PIN=0
GPIO_PULL_UPDN=1
GPIO_ON_STATE=0

[ZONE:1]
NAME=Island
DEFAULT_RUNTIME=10
#GPIO_PIN=18
GPIO_PIN=2
GPIO_PIN=3
WPI_PIN=1
GPIO_PULL_UPDN=1
GPIO_ON_STATE=0
DOMOTICZ_IDX=200
DOMOTICZ_IDX=2000

[ZONE:2]
NAME=Driveway
DEFAULT_RUNTIME=10
#GPIO_PIN=27
GPIO_PIN=3
GPIO_PIN=40
WPI_PIN=2
GPIO_PULL_UPDN=1
GPIO_ON_STATE=0
DOMOTICZ_IDX=201
DOMOTICZ_IDX=2010

[ZONE:3]
NAME=Diningroom Flowerbeds
DEFAULT_RUNTIME=10
GPIO_PIN=22
GPIO_PIN=5
WPI_PIN=3
GPIO_PULL_UPDN=1
GPIO_ON_STATE=0
DOMOTICZ_IDX=202
DOMOTICZ_IDX=2020

[ZONE:4]
NAME=Diningroom error
DEFAULT_RUNTIME=10
GPIO_PIN=28
[INPUT]
[INPUT:1]
NAME=Test Switch
GPIO_PIN=6
WPI_PIN=3
GPIO_PULL_UPDN=1
GPIO_ON_STATE=0
DOMOTICZ_IDX=202
COMMAND_ON=/usr/bin/curl -s -o /dev/null 'http://localhost?type=option&option=24hdelay&state=on'
COMMAND_OFF=/usr/bin/curl -s -o /dev/null 'http://localhost?type=option&option=24hdelay&state=off'

#[ZONE:4]
#NAME=Diningroom error
#DEFAULT_RUNTIME=10
#GPIO_PIN=28
#WPI_PIN=3
#GPIO_PULL_UPDN=1
#GPIO_ON_STATE=0
#DOMOTICZ_IDX=2020

#[ZONE:4]
#NAME=Front Flowerbeds
Expand Down
Loading

0 comments on commit 9648433

Please sign in to comment.