Raspberry Pi - Light Sensor for Gregg

Part 1: General setup

  • run raspbian-ua-netinst on SD card (default install - server packages of debian buster)
  • SSH in as root…
> /etc/motd
echo rpi-lightsensor > /etc/hostname
sed -i s/pi/rpi-lightsensor/g /etc/hosts
apt-get update && apt-get --no-install-recommends install aptitude

ln -sf /usr/share/zoneinfo/America/Chicago /etc/localtime
echo "America/Chicago" > /etc/timezone
dpkg-reconfigure -f noninteractive tzdata
sed -i -e 's/^# en_US.UTF-8/en_US.UTF-8/' /etc/locale.gen
dpkg-reconfigure -f noninteractive locales

aptitude full-upgrade

aptitude install netcat-traditional screen rsync psmisc file patch ethtool strace tcpdump vim bzip2 xz-utils
aptitude --without-recommends install dnsutils
aptitude install f2fs-tools

aptitude install watchdog
sed -i -e 's/^#watchdog-device/watchdog-device/g' /etc/watchdog.conf
sed -i -e 's/^#max-load-1\t/max-load-1\t/g' /etc/watchdog.conf
echo "# Raspberry Pi doesn't work with the default timeout of 60; set it to 10 instead" >> /etc/watchdog.conf
echo $'watchdog-timeout\t= 10' >> /etc/watchdog.conf

adduser tdobes
adduser tdobes adm
adduser tdobes systemd-journal

aptitude install firmware-brcm80211 # rpi3 wifi firmware
aptitude install ifmetric iw wireless-tools ifplugd wpasupplicant
sed -i -e 's/^REGDOMAIN=$/REGDOMAIN=US/g' /etc/default/crda
sed -i -e 's/^INTERFACES=""/INTERFACES="eth0"/g' /etc/default/ifplugd
sed -i -e 's/^allow-hotplug eth0/#allow-hotplug eth0/g' /etc/network/interfaces
systemctl restart ifplugd

echo >> /etc/network/interfaces
echo 'allow-hotplug wlan0' >> /etc/network/interfaces
echo 'iface wlan0 inet manual' >> /etc/network/interfaces
echo '  wpa-driver nl80211,wext' >> /etc/network/interfaces
echo '  wpa-roam /etc/wpa_supplicant/wpa_supplicant.conf' >> /etc/network/interfaces
echo >> /etc/network/interfaces
echo 'iface wifi inet dhcp' >> /etc/network/interfaces
echo '  metric 1' >> /etc/network/interfaces

cat > /etc/wpa_supplicant/wpa_supplicant.conf <<EOF
ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev

#  ssid="williams-2.4"
#  scan_ssid=1
#  psk="**PASSWORD**"
#  priority=10
#  id_str="wifi"
chmod -x /etc/wpa_supplicant/action_wpa.sh # ensures that ifplugd doesn't disconnect wifi - see http://raspberrypi.stackexchange.com/questions/31780/wpa-supplicant-conf-with-two-network-cards-not-working-when-wpa-roam

# Clean-up and optimization

systemctl disable rsyslog.service
systemctl disable rsync.service
systemctl disable sshswitch.service
systemctl stop rsyslog.service
rm /var/log/auth.log* /var/log/daemon.log* /var/log/debug /var/log/kern.log* /var/log/messages /var/log/user.log* /var/log/syslog*

Part 2: I2C Bus

sed -i -e 's/^#dtparam=i2c_arm=on$/dtparam=i2c_arm=on/' /boot/config.txt
# we don't need to manually specify i2c-bcm2708 in /etc/modules, as it'll be automatically loaded when the device tree overlay is active
echo i2c-dev >> /etc/modules
aptitude --without-recommends install i2c-tools
reboot # to load device tree overlay

adduser tdobes i2c

# look for i2c modules
i2cdetect -y 1
# ...you should see the VEML7700 on I2C address 0x10

Part 3: Python Modules + Custom Code

aptitude install python3 python3-venv python3-requests
aptitude install --without-recommends python3-pip python3-wheel
aptitude markauto python3-wheel

# set up venv:
mkdir -p /opt/veml7700 && chown tdobes:tdobes /opt/veml7700
su - tdobes
python3 -m venv --system-site-packages /opt/veml7700
. /opt/veml7700/bin/activate
pip3 install adafruit-circuitpython-veml7700
cat > /opt/veml7700/print_values.py <<EOF
#!/usr/bin/env python3

import time
import board
import busio
import adafruit_veml7700

DEBUG = True

if __name__ == "__main__":
  i2c = busio.I2C(board.SCL, board.SDA)
  veml7700 = adafruit_veml7700.VEML7700(i2c)

  while True:
    light = veml7700.light
    lux = veml7700.lux
    print('Ambient light:', light)
    print('Lux:', lux)
chmod +x /opt/veml7700/print_values.py

cat > /opt/veml7700/veml7700_to_isy.py <<EOF
#!/usr/bin/env python3

import time
import board
import busio
import adafruit_veml7700
import requests
import xml.etree.ElementTree as ET
import datetime
import os

ISY_CREDS=('williams', '**PASSWORD**')
DATAFILE='/mnt/data/Insteon home automation/brightness_sensor_log.csv'
DEBUG = False

def isy_get_var_id(isy, isy_creds, var_type, var_name):
  req = requests.get('http://%s/rest/vars/definitions/%s' % (isy, var_type), auth=isy_creds)
  var_defs = ET.fromstring(req.text)
  for var_def in var_defs:
    if var_def.attrib['name'] == var_name:
      return int(var_def.attrib['id'])

def write_to_file(value):
  if DEBUG: print('...writing to file...', DATAFILE, value)
    f = open(DATAFILE, 'a+')
    f.seek(0, os.SEEK_END)
    if f.tell() == 0:
      # write header
      if DEBUG: print('writing header to file')
      f.write('Timestamp,Sensor Value\n')
    f.write('%s,%.2f\n' % (datetime.datetime.now().isoformat(timespec='seconds'), value))
    print('WARNING: Unable to write this data to file.  Uh-oh.')

def submit_to_isy(isy_var_id, light):
  submit_vars = (ISY, ISY_VAR_TYPE, isy_var_id, light)
  if DEBUG: print('...submitting to isy...', submit_vars)
    requests.get('http://%s/rest/vars/set/%d/%d/%d' % submit_vars, auth=ISY_CREDS)
    print('WARNING: Unable to submit this data to ISY.  Uh-oh.')

if __name__ == "__main__":
  i2c = busio.I2C(board.SCL, board.SDA)
  veml7700 = adafruit_veml7700.VEML7700(i2c)

  isy_var_id = isy_get_var_id(ISY, ISY_CREDS, ISY_VAR_TYPE, ISY_VAR_NAME)

  lightavg = 0
  lightfileavg = 0
  lightavgitems = 0
  lightfileavgitems = 0
  while True:
    light = veml7700.light
    if DEBUG: print('...ambient light:', light)
    lightavg += light
    lightavgitems += 1
    lightfileavg += light
    lightfileavgitems += 1
    if lightavgitems >= (ISY_SECS_TO_AVERAGE * SAMPLES_PER_SECOND):
      submit_to_isy(isy_var_id, lightavg / lightavgitems)
      lightavg = 0
      lightavgitems = 0
    if lightfileavgitems >= (DATAFILE_SECS_TO_AVERAGE * SAMPLES_PER_SECOND):
      write_to_file(lightfileavg / lightfileavgitems)
      lightfileavg = 0
      lightfileavgitems = 0
chmod +x /opt/veml7700/veml7700_to_isy.py

exit # back to root

Part 4: Network storage (for data export)

mkdir -p /mnt/data
echo >> /etc/fstab
echo $'memoryalpha:/tank/data\t/mnt/data\tnfs\t_netdev,auto,soft\t0\t0' >> /etc/fstab
aptitude install nfs-common
mount /mnt/data

Part 5: Autostart as Service

adduser --system --home /nonexistent --no-create-home --gecos "Light Sensor" sensor
adduser sensor i2c

echo '[Unit]' > /etc/systemd/system/veml7700_to_isy.service
echo 'Description=Send VEML7700 value to ISY' >> /etc/systemd/system/veml7700_to_isy.service
echo 'After=network.target' >> /etc/systemd/system/veml7700_to_isy.service
echo >> /etc/systemd/system/veml7700_to_isy.service
echo '[Service]' >> /etc/systemd/system/veml7700_to_isy.service
echo 'ExecStart=/opt/veml7700/bin/python3 /opt/veml7700/veml7700_to_isy.py' >> /etc/systemd/system/veml7700_to_isy.service
echo 'User=sensor' >> /etc/systemd/system/veml7700_to_isy.service
echo 'Environment=PYTHONUNBUFFERED=1' >> /etc/systemd/system/veml7700_to_isy.service
echo >> /etc/systemd/system/veml7700_to_isy.service
echo 'Restart=always' >> /etc/systemd/system/veml7700_to_isy.service
echo 'RestartSec=1' >> /etc/systemd/system/veml7700_to_isy.service
echo 'StartLimitInterval=10s' >> /etc/systemd/system/veml7700_to_isy.service
echo 'StartLimitBurst=10' >> /etc/systemd/system/veml7700_to_isy.service
echo >> /etc/systemd/system/veml7700_to_isy.service
echo '[Install]' >> /etc/systemd/system/veml7700_to_isy.service
echo 'WantedBy=multi-user.target' >> /etc/systemd/system/veml7700_to_isy.service
systemctl enable veml7700_to_isy.service
systemctl start veml7700_to_isy.service

Wiring Notes

Cable from Raspberry Pi to sensor…

  • Shield + Blk = Ground
  • Red = 3.3V
  • Grn = SCL
  • Wht = SDA
computer/raspberry_pi_light_sensor.txt · Last modified: 2020/07/13 11:22 by tdobes
Recent changes RSS feed Driven by DokuWiki Valid XHTML 1.0 Valid CSS