Initial commit
This commit is contained in:
+168
@@ -0,0 +1,168 @@
|
||||
#
|
||||
# Makefile:
|
||||
# wiringPi - Wiring Compatable library for the Raspberry Pi
|
||||
# https://projects.drogon.net/wiring-pi
|
||||
#
|
||||
# Copyright (c) 2012 Gordon Henderson
|
||||
#################################################################################
|
||||
# This file is part of wiringPi:
|
||||
# Wiring Compatable library for the Raspberry Pi
|
||||
#
|
||||
# wiringPi is free software: you can redistribute it and/or modify
|
||||
# it under the terms of the GNU Lesser General Public License as published by
|
||||
# the Free Software Foundation, either version 3 of the License, or
|
||||
# (at your option) any later version.
|
||||
#
|
||||
# wiringPi is distributed in the hope that it will be useful,
|
||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
# GNU Lesser General Public License for more details.
|
||||
#
|
||||
# You should have received a copy of the GNU Lesser General Public License
|
||||
# along with wiringPi. If not, see <http://www.gnu.org/licenses/>.
|
||||
#################################################################################
|
||||
|
||||
|
||||
#DEBUG = -g -O0
|
||||
DEBUG = -O3
|
||||
CC = gcc
|
||||
INCLUDE = -I/usr/local/include,/usr/include/mysql
|
||||
CFLAGS = $(DEBUG) -Wall $(INCLUDE) -Winline -pipe -std=c99
|
||||
|
||||
LDFLAGS = -L/usr/local/lib,/usr/lib/arm-linux-gnueabihf -lmysqlclient -lpthread -lz -lm -lrt -ldl -lconfig
|
||||
LDLIBS = -lwiringPi -lwiringPiDev -lpthread -lm
|
||||
|
||||
# Should not alter anything below this line
|
||||
###############################################################################
|
||||
|
||||
SRC = blink.c blink8.c blink12.c \
|
||||
blink12drcs.c \
|
||||
pwm.c \
|
||||
speed.c wfi.c isr.c isr-osc.c \
|
||||
lcd.c lcd-adafruit.c clock.c \
|
||||
nes.c \
|
||||
softPwm.c softTone.c \
|
||||
delayTest.c serialRead.c serialTest.c okLed.c ds1302.c \
|
||||
lowPower.c \
|
||||
rht03.c piglow.c
|
||||
|
||||
OBJ = $(SRC:.c=.o)
|
||||
|
||||
BINS = $(SRC:.c=)
|
||||
|
||||
all:
|
||||
@cat README.TXT
|
||||
@echo " $(BINS)" | fmt
|
||||
@echo ""
|
||||
|
||||
really-all: $(BINS)
|
||||
|
||||
blink: blink.o
|
||||
@echo [link]
|
||||
@$(CC) -o $@ blink.o $(LDFLAGS) $(LDLIBS)
|
||||
|
||||
blink8: blink8.o
|
||||
@echo [link]
|
||||
@$(CC) -o $@ blink8.o $(LDFLAGS) $(LDLIBS)
|
||||
|
||||
blink12drcs: blink12drcs.o
|
||||
@echo [link]
|
||||
@$(CC) -o $@ blink12drcs.o $(LDFLAGS) $(LDLIBS)
|
||||
|
||||
blink12: blink12.o
|
||||
@echo [link]
|
||||
@$(CC) -o $@ blink12.o $(LDFLAGS) $(LDLIBS)
|
||||
|
||||
speed: speed.o
|
||||
@echo [link]
|
||||
@$(CC) -o $@ speed.o $(LDFLAGS) $(LDLIBS)
|
||||
|
||||
lcd: lcd.o
|
||||
@echo [link]
|
||||
@$(CC) -o $@ lcd.o $(LDFLAGS) $(LDLIBS)
|
||||
|
||||
lcd-adafruit: lcd-adafruit.o
|
||||
@echo [link]
|
||||
@$(CC) -o $@ lcd-adafruit.o $(LDFLAGS) $(LDLIBS)
|
||||
|
||||
clock: clock.o
|
||||
@echo [link]
|
||||
@$(CC) -o $@ clock.o $(LDFLAGS) $(LDLIBS)
|
||||
|
||||
wfi: wfi.o
|
||||
@echo [link]
|
||||
@$(CC) -o $@ wfi.o $(LDFLAGS) $(LDLIBS)
|
||||
|
||||
isr: isr.o
|
||||
@echo [link]
|
||||
@$(CC) -o $@ isr.o $(LDFLAGS) $(LDLIBS)
|
||||
|
||||
isr-osc: isr-osc.o
|
||||
@echo [link]
|
||||
@$(CC) -o $@ isr-osc.o $(LDFLAGS) $(LDLIBS)
|
||||
|
||||
nes: nes.o
|
||||
@echo [link]
|
||||
@$(CC) -o $@ nes.o $(LDFLAGS) $(LDLIBS)
|
||||
|
||||
rht03: rht03.o
|
||||
@echo [link]
|
||||
@$(CC) -o $@ rht03.o $(LDFLAGS) $(LDLIBS)
|
||||
|
||||
pwm: pwm.o
|
||||
@echo [link]
|
||||
@$(CC) -o $@ pwm.o $(LDFLAGS) $(LDLIBS)
|
||||
|
||||
softPwm: softPwm.o
|
||||
@echo [link]
|
||||
@$(CC) -o $@ softPwm.o $(LDFLAGS) $(LDLIBS)
|
||||
|
||||
softTone: softTone.o
|
||||
@echo [link]
|
||||
@$(CC) -o $@ softTone.o $(LDFLAGS) $(LDLIBS)
|
||||
|
||||
delayTest: delayTest.o
|
||||
@echo [link]
|
||||
@$(CC) -o $@ delayTest.o $(LDFLAGS) $(LDLIBS)
|
||||
|
||||
serialRead: serialRead.o
|
||||
@echo [link]
|
||||
@$(CC) -o $@ serialRead.o $(LDFLAGS) $(LDLIBS)
|
||||
|
||||
serialTest: serialTest.o
|
||||
@echo [link]
|
||||
@$(CC) -o $@ serialTest.o $(LDFLAGS) $(LDLIBS)
|
||||
|
||||
okLed: okLed.o
|
||||
@echo [link]
|
||||
@$(CC) -o $@ okLed.o $(LDFLAGS) $(LDLIBS)
|
||||
|
||||
tone: tone.o
|
||||
@echo [link]
|
||||
@$(CC) -o $@ tone.o $(LDFLAGS) $(LDLIBS)
|
||||
|
||||
ds1302: ds1302.o
|
||||
@echo [link]
|
||||
@$(CC) -o $@ ds1302.o $(LDFLAGS) $(LDLIBS)
|
||||
|
||||
piglow: piglow.o
|
||||
@echo [link]
|
||||
@$(CC) -o $@ piglow.o $(LDFLAGS) $(LDLIBS)
|
||||
|
||||
|
||||
.c.o:
|
||||
@echo [CC] $<
|
||||
@$(CC) -c $(CFLAGS) $< -o $@
|
||||
|
||||
clean:
|
||||
@echo "[Clean]"
|
||||
@rm -f $(OBJ) *~ core tags $(BINS)
|
||||
|
||||
tags: $(SRC)
|
||||
@echo [ctags]
|
||||
@ctags $(SRC)
|
||||
|
||||
depend:
|
||||
makedepend -Y $(SRC)
|
||||
|
||||
# DO NOT DELETE
|
||||
+203
@@ -0,0 +1,203 @@
|
||||
/*
|
||||
* th.c capture Temperature and Humidity readings, write them to sql database
|
||||
* https://projects.drogon.net/raspberry-pi/wiringpi/
|
||||
|
||||
rev 1.0 12/01/2013 WPNS built from Gordon Hendersen's rht03.c
|
||||
rev 1.1 12/01/2013 WPNS don't retry, takes too long
|
||||
rev 1.2 12/01/2013 WPNS allow one retry after 3-second delay
|
||||
rev 1.3 12/01/2013 WPNS make cycle time variable
|
||||
rev 1.4 12/01/2013 WPNS add mysql stuff in
|
||||
rev 1.5 12/01/2013 WPNS do 60 second cycle, cleanup, trial run
|
||||
rev 1.6 12/01/2013 WPNS clean up output format
|
||||
rev 1.7 12/02/2013 WPNS allow more retries, minor cleanups
|
||||
rev 1.79 12/04/2013 WPNS release to instructables
|
||||
rev 1.8 01/06/2015 JDE updated code to try and drop anomolous temperature humidity readings before hitting the database
|
||||
rev 1.9 04/06/2015 JDE updated to try and read MySQL configuration from /etc/th.conf file rather than hard coding it
|
||||
rev 1.10 11/06/2015 JDE updated to remove echoing the config options to the th.log file
|
||||
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
#include <wiringPi.h>
|
||||
#include <maxdetect.h>
|
||||
#include <time.h>
|
||||
|
||||
#include <libconfig.h>
|
||||
|
||||
#include <mysql/mysql.h>
|
||||
|
||||
#define RHT03_PIN 7
|
||||
#define CYCLETIME 60
|
||||
#define RETRIES 3
|
||||
|
||||
void finish_with_error(MYSQL *con)
|
||||
{
|
||||
fprintf(stderr, "%s\n", mysql_error(con));
|
||||
mysql_close(con);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
config_t cfg; /*Returns all parameters in this structure */
|
||||
config_setting_t *setting;
|
||||
const char *str1, *sqlServer, *databaseName, *username, *password;
|
||||
|
||||
char *config_file_name = "/etc/th.conf";
|
||||
|
||||
int temp, rh, oldtemp, difftemp ; // temperature and relative humidity readings
|
||||
int loop; // how many times through the loop?
|
||||
time_t oldtime,newtime; // when did we last take a reading?
|
||||
|
||||
char SQLstring[64]; // string to send to SQL engine
|
||||
char TimeString[64]; // formatted time
|
||||
time_t rawtime;
|
||||
struct tm * timeinfo;
|
||||
|
||||
int status; // how did the read go?
|
||||
|
||||
/*Initialization */
|
||||
config_init(&cfg);
|
||||
|
||||
/* Read the file. If there is an error, report it and exit. */
|
||||
if (!config_read_file(&cfg, config_file_name))
|
||||
{
|
||||
printf("\n%s:%d - %s", config_error_file(&cfg), config_error_line(&cfg), config_error_text(&cfg));
|
||||
config_destroy(&cfg);
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* Get the configuration file name. */
|
||||
if (config_lookup_string(&cfg, "filename", &str1))
|
||||
printf("\nFile Type: %s", str1);
|
||||
else
|
||||
printf("\nNo 'filename' setting in configuration file.");
|
||||
|
||||
/*Read the parameter group*/
|
||||
setting = config_lookup(&cfg, "params");
|
||||
if (setting != NULL)
|
||||
{
|
||||
/*Read the server*/
|
||||
if (config_setting_lookup_string(setting, "sqlServer", &sqlServer));
|
||||
else
|
||||
printf("\nERROR: No 'sqlServer' setting in configuration file.");
|
||||
|
||||
/*Read the database*/
|
||||
if (config_setting_lookup_string(setting, "databaseName", &databaseName));
|
||||
else
|
||||
printf("\nERROR: No 'databaseName' setting in configuration file.");
|
||||
|
||||
/*Read the username*/
|
||||
if (config_setting_lookup_string(setting, "username", &username));
|
||||
else
|
||||
printf("\nERROR: No 'username' setting in configuration file.");
|
||||
|
||||
/*Read the password*/
|
||||
if (config_setting_lookup_string(setting, "password", &password));
|
||||
else
|
||||
printf("\nERROR: No 'password' setting in configuration file.");
|
||||
|
||||
printf("\n");
|
||||
}
|
||||
|
||||
/* config_destroy(&cfg); */
|
||||
|
||||
/*MYSQL *con = mysql_init(NULL);
|
||||
if (con == NULL) finish_with_error(con);
|
||||
if (mysql_real_connect(con, sqlServer, username, password, databaseName, 0, NULL, 0) == NULL) finish_with_error(con);
|
||||
sprintf(SQLstring,"SELECT ComputerTime, Temperature, Humidity FROM TempHumid ORDER BY id DESC LIMIT 1");
|
||||
if (mysql_query(con, SQLstring)) finish_with_error(con);
|
||||
MYSQL_RES *result = mysql_store_result(con);
|
||||
if (result == NULL)
|
||||
{
|
||||
finish_with_error(con);
|
||||
}
|
||||
|
||||
int num_fields = mysql_num_fields(result);
|
||||
MYSQL_ROW row;
|
||||
|
||||
row = mysql_fetch_row(result);
|
||||
for(int i = 0; i < num_fields; i++)
|
||||
{
|
||||
printf("%s ", row[i] ? row[i] : "NULL");
|
||||
}
|
||||
printf("\n"); */
|
||||
|
||||
/* THIS IS WHERE THE Temperautre humidity monitoring Code starts - really untidy and needs tidying up */
|
||||
|
||||
temp = rh = loop = 0 ;
|
||||
oldtime = (int)time(NULL);
|
||||
|
||||
wiringPiSetup () ;
|
||||
piHiPri (55) ;
|
||||
|
||||
printf("th.c rev 1.10 11/06/2015 JDE %sCycle time: %i seconds, %i retries\n",ctime(&oldtime),CYCLETIME,RETRIES);
|
||||
fflush(stdout);
|
||||
|
||||
MYSQL *con = mysql_init(NULL);
|
||||
|
||||
if (con == NULL) finish_with_error(con);
|
||||
|
||||
if (mysql_real_connect(con, sqlServer, username, password, databaseName, 0, NULL, 0) == NULL) finish_with_error(con);
|
||||
|
||||
// wait for an interval to start and end
|
||||
printf("Sync to cycletime...");
|
||||
fflush(stdout);
|
||||
while ((((int)time(NULL))%CYCLETIME)) delay(100);
|
||||
oldtime = (int)time(NULL);
|
||||
oldtemp = 500;
|
||||
while (!(((int)time(NULL))%CYCLETIME)) delay(100);
|
||||
printf("\n");
|
||||
fflush(stdout);
|
||||
|
||||
for (;;)
|
||||
{
|
||||
// wait for an interval to start
|
||||
while ((((int)time(NULL))%CYCLETIME)) delay(100);
|
||||
|
||||
/*****************************************************************/
|
||||
|
||||
// read new data
|
||||
temp = rh = -1;
|
||||
loop=RETRIES;
|
||||
|
||||
status = readRHT03 (RHT03_PIN, &temp, &rh);
|
||||
while ((!status) && loop--)
|
||||
{
|
||||
printf("-Retry-");
|
||||
fflush(stdout);
|
||||
delay(3000);
|
||||
status = readRHT03 (RHT03_PIN, &temp, &rh);
|
||||
}
|
||||
|
||||
newtime = (int)time(NULL);
|
||||
// deltime = newtime-oldtime;
|
||||
time(&rawtime);
|
||||
timeinfo = localtime (&rawtime);
|
||||
strftime (TimeString,64,"%F %T",timeinfo);
|
||||
|
||||
fflush(stdout);
|
||||
oldtime = newtime;
|
||||
difftemp = temp - oldtemp;
|
||||
|
||||
printf("Current Temp: %5.1f, Previous Temp: %5.1f, Temperature Difference: %5.1fC\n",(temp / 10.0),(oldtemp / 10.0),(difftemp / 10.0)) ;
|
||||
if (temp > oldtemp && difftemp > 100) {
|
||||
printf("Damn ... the sensor returned a bad reading!\n");
|
||||
}
|
||||
else {
|
||||
sprintf(SQLstring,"INSERT INTO TempHumid VALUES(unix_timestamp(now()),%5.1f,%5.1f,NULL)",(temp / 10.0),(rh / 10.0));
|
||||
if (mysql_query(con, SQLstring)) finish_with_error(con);
|
||||
oldtemp = temp;
|
||||
}
|
||||
|
||||
/*****************************************************************/
|
||||
|
||||
// wait for the rest of that interval to finish
|
||||
while (!(((int)time(NULL))%CYCLETIME)) delay(100);
|
||||
}
|
||||
|
||||
return 0 ;
|
||||
|
||||
config_destroy(&cfg);
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
The stucture of th.c is horrible at the moment. Everything is called from main()
|
||||
|
||||
At some point I'd like to break out the reading of the configuration into a separate sub-routine. The printing of the configureation
|
||||
file details into the log file should also be disabled for a live situation.
|
||||
|
||||
TODO:
|
||||
1. Stop printing out config to the log file.
|
||||
2. Add an option to specify the config file on the command line.
|
||||
3. Add the option to log to the syslog facility.
|
||||
4. Maybe add code here to only start up when MySQL is available, and to wait/retry until this works.
|
||||
@@ -0,0 +1,12 @@
|
||||
//config.txt
|
||||
// Basic Information:
|
||||
filename = "Temperature and Humidity Monitoring Configuration File";
|
||||
|
||||
// Parameters
|
||||
params =
|
||||
{
|
||||
sqlServer = "localhost";
|
||||
databaseName = "Monitoring";
|
||||
username = "USERNAME";
|
||||
password = "PASSWORD";
|
||||
};
|
||||
+87
@@ -0,0 +1,87 @@
|
||||
#!/bin/sh
|
||||
|
||||
### BEGIN INIT INFO
|
||||
# Provides: th
|
||||
# Required-Start: $remote_fs $syslog
|
||||
# Required-Stop: $remote_fs $syslog
|
||||
# Default-Start: 2 3 4 5
|
||||
# Default-Stop: 1
|
||||
# Short-Description: Temperature and Humidity Monitoring Daemon
|
||||
### END INIT INFO
|
||||
|
||||
set -e
|
||||
|
||||
DAEMON="/usr/sbin/th"
|
||||
NAME="th"
|
||||
PATH="/sbin:/bin:/usr/sbin:/usr/bin"
|
||||
LOGFILE="/var/log/th.log"
|
||||
|
||||
test -x "${DAEMON}" || exit 0
|
||||
|
||||
if [ ! -e "${LOGFILE}" ]
|
||||
then
|
||||
touch "${LOGFILE}"
|
||||
chmod 640 "${LOGFILE}"
|
||||
chown root:adm "${LOGFILE}"
|
||||
fi
|
||||
|
||||
|
||||
case "${1}" in
|
||||
start)
|
||||
echo -n "Starting Temperature and Humidity Monitoring Daemon: "
|
||||
sleep 10 # We need to wait until mysqld has started - this is a horrible workaround for now
|
||||
start-stop-daemon --start --background -m --oknodo --pidfile /var/run/th.pid --startas /bin/bash -- -c "exec ${DAEMON} >> ${LOGFILE} 2>&1"
|
||||
|
||||
echo "${NAME}."
|
||||
;;
|
||||
|
||||
stop)
|
||||
echo -n "Stopping Temperature and Humidity Monitoring Daemon: "
|
||||
|
||||
start-stop-daemon --stop --pidfile /var/run/th.pid --oknodo --exec ${DAEMON}
|
||||
rm -f /var/run/th.pid
|
||||
|
||||
echo "${NAME}."
|
||||
|
||||
;;
|
||||
|
||||
restart)
|
||||
echo -n "Stopping Temperature and Humidity Monitoring Daemon: "
|
||||
|
||||
start-stop-daemon --stop --pidfile /var/run/th.pid --oknodo --exec ${DAEMON}
|
||||
rm -f /var/run/th.pid
|
||||
|
||||
echo "${NAME}."
|
||||
echo -n "Starting Temperature and Humidity Monitoring Daemon: "
|
||||
|
||||
start-stop-daemon --start --background -m --oknodo --pidfile /var/run/th.pid --startas /bin/bash -- -c "exec ${DAEMON} >> ${LOGFILE}"
|
||||
|
||||
echo "${NAME}."
|
||||
;;
|
||||
|
||||
status)
|
||||
PID="$(cat /var/run/th.pid 2>/dev/null)" || true
|
||||
|
||||
if [ ! -f /var/run/th.pid ] || [ -z "${PID}" ]
|
||||
then
|
||||
echo "${NAME} is not running"
|
||||
exit 3
|
||||
fi
|
||||
|
||||
if ps "${PID}" >/dev/null 2>&1
|
||||
then
|
||||
echo "${NAME} is running"
|
||||
exit 0
|
||||
else
|
||||
echo "${NAME} is not running"
|
||||
exit 1
|
||||
fi
|
||||
;;
|
||||
|
||||
*)
|
||||
echo "Usage: /etc/init.d/${NAME} {start|stop|restart|status}"
|
||||
exit 1
|
||||
;;
|
||||
esac
|
||||
|
||||
exit 0
|
||||
Reference in New Issue
Block a user