diff --git a/.gitignore b/.gitignore index ad2d87be..ea6b5436 100644 --- a/.gitignore +++ b/.gitignore @@ -133,4 +133,7 @@ env/ .vscode/ staticfiles/ -db.sqlite3 \ No newline at end of file +db.sqlite3 + +# Vim temporary files +*.swp diff --git a/hardware/CommunicationsPi/find_port.py b/hardware/CommunicationsPi/find_port.py new file mode 100644 index 00000000..fb739b86 --- /dev/null +++ b/hardware/CommunicationsPi/find_port.py @@ -0,0 +1,130 @@ +#!/usr/bin/env python3 +import sys +import argparse +import serial +import serial.tools.list_ports + +def is_usb_serial(port, args): + if port.vid is None: + return False + if not args.vid is None: + if port.vid != args.vid: + return False + if not args.pid is None: + if port.pid != args.pid: + return False + if not args.vendor is None: + if not port.manufacturer.startswith(args.vendor): + return False + if not args.serial is None: + if not port.serial_number.startswith(args.serial): + return False + if not args.intf is None: + if port.interface is None or not args.intf in port.interface: + return False + return True + + +def extra_info(port): + extra_items = [] + if port.manufacturer: + extra_items.append("vendor '{}'".format(port.manufacturer)) + if port.serial_number: + extra_items.append("serial '{}'".format(port.serial_number)) + if port.interface: + extra_items.append("intf '{}'".format(port.interface)) + if extra_items: + return ' with ' + ' '.join(extra_items) + return '' + +def getPort(): + for port in serial.tools.list_ports.comports(): + if is_usb_serial(port, None): + print(port) + print(port.device) + return + return + +def main(): + """The main program.""" + parser = argparse.ArgumentParser( + prog="find-port.py", + usage="%(prog)s [options] [command]", + description="Find the /dev/tty port for a USB Serial devices", + ) + parser.add_argument( + "-l", "--list", + dest="list", + action="store_true", + help="List USB Serial devices currently connected" + ) + parser.add_argument( + "-s", "--serial", + dest="serial", + help="Only show devices with the indicated serial number", + default=None, + ) + parser.add_argument( + "-n", "--vendor", + dest="vendor", + help="Only show devices with the indicated vendor name", + default=None + ) + parser.add_argument( + "--pid", + dest="pid", + action="store", + help="Only show device with indicated PID", + default=None + ) + parser.add_argument( + "-v", "--verbose", + dest="verbose", + action="store_true", + help="Turn on verbose messages", + default=False + ) + parser.add_argument( + "--vid", + dest="vid", + action="store", + help="Only show device with indicated VID", + default=None + ) + parser.add_argument( + '-i', '--intf', + dest='intf', + action='store', + help='Shows devices which conatin the indicated interface string', + default=None + ) + args = parser.parse_args(sys.argv[1:]) + + if args.verbose: + print('pyserial version = {}'.format(serial.__version__)) + print(' vid =', args.vid) + print(' pid =', args.pid) + print('serial =', args.serial) + print('vendor =', args.vendor) + + if args.list: + detected = False + for port in serial.tools.list_ports.comports(): + if is_usb_serial(port, args): + print('USB Serial Device {:04x}:{:04x}{} found @{}\r'.format( + port.vid, port.pid, + extra_info(port), port.device)) + detected = True + if not detected: + print('No USB Serial devices detected.\r') + return + + for port in serial.tools.list_ports.comports(): + if is_usb_serial(port, args): + print(port) + print(port.device) + return + sys.exit(1) + +if __name__ == "__main__": + main() diff --git a/hardware/CommunicationsPi/server.py b/hardware/CommunicationsPi/server.py new file mode 100644 index 00000000..f0923d9e --- /dev/null +++ b/hardware/CommunicationsPi/server.py @@ -0,0 +1,48 @@ +#!/usr/bin/env python3 +""" +Very simple HTTP server in python for logging requests +Usage:: + ./server.py [] +""" +from http.server import BaseHTTPRequestHandler, HTTPServer +import logging + +class S(BaseHTTPRequestHandler): + def _set_response(self): + self.send_response(200) + self.send_header('Content-type', 'text/html') + self.end_headers() + + def do_GET(self): + logging.info("GET request,\nPath: %s\nHeaders:\n%s\n", str(self.path), str(self.headers)) + self._set_response() + self.wfile.write("GET request for {}".format(self.path).encode('utf-8')) + + def do_POST(self): + content_length = int(self.headers['Content-Length']) # <--- Gets the size of data + post_data = self.rfile.read(content_length) # <--- Gets the data itself + logging.info("POST request,\nPath: %s\nHeaders:\n%s\n\nBody:\n%s\n", + str(self.path), str(self.headers), post_data.decode('utf-8')) + + self._set_response() + self.wfile.write("POST request for {}".format(self.path).encode('utf-8')) + +def run(server_class=HTTPServer, handler_class=S, port=8080): + logging.basicConfig(level=logging.INFO) + server_address = ('', port) + httpd = server_class(server_address, handler_class) + logging.info('Starting httpd...\n') + try: + httpd.serve_forever() + except KeyboardInterrupt: + pass + httpd.server_close() + logging.info('Stopping httpd...\n') + +if __name__ == '__main__': + from sys import argv + + if len(argv) == 2: + run(port=int(argv[1])) + else: + run() diff --git a/hardware/README.md b/hardware/README.md new file mode 100644 index 00000000..5564dc4b --- /dev/null +++ b/hardware/README.md @@ -0,0 +1,16 @@ +# Hardware Firmware + +This directory contains the firmware for data collection. + +Sub-directories and their usage + +- `CommunicationsPi`: files related to radio and internet communications + +- `SensorPi`: files related to Sensors + +- `setup`: files related to initial setup + + +#### Credits + +- [Dave Hylands](https://github.com/dhylands/dotfiles/blob/master/bin/find_port.py) diff --git a/hardware/setup/raspberrypi-common.sh b/hardware/setup/raspberrypi-common.sh index 516727a2..8f15086c 100644 --- a/hardware/setup/raspberrypi-common.sh +++ b/hardware/setup/raspberrypi-common.sh @@ -1,13 +1,63 @@ +#!/bin/bash + function showStatus() { printf "\n${1}\n" } showStatus 'Running Raspberry Pi Common Setup' +cd ~/Downloads + +showStatus 'Updating system...' +sudo apt-get update -y && sudo apt-get upgrade -y && sudo apt-get autoremove -y + showStatus 'Enabling VNC' sudo raspi-config nonint do_vnc 0 showStatus 'Enabling SSH' sudo raspi-config nonint do_ssh 0 -showStatus 'Completed Raspberry Pi Common Setup' +showStatus 'Setting screen resolution for remote access' +sudo raspi-config nonint do_resolution 2 16 + +showStatus 'Installing essentials' + +showStatus 'Installing vim' +sudo apt-get install vim -y + +showStatus 'Installing curl and wget' +sudo apt-get install curl wget -y + +showStatus 'Installing htop' +sudo apt-get install git htop -y + +showStatus 'Installing screen' +sudo apt-get install screen -y + +showStatus 'Installing nmap' +sudo apt-get install nmap -y + +showStatus 'Installing AnyDesk' +wget https://download.anydesk.com/rpi/anydesk_5.5.4-1_armhf.deb +sudo dpkg -i anydesk*.deb +sudo apt-get install -f +sudo systemctl daemon-reload + +cd ~/ + +echo "Completed Raspberry Pi Common Setup + +Your Raspberry Pi is update to date. The following packages were installed and enabled: +- SSH: remote secure shell +- VNC: remote access on the same network, works without internet +- AnyDesk: remote access via internet +- Vim: file editing in SSH +- curl and wget: for file downloads and ping, Rasbian comes with these. +- htop: an interactive process viewer for Unix systems +- screen: virtual terminals to run tasks in background +- nmap: for scanning IP of devices connected in the network + + +NOTE!!! + +- Changing HOSTNAME of Raspberry Pi from raspberrypi would also change url for communication server. Default URL for communication server is http://raspberrypi.local"