Building MQTT hardware to use this project, Step by Step - SteveCossy/IOT GitHub Wiki

First catch your Pi (with reference to https://www.womensbookshop.co.nz/p/nz-non-fiction-first-catch-your-weka-a-story-of-new-zealand-cooking)
These instructions are written for:

Install Raspbian on your Micro SD card by following the instructions here: https://www.raspberrypi.org/documentation/installation/installing-images/README.md Enable booting to the command line: https://www.raspberrypi.org/documentation/remote-access/ssh/ Insert your Micro SD card. The boot partition will open in File Explorer, probably D: Open a Windows command prompt and run the command 'copy con ssh'. Hit enter then Z:

D:>copy con ssh

^Z

Put the Micro SD card into a working Pi, and follow these instructions to get wifi working if necessary:

https://www.raspberrypi.org/documentation/configuration/wireless/wireless-cli.md

pi@raspberrypi:~ $ sudo cp /etc/wpa_supplicant/wpa_supplicant.conf /boot
pi@raspberrypi:~ $ sudo nano /boot/wpa_supplicant.conf

https://www.raspberrypi.org/forums/viewtopic.php?t=272794

... and add text something like ...

country=NZ

network={      ssid="NewtownCommunity"      psk=18d31932d1a4a7dc722d6cdcf015cce8ab3b3415e2b07b1928ddd3311d749665 }

Use the command wpa_passphrase to create more encrypted PSKs.

This will give you a Pi with a command prompt.

You can plug a screen and keyboard into the Pi to connect to it.  Use this command to get the IP address:

ip addr | grep inet

Your IP address will probably be on the fourth line down.
You will need to know the Pi’s IP address to connect to it.  Once you have that, you can use an SSH client such as PuTTY, Bitvise, or Mobaxterm (in increasing order of complexity) to get a command prompt on your Windows computer.  That will allow you to copy and paste these instructions.

Set up the Environment

Update Raspbian to latest versions

sudo apt-get update

sudo apt-get upgrade

Change the computer name

The default computer name is ‘raspberrypi’ it is recommended to change this name to one that describes what computer is going to be used for.  The name can be letters or numbers or the symbols ‘_’ or ‘-’.  An example is: pi0w-LoRa.  This command will open each of the two files that contain the hostname.  Replace the current hostname with your new one in each file.

pi@pi0w-LoRa:~ $ sudo nano /etc/hostname /etc/hosts

Enable serial port control by Python

Open the Raspberry Pi config program

sudo raspi-config

Select

  • 5 Interfacing Options 
  • P6 Serial
  • No to ‘Would you like a login shell to be accessible over serial?‘
  • Yes to ‘Would you like the serial port hardware to be enabled?’

Reboot your computer

sudo reboot

Add additional required packages

At the time of writing, there are no essential packages required beyond those that come with Raspbian Lite, other than Git

Set up GitHub

pi@raspberrypi:~ $ sudo apt-get install git
clone the IOT repository into your new machine
pi@raspberrypi:~ $ git clone [email protected]:SteveCossy/IOT.git

https://docs.github.com/en/free-pro-team@latest/github/authenticating-to-github/adding-a-new-ssh-key-to-your-github-account

Set up Python

For the setup when these instructions were written, the Python packet manager ‘pip’ was not working properly.  The following commands were used to install the necessary Python libraries for the CayenneMQTT Python package:

sudo apt-get install python3-pip

pip3 install --upgrade pip

python3 -m pip install --user pyserial cayenne.mqtt gpiozero datetime serial requests datetime uuid toml RPi.GPIO

(modified 28 Nov 2020 from: python3 -m pip install --user pyserial cayenne.mqtt datetime serial csv requests datetime time glob uuid sys toml )

Further packages can be installed using: python3 -m pip install --user <package name>

Run the CayenneMQTT Package

For details of how this package works, refer to https://github.com/SteveCossy/IOT/wiki/CayenneMQTT

Computer that reads values from a sensor

This computer will typically be a Raspberry Pi connected to a PicAxe device, or directly to a serial sensor.

Change to the CayenneMQTT folder

pi@pi0w-LoRa:~ $ cd IOT/CayenneMQTT/

Create an Authorisation Settings file.  The numbers required are from your Cayenne Project Dashboard (details are in https://github.com/SteveCossy/IOT/wiki/CayenneMQTT).

python3 /home/pi/IOT/CayenneMQTT/MkAuthSettings.py <Cayenne Username> <Cayenne Password> <Cayenne ClientID>

This creates a text file stored in your home folder, you can read it with a cat command:

cat ~/cayenneMQTT.txt

… or check it with a Python test file

pi@pi0w-LoRa:~/IOT/CayenneMQTT $ python3 RdAuthSettings.py

You can now test the actual Python code

pi@pi0w-LoRa:~/IOT/CayenneMQTT $ python3 Serial_to_MQTT.py

Use CTRL-C to exit once you have confirmed that it works OK

If you want it to run all the time, then it needs to be added to the computer’s Cron Table.  Open the file:

crontab -e

Add the following lines to this file:

@reboot /bin/sleep 60 ; sudo nohup python /home/pi/IOT/CayenneMQTT/Serial_to_MQTT.py

13 00 * * * sudo reboot

Reboot the computer again, it everything should run automatically.

sudo reboot

Add SSH keys if required

https://www.cyberciti.biz/faq/how-to-set-up-ssh-keys-on-linux-unix/

Create keys on client Linux desktop if required

ssh-keygen

Copy public key from client to new Raspberry Pi installation

ssh-copy-id -i $HOME/.ssh/id_rsa.pub [email protected]

⚠️ **GitHub.com Fallback** ⚠️