The setup of logger's software

	2015/10/19 Hiroshima-gas Technology Research Institute

0.Notifies

These softwares are not necessarily written by English. 
We can not provide English version.
We can not redness your any damage to use these softwares.
 
1.Advance preparation

Hardware is a Raspberry Pi 2 Model B
OS is newest raspbian

Excute below:
sudo raspi-config

Select 8. Advanced Options 
You have to be sure that SSH　i2c spi serial are effective.
if not be effective,there are to be effective.

raspberry Pi 's IP address must be fixed by local private one.

Put to Raspi below pakkages.
apt-get install
(1)	i2c-dev,i2c-tools,ftp,vsftpd,apache2,mutt,msmtp,python3-dev,ntpdate


(2)	pigpio,wiringPi　（read below）

pigpio for pulse counter
---
wget http://abyz.co.uk/rpi/pigpio/pigpio.zip
unzip pigpio.zip
cd PIGPIO
make
sudo make install
---

wiringPi for DHT11 22 event reocrds,shutdown switch
---
sudo apt-get insatll git-core
cd /usr/local/wiringPi
git clone git://git.drogon.net/wiringPi
cd wiringPi
sudo ./build
----

Watch the wiringPi's version
---
gpio -v
---

2.29 is right on Oct.2015 now.
if it is not 2.29,command below:

---
cd /usr/local/wiringPi
sudo git pull origin
sudo ./build
---



2.Unfreeze of zip and compile、copy or move the files

put logger20151019.zip on raspi home directory.

---
unzip logger20151019.zip
---


（１）Compile of C language source codes

----
cd hpzip　　

---
put the source codes on the same directory with alarm_mail8.h 
---

sudo gcc am73.c -o am
sudo gcc pc188.c -o pc -lpigpio -lrt -lpthread
sudo gcc evt27.c -o evt -L/usr/local/lib -lwiringPi
sudo gcc tca7.c -o tca -lm
sudo gcc tes7.c -o tcs
sudo gcc trh39.c -o trh -L/usr/local/lib -lwiringPi
sudo gcc cssp16.c -o cssp


（2）make directory
in （）:permitions

/usr/local/measure (755)
/usr/local/measure/am (755)
/usr/local/measure/pc (755)
/usr/local/measure/tc (755)
/usr/local/measure/trh (755)
/usr/local/measure/ftp (755)
/usr/local/measure/cssp (755)
/usr/local/measure/tempdata (755)
/usr/local/measure/csv (755)
/usr/local/measure/set (755)
/var/www/set (755)
/var/www/set/cgi-bin (755)

(3)copy of files

filename 		 		copy to the directories and file-names
					（permitons）
---------------------------------------------------------------------
am					/usr/local/measure/am/am (700)
pc					/usr/local/measure/pc/pc (700)
evt					/usr/local/measure/pc/evt (700)
tca					/usr/local/measure/tc/tca (700)
tcs					/usr/local/measure/tc/tcs (700)
trh					/usr/local/measure/trh/trh (700)
tcs26.py				/usr/local/measure/tc/tcs.py (600)
amd					/usr/local/measure/am/amd (700)
pcd					/usr/local/measure/pc/pcd (700)
tcad					/usr/local/measure/tc/tcad (700)
tcsd					/usr/local/measure/tc/tcsd (700)
trhd					/usr/local/measure/trh/trhd (700)
amstart.mem				/usr/local/measure/am/amstart.mem (644)
pcstart.mem				/usr/local/measure/pc/pcstart.mem (644)
tcastart.mem				/usr/local/measure/tc/tcastart.mem (644)
tcsstart.mem				/usr/local/measure/tc/tcsstart.mem (644)
trhstart.mem				/usr/local/measure/trh/trhstart.mem (644)
cssp					/usr/local/measure/cssp/cssp (700)
loggerftp1				/usr/local/measure/ftp/loggerftp (700)
other/muttrc				~/.muttrc (600)
other/msmtprc				~/.msmtprc (600)
other/shutdown_daemon			/usr/local/bin/shutdown-daemon	(700)
set/measure.setting			/usr/local/measure/set/measure.setting	(666)			
set/am.setting				/usr/local/measure/set/am.setting (666)
set/pc.setting				/usr/local/measure/set/pc.setting (666)
set/tca.setting				/usr/local/measure/set/tca.setting (666)
set/tcs.setting				/usr/local/measure/set/tcs.setting (666)	
set/trh.setting				/usr/local/measure/set/trh.setting (666)
set/cssp.setting			/usr/local/measure/set/cssp.setting (666)
set/ftp.setting				/usr/local/measure/set/ftp.setting  (666)
set/mail.setting			/usr/local/measure/set/mail.setting (600) 
/www/index5.html			/var/www/index.html  (644)
/www/setting4.html			/var/www/set/setting.html  (644)
/www/setting56.cgi			/var/www/set/cgi-bin/setting.cgi  (755)
/www/sf_run1.cgi			/var/www/set/cgi-bin/sf_run.cgi  (755)
/www/speedy_ftp2.cgi			/var/www/set/cgi-bin/speedy_ftp.cgi  (755)
/www/write_setting36.cgi		/var/www/set/cgi-bin/write_setting.cgi (755)
/www/alarm_log3.cgi			/usr/lib/cgi-bin/alarm_log.cgi (755)
/www/event2.cgi				/usr/lib/cgi-bin/event.cgi (755)
/www/measure67.cgi			/usr/lib/cgi-bin/measure.cgi (755)


(4)Customize of files

Attention:These files are usually changed by CGI.

amstart.mem		Analog measure 	now measureing:1	 not measure:0
pcstart.mem		pulse counter  now measureing:1	 not measure:0
tcastart.mem		Thermo Cuples measure(analog) now measureing:1	 not measure:0
tcsstart.mem		Thermo Cuples measure（SPI） now measureing:1	 not measure:0
trhstart.mem		temprature and relative humidity measure by DHT11 or DHT22 now measureing:1	 not measure:0
measure.setting		,mesaure span time (sec),measure name,span control
am.setting		ChNr.,measure type,Channel name,unit,lower assign,upper assign,alarm type,value of upper alarm,value of lower alarm
			measure type	0:0-5V	1:1-5V
			alarm-type	0:none	1:only upper alarm	2:only lower alarm	3:both upper and lower
pc.setting		ChNr.,measure-type,Channel name,unit,pulse weight,alarm type,value of upper alarm,value of lower alarm
			measure-type	0:pulse counter	8:event recorder(L to H)	9:event recorder(H to L)
			alarm-type  the same as above.
tca.setting		ChNr.,measure-type,Channel name,Span control,alarm type,value of upper alarm,value of lower alarm
			0から7Chの設定状態を上の行から１行ずつ記載しており、各カンマで区切られた値や文字の意味は、
			measure-type	1:K type	2:T type thermo cuples
			alarm-type  the same as above.	
tcs.setting		ChNr.,measure-type,Channel name,alarm type,value of upper alarm,value of lower alarm
			measure-type	1:K type only	(K type exclusive using）
			alarm-type  the same as above.
trh.setting		ChNr.,measure-type,Channel name,alarm type(temperature),value of upper alarm(T),value of lower alarm(T),alarm type(relative Humidity),value of upper alarm(H),value of lower alarm(H)		
温湿度計測（専用センサー）の各チャンネルの計測内容を記憶するファイルです。
			measure-type	11:DHT11	22:DHT22
			alarm-type  the same as above.

cssp.setting		the memories of start-up or stopping instructions.
			0:start-up instruction	1:stopping instruction
			the order of am,pc,tca,tcs,trh
ftp.setting		the memories of rules for uploading FTP servers
			the order of am,pc,tca,tcs,trh
			section,section name,IP address or domain name of external FTP server,login ID,login password,file-name in the logger,the header text file name and pass,file name of alarm log,file name header to exchange,upload-type,terget directory of external FTP severs		
			*** Attentions:
				Usually section,section name,IP address or domain name of external FTP server,login ID,login password,file-name in the logger must not change. "The header text file name and pass" and "file name of alarm log" also not change.	
			upload-type	1:interbnal FTP server only 	2:external server only 	3:both

mail.setting		The first row:	subject of alarm mail
			From the second to forth rows : 	mail address of sending

.muttrc			
			set sendmail="/usr/bin/msmtp"
			set use_from=yes
			set envelope_from="yes"
			set realname="(alias of mail address)"
			set from="(mail address of alarm mail)"
			set charset="utf-8"
			set send_charset="iso-2022-jp:utf-8"
			my_hdr Return-Path:(mail address) 

			in () are need customize

.msmtprc		SMTP server's Configurations	

			# Set default values for all following accounts.
			defaults
			logfile ~/.msmtp.log

			# The SMTP server of the provider(2).
			account provider2
			host (SMTP server Domain)
			port 465 (check your SMTP server）
			from （mail address）
			tls on　(may need to change）
			tls_starttls off　(may need to change）
			tls_certcheck off　(may need to change）
			auth on
			user （SMTP Auth ID)
			password (SMTP Auth password)

			# Set a default account
			account default : provider2

3.Setting of raspbian

(1)vsftpd setting
vsftpd is FTPD.It make internal FTP server in raspi 
------

sudo /etc/init.d/vsftpd stop

***Be backup vsftpd.conf
sudo cp /etc/vsftpd.conf /etc/vsftpd.conf.org

***make the directories
sudo mkdir /etc/vsftpd				for "user_list" and "chroot_list"
sudo mkdir /var/log/vsftpd			for vsftpd_xfer.log

***make user of enabled ftp client connection
exaple:in case "logginguser" ,Any name is OK. 
sudo useradd logginguser
sudo passwd logginguser

sudo mkdir /home/logginguser
sudo chown logginguser:logginguser /home/logginguser

****Edit vsftpd.conf
sudo nano /etc/vsftpd.conf


-------------Edit
anonymous_enable=NO ←　
local_enable=YES　←　
write_enable=YES　←　
local_umask=022　←　
#anon_upload_enable=YES
#anon_mkdir_write_enable=YES
dirmessage_enable=YES　←　
use_localtime=YES　←　
xferlog_enable=YES　←　
connect_from_port_20=YES　←　
#chown_uploads=YES
#chown_username=whoever
xferlog_file=/var/log/vsftpd/vsftpd_xfer.log　←　
xferlog_std_format=YES　←　
#idle_session_timeout=600
#data_connection_timeout=120
#nopriv_user=ftpsecure
#async_abor_enable=YES
ascii_upload_enable=YES　←　
ascii_download_enable=YES　←　
ftpd_banner=Welcome to testftpserver FTP service.　←　any word is OK.
#deny_email_enable=YES
#banned_email_file=/etc/vsftpd.banned_emails
chroot_local_user=YES		←　
chroot_list_enable=YES　←
chroot_list_file=/etc/vsftpd/chroot_list　←
ls_recurse_enable=YES　←　
secure_chroot_dir=/var/run/vsftpd/empty　←　
pam_service_name=vsftpd　←　
rsa_cert_file=/etc/ssl/private/vsftpd.pem　←　

*** addition the below text
 userlist_enable=YES　←　
 userlist_deny=NO　←　
 userlist_file=/etc/vsftpd/user_list　←　
 log_ftp_protocol=NO　←　
 vsftpd_log_file=/var/log/vsftpd.log　←　
 dual_log_enable=YES　←　
 local_root=/home	← 
---------the end of edit

***customize of chroot_list and user_list
sudo nano /etc/vsftpd/chroot_list
***no write

sudo nano /etc/vsftpd/user_list
***Write in first row: logginguser
***and save

sudo /etc/init.d/vsftpd start
sudo reboot

------------------------------

(2)setting of apache2

apache2 is http server package of RaspberryPi.
		
*** Edit of apache2.conf
------
near of row 178

User pi			exchange
Group pi		exchange
ServerName rapsberrypi	Additonal

sudo /etc/init.d/apache2 restart

-------
***browse to input raspberry pi 's ip address
(ex)http://192.168.1.○○○/
It is success to display "It works!"

*** When you set logger-setting,Raspberry Pi request to input the password.

sudo a2enmod auth_digest
ls /etc/apache2/mods-enabled
*** if it have　"auth-digest.load",it works
sudo nano /etc/apache2/sites-enabled/000-default

***Additons below
-----
<Directory "/var/www/set">
	AuthType Digest
	AuthName "Setting Auth User"
	AuthUserFile /etc/apache2/.htdigest
	Require valid-user
</Directory>

<Directory "/var/www/set/cgi-bin/">
	Options ExecCGI
</Directory>
-----


sudo nano /etc/apache2/mods-enabled/mime.conf
#Addhandler cgi-bin .cgi
*** remove #
Addhandler cgi-bin .cgi

sudo /etc/init.d/apache2 restart

***if digest Auth username:hoge,password:hogehogex

sudo htdigest -c /etc/apache2/.htdigest "Setting Auth User"　hoge 
**** try to input password two times
hogehogex


(3) stopping of ntpd

sudo chkconfig ntp off
sudo reboot

***not to be start-up, but it is better that you may remove the ntpd package.
sudo apt-get --purge remove ntp

(4) setting of /etc/rc.local

additions below

-------------
#Pripare shutdown signal
/usr/local/bin/shutdown_daemon &

# RTC module start up
modprobe i2c-dev
#modprobe rtc-ds3232
modprobe rtc-ds1307
#echo ds3231 0x68 > /sys/class/i2c-adapter/i2c-1/new_device &
echo ds1307 0x68 > /sys/class/i2c-adapter/i2c-1/new_device &
sleep 2
hwclock -s

#if raspi connect to WAN,it get the timedata from ntp.
ping -c 5 -n -w 10 8.8.8.8 > /dev/null 2>&1
if [ $? -eq 0 ];then
        ntpdate ntp.nict.jp
        sleep 1
        hwclock -w
        sleep 2
fi

#start-up and stopping measure
/usr/local/measure/cssp/cssp

exit 0
----------------


(5) setting of cron.hourly
sudo nano /etc/cron.hourly/sync-hwclock

***sync-hwclock is a new file,addtions below 

-----
#!/bin/sh
#
# Simple cron script - update and synchronize hardware clock to NTP
hwclock -s

#if raspi connect to WAN,it get the timedata from ntp.
ping -c 1 -n -w 2 8.8.8.8 > /dev/null 2>&1
if [ $? -eq 0 ];then
        ntpdate ntp.nict.jp
        sleep 1
        hwclock -w
fi
--------

sudo nano /etc/cron.hourly/fake-hwclock
*** all rows comment-out by adding #


(5) setting of crontab

sudo crontab -e
***setting upload measure csv data files regularly.

------
# am
 00 00 * * * sudo /usr/local/measure/ftp/loggerftp 0
# pc
 00 00 * * * sudo /usr/local/measure/ftp/loggerftp 1
# tca
 00 00 * * * sudo /usr/local/measure/ftp/loggerftp 2
# tcs
 00 00 * * * sudo /usr/local/measure/ftp/loggerftp 3
# trh
 00 00 * * * sudo /usr/local/measure/ftp/loggerftp 4
------
*** in above examples:upload on 00:00 every day.

4.How to use

You browse to input the IP address of raspberry pi.
You can find to use operations almost by browsing.

5.Known problems

We are certain to be can work things out in the next version.

(1) The measure of analog thermo cuples is unstable.
(2) The change of setting immediately is not refrect to show browser.
	Now,It became effective to stop and start up the measure,or to upload data to FTP server.
(3) Though you watch the opening browser,You can not understand measuring or not measuring in present time.


6.Raspberry pi 's GPIO using map

pinNr.	name		logger-assign
--------------------------------------
1	3.3V		not in use
2	5V		not in use
3	i2C SDA		i2C SDA(RTC DS1307+)
4	5V		not in use
5	i2C SCL		i2C SCL(RTC DS1307+)
6	GND		not in use
7	GPCLK0		not in use
8	UART_TXD	serial connection RX
9	GND		common graund(both raspi and logger)
10	UART_RXD	serial connection TX
11	GPIO17		shutdown switch
12	GPIO18		reserve port
13	GPIO27		reserve port
14	GND		not in use
15	GPIO22		not in use
16	GPIO23		TRH_Ch0
17	3.3V		not in use
18	GPIO24		Trh_Ch1
19	SPI_MOSI	SPI for AM and TCA
20	GND		not in use
21	SPI_MISO	SPI for AM and TCA
22	GPIO25		TCS_Ch0 cs
23	SPI_SCLK	SPI for AM and TCA
24	SPI_CE0		SPI for AM cs
25	GND		not in use
26	SPI_CE1		SPI for TCA cs
27	ID_SD		not in use
28	ID_SC		not in use
29	GPIO5		TCS_Ch1 cs
30	GND		not in use
31	GPIO6		TCS_Ch2 cs
32	GPIO12		PC_ch0
33	GPIO13		TCS_Ch3 cs
34	GND		not in use
35	GPIO19		TCS Do
36	GPIO16		PC_Ch1
37	GPIO26		TCS_Clk
38	GPIO20		PC_Ch2
39	GND		not in use
40	GPIO21		PC_ch3



