Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: discover other rpi's and setup them #11

Merged
merged 10 commits into from
Feb 8, 2021
8 changes: 8 additions & 0 deletions raspberrypi_central/change_hostname.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/bin/bash

CURRENT_HOSTNAME=$1
NEW_HOSTNAME=$2

sudo hostnamectl set-hostname $NEW_HOSTNAME
sudo sed -i "s/127.0.1.1.*$CURRENT_HOSTNAME/127.0.1.1\t$NEW_HOSTNAME/g" /etc/hosts
sudo reboot
1 change: 1 addition & 0 deletions raspberrypi_central/core/config/mosquitto/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
passwd
23 changes: 23 additions & 0 deletions raspberrypi_central/core/config/mosquitto/mosquitto.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
listener 8883

password_file /mosquitto/config/passwd
allow_anonymous true

# Types of messages to log. Use multiple log_type lines for logging
# multiple types of messages.
# Possible types are: debug, error, warning, notice, information,
# none, subscribe, unsubscribe, websockets, all.
# Note that debug type messages are for decoding the incoming/outgoing
# network packets. They are not logged in "topics".
log_type all


# cafile /mosquitto/config/certs/ca.crt
# keyfile /mosquitto/config/certs/mx-pc.key
# certfile /mosquitto/config/certs/mx-pc.crt

# require_certificate true
# tls_version tlsv1.2

persistence true
persistence_location /mosquitto/data/
Binary file not shown.
2 changes: 1 addition & 1 deletion raspberrypi_central/install.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/bin/bash

sudo apt install -y git vim libffi-dev libssl-dev python python-pip python3-pip pipenv
sudo apt install -y ipcalc nmap git vim libffi-dev libssl-dev python python-pip python3-pip pipenv

# Docker
sudo apt-get remove python-configparser
Expand Down
3 changes: 3 additions & 0 deletions raspberrypi_central/scripts/rpi/get_rpi_model.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/bash

cat /proc/device-tree/model | awk -F 'Rev' '{print $1}'
59 changes: 59 additions & 0 deletions raspberrypi_central/scripts/rpi/network.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
#!/bin/bash

SELF_LAN_IP=$(hostname -I | awk '{print $1}')

echo "My IP in the LAN is: $SELF_LAN_IP"

exclude_my_self="--exclude $SELF_LAN_IP"

gateway_ip=$(ip route show 0.0.0.0/0 | cut -d\ -f3)

# ip/mask, ex: 192.168.1.0/24
network_ip_cidr=$(ipcalc $gateway_ip | grep 'Network' | awk '{print $2}')

for raspberry_pi_ip in `sudo nmap -sn $network_ip_cidr | awk 'f==2{print s; f=s=""}/^(Nmap scan|MAC Address)/{sub(/^.*(for|:..) /,"");f++;s=(s?s OFS :"")$0}END{if(f==2)print s}' | grep 'Raspberry' | grep -o '[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}'`
do
echo "I've found another RPI on the network: $raspberry_pi_ip."

# check if we've already setup ssh for this rpi's
nb_line=$(cat ~/.ssh/config| grep -o $raspberry_pi_ip | wc -l)

# gt 0
if [ "$nb_line" -gt 10 ]; then
echo "The system already knows this device."
else
# connect with SSH
# Change the hostname with the device id. raspberrypi_deviceid
DEVICE_ID=$(cat /proc/sys/kernel/random/uuid | cut -d "-" -f 1)

# Default HOSTNAME given by RPI by default.
CURRENT_HOSTNAME="raspberrypi"
NEW_HOSTNAME="rpi-$DEVICE_ID"

# model=$(ssh pi@mx_rpi 'bash -s' < ./get_rpi_model.sh)

echo "$DEVICE_ID/$raspberry_pi_ip" >> ../../webapp/app/data/new_devices

# echo "This device id is: $DEVICE_ID"
# echo "I'm configuring SSH with keys."

# KEYNAME="id_$DEVICE_ID"

# ssh-keygen -t ed25519 -f ~/.ssh/$KEYNAME
# eval "$(ssh-agent -s)" >> /dev/null
# ssh-add ~/.ssh/$KEYNAME

# ssh-copy-id -i ~/.ssh/$KEYNAME pi@$raspberry_pi_ip

# conf="Host $DEVICE_ID \n\t HostName $raspberry_pi_ip \n\t User pi \n\t IdentityFile ~/.ssh/$KEYNAME \n\t IdentitiesOnly yes"
# echo -e $conf >> ~/.ssh/config

# echo "Use ssh pi@$DEVICE_ID to connect."

# echo "Changing the hostname from $CURRENT_HOSTNAME to $NEW_HOSTNAME"

# ssh pi@$DEVICE_ID 'bash -s' < change_hostname.sh $CURRENT_HOSTNAME $NEW_HOSTNAME
fi
done

# I've found another RPI on the network: rpi-ec6d33df.
3 changes: 2 additions & 1 deletion raspberrypi_central/webapp/app/devices/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ class DeviceAdmin(admin.ModelAdmin):
readonly_fields = ('id',)

admin.site.register(models.DeviceType)
admin.site.register(models.Device, DeviceAdmin)
admin.site.register(models.Device)
admin.site.register(models.Location)
# admin.site.register(SensorInformation)
# admin.site.register(Sensor)
30 changes: 30 additions & 0 deletions raspberrypi_central/webapp/app/house/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# House

## Telegram bot
The system can communicate to the resident through Telegram API.
To setup this communication, you have to:
- Register your telegram bot api token.
- Register the chat_id's that will receive messages.

### Create your Telegram Bot
Please check the [telegram bot documentation](https://core.telegram.org/bots), so every information is up to date.

Basically, you have to:

1) Start a conversation with "BotFather"
2) Create a new bot, by sending to him this message: `/newbot`
3) Tada! Get your token *("HTTP Api")* and save it in the admin.

### Get chat id's
Of course you do not want that everyone can have access to your bot to manage your house and get information about it!


To restrict access, the telegram bot will only send messages and accept messages from `chad_id` that are saved to the database.

To get this information, it can be a little bit tricky, so follow this:

1) With your telegram account, join a conversation with your bot previously created.
2) Go to `https://api.telegram.org/bot<yourtoken>/getUpdates`. You will have the information in `chat.id`, copy it and go to the admin to save it.
3) If you cannot see your chat id, remove your bot from the chat and add it back. Resend the previous request and you will see the chat id.

Voilà!
27 changes: 27 additions & 0 deletions raspberrypi_central/webapp/app/register_devices.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import os
import django

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'hello_django.settings')
django.setup()

from devices.models import Device, DeviceType

FILE = './data/new_devices'

if os.path.isfile(FILE):
with open(FILE) as new_devices:
for new_device in new_devices:
data = new_device.split('/')

device_id = data[0]
device_ip = data[1]
device_type = data[2]

obj, created = DeviceType.objects.get_or_create(
type=device_type,
defaults={'type': device_type},
)

Device(device_id=device_id, device_type=obj)

os.remove(FILE)
8 changes: 8 additions & 0 deletions raspberrypi_central/webapp/setup.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Setup
Please follow these instructions before doing anything! Otherwise you will get errors / crashes.

## House
You have to setup your house configuration. For instance, you have to define the timezone.

## Alarm
You have to setup the alarm status. To do so, just create an entry with the value.