Skip to content

Code for reporting temperature from sensors attached to Raspberry Pi and to trigger manual alerts via press of a button

License

Notifications You must be signed in to change notification settings

ankurp/thermostat-sensor

Repository files navigation

thermostat-sensor

This repository only contains code and instructions on how to setup the Raspberry Pi device to report temperature/humidity data along with manual alerts to the server. The server code where data is received and saved, notifications are sent, and the entire system configured via the admin portal, can be found on thermostat repo.

Raspberry Pi 3

Requirements

You will need the following items

  1. Raspberry Pi with Wifi
  2. Temperature/Humidity Sensor
  3. Push Buttons
  4. 4GB or bigger Micro SD Card.
  5. 5V Micro USB Power Adapter. You can use any Android Phone/Tablet Charger found in home as well or power it using a USB to MicroUSB cable as well.
  6. Case (Optional to package everything together)

Raspberry Pi/Hardware Setup Instructions

  1. Install Raspbian Image (Operating System to run on Raspberry Pi) from official Raspberry Pi website onto the Micro SD Card. You can find the installation instructions on how to install the Raspbian OS onto the Micro SD Card here
  2. Once you have the OS on the MicroSD Card you should mount it add create an empty called ssh in the /boot volume of the MicroSD card image to remote login via ssh into the Raspberry Pi
  3. Also you can auto connect your Raspberry Pi to your Wifi by creating a wpa_supplicant.conf file in the /boot volume of the MicroSD card image. Enter the credentials to your Wifi in this file by copy pasting the contents below and replacing it with your Wifi name and password.
ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
update_config=1
country=US

network={
	ssid="WIFINAME"
	psk="PASSWORD"
	key_mgmt=WPA-PSK
}
  1. Once OS is installed and remote login and Wifi configured, insert the MicroSD Card in the Raspberry Pi.
  2. Connect the Temperature/Humidity Sensor to the Raspberry Pi by Connecting the Positive (+) end via the jumper cable to the 3V Pin, Connecting the Negative (-) end via the jumper cable to the Ground (GND) Pin and Connecting the Data end via the jumper cable to the GPIO Pin 4 on the Raspberry Pi.
  3. Connect the Push Button to the Raspberry Pi by connecting any one of the jumper cables to GPIO Pin 25 and the other to Ground (GND)
  4. Power up the Raspberry Pi and login via SSH or connect it to a monitor/keyboard/mouse and open the terminal and follow the software installation instructions below

Raspberry Pi 3 with Temperature Sensor and Button Showing Pin Connection

Software Install Instructions

Run the following commands in the Terminal. To remote log in you will need to SSH into the Raspberry Pi after powering it. Make sure you are in the same Wifi network to SSH. The default hostname will be raspberrypi so you can login via ssh [email protected]. The default password is raspberry.

  1. First change the password using passwd
  2. Then install docker using this command curl -sSL https://get.docker.com | sh
  3. Then make the pi user be able to executa commands as sudo user inside of the docker container via this command sudo usermod -aG docker pi
  4. Reboot raspberry pi using sudo shutdown -r now
  5. Then ssh into the raspberry pi again and pull the docker container containing our code docker pull encoreptl/thermostat-sensor:latest
  6. Finally start the container using the command below and it will always run the container even when raspberry pi gets restarted
docker run \
  --privileged \
  --restart=unless-stopped \
  -e PYTHONUNBUFFERED=1 \
  -e SERVER_DOMAIN=thermostat.encoredevlabs.com \
  -e BUTTON_PIN=25 \
  -e TEMP_SENSOR_PIN=4 \
  -e REPORT_INTERVAL=60 \
  -v /sys:/sys \
  -d \
  --name thermostat \
  encoreptl/thermostat-sensor

Configure where to send Temperature Data, Reading Frequency and GPIO PIN numbers

You can configure the container by setting the following environment variables in the docker run command to your liking:

  • SERVER_DOMAIN - Setting this will post the reading to this domain and it can container port number as well
  • REPORT_INTERVAL - Frequency of how often you want to send temperature readings in seconds
  • BUTTON_PIN - Pin number where you have the button installed
  • TEMP_SENSOR_PIN - Pin number where you have the temperature sensor installed

Register Device to Website

You will need to get the MAC address of the Wifi (wlan0) network interface so that you can register the device on the server via the Admin Portal otherwise the server will not accept readings from this sensor.

Hostname

Change hostname of Raspberry Pi if needed to help differentiate from one another.

Remote Access

Setup remote access via SSH using ngrok over the internet by downloading the binary from here: https://bin.equinox.io/c/4VmDzA7iaHb/ngrok-stable-linux-arm.zip

Then unzip it and setup the ngrok yml config file as such:

authtoken: <Get Auth Token after registering on ngrok.com>
json_resolver_url: ""
dns_resolver_ips: []
tunnels:
  ssh:
    proto: tcp
    addr: 22

To run ngrok in background just run the following line /path/to/ngrok start ssh --config=/home/pi/.ngrok2/ngrok.yml > /dev/null &. You can add this to the rc.local file to start ngrok on reboot.

Scheduled Reboot

Setup crontab to reboot every few hours incase there are issues in reporting or being able to connect remotely using ssh via ngrok.

  1. sudo -i
  2. crontab -e
  3. Add the following line 0 * * * * /sbin/shutdown -r now

About

Code for reporting temperature from sensors attached to Raspberry Pi and to trigger manual alerts via press of a button

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages