Tests, configs and setup instructions for our Raspberry Pi - Arduino IoT Smart Garden: Plantly.
Data capturing using Arduino Microcontroller
requirements.txt
Install virtualenv, and create a virtual environment to install dependencies
pip3 install virtualenv
virtualenv venv
Activate the venv (virtual env called venv)
source venv/bin/activate
Install dependencies
pip3 install -r requirements.txt
Running on RaspianOS (Install MQTT)
sudo apt install mosquitto
Ensure that your credentials are assigned to environment variables
You can do this by:
export AWS_ACCESS_KEY=XXXXXXX
export AWS_SECRET_ACCESS_KEY=XXXXXXX
export AWS_DEFAULT_REGION=XXXXXXX
Make sure docker-videostream.sh executable, if its not then run
chmod +x <scriptname>.sh
Refer to 4.1 How to run bridge MQTT Broker to AWS IoT to link the device to IoT Core and establish PUB/SUB communication with Cloud Architecture
Remember to set a .env file
touch .env
vim .env
Copy
BROKER_IP=localhost
BROKER_PORT=1883
Ensure that the Arduino is connected via serial cable, refer to Arduino repo for instructions to deploy controller_main.ino sketch
Pre-req: Arduino must be connected via serial cable, and the .ino file (sensor code) deployed
To push data collected from sensor
python3 main.py
Open up another terminal tab WHY: This method is for DEBUG to see the logs
To run subscriber to receieve commands from Dashboard
python3 startup.py
Open up another terminal tab
*check your environment variables are set for your credentials
./docker-videostream.sh
Now the device is ready to receive commands from the dashboard, to push collected data from sensors & videostreaming is running.
To run the tests, run this command:
pytest
In the terminal of your device
mosquitto_pub -h localhost -p 1883 -q 1 -d -t localgateway_to_awsiot -i clientid1 -m "{\"key\": \"helloFromLocalGateway\"}"
*Note: This will publish the localhost, being the IP Address of the PI, on port 1883, sending a QoS of 1, on the topic localgateway_to_awsiot with the identity of clientid1, sending a JSON Format message
Make sure you install Mosquitto onto the Pi
sudo apt-get install mosquitto && sudo apt-get instal mosquitto-clients
aws configure
aws iot create-policy --policy-name bridge --policy-document '{"Version": "2012-10-17","Statement": [{"Effect": "Allow","Action": "iot:*","Resource": "*"}]}'
cd /etc/mosquitto/certs/
sudo aws iot create-keys-and-certificate --set-as-active --certificate-pem-outfile cert.crt --private-key-outfile private.key --public-key-outfile public.key --region <region>
aws iot list-certificates
aws iot attach-principal-policy --policy-name bridge --principal <arn>
sudo chmod 644 private.key
sudo chmod 644 cert.crt
sudo chmod 644 private.key
sudo touch rootCA.pem
-----BEGIN CERTIFICATE-----
and -----END CERTIFICATE-----
-----BEGIN CERTIFICATE-----
MIIE0zCCA7ugAwIBAgIQGNrRniZ96LtKIVjNzGs7SjANBgkqhkiG9w0BAQUFADCB yjELMAkGA1UEBhMCVVMxFzAVBgNVBAoTDlZlcmlTaWduLCBJbmMuMR8wHQYDVQQL ExZWZXJpU2lnbiBUcnVzdCBOZXR3b3JrMTowOAYDVQQLEzEoYykgMjAwNiBWZXJp U2lnbiwgSW5jLiAtIEZvciBhdXRob3JpemVkIHVzZSBvbmx5MUUwQwYDVQQDEzxW ZXJpU2lnbiBDbGFzcyAzIFB1YmxpYyBQcmltYXJ5IENlcnRpZmljYXRpb24gQXV0 aG9yaXR5IC0gRzUwHhcNMDYxMTA4MDAwMDAwWhcNMzYwNzE2MjM1OTU5WjCByjEL MAkGA1UEBhMCVVMxFzAVBgNVBAoTDlZlcmlTaWduLCBJbmMuMR8wHQYDVQQLExZW ZXJpU2lnbiBUcnVzdCBOZXR3b3JrMTowOAYDVQQLEzEoYykgMjAwNiBWZXJpU2ln biwgSW5jLiAtIEZvciBhdXRob3JpemVkIHVzZSBvbmx5MUUwQwYDVQQDEzxWZXJp U2lnbiBDbGFzcyAzIFB1YmxpYyBQcmltYXJ5IENlcnRpZmljYXRpb24gQXV0aG9y aXR5IC0gRzUwggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAwggEKAoIBAQCvJAgIKXo1 nmAMqudLO07cfLw8RRy7K+D+KQL5VwijZIUVJ/XxrcgxiV0i6CqqpkKzj/i5Vbex t0uz/o9+B1fs70PbZmIVYc9gDaTY3vjgw2IIPVQT60nKWVSFJuUrjxuf6/WhkcIz SdhDY2pSS9KP6HBRTdGJaXvHcPaz3BJ023tdS1bTlr8Vd6Gw9KIl8q8ckmcY5fQG BO+QueQA5N06tRn/Arr0PO7gi+s3i+z016zy9vA9r911kTMZHRxAy3QkGSGT2RT+ rCpSx4/VBEnkjWNHiDxpg8v+R70rfk/Fla4OndTRQ8Bnc+MUCH7lP59zuDMKz10/ NIeWiu5T6CUVAgMBAAGjgbIwga8wDwYDVR0TAQH/BAUwAwEB/zAOBgNVHQ8BAf8E BAMCAQYwbQYIKwYBBQUHAQwEYTBfoV2gWzBZMFcwVRYJaW1hZ2UvZ2lmMCEwHzAH BgUrDgMCGgQUj+XTGoasjY5rw8+AatRIGCx7GS4wJRYjaHR0cDovL2xvZ28udmVy aXNpZ24uY29tL3ZzbG9nby5naWYwHQYDVR0OBBYEFH/TZafC3ey78DAJ80M5+gKv MzEzMA0GCSqGSIb3DQEBBQUAA4IBAQCTJEowX2LP2BqYLz3q3JktvXf2pXkiOOzE p6B4Eq1iDkVwZMXnl2YtmAl+X6/WzChl8gGqCBpH3vn5fJJaCGkgDdk+bW48DW7Y 5gaRQBi5+MHt39tBquCWIMnNZBU4gcmU7qKEKQsTb47bDN0lAtukixlE0kF6BWlK WE9gyn6CagsCqiUXObXbf+eEZSqVir2G3l6BFoMtEMze/aiCKm0oHw0LxOXnGiYZ 4fQRbxC1lfznQgUy286dUV4otp6F01vvpX1FQHKOtw5rDgb7MzVIcbidJ4vEZV8N hnacRHr2lVz2XTIIM6RUthg/aFzyQkqFOFSDX9HoLPKsEdao7WNq
-----END CERTIFICATE-----
Save as rootCA.pem in the same directory as above certificated. You can also create your own CA, please refer to online documentation to IoT Core Credentials.
#Create the configuration file
sudo nano /etc/mosquitto/conf.d/bridge.conf
To find your AWS endpoint, enter aws iot describe-endpoint
#Copy paste the following in the nano editor: # ================================================================= # Bridges to AWS IOT # ================================================================= # AWS IoT endpoint, use AWS CLI 'aws iot describe-endpoint' connection awsiot address XXXXXXXXXX.iot.<region>.amazonaws.com:8883 # Specifying which topics are bridged topic awsiot_to_localgateway in 1 topic localgateway_to_awsiot out 1 topic both_directions both 1 # Setting protocol version explicitly bridge_protocol_version mqttv311 bridge_insecure false # Bridge connection name and MQTT client Id, # enabling the connection automatically when the broker starts. cleansession true clientid bridgeawsiot start_type automatic notifications false log_type all # ================================================================= # Certificate based SSL/TLS support # ----------------------------------------------------------------- #Path to the rootCA bridge_cafile /etc/mosquitto/certs/rootCA.pem # Path to the PEM encoded client certificate bridge_certfile /etc/mosquitto/certs/cert.crt # Path to the PEM encoded client private key bridge_keyfile /etc/mosquitto/certs/private.key
Starts Mosquitto in the background
sudo mosquitto -c /etc/mosquitto/conf.d/bridge.conf –d
Starts Mosquitto with logs (use for DEBUG)
sudo mosquitto -c /etc/mosquitto/conf.d/bridge.conf
Enable Mosquitto to run at startup automatically
sudo chkconfig --level 345 scriptname on
Test by publishing to the topic, going into AWS IoT > Test and subcribing to 'localgate_to_awsiot'
mosquitto_pub -h localhost -p 1883 -q 1 -d -t localgateway_to_awsiot -i clientid1 -m "{\"key\": \"helloFromLocalGateway\"}"