Skip to content

Commit

Permalink
Merge pull request #7 from wizzdev-pl/devel
Browse files Browse the repository at this point in the history
Merge-devel-v1.0.3
  • Loading branch information
marcin-nawrocki-wizzdev authored Feb 22, 2021
2 parents 5fbc33d + 9247e64 commit 5218f97
Show file tree
Hide file tree
Showing 15 changed files with 102 additions and 55 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,4 @@ commit_hash.txt
#*moc*
*.backup*
*LOCAL*
!locals.tf
16 changes: 8 additions & 8 deletions MicroPython/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
### To develop in PyCharm:
1. Create and activate virtual environment with Python 3.6 using PyCharm GUI
or do it manually.
````
````bash
cd Micropython
virtualenv venv
pip3 install -r requirements.txt
Expand All @@ -19,7 +19,7 @@ pip3 install -r requirements.txt

### AWS and terraform
Make sure that your AWS cloud is configured. For more information please go to
ReadMe in ".terraform" directory.
ReadMe in "terraform" directory [here](../terraform/README.md).

If you already have configured AWS infrastructure, make sure that:
- terraform binaries (https://www.terraform.io/downloads.html) are in ".terraform" directory.
Expand All @@ -31,32 +31,32 @@ Make sure that your AWS cloud is configured, and your computer has AWS credentia

#### Creating virtual environment
If you already have made virtual environment, you can skip this step.
```
```bash
cd Micropython
python3 -m venv venv
```

#### Activating virtual environment
This step is platform dependent
###### Windows
```
```bash
venv/Scripts/activate.bat
```

###### Linux/ Mac OS
```
```bash
source venv/bin/activate
```

#### Install requirements
```
```bash
pip3 install -r requirements.txt
```

#### Flashing the board
Make sure that your board is connected to computer. Check the port number.

```
```bash
source venv/bin/activate
python3 scripts/upload_all.py -p <port>
```
Expand Down Expand Up @@ -88,7 +88,7 @@ communication with board. We recommend you programs listed below.

#### Linux
On Linux you can use picocom:
```
```bash
sudo apt-get install picocom
picocom port_name --baud 115200
```
Expand Down
17 changes: 15 additions & 2 deletions MicroPython/src/common/config.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from machine import Pin, reset
from esp32 import wake_on_ext0
from machine import Pin, reset, SLEEP, DEEPSLEEP
from esp32 import wake_on_ext0, WAKEUP_ALL_LOW, WAKEUP_ANY_HIGH
from lib.logging import debug, info
from data_upload.handlers_container import HandlerContainer
from communication.wirerless_connection_controller import get_mac_address_as_string
Expand Down Expand Up @@ -170,8 +170,17 @@ def as_dictionary(self):
# Below functions as static methods to config class?
# Interrput on pin 0
def button_irq(p):
debug("=== RESET BUTTON PRESSED ===")
save()
reset()


def reset_config(p):
debug("=== CONFIG BUTTON PRESSED ===")
global cfg
cfg.ap_config_done = False
cfg.ssid = DEFAULT_SSID
cfg.password = DEFAULT_PASSWORD
save()
reset()

Expand All @@ -188,6 +197,10 @@ def init():
button.irq(trigger=Pin.IRQ_FALLING, handler=button_irq)
wake_on_ext0(pin=button, level=False)

button2 = Pin(32, Pin.IN, Pin.PULL_UP)
button2.irq(trigger=Pin.IRQ_FALLING, handler=reset_config)
wake_on_ext0(pin=button2, level=WAKEUP_ALL_LOW)

debug("Configuration loaded")


Expand Down
1 change: 1 addition & 0 deletions MicroPython/src/common/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ def check_if_file_exists(path_to_file: str):
try:
return uos.stat(path_to_file)[6]
except OSError:
logging.error("No setup file: {}".format(path_to_file))
return False


Expand Down
2 changes: 1 addition & 1 deletion MicroPython/src/communication/API_communication.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def authorization_request() -> str:
logging.info("Failed to authorize in API {}".format(e))
return None

if response.status_code != '200':
if response.status_code != '200' and response.status_code != 200:
logging.error(response.text)
return None

Expand Down
12 changes: 9 additions & 3 deletions MicroPython/src/main.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import time

from machine import Pin, reset, reset_cause, wake_reason, HARD_RESET, PWRON_RESET, SOFT_RESET, PIN_WAKE
from logging import debug
import _thread
Expand All @@ -24,7 +26,6 @@ def configuration_access_point():
def main():
debug("=== MAIN START ===")

# TODO: Czy to potrzebne?
# Increase stack size per thread this increases micropython recursion depth
_thread.stack_size(8192*2)

Expand All @@ -33,7 +34,13 @@ def main():

# Check if configuration via access point has to be started
if not config.cfg.ap_config_done or wake_reason() == PIN_WAKE:
configuration_access_point()
debug("AP_DONE: {}, wake_reason: {}".format(config.cfg.ap_config_done, wake_reason()))
debug("SSID: {}, Password: {}".format(config.cfg.ssid, config.cfg.password))
if config.cfg.ssid != 'ssid' and config.cfg.password != 'password':
debug("SSID and password aren't default. Try to connect")
pass
else:
configuration_access_point()

debug("Main loop")
# If the device is powered on, then actual time from NTP server must be downloaded
Expand All @@ -52,6 +59,5 @@ def main():
# Good night!
power_save(config.cfg.data_publishing_period_in_ms)


if __name__ == '__main__':
main()
25 changes: 12 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,20 @@ The board was programmed using MicroPython, which is a Python implementation
for embedded devices.


## Cloning repository
To clone repository use following lines:
```bash
[email protected]:wizzdev/internal-projects/iot-starter.git
cd iot-starter
git submodule init
git submodule update --init --recursive
```

## Prerequisites

Before compiling there are some packages required to install on the PC. Run the following command to install them:

```
sudo apt-get install git wget bison gperf python python-pip python-setuptools python-serial python-click python-cryptography python-future python-pyparsing python-pyelftools ninja-build libssl-dev
sudo apt-get install git wget bison gperf python python-pip python3-virtualenv python-setuptools python-serial python-click python-cryptography python-future python-pyparsing python-pyelftools ninja-build libssl-dev
```

## Cloning repository
To clone repository use following lines:
```bash
git clone https://github.com/wizzdev-pl/iot-starter.git
cd iot-starter
git submodule init
git submodule update --init --recursive
```

To flash and debug device it is required to add user to dialout group:
Expand All @@ -41,11 +40,11 @@ sudo usermod -a -G dialout $USER
## Installing
### AWS configuration
AWS's configuration is handled using terraform. Detailed description of this
procedure is available in the ".terraform" directory.
procedure is available in the "terraform" directory [here](terraform/README.md).

### Flashing the board
A dedicated Python script was prepared for this procedure. For more information,
please go to the "MicroPython" directory.
please go to the "MicroPython" directory [here](MicroPython/README.md).

## Working with configured device
After all these steps, the board should send data from the sensor into AWS.
Expand Down
39 changes: 26 additions & 13 deletions terraform/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,30 +12,39 @@ _(via: https://www.terraform.io/intro/index.html)_
#### Terraform
You can download terraform binary from this website: https://www.terraform.io/downloads.html
Downloaded file should be placed in this directory (iot-starter/terraform). You can check your installation with this command:
```
terraform --version
```bash
terraform --version
```

#### Python 3, PIP
Python can be downloaded from this website: https://www.python.org/downloads/
Please follow installation instructions from their website.
After installation, you can check if it is installed correctly by typing the following commands in your terminal:
```
python3 --version
pip3 --version
```bash
python3 --version
pip3 --version
```

#### Node.js, npm
These requirements are needed to build and bundle visualization. Nodejs can be installed from this website:
https://nodejs.org/en/download/. Npm should be installed automatically along with nodejs.
You can check if installation succeed with these commands:
```
node --version
npm --version
```bash
node --version
npm --version
```
Project requires to install node.js in version >= 10.0.
## Installation
To update npm to the latest version
```bash
npm install -g npm@latest
```
or to update to the most recent release:
```bash
npm install -g npm@next
```


## Installation

##### 1. Create virtual environment:
First, you need to create virtual environment
Expand Down Expand Up @@ -74,6 +83,9 @@ in this website: https://docs.aws.amazon.com/AmazonS3/latest/user-guide/create-b
Next, you should change "_bucket_" value to the name of the created bucket
for s3 backend in main.tf in this directory.
(AWS policy says that s3 bucket name should be unique in the world).
Value "_region_" refers to one of AWS regions. Remember to change "_region_" according to region
where you have set your s3 bucket.

```
terraform {
backend "s3" {
Expand Down Expand Up @@ -108,7 +120,7 @@ ESP_HARD_PASSWORD = "TopSecretPassword"
ESP_HARD_LOGIN = "TopSecretLogin"
```

##### 6. Initialize terraform, from directory ./.terraform run following commands:
##### 6. Initialize terraform, from directory ./terraform run following commands:
```
./terraform init
./terraform workspace new production
Expand Down Expand Up @@ -157,7 +169,8 @@ Another solution to deal with errors is to destroy terraform and build again.
### Obtaining `ACCESS_CODE` and `SECRET_CODE` from the AWS
You need to gain programmatic access to your AWS account from computer if you want to automatically build your infrastructure with terraform.
To do that:
1. Go to: https://aws.amazon.com/console/ > IAM service (Identity and Access Management) > Users
1. Go to: https://aws.amazon.com/console/ > IAM service (Identity and Access Management) > Users.
2. Find your name and click on it. You should be redirected to details view of your account.
3. Switch to "Security credentials tabs"
4. Click on "Create access key" button
3. Switch to "Security credentials tabs".
4. Click on "Create access key" button.
5. Remember both keys because secret key will not be able to get the secret key from AWS system.
9 changes: 9 additions & 0 deletions terraform/modules/lambdas/locals.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Create lambdas specific names
# Due to the lack of consistency of naming on AWS, some names require only '-' and some require only '_'

locals {
lambda_collect_measurements_name = replace(var.lambda_collect_measurements.function_name, "_", "-")
lambda_collect_measurements_name_ = replace(var.lambda_collect_measurements.function_name, "-", "_")
lambda_health_check_name = replace(var.lambda_health_check.function_name, "_", "-")
lambda_health_check_name_ = replace(var.lambda_health_check.function_name, "-", "_")
}
7 changes: 7 additions & 0 deletions terraform/modules/rest_api/locals.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Create REST API specific names
# Due to the lack of consistency of naming on AWS, some names require only '-' and some require only '_'

locals {
name = replace("${var.prefix}-${basename(var.rest_api_python_source_directory)}", "_", "-")
s3_bucket_name = replace(var.rest_api_s3_bucket_name, "_", "-")
}
4 changes: 1 addition & 3 deletions web_server/client/src/store/modules/device.shadow.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,7 @@ export default {
},
actions: {
loadDeviceShadow: async (context, deviceID) => {
return DeviceShadowActions.apiHelper.getOne(context, "ADD_ONE_ITEM", deviceID, {
suffix: deviceID + "/shadow", skipError: true
})
return 1
}
}
}
Expand Down
8 changes: 0 additions & 8 deletions web_server/client/src/views/device.view.vue
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,6 @@
Measurements
</md-button>
</router-link>

<router-link :to="'/devices/' + deviceId + '/configuration'">
<md-button id="btn-configuration">
<md-icon class="mb-1 mr-1">settings</md-icon>
Configuration
</md-button>
</router-link>

</div>
</div>
</div>
Expand Down
6 changes: 3 additions & 3 deletions web_server/server/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
PAGE_SIZE = int(os.environ.get('PAGE_SIZE', 15))
NO_ROBOTS = bool(os.environ.get('NO_ROBOTS', True)) # Define if page should be indexed
CORS = bool(os.environ.get('CORS', True))
ENV_LOGIN = os.environ.get('ESP_HARD_PASSWORD', 'DEBUG')
ENV_PASSWORD = os.environ.get('ESP_HARD_LOGIN', 'DEBUG')
ENV_LOGIN = os.environ.get('ESP_HARD_LOGIN', 'DEBUG_LOGIN')
ENV_PASSWORD = os.environ.get('ESP_HARD_PASSWORD', 'DEBUG_PASSWORD')
AWS_ROOT_CA_CERTIFICATE_URL = "https://www.amazontrust.com/repository/AmazonRootCA1.pem"
AWS_REGION = os.environ.get('API_REGION_AWS')
AWS_BASE_THING_TYPE = os.environ.get('THING_TYPE_BASE_AWS')
AWS_BASE_THING_POLICY = os.environ.get('THING_POLICY_BASE_AWS')
AWS_BASE_THING_POLICY = os.environ.get('THING_POLICY_BASE_AWS')
7 changes: 7 additions & 0 deletions web_server/server/core/things_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,13 @@ def delete_thing_if_exist(thing_name: str):
def get_thing_certificates(thing_name: str):
certs_handler = CertificatesHandler(thing_client)
certs = certs_handler.get_certificates()
remove_old_thing_certificates(thing_name)
thing_client.attach_thing_principal(thingName=thing_name, principal=certs_handler.cert_arn)
thing_client.attach_principal_policy(principal=certs_handler.cert_arn, policyName=config.AWS_BASE_THING_POLICY)
return certs


def remove_old_thing_certificates(thing_name: str):
old_certs = thing_client.list_thing_principals(thingName=thing_name)
for principal in old_certs.get('principals'):
thing_client.detach_thing_principal(thingName=thing_name, principal=principal)
3 changes: 2 additions & 1 deletion web_server/server/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ requests==2.24.0
flask-lambda-support==0.1.5
flask_jwt_extended==3.25.0
sentry_sdk==0.17.4
werkzeug
werkzeug

0 comments on commit 5218f97

Please sign in to comment.