MyComm Server Set-up
These are step by step build instructions for the server side of our MyComm Hackaday prize entry.
First up, if you haven’t already you should create an account on Github.com and download git for your preferred operating system.
Now go to the myCommServer repository at: https://github.com/johngrantuk/myCommServer
In the top-right corner, you will see a ‘Fork’ button, click it!
Github will now create a copy of the myCommServer project in your account and will give you read and write access to this copy.
When you login to github.com on the web, you should see your repositories on the right-hand side of the screen. Navigate to your_user_name/myCommServer. You will see the files contained in the repository.
Look for the green “Clone or download” button. Click this and copy the web URL to your clipboard, it should be something like: https://github.com/your_user_name/myCommServer.git
When you create a repository on GitHub, it exists as a remote repository. You can clone your repository to create a local copy on your computer and sync between the two locations.
In a terminal change the current working directory to the location where you want the cloned directory to be made.
Type git clone, and then paste the URL you copied:
Once complete you should see the app files.
Django comes with a lightweight development server that can be used to speed up development. Before we run the server we need to configure a couple more things.
In the terminal change to the app directory:
Create a virtual environment (good practise for working with python packages):
virtualenv -p python3 venv
Activate the environment:
Enter the following command to install all the required python packages:
pip install -r requirements.txt
Now we need to add a .env file with the following config:
rockBlockUsername=Your Iridium Rock7 account name
rockBlockPassword=Your Iridium Rock7 account password
SECRET_KEY=Generate a key: http://www.miniwebtool.com/django-secret-key-generator/
GOOGLE_API_KEY=Get a google API key for maps.
python manage.py migrate
And finally to run the development server:
python manage.py runserver
Now if you browse to http://127.0.0.1:8000/ you should see the front page of the app. At the moment it’s pretty empty because your databases are empty. To add some data start off by creating a super user account so you can log in:
python manage.py createsuperuser
Follow the instructions and remember your username and password.
Now browse to: http://127.0.0.1:8000/admin/ and login. Here you can add a myComm device directly to the database then create test messages that will be displayed on the front page.
This has set you up nicely to develop on your local machine and any code changes can be committed and pushed to your gitHub repository using the normal git commands:
git commit -m “Your message.”
git push -u origin master
But if you have an Iridium/RockBlock device you will want to deploy your app to the internet so you can receive and send messages using the API and also so people can see your app. For this I used Heroku, it has a good free tier and it is pretty nice to use.
Start off by signing up at: https://www.heroku.com/ and install the command line tools: https://devcenter.heroku.com/articles/heroku-command-line.
Once you’ve logged in, return to the Heroku dashboard, click “New” and “Create new app”. Give it a name (this will be the prefix of your free subdomain, YOUR_APP_NAME.herokuapp.com). Choose your region appropriately.
In the Heroku dashboard, browse to the “Deploy” tab and you’ll see a bunch of options for configuring your deployment — we’ll use Github. Select “GitHub” under “Deployment Method” and then click “Connect to GitHub”, following the steps to authorize your application.
Next, input your repository name, hit search, and click “Connect” next to your repository. We won’t deploy just yet, but when we’re ready we’ll click “Deploy Branch” under “Manual Deploy.”
Now browse to the “Settings” tab and under “Buildpacks” add a new buildpack called “heroku/python”. This tells Heroku to install the python tools and run pip install -r requirements.txt when it deploys our server.
Next, under “Config Variables” click “Reveal Config Vars”. This will show which environment variables are configured for your app’s deployment. It starts empty, so you should add a variable for each line in your .env file, except for the DATABSE_URL variable, which Heroku will add for us momentarily.
Heroku doesn’t allow you to have a database stored on the local filesystem, because Heroku machines are stopped and restarted very often, and all local data is wiped. Heroku stores databases in a separate place. To set this up, run the following at the command line.
heroku addons:create heroku-postgresql:hobby-dev -a YOUR_APP_NAME
This will provision a new postgres database for your app. Next we need to add the database url that was just provisioned for us to the Heroku config variables. Copy the output of the following command:
heroku config -a YOUR_APP_NAME
If you didn’t already have a DATABASE_URL config variable in the Heroku dashboard, Heroku will automatically add this one for you. Otherwise you have to manually copy the url.
Now back to the Manual Deploy tab click “Deploy Branch” — Heroku will do it’s thing, pulling the code from your repo and deploying it, you can watch the progress on the build log. It should finish with a “Your app was successfully deployed” message, click the View button to go to your deployed app.
The first time you deploy the app will crash because you need to migrate the database, from the command line run:
heroku run python manage.py migrate — app yourappname
And we should also create a super user as before using:
heroku run python manage.py createsuperuser — app yourappnamb
Now your app is deployed to the Internet!