Welcome

Welcome to Christchurch Tech. We are a community of people who have an interest in various tech.

The aim is to bring people, ideas, knowledge, and resources together.

The ability of the group ranges from knowing nothing and wanting to learn through being experts.

We run a meetup on the 3rd Saturday of the month at the Christchurch South Learning Centre.

The scope of what we cover is broad. Some of the things that have come up at the meetings include:

  • building 3d scanners
  • 3d printing a robotic arm
  • building electronics for an airsoft gun
  • development environments
  • getting started with Arduino

So if you have anything tech-related that you want to share or want a hand with, come to a meeting or post on the forums.

Telegram Controlled Led

We will be using an ESP32 running Micropython to talk to Telegram via its API and a bot.

Telegram is like SMS and email combined 

The ESP32 is a powerfull microcontroller with build in wifi

Setting up Telegram

First you’ll need to install Telegram https://telegram.org/

BotFather

Search for the user BotFather

Type /newbot and follow the prompts

Take note of the API token it gives you

Get User Id

Search for the user userinfobot and take note of your user id

ESP32 Setup

Add a file called config.json and save it to the device

{
    "wifi": {
        "ssid": ""
        "password": ""
    },
    "token": "",
    "recipient": ""
}

Add your wifi details, Telegram API token and id

Add a file called boot.py and save it to the device

# This file is executed on every boot (including wake-boot from deepsleep)
import esp
esp.osdebug(None)

import network
import json

with open("config.json", 'r') as f:
    config = json.load(f)
    
print(config)

sta_if = network.WLAN(network.STA_IF)
if not sta_if.isconnected():
    print('connecting to network...')
    sta_if.active(True)
    sta_if.connect(config["wifi"]["ssid"], config["wifi"]["password"])
   
while not sta_if.isconnected():
    pass

print('network config:', sta_if.ifconfig())

Add a file called telegram.py and save it to the device

try:
    import urequests as requests #For MicroPython
except :
    import requests
import json

with open("config.json", 'r') as f:
    config = json.load(f)

offset = "0"
while True:
    #long poll for response
    querry = 'limit=1&offset={}&timeout=10'.format(offset)
    try:
        resp = requests.get(url='https://api.telegram.org/bot{}/{}?{}'.format(config["token"], "getUpdates", querry))
        json = resp.json()
        resp.close()
        if json.get("result"):
            #get latest message
            offset = json.get("result")[0].get("update_id") + 1
            text = json["result"][0]["message"]["text"]
            print(text)
            #echo message
            querry = 'chat_id={}&text={}'.format(config["recipient"], text)
            requests.get(url='https://api.telegram.org/bot{}/{}?{}'.format(config["token"], "sendMessage", querry))
    except Exception as e:
        print(e)
        resp.close() 

Now run it, and send a message to the bot you created, it should echo what ever you sent.

If you type /on it’ll turn the led on. /off will turn it off.

Code can be found here

Nginx Letsencrypt

This is still work in progress

Install Nginx and Certbot

Install Nginx and Certbot

sudo apt update
sudo apt install nginx python3-certbot-nginx

Create a folder for your site. Replace example.com with your domain

sudo mkdir -p /var/www/example.com
sudo nano /var/www/example.com/index.html
<!doctype html>
<html>
<head>
    <title>Hello</title>
</head>
<body>
    <h1>Hello World</h1>
</body>
</html>

Create a config file for your site.

sudo nano /etc/nginx/sites-available/example.com.conf

Put this into the file above. Replace example.com with your domain

server {
       listen 80;
       listen [::]:80;

       server_name example.com;

       root /var/www/example.com;
       index index.html;

       location / {
               try_files $uri $uri/ =404;
       }
}

Enable your site.

sudo ln -s /etc/nginx/sites-available/example.com.conf /etc/nginx/sites-enabled

Check your config is correct

sudo nginx -t

Reload Nginx

sudo systemctl reload nginx

Set up Letsencrypt

sudo certbot --nginx -d example.com

MicroPython with the ESP32

This article will show you how to setup Thonny, an editor that works well with the ESP32, the hardware you’ll need and how to flash Micropython onto the ESP32.

Installing Thonny

Thonny is a Python editor that works well with the ESP32 and works out of the box. Installation instructions can be found on the Thonny home page https://thonny.org/

Getting the Hardware

You will need an ESP32 WROOM-32 30 pin version and 2 breadboards

You can find them on Aliexpress.

ESP32

Breadboard

Setting up the ESP32

First you’ll need to download the firmware fo the ESP32. It can be found here https://micropython.org/download/esp32/ . You’ll want the ESP-IDF v3.x, with support for BLE, LAN and PPP. You’ll want GENERIC unless you have external ram.

Now open Thonny and go to Tools > Manage plug-ins… in the menu. Search for “esptool” and install it.

Plug your ESP32 into you computer.

Now go to Tools > Options > Interpreter.

Select MicroPython(ESP32) from the first box.

Select the port.

Now click on “Install or update firmware” (above OK button).

Select the port. Then click browse to find the firmware you downloaded earlier.

Wait for it to install. Now your ESP32 is ready for its first program.

Blinkin’ Led

from machine import Pin
from time import sleep

led = Pin(2, Pin.OUT)

while True:
    led.on()
    sleep(1)
    led.off()
    sleep(1)

Click the green button with a triangle or go to Run->Run current script, you can also hit F5

Saving

You can either save a file locally or to the device

To save go to File->Save Copy… this will give option to save locally or to the device.

If the code you are running is saved to the device you’ll have to stop the script before saving.

Gotchas

If you are running files locally, if you import or open another file it won’t work. Those files need to be saved to the device. Red button or Ctrl+F2

Linux Commands

Here are some useful Linux commands. It’ll be added to over time. Comment any useful ones you have.

Print disk usage in K/M/G and at a depth of 1

du -h -d 1