From 58a1a961d79991641be5b9c3ccec2ef750085d8d Mon Sep 17 00:00:00 2001 From: Cody Hiar Date: Sat, 12 May 2018 18:50:17 -0600 Subject: Initial commit --- dhcp-server.sh | 33 +++++++++++++++++++++++++++++++++ moisture.py | 15 +++++++++++++++ temp.py | 31 +++++++++++++++++++++++++++++++ 3 files changed, 79 insertions(+) create mode 100755 dhcp-server.sh create mode 100644 moisture.py create mode 100644 temp.py diff --git a/dhcp-server.sh b/dhcp-server.sh new file mode 100755 index 0000000..7a84d23 --- /dev/null +++ b/dhcp-server.sh @@ -0,0 +1,33 @@ +#!/bin/bash +# Turn ethernet cable into DHCP device +set -xeuo pipefail + +DEVICE='ens9' +INTERNET='wlp3s0' +ROUTERIP='192.168.123.1' + +echo 'Restarting Device' +ip link set "$DEVICE" down +ip link set "$DEVICE" up +ip addr add "$ROUTERIP"/24 dev "$DEVICE" # arbitrary address + +echo 'Allowing ip forwarding/enabling Nat' +sysctl net.ipv4.ip_forward=1 +iptables -t nat -A POSTROUTING -o "$INTERNET" -j MASQUERADE +iptables -A FORWARD -m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT +iptables -A FORWARD -i "$DEVICE" -o "$INTERNET" -j ACCEPT + +echo 'Creating /etc/dhcpd.conf file' +#Config file: /etc/dhcpd.conf +cat > /etc/dhcpd.conf <<- EOM +option domain-name-servers 1.1.1.1, 1.0.0.1; +option subnet-mask 255.255.255.0; +option routers 192.168.123.100; +subnet 192.168.123.0 netmask 255.255.255.0 { + range 192.168.123.150 192.168.123.250; +} +EOM +#Database file: /var/lib/dhcp/dhcpd.leases +# cat this to see if a device has been assigned a lease +#PID file: /var/run/dhcpd.pid +dhcpd $DEVICE diff --git a/moisture.py b/moisture.py new file mode 100644 index 0000000..c393905 --- /dev/null +++ b/moisture.py @@ -0,0 +1,15 @@ +#!/usr/bin/python3 + +import time + +import RPi.GPIO as GPIO + +moisture_pin = 21 + +GPIO.setmode(GPIO.BCM) +GPIO.setup(moisture_pin, GPIO.IN) + +while True: + print(GPIO.input(moisture_pin)) + time.sleep(1) + diff --git a/temp.py b/temp.py new file mode 100644 index 0000000..da7c310 --- /dev/null +++ b/temp.py @@ -0,0 +1,31 @@ +import glob +import time + +os.system('modprobe w1-gpio') +os.system('modprobe w1-therm') + +base_dir = '/sys/bus/w1/devices/' +device_folder = glob.glob(base_dir + '28*')[0] +device_file = device_folder + '/w1_slave' + +def read_temp_raw(): + f = open(device_file, 'r') + lines = f.readlines() + f.close() + return lines + +def read_temp(): + lines = read_temp_raw() + while lines[0].strip()[-3:] != 'YES': + time.sleep(0.2) + lines = read_temp_raw() + equals_pos = lines[1].find('t=') + if equals_pos != -1: + temp_string = lines[1][equals_pos+2:] + temp_c = float(temp_string) / 1000.0 + temp_f = temp_c * 9.0 / 5.0 + 32.0 + return temp_c, temp_f + +while True: + print(read_temp()) + time.sleep(1) -- cgit v1.2.3