---
# System prepended metadata

title: Memory factory
tags: [iot, monitoring, factory, sensors, wifi, iiot]

---

## Condition monitoring in a "Memory factory" using vibration data

* **Student name**: Mirachandra Klostermann
* **Credentials**: mk225kn
* **Estimated time**: 0.5-1.5 days (depending on prior knowledge)

[toc]


### Introduction
The project revolves around a "Memory factory" that uses a vibration sensor to gather condition monitoring data.

![](https://hackmd.io/_uploads/rkIbObyYn.jpg)

*Figure 1: Factory project setup*

### Objective
#### Theme
The theme of the project was inspired by the Pixar/Disney movie "[Inside out](https://www.pixar.com/feature-films/inside-out#inside-out-1)", and represents a "Memory factory", with colored marbles symbolizing memories associated with certain emotions.

![Inspiration of the project: Pixar's Inside Out](https://hackmd.io/_uploads/S1E0I_pdh.jpg)
*Figure 2: Pixar's Inside Out as inspiration for the project*

#### Reason for choosing the project
Due to my work environment I was interested to create a project that was related to manufacturing processes and/or along the line of an industrial IoT theme. I'm normally involved after all of of the steps done in this project, so was interested to understand them as well to get a better picture on what is actually behind all the connectivity part.

#### Purpose and insights
The project is designed to simulate a manufacturing process in order to test vibration monitoring possibilities that can be used to analyse the state of a machine or line within a factory and afterwards transfer it to real manufacturing applications.


### Material
In this project I used the following material:

#### List of material

|                        Image                        |               Material                |                               Utilization/Description                                |
|:---------------------------------------------------:|:-------------------------------------:|:------------------------------------------------------------------------:|
|    ![](https://hackmd.io/_uploads/BktWpYauh.png)    |         Raspberry Pi Pico WH          |                             Programmable microcontroller                              |
|    ![](https://hackmd.io/_uploads/B1PT2Fa_h.png)    | Solderless Breadboard 840 tie-points  |                                Helping in prototyping electronic circuits without soldering                                 |
|    ![](https://hackmd.io/_uploads/BkxJmTTd2.png)    | Jumper wires 40-pin 30cm  |                               Connecting different components on the breadboard                                 |
|    ![](https://hackmd.io/_uploads/H1Ghfa6_3.png)    | USB cable A-male – microB-male 1.8m  |                                Connecting microcontroller to MacBook                                 |
| ![](https://hackmd.io/_uploads/SyhxF-yth.png =x100) | Vibration Sensor |              Collecting vibration data over time as condition monitoring values               |
|    ![](https://hackmd.io/_uploads/HJ110KT_3.png)    |           VTech Marble Rush           |                              Representation of a "Memory factory"                              |


#### Cost of material
I bought all of my sensors, the breadboard, wires and Raspberry Pi through Elektrokit as part of the [LNU - 1DT305 Standard Kit (2023)](https://www.electrokit.com/produkt/start-kit-applied-iot-at-linnaeus-university-2023/) for 34.57 €, [LNU - 1DT305 Addon Kit (2023)](https://www.electrokit.com/produkt/addon-kit-applied-iot-at-linnaeus-university-2023/) for 30.24 € and [Sensor kit - 25 modules](https://www.electrokit.com/produkt/sensor-kit-25-moduler/) for 25.91 €. The [marble rush](https://www.amazon.de/dp/B08ZM8TC4M?ref=ppx_yo2ov_dt_b_product_details&th=1) was bought through amazon.de for a reduced price of 47.98 €.

The total cost for all material + shipping was 158.19 €. But since not all material from the kits was used, the actual cost for each singular material would be less.

### Computer setup

#### Used hardware/prerequesits
* I used a 13 inch, MacBook Pro (2016) running macOS Monterey 12.6.2, to conduct the project. 
* The chosen IDE was Microsoft's [Visual Studio Code](https://code.visualstudio.com/).
* Additionally a wifi connection was used for the project.

#### Setup steps
The work environment was set up through the following steps:

1. Downloading and installing [Node.js](https://nodejs.org/en/download) using the terminal app.
2. Downloading and installing [Visual Studio Code](https://code.visualstudio.com/Download).
3. Searching and installing the [Pymakr](https://marketplace.visualstudio.com/items?itemName=pycom.Pymakr) extension in Visual Studio Code through the "Extensions" tab. ![](https://hackmd.io/_uploads/HkeQuaT_h.jpg)
*(Figure 3: Pymakr extension)*
4. Downloading the Raspberry Pi [firmware](https://micropython.org/download/rp2-pico-w).
5. Updating the firmware on the Raspberry Pi following the [recommended steps](https://hackmd.io/@lnu-iot/rkFw7gao_):
    5.1. Holding down the BOOTSEL button on the Raspberry Pi while connecting it with the computer using the USB cable.
    5.2 Moving the firmware file into the Raspbbery Pi storage.
6. Open the Pymakr extension in VIsual Studio Code, connect the device, opening the terminal (as seen in Figure 4) and typing `print("Test")` to test if the connection was succesful. ![](https://hackmd.io/_uploads/rkEvwpaOn.jpg)
*(Figure 4: Testing the connection)*
7. Creating a new project and file directory in Visual Studio Code. ![](https://hackmd.io/_uploads/rypn_TaO2.jpg)
*(Figure 5: Creating a project)*
8. Opening the "more options" icon with the three dots and clicking on #pymakr" > "upload to device" ![](https://hackmd.io/_uploads/H1i69pauh.png)
*(Figure 6: Uploading code to the device)*


### Putting everything together
**!Before any wiring is done, it's important to make sure that the Raspberry Pi is disconnected from the computer.**

First the microcontroller was attached to the breadboard (as shown in Figure 7), then blue and green male/male jumper wires were used to connect the ground and 3V3 Out respectively. 

![](https://hackmd.io/_uploads/BJ_OmrZF3.png)

*Figure 7: Circuit diagram (created using the [Fritzing](https://fritzing.org/) app)*

The vibration sensor was connected to the breadbord using three female/male and male/male jumper wires (as seen in Figure 7). The red wire was used to connect the ground, the orange one for the 3V3 and the brown one was used for the signal that was connected to the microcontroller's general purpose pin 12 (GP12). See Figure 8 for a reference of the microcontroller pin positions.

![](https://hackmd.io/_uploads/BJKe5-JK2.png)
*Figure 8: Schematic of a Raspberry Pi Pico W (image derived from the 1DT305 course material)*

Another important step in the setup was to make sure that all components are connected correctly and are secured on the board and between the different components in order to ensure flawless operation.

The whole setup is only suitable for development purposes, within a production environment there would be a need for more precise sensors and equipment. 

#### Electrical calculations
Since I couldn't find consumption information for my specific sensor, I used the information found here for the power consumption while active (with wifi) and in sleep mode for the Raspberry Pi (https://www.elektronik-kompendium.de/sites/raspberry-pi/2707171.htm) and this one (https://media.digikey.com/pdf/Data%20Sheets/Adafruit%20PDFs/1766_Web.pdf) for the vibration sensor instead for the calculations. The calculations won't be perfectly accurate but with those data the following estimates can be made:

| Component | Active | 
| -------- | -------- | 
| Raspberry Pi     | 55 mA     | 
| Vibration Sensor     | 20 mA     | 
| Total     | 75 mA     | 
	

*Additional information: Power consumption in sleep mode was omitted for the calculation as microampere (µA) are a very low unit, a voltage of 3.3V for the Raspberry Pi and a 2600 mAh battery were used for the calculations.*


Using the above data a total power consumption for the project running 1 minute (60 seconds) long could be calculated as follows:

* Total Power Consumption (mW) = Total Current Consumption (mA) × Voltage (V) 
* Total Power Consumption (mW) = 75 mA × 3.3 V = 247.5 mW

Thus:
* Power Consumption (Wh) = Total Power Consumption (mW) × Time (h)
* Time = 60 seconds / 3600 = 0.0167 hours
* Power Consumption for 1 minute (Wh) = 247.5 mW × 0.0167 h = 4.13 mWh

The estimated battery life of a 2600 mAh battery (even though not used in this project) in hours could be calculated as follows:

* Estimated Battery Life (hours) = Battery Capacity (mAh) / Power Consumption (mA)
* Estimated Battery Life (hours) = 2600 mAh / 75 mA = 34.67 hours

Which means that for a project as this one that constantly monitors vibration data, a battery could theoretically last approximately 34.67 hours.


### Platform
As a platform I chose [Adafruit IO](https://io.adafruit.com/). The decision was mainly made because at my technical knowledge level I could realize my project more efficiently with a low code solution and out of the three low code platforms that I tested (Ubidots, Adafruit and Datacake) I simply prefered Adafruit IO handling-wise. Apart from that I liked its integration of MQTT, the feed system, dashboards and trigger functionality as well as the available documentation.

The project was realized in a cloud environment with a free plan of Adafruit IO. In case that a similar project will be conducted in a real-world manufacturing environment, a paid subscription and/or possibly different big data IoT platform like Google Cloud IoT or Amazon IoT would very likely be a better solution. 

### The code
The actual Visual Studio Code project was structured as seen in Figure 9. With the "main" file performing all the main tasks and the files within the "lib" folder being used as references e.g. for login data and directories.

![](https://hackmd.io/_uploads/rJdLC-kF2.png =x120)
*Figure 9: Project structure*

Below are a few code snippets of core functionalities used within the project.

#### Lib folder
The "keys" file stores the credentials used within the other files (e.g. the wifi as well as the Adafruit IO credentials and feed path to store the recorded data), as seen below (real credentials were removed in this documentation and replaced by "XXXX"):

```
# Wireless network
WIFI_SSID = 'XXXX' #Wifi username - blocked out due to privacy purposes.
WIFI_PASS = 'XXXX' #Wifi password - blocked out due to privacy purposes.

# Adafruit IO (AIO) configuration
AIO_SERVER = "io.adafruit.com"
AIO_PORT = 1883
AIO_USER = "XXXX" #Adafruit username - blocked out due to privacy purposes.
AIO_KEY = "XXXX" #Adafruit key - blocked out due to privacy purposes.
AIO_CLIENT_ID = ubinascii.hexlify(machine.unique_id())  # Can be anything
AIO_VIBRATION_FEED = "XXXX" #Adafruit vibration feed path - blocked out due to privacy purposes.
```
While the "MQTT" and "wifiConnection" files are needed to establish connections to the wifi and MQTT.

#### Main file

The "main" file is logically devided into four sections to keep some order within the code, namely the "import", "settings", "sensor" and "wifi" sections.

First necessary libraries are imported, as seen in the snippet:

```
import time
from mqtt import MQTTClient
import machine
from machine import Pin
import keys
import wifiConnection

```

Then the settings section defines the used pins of the microcontroller:

```
vibratePin = Pin(12, Pin.IN)
led = Pin("LED", Pin.OUT)

```
While the main part defines the actions taken when vibrations are detected (that is: when a vibration is detected the LED on the microcontoller will turn on, a string will be printed in the terminal and a value of "1" is sent to Adafruit IO):

```
def send_vibration():
    if vibratePin.value() == 1:
        print("No vibration...")
        led.off()
        publish_vibration_state("0")
    else:
        print("Vibration detected...")
        led.on()
        publish_vibration_state("1")
```

And finally the wifi connection is established: 

```
try:
    ip = wifiConnection.connect()
except KeyboardInterrupt:
    print("Keyboard interrupt")
    
```


### Transmitting the data / connectivity
Data from the vibration sensor is transmitted via WiFi to the Adafruit IO platform using MQTT protocols in an interval of 3 seconds as the Adafruit IO free plan limits the amount of data sent and since it gives enough information to gather vibration data over time and analyse it.

Wifi was chosen due to the bad LoRa coverage in the area and because it was convenient within the shown setup.


### Presenting the data
The dashboard was built using the Adafruit IO feeds and dashboard presenting the vibration data using 3 different visualization methods (see Figure 10):

* **Status indicator**: Indicating the last state sent by the vibration sensor as green for no vibration and red when vibration is detected.
* **Gauge**: the gauge is set to 0 when no vibration is detected and 1 (graph filled) when vibrations are detected. It also only shows the last state sent.
* **Line chart**: the line chart records historical data showcasing the progress of vibration data over time on a binary scale (values of 0 or 1).

![](https://hackmd.io/_uploads/rkW8MIkY3.png)
*Figure 10: Memory factory vibration monitoring using Adafruit IO dashboards*

The databases used were Adafruit IO's time-series databases (see Figure 11) due to convenience as the chosen platform was already set at that point. Due to the limit of 30 data points per minute in the free Adafruit IO plan, vibration states are sent every 3 seconds to the platform. Also the feed data is limited to 1KB (1024 bytes) when recording historical data and stored for 30 days.

![](https://hackmd.io/_uploads/Hymr4L1K2.png)
*Figure 11: Adafruit feed of the vibration data*

#### Automation/triggers of the data

The system additionally sends the information that vibration was detected to a discord feed (see Figure 12).

![](https://hackmd.io/_uploads/SkU2ZQ1Y2.png)
*Figure 12: Discord integration and feed*

The setup is using a webhook created in Descord and the "Actions" automation included in Adafruit IO as seen in Figure 13, only posting messages to Discord when a value of 1 (= vibration was detected) is logged.

![](https://hackmd.io/_uploads/ryS4M71Yh.png)
*Figure 13: Adafruit IO Discord integration and automation*


### Finalizing the design

#### Results

The project showcased a technical IoT proof of concept, covering the setup and wiring of a sensor and a Raspberry Pi (see Figure 14), the setup of an integrated development environment (IDE) in the form of Microsoft's Visual Studio Code (see Figure 15) and the connection of the device using Pymakr. In a second step the sensor was programmed and data was sent to an IoT platform (Adafruit IO) using a wifi connection and the MQTT protocol, storing and visualising the data in Adafruit IO (see Figure 16) and pushing it through an automation to a Discord feed.

![](https://hackmd.io/_uploads/HJ7VNXyFn.jpg)
*Figure 14: Wiring of the sensor and Raspberry Pi*

![](https://hackmd.io/_uploads/HJ5sLXJYn.jpg)
*Figure 15: Project in Visual Stuidio Code*

![](https://hackmd.io/_uploads/HyEODXJFn.jpg)
*Figure 16: Physical project setup and visualization in an Adafruit IO dashboard*

#### Final thoughts

The course was very enjoyable, well structured and provided very good insights on each step needed to realize an IoT project. 

Regarding the design and execution, I'll extend and change the project as follows:

* I think the vibration sensor's redordings aren't very accurate. I'll therefore invest in a more sophisticated sensor. 
* I'll also put more time into the preperation of the data sent to the platform as well as possible calibration of the sensor.
* As well as record more steps within the values, not just vibration detected or not detected but the specific value.
* I might also switch to another visualization platform to be more flexible in regards to the evaluation and presentation of the data. There's definitely a lot of potential to that.

#### Video

A video of the project can be found on Youtube: https://youtu.be/Llvw5fKbN_U 

{%youtube Llvw5fKbN_U %}