Skip to content
Permalink
Browse files
  • Loading branch information
aa7401 committed Oct 14, 2018
2 parents c9ca114 + f2c9cff commit d1698cf1d5b0bb737de06e7af6b57e258a4c85b8
Show file tree
Hide file tree
Showing 21 changed files with 110 additions and 915 deletions.
@@ -59,6 +59,27 @@ In this lab you will be focussing on the commandline however there are a number

Use the file menu in VS Code to open the `blink/` directory then open the integrated terminal, it should point to the current project directory.

### 1.1 Understanding the Code

You should now use the IDE to open the `exercises/02_tdd/nodemcu/` directory which contains a simple Arduino sketch with a library. Lets look at the files:

```
├── platformio.ini
├── readme.md
└── src
   └── main.cpp
```

1. The `platformio.ini` file is the project configuration. It defines two _environment definitions_:
1. The `nodemcuv2` environment supports the NodeMCU boards based on the ESP8266 microcontroller.
2. The `lolin32` environment supports the Lolin32 boards based on the ESP32 microcontroller.
2. The `native` definition allows us to develop and test our library using the native compiler on our workstation.
2. The `readme.txt` file contains the documentation.
3. The `src/` directory contains the main Arduino sketch. Notice that this imports:
1. The Arduino libraries.
2. Our `avg` library.
```
We can compile the code for a specified environment using either the `platformio` or `pio` command. The chosen environment is indicated either using the _long flag_ `--environment` or the _short flag_ `-e`. All of the commands listed below are equivalent:
```shell
@@ -190,5 +211,76 @@ If your sensor is mounted on a breakout board it will only have 3 pins and these

The current program simply reads an analog value from the sensor however this contains information on both temperature and humidity. To access each individual reading you will need to use a dedicated DHT11 library.

1. Create a new PlatformIO project.
2. Use the knowledge from this lab to build a program to report both temperature and humidity.
1. Search for a suitable library for the DHT11 and add it to the `platformio.ini` file.
2. Use the knowledge from this lab to build a program to capture both temperature and humidity.
3. Send this data (together with the correct units) to the serial monitor.

## 4 Creating a New Project

Up to this point the overall project structure has been provided for you but how was this done? PlatformIO provides the `init` subcommand that creates the required directory structure and builds a basic ini file.

The command requires you to pass it the **board identifier ID**. This can be found using the [Boards Catalog](http://docs.platformio.org/en/latest/boards/index.html#boards), the [Boards Explorer](https://platformio.org/boards) or by running the `boards` subcommand. Run the following command to find a board ID for the nodemcu:

```shell
$ platformio boards nodemcu
Platform: espressif32
-----------------------------------------------------------------------------
ID MCU Frequency Flash RAM Name
-----------------------------------------------------------------------------
nodemcu-32s ESP32 240MHz 4MB 320KB NodeMCU-32S
Platform: espressif8266
-----------------------------------------------------------------------------
ID MCU Frequency Flash RAM Name
-----------------------------------------------------------------------------
nodemcu ESP8266 80MHz 4MB 80KB NodeMCU 0.9 (ESP-12 Module)
nodemcuv2 ESP8266 80MHz 4MB 80KB NodeMCU 1.0 (ESP-12E Module)
```

From this we can see that the platform for our ESP8266 is `expressiv8266` and the ID is either `nodemcu` or `nodemcuv2`, we have the latest version (v2).

Lets repeat this for the more powerful ESP32-based board. This board is labelled `wemos`. Searching for this returns:

```shell
$ platformio boards wemos
Platform: espressif32
--------------------------------------------------------------------------------
ID MCU Frequency Flash RAM Name
--------------------------------------------------------------------------------
lolin_d32 ESP32 240MHz 4MB 320KB WEMOS LOLIN D32
lolin_d32_pro ESP32 240MHz 4MB 320KB WEMOS LOLIN D32 PRO
lolin32 ESP32 240MHz 4MB 320KB WEMOS LOLIN32
wemosbat ESP32 240MHz 4MB 320KB WeMos WiFi & Bluetooth Battery
Platform: espressif8266
--------------------------------------------------------------------------------
ID MCU Frequency Flash RAM Name
--------------------------------------------------------------------------------
d1 ESP8266 80MHz 4MB 80KB WEMOS D1 R1 (Retired)
d1_mini ESP8266 80MHz 4MB 80KB WeMos D1 R2 & mini
d1_mini_lite ESP8266 80MHz 1MB 80KB WeMos D1 mini Lite
d1_mini_pro ESP8266 80MHz 16MB 80KB WeMos D1 mini Pro
```

Since we know the MCU is an ESP32, not an ESP8266 we focus on the first table. The version we need is the `lolin32`.

Create a new directory called `sound/` in the `01_arduino/` directory.

Once you have created your project directory you should run the `init` _inside_ it, passing the IDs of the boards we want to use:

```shell
$ platformio init --board nodemcuv2 --board lolin32
```

You can use the `init` command multiple times to add additional boards. Add environments for every microcontroller board in your kit.

This creates a project and defines multiple environments. You will need to add the **Native** environment manually by editing the `platformio.ini` file and adding:

```
[env:native]
platform = native
```

### 4.1 Test Your Understanding

Create a program in your `sound/` directory to capture the noise level and display this in the Serial Monitor.
@@ -5,7 +5,7 @@ In this lab you will learn how to carry out the process of Test-Driven developme

## 1 Understanding the Code

You should now use the IDE to open the `exercises/02_tdd/nodemcu/` directory which contains a simple Arduino sketch with a library. Lets look at the files:
You should now use the IDE to open the `exercises/02_tdd/temp/` directory which contains a simple Arduino sketch with a library. Lets look at the files. As you can see there are some additional ones:

```
├── lib
@@ -23,81 +23,9 @@ You should now use the IDE to open the `exercises/02_tdd/nodemcu/` directory whi
```

1. The `lib/` directory contains our custom library. This is where we will build the business logic for our software. It will be thoroughly unit tested.
2. The `platformio.ini` file is the project configuration. It defines two _environment definitions_:
1. The `nodemcuv2` definition is used to build, compile and upload the code to the dev board.
2. The `native` definition allows us to develop and test our library using the native compiler on our workstation.
3. The `src/` directory contains the main Arduino sketch. Notice that this imports:
1. The Arduino libraries.
2. Our `avg` library.
4. The `test/` directory contains the unit testing files that will be used to test the library code.
2. The `test/` directory contains the unit testing files that will be used to test the library code.

## 2 Creating a New Project

In this lab the overall project structure has been provided for you but how was this done? PlatformIO provides the `init` subcommand that creates the required directory structure and builds a basic ini file.

The command requires you to pass it the **board identifier ID**. This can be found using the [Boards Catalog](http://docs.platformio.org/en/latest/boards/index.html#boards), the [Boards Explorer](https://platformio.org/boards) or by running the `boards` subcommand. Run the following command to find a board ID for the nodemcu:

```shell
$ platformio boards nodemcu
Platform: espressif32
-----------------------------------------------------------------------------
ID MCU Frequency Flash RAM Name
-----------------------------------------------------------------------------
nodemcu-32s ESP32 240MHz 4MB 320KB NodeMCU-32S
Platform: espressif8266
-----------------------------------------------------------------------------
ID MCU Frequency Flash RAM Name
-----------------------------------------------------------------------------
nodemcu ESP8266 80MHz 4MB 80KB NodeMCU 0.9 (ESP-12 Module)
nodemcuv2 ESP8266 80MHz 4MB 80KB NodeMCU 1.0 (ESP-12E Module)
```

From this we can see that the platform for our ESP8266 is `expressiv8266` and the ID is either `nodemcu` or `nodemcuv2`, we have the latest version (v2).

Lets repeat this for the more powerful ESP32-based board. This board is labelled `wemos`. Searching for this returns:

```shell
$ platformio boards wemos
Platform: espressif32
--------------------------------------------------------------------------------
ID MCU Frequency Flash RAM Name
--------------------------------------------------------------------------------
lolin_d32 ESP32 240MHz 4MB 320KB WEMOS LOLIN D32
lolin_d32_pro ESP32 240MHz 4MB 320KB WEMOS LOLIN D32 PRO
lolin32 ESP32 240MHz 4MB 320KB WEMOS LOLIN32
wemosbat ESP32 240MHz 4MB 320KB WeMos WiFi & Bluetooth Battery
Platform: espressif8266
--------------------------------------------------------------------------------
ID MCU Frequency Flash RAM Name
--------------------------------------------------------------------------------
d1 ESP8266 80MHz 4MB 80KB WEMOS D1 R1 (Retired)
d1_mini ESP8266 80MHz 4MB 80KB WeMos D1 R2 & mini
d1_mini_lite ESP8266 80MHz 1MB 80KB WeMos D1 mini Lite
d1_mini_pro ESP8266 80MHz 16MB 80KB WeMos D1 mini Pro
```

Since we know the MCU is an ESP32, not an ESP8266 we focus on the first table. The version we need is the `lolin32`.

Once you have created your project directory you should run the `init` _inside_ it, passing the IDs of the boards we want to use:

```shell
$ platformio init --board nodemcuv2 --board lolin32
```

You can use the `init` command multiple times to add additional boards.

This creates a project and defines multiple environments. You will need to add the **Native** environment manually by editing the `platformio.ini` file and adding:

```
[env:native]
platform = native
```

Remember that this has already been done for you in this lab but you will need to complete these steps when you create your own project.

## 3 Running the Unit Tests
## 2 Running the Unit Tests

We will be executing our test suite from the terminal by invoking the `pio` command together with the `test` subcommand.

@@ -122,23 +50,23 @@ $ pio test -v -e native

Notice that the test suite fails!

## 4 Test-Driven Development
## 3 Test-Driven Development

You are now going to modify the code to ensure that the test passes. The test adds a single value to the library. It then reads the average value which should match the value we passed (but it doesn't). We are going to apply TDD principles.

### 4.1 Define Requirements as a Test
### 3.1 Define Requirements as a Test

For this first iteration, this has already been done, look in the `test_avg.cpp` file.

### 4.2 Write Code to pass the Test
### 3.2 Write Code to pass the Test

We now need to write just enough code to pass the test. Edit the `lib/avg/avg.cpp` file. Test your code by running the test suite. Stop as soon as the test passes.

### 4.3 Refactor the Code
### 3.3 Refactor the Code

The third and final step is to tidy up the code, making sure the tests still pass.

## 5 Test Your Understanding
### 3.4 Test Your Understanding

Now apply the TDD principles to fix the following issues, this will take 6 TDD iterations:

@@ -153,12 +81,14 @@ Now apply the TDD principles to fix the following issues, this will take 6 TDD i

For an extension task, use TDD to implement an average function that returns the mean reading since the device was powered up.

## 6 Integrate into an Arduino Project
## 4 Integrate into an Arduino Project

The final step is to use your custom library to create a complete temperature and humidity sensor.

1. Create a new PlatformIO project called `temperature` and copy over your custom library.
2. Connect a DHT11 sensor and capture the temperature and humidity from this.
3. Send both the new random number and the rolling average to the serial monitor with both values on the same line:
1. You can use `Serial.print()` to send data without a newline terminator.
4. Use your library to calculate the overall mean value and add a third value to each row in the serial monitor.
1. Connect a DHT11 sensor and capture the temperature and display in the serial monitor.
2. If you are using the Lolin32 board you should also display the temperature on the screen.
2. Use your custom library to smooth the reading using a rolling average, display this next to each raw reading (including the screen if available).
3. Capture the humidity as well as temperature and display this next to the temp values.
4. How could you modify your library to smooth both the temperature and humidity?
1. You may need to make changes to your library.
2. Don't forget to apply the TDD methdology!

This file was deleted.

This file was deleted.

This file was deleted.

0 comments on commit d1698cf

Please sign in to comment.