https://www.hostinger.com/tutorials/linux-commands - has a summary of useful commands.
I like the PC version that runs off a USB memory stick in an old laptop.
The Raspberry Pi seems an ideal board for some home logging.
I use it headerless so use:
ssh pi:raspberrypi
or if I have changed the hostname to rpiw.
ssh pi@rpiw
The logger is summarised as:
So I need a script on the Raspberry Pi that:
I need to download the measurements to a PC. I can use:
This has many challenges! This list may explain some of the items on this page.
http://www.dougrice.plus.com/rt004/sr3/Logger.html
Arduino Based Logger And SVG Graph
I brought a module that measures temperature and humidity and some RS-485 dongles.
The XY-MD01 module that supports RS-485 modbus RTU and can be made to stream readings.
- connect to dongle and use a USB lead to supply 5V.
When plugged into the Raspberry Pi, the one below appears as /dev/ttyACM0 and flashes when there is activity.
The other ones appear as /dev/ttyUSB0. So the serial API can be used.
The page below can be used to send and receive commands over the MODBUS.
https://homepages.plus.net/dougrice/dev/modbus/Simpleterm_XY-MD01v2.htm
http://www.dougrice.plus.com/dev/modbus/Simpleterm_XY-MD01v2.htm is the same page. It needs to be connected via HTTPS to read the RS-485 plugged into the laptop's USB port and use EDGE or Chrome.
http://www.dougrice.plus.com/dev/modbus/py_dhr_serial_XY_MD01_pi.py is a python script for my Raspberry Pi. It appends readings to gbFat.js
Add this to cron job on the RaspberryPi, so it run every 15 minutes.
crontab -e
Add:-
#run python script 0,15,30,45 * * * * /usr/bin/python /home/pi/py_dhr_serial_XT_MD01_pi.py
some datasheet links
https://github.com/TronixLab/ModBus-RTU-Temperature-Humidity-SHT20-Sensor
https://www.hestore.hu/prod_getfile.php?id=18062
The XY_MD01 module also has an AUTO mode where it streams readings:-
http://www.dougrice.plus.com/dev/modbus/py_dhr_auto.py is a python script for my Raspberry Pi. It appends readings to gbFaAuto.js. It is work i progress
http://www.dougrice.plus.com/dev/modbus/tt.awk - could be used to split up the captured data.
Reading the DS18B20 temperature chip is made simple on the Raspberry Pi.
Plotting DS18S20 Temperature Readings Using SVG
It would be nice if the RPi could be made to start up logging on Reboot.
Use a Cron job to poll the DS1820 every 15 minutes and log to a file.
crontab -e
Add:-
#run bash script every 15 minutes 0,15,30,45 * * * * /usr/bin/bash /home/pi/log18S20.sh
Maybe limit the size of the file by appending the date in a structured way.
temp_DS_YYYYMMDD.log
I had an idea of storing the samples in a simple function call gbFa( "readings ") in a file ending in .js and putting this file in the web server folder.
A remote webpage could include these:
http://www.dougrice.plus.com/dev/graphs/graphPi.htm
It is is covered in my Guestbook idea is great for a simple experiments.
Or use SCP to get a file called gbFaAuto.js from my raspberryPi with hostname "rpiw"
scp pi@rpiw:~/gbFaAuto.csv .
It is possible run a cron job to poll the DS18B20 temperature chip and log the readings.
Function_Calls
The RaspberryPi is super as it is small and quiet, and can be left turned on.
It is easy to run scripts and compile c programs.
I want them to keep running when I logout.
I thought they get killed when you log out, but it seems that STDOUT get disconnected.
I could write my program to publish to a log file or a status file.
I compile and run helloLoop.c
gcc helloLoop.c -o helloLoop ./helloLoop & exit
Log back in and the process reports the STDIN STDOUT and STDERROR have been deleted.
pi@rpiw:~ $ pgrep -fa hello 3936 ./helloLoop
pi@rpiw:~ $ pgrep -fa hello 3936 ./helloLoop
pi@rpiw:~ $ ls -l /proc/3936/fd total 0 lrwx-- 1 pi pi 64 Dec 28 16:55 0 -> '/dev/pts/0 (deleted)' lrwx-- 1 pi pi 64 Dec 28 16:55 1 -> '/dev/pts/0 (deleted)' lrwx-- 1 pi pi 64 Dec 28 16:55 2 -> '/dev/pts/0 (deleted)'
It does not seem to be simple to login in and run a command to reconnect to a process in the background to get STDOUT and send STDIN.
Here are some tips on how to leave a script running after logging out
Example python script randomDHR.py loops and sleeps.
I want it to keep running after I log out.
Use nano to create script: randomDHR.py
nano randomDHR.py
# # randomDHR.py - generate random number and log # # this writes to a file: "gbFaAuto.csv" # copy this file from my raspberrypi with hostname "rpiw" # # scp pi@rpiw:~/gbFaAuto.csv . #
from time import sleep, gmtime, strftime from random import randint
p1 = 0
while True:
p1 += randint(-1, 1)
p2 = randint(-10, 10)
#print("Random walk:", p1, " just random:", p2)
#build up csv string, wrapped in gbFa( ', p1, p2 , ' )
ts = strftime("%Y-%m-%d %H:%M:%S", gmtime() )
opStr = "gbFa( ' ,"+ ts +" , "+str( p1 ) + "," + str( p2 ) + ", ' )\r\n"
print( opStr )
#Flush buffered readings to file
with open("gbFaAuto.csv", "a") as f:
#flush buffered rea
f.write( opStr )
f.close()
#
opStr = ""
# sleep for 5 seconds
sleep(5)
# end
Download: http://www.dougrice.plus.com/dev/modbus/randomDHR.py
Use cat to review the script:
cat randomDHR.py
Run it using this command:
python randomDHR.py
#== start this in the background and exit()
nohup python randomDHR.py &
The & on the end runs the script in the background and allows you to exit the login.
The printout from the program is disconnected when you exit the login, it seems that the script remains running.
nohup captures the output when you type exit and log out.
In the distant past, you used a telephone and modem to connect to a remote computer.
When the call hangs up, a SIGHUP signal was sent to the process, which could be used to force logout.
#== check if data is updating the file
tail -f gbFaAuto.csv
#== list processes with rand in the filename: randomDHR.py
pgrep rand -a -f
#== kill processes with rand in the filename
pkill -f rand
#== continue processes with rand in the filename randomDHR.py
pkill -SIGCONT -f rand
#== if process is not running start it
pgrep -a -f rand || python randomDHR.py
#== if process is running start another copy
pgrep -a -f rand && python randomDHR.py
#== run python randomDHR.py
Cntl-Z puts in in the background, and sometime stops it.
"kill -SIGCONT pid " can be used to start it.
"pkill -SIGCONT -f rand " can be used to start it.
fg brings it to the foregroud
#== List jobs running in the background
jobs
#== use commands: fg , jobs ,disown to manage the job
Cntrl-z puts the job in the background, jobs lists jobs, fg brings it to the foreground, disown disowns it, if it is stopped use pkill or kill to send SIGCONT to continue.
pkill -l -SIGCONT rand
https://oldforum.puppylinux.com/viewtopic.php?t=115252
sudo busybox httpd -p 0.0.0.0:8086 -h /home/pi/www
I changed the hostname of my raspberry pi to patrickpi
http://patrickpi:8086/
http://patrickpi.local:8086/
The Broadband router, I have has a useful frontpage
http://192.168.1.254/
cron -A super-simple way to run scripts on boot
https://learn.pimoroni.com/article/running-scripts-at-boot
crontab -e
Add:
@reboot python /home/pi/Pimoroni/blinkt/examples/rainbow.py &
# start busybox @reboot sudo busybox httpd -p 0.0.0.0:8086 -h /home/pi/www
#run bash script every 15 minutes 0,15,30,45 * * * * /usr/bin/bash /home/pi/log18S20.sh
#run python script 0,15,30,45 * * * * /usr/bin/python /home/pi/py_dhr_serial_XY_MD01_pi.py
A way to start a web server
A Way to start a web server when the RPi boots:-
A way to start a web server using /etc/rc.local
# Howevery this already has two web servers you can use. # * busybox httpd # * python -m httpd.server 8080 & # # To get it to boot on power up set up /etc/rc.local # # scp pi@patrickpi:/etc/rc.local rc.local.txt # #=================================================
sudo nano /etc/rc.local
#!/bin/sh -e # owned by root # chmod 755 /etc/rc.local #!/usr/bin/ env python # # You could start a webserver using:- # python -m http.server 8080 &
# log last boot time for debug purposes date > /home/pi/rclocal.txt
# log env to file for debug purposes /usr/bin/env >> /home/pi/rclocal.txt # check where busybox is whereis busybox >> /home/pi/rclocal.txt # # Start busybox # sudo /usr/bin/busybox httpd -p 0.0.0.0:8080 -h /home/pi/www
# # Port 80 , index.html is the default page # sudo /usr/bin/busybox httpd -p 0.0.0.0:80 -h /home/pi/www
python dhr.py
sudo nano /lib/systemd/system/dhr.service
sudo systemctl start dhr.service
sudo systemctl stop dhr.service
sudo systemctl enable dhr.service
sudo systemctl disable dhr.service
sudo nano /lib/systemd/system/dhr.service
[Unit] Description=dhrPythonScript. After=network.target
[Service] ExecStart=/usr/bin/python3 /home/pi/dhr.py Restart=always User=pi
[Install] WantedBy=multi-user.target
This will start a webserver. this can be very useful for getting files off the Raspberry Pi.
python -m http.server 8080 --directory /home/pi
sudo nano /home/ip/dhr.py
import http.server import socketserver
PORT = 8080 DIRECTORY = "/home/pi"
class Handler( http.server.SimpleHTTPRequestHandler ): def __init__(self, *args, **kwargs): super().__init__(*args, directory=DIRECTORY, **kwargs)
# Handler = http.server.SimpleHTTPRequestHandler
with socketserver.TCPServer(("", PORT), Handler ) as httpd:
print("serving at port", PORT)
httpd.serve_forever()
Somewhere you need a process that polls the sensors on a timer tick and stores the measurements.
This should happen when the user is not logged in with their laptop or smart phone.
You could use a CRON job on a RPi to poll the NETIOM board to read the state of the pins.
An Arduino could also be used and log the data to an SD Card.
Arduino Based Logger And SVG Graph
The Netiom could remotely read and set outputs but did not seem to support a regular timer tick.
At the time I was using Microchip PIC's in assembler.
http://www.dougrice.plus.com/rt004/sr3/Logger.html - A not very successful attempt to measure the performance of a solar powered light.
Python script randomDHR.py loops and sleeps, I want it to keep running
python randomDHR.py
#== start this in the background and exit()
nohup python randomDHR.py &
#== list processes with rand in the filename
pgrep rand -a -f
#== stop processes with rand in the filename
pkill rand -f
#== if process is not running start it
pgrep -a f rand || python randomDHR.py
#== if process is running start another copy
pgrep -a f rand && python randomDHR.py
This works:-
python3 -m http.server 8080 --directory /home/pi
I cannot find busybox on my old Whezzy SD cards.
The Raspberry Pi User guide by Ben Upton and Gareth Halfacre talked about LAMP
This installed Appache, MySQL and PHP
You can also write PERL scripts, and Matt Wright guestbook script was a base for my Guestbook script
https://www.scriptarchive.com/readme/guestbook.html
I used to have a box that ran Win98
Getting files on and off it was using Floppy Disks.
Running TinyWeb was the simplest way to download a big file from the box.
https://www.ritlabs.com/en/products/tinyweb/
zip up the files and write a minimal web page to allow backup.zip to be downloaded.
I used mawk and could write an awk script to write a web page.
It had IE4 which could be used to upload a file, if I ran TinyWeb on my laptop.
Appache has cgi-bin script. I could not use these on btinternet.
Some simple perl scripts could do simple IOT type webserver.
http://ccgi.dougrice.plus.com/cgi-bin/wiki.pl?ESP01 has some notes on IoT
This PERL script can be started and acts as a wevry simple webserver that responds with a page.
http://www.dougrice.plus.com/dev/tcpip/tcpip_perl/server_iot.pl
Plotting DS18S20 Temperature Readings Using SVG
Arduino Based Logger And SVG Graph
http://www.emacsys.com/index.html is a later version.
A long time ago, in 2012, before the Raspberry Pi I brought one of these:-
http://www.phaedrusltd.com/index.html -
http://www.phaedrusltd.com/acatalog/Netiom_Produtcs.html
Netiom Web enabled boards. Standard Netiom Ref: Netiom-std Netiom is a stand-alone network aware Input / Output module which can be controlled over most networks including the Internet. It can be accessed from the Internet over a standard ADSL or other always on connection making remote monitoring and control a cost effective option.
You could upload a web page of your own design and read the state of switches and analogue inputs.
I uploaded this file so I could include it into my web pages and read the values.
http://www.dougrice.plus.com/dev/js2_new.cgi
ioF( "%31,analogue,%01,%02,%03,%04,"); %99
ioF( "%31,digital1,%05,%06,%07,%08,"); %99
ioF( "%31,digital2,%27,%28,%29,%30,"); %99
ioF( "%31,counts,%11,%12,%13,%14,"); %99
ioF( "%31,digital3,%41,%42,%43,%44,%47,%48,%49,%50,%51,%52,%53,%54,%55,%56,"); %99
ioF( "%31,serial,%00,%12,%13,%14"); %99
http://www.dougrice.plus.com/dev/netiom.svg included the file and try and plot them:-
< script language = "JavaScript" type = "text/javascript" src = "_http://192.168.1.22/js2.cgi" > < / script >
I do not think it was possible to get it to poll the inputs and store in a file until the unit was polled.
Using a Raspberry Pi, this can now be done.
Somewhere you need a process that polls the NETION sensors on a timer tick and stores the measurements.
This should happen when the user is not logged in with their laptop or smart phone.
You could use a CRON job on a RPi to poll the NETIOM board to read the state of the pins.
wget can be used to get a web page.
curses can also be used.
When you use the Imager program to write a MicroSD, you can set up the Router's WiFi login credentials.
I need to add another router.
The raspi-config reported an error on the "Trixie" build. This may work.
Obtain the credetials of the SSID and password possibly from you mobile phone or the card on the router.
sudo nmcli device wifi connect PLUSNET-S1234
nmcli dev wifi connect PLUSNET-S1234 password 1234567890 ifname wlan0
sudo nmcli connection add type wifi con-name "SecondWiFi" ifname wlan0 ssid "YourSecondSSID" wifi-sec.key-mgmt wpa-psk wifi-sec.psk "YourSecondPassword"
Use this if you want to delete a connection.
sudo nmcli connection delete SecondWiFi
https://opensource.com/article/19/11/awk-to-python
Using a batch file called do.bat
I use do.bat to put commands I run often.
rem rem do.bat rem
cd pause
Open some windows command ,
CD to working directory
copy files from Rapsberry Pi
scp <source> <dest>
scp pi@rpiw:capture.csv .
scp pi@rpiw:capture.csv capture_.csv
remote log into Raspberry Pi Zero W
ssh pi@rpiw
list files using tree tree tree - help
list files in the folder ls ls /dev
ls -l ls -l /dev
ls -l /dev/tty*
concaionate contents of files cat readme.txt cat nohup.out
concatonate Serial Port - XY-M01 temp sensor echo "AUTO" > /dev/ttyACM0
date >> serialLog.txt cat /dev/ttyACM0 >> serialLog.txt echo "AUTO" > /dev/ttyACM0
concatonate contents of files more cat readme.txt cat nohup.out cat cat serialLog.txt
more cats file page by page more -h more cat serialLog.txt
edit a file nano -h nano filename nano -l filename
Some ideas. It is not that simple, but the Raspberry Pi and Arduino boards make it much simpler.