Django datalogger

Sample datalogger with Django
Django datalogger

Here is a datalogger I have built with an old PCB from a project. The PCB itself has different sensors such as NTC, flow sensors, resistors and leds. I have used the NTCs sensors to retrieve the values every minute and send to a Server.
For this I have used Django and Charisma django, a port from a html template I have made (for more information visit this post).

I placed a sensor outside my window, two inside the room and one inside the radiator. The microcontroller use ADC interrupts to poll the temperature and send it over serialport to a WiFi module. This WiFi module send the data to the server.

Check this project live here

PCB with NTC

How to set up Django, nginx, postgresql and gunicorn

This post is a guideline that I use to set up Django in linux.

Install nginx, virtualenv, pip and postgresql

apt-get install nginx virtualenv python-pip python-dev libpq-dev postgresql postgresql-contrib

Nginx will act as a proxy server and virtualenv will keep different Django’s instances separated.

Create a folder for the web. I will use /opt/DjangoWeb1/ and install there the virtualenv.

mkdir /opt/DjangoWeb1/
cd /opt/DjangoWeb1/
virtualenv venbdjango1

Log with postgres user and launch psql.

su postgres

Create database for Django with postgresql.

CREATE USER djangouser1 WITH PASSWORD 'djangopassword1';
ALTER ROLE djangouser1 SET client_encoding TO 'utf8';
ALTER ROLE djangouser1 SET default_transaction_isolation TO 'read committed';
ALTER ROLE djangouser1 SET timezone TO 'UTC';

Activate the virtual enviroment and install Django, gunicorn and support for postgresql.

source venvdjango1/bin/activate
pip install Django
pip install psycopg2
pip install gunicorn

Start Django project

django-admin startproject DjangoWeb1

Modify settings in order add database we have just created.

cd DjangoWeb1
nano DjangoWeb1/

    'default': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'NAME': 'djangoweb1',
        'USER': 'djangouser1',
        'PASSWORD': 'djangopassword1',
        'HOST': 'localhost',
        'PORT': '',

Migrate the database and create admin user

python makemigrations
python migrate

Add a service to run gunicorn as a webserver. I use systemd with this set up. Create a file  /etc/systemd/system/djangoweb1.service

Description=gunicorn daemon

ExecStart=/opt/DjangoWeb1/venvdjango1/bin/gunicorn --access-logfile - --workers 3 --bind unix:/opt/DjangoWeb1/DjangoWeb1.sock DjangoWeb1.wsgi:application


Enable and activate the service

systemctl enable djangoweb1.service
systemctl start djangoweb1.service

Last step is to redirect web traffic through nginx proxy.

nano /etc/nginx/sites-available/djangoweb1

Set up the config file to point the socket gunicorn is linked to. For static files add in . Also allow all hosts to connect to the site.

STATIC_ROOT = '/opt/DjangoWeb1/DjangoWeb1/static/'

server {
    listen 80;

    location = /favicon.ico { access_log off; log_not_found off; }
    location /static/ {
        root /opt/DjangoWeb1/DjangoWeb1/static;

    location / {
        include proxy_params;
        proxy_pass http://unix:/opt/DjangoWeb1/DjangoWeb1.sock;

Enable the new site by linking in sites-enabled.

cd /etc/nginx/sites-available/
ln -s /etc/nginx/sites-available/djangoweb1 /etc/nginx/sites-enabled/
systemctl restart nginx

Now you can visit the webpage

Welcome Django
Django Website

If you want to create a ftp user to handle the files of the web visit the post How to set up proftd

How to set up proftpd in Linux

This post is a guideline that I use to set up proftpd in a linux server.

 I create a user group for FTP users and assign a specific home folder where the WEB is located.

 I am using Debian Jessy.
First step install package:

apt-get install proftpd-basic

Edit config file and add the following lines

nano /etc/proftpd/proftpd.conf

    RequireValidShell off

DefaultRoot ~ ftpuser

<Limit LOGIN>
    DenyGroup !ftpuser

This way we will handle ftp users by adding them to a group ftpuser without a valid shell. They will also be jailed in the user’s own home directory.

Create new group, user and asign him the home directory. I am using /var/www/wordpress_one as home directory.

addgroup ftpuser
adduser ftpuserone -shell /bin/false -home /var/www/wordpress_one
adduser ftpuserone ftpuser

Change the group permissions of the folder so apache or other web servers can work with it. Apply also the default group for new files created. This is usefull when wordpress or other program create new files.

chown ftpuserone:www-data wordpress_one/
chmod g+s wordpress_one/

Restart the service and test the user

service proftpd restart

ftp -nv
220 ProFTPD 1.3.5b Server (Debian)
ftp> user ftpuserone
331 Password required for ftpuserone
230 User ftpuserone logged in
Remote system type is UNIX.

Now web files can be uploaded over ftp directly.