README.md 7.49 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# Edge-IoT Simulator

The following project contains a small edge-IoT simulator.

Components:

* `core`: IoT device simulators, e.g., `temperature` simulator
* `messaging`: MQTT messaging components to send data to MQTT broker
* `web`: A small flask application which provides access to selected data provided by `core` and `messaging`.

Architecture:

```bash
web <-> core <-> queue <-> mqtt_publisher
```

Details:

* `web`: The web application has a reference to the core services (e.g., temperature) and has currently two endpoints:
  * `/`: Dashboard which shows the latest temperature value
  * `/temperature`: Returns the latest temperature value as json
* `core, queue, mqtt_publisher`: The core services (e.g., `temperature`) are decoupled from the `mqtt_publisher` by a shared memory `threadsafe` queue. The `mqtt_publisher` forwards the message with a corresponding topic and payload.

For example:

```bash

temperature <-> TemperatureMeasurement (timestamp=XXX,value=XXX,unit=XXX)
                            |
                            v
                MqttMessage(topic=sensors/id/data payload=TemperatureMeasurement)
                            |
                            v
                          queue
                            ^
                            |
                mqtt_publisher.client.publish(MqttMessage.topic, MqttMessage.payload)
```

All components can easily be configured with the following `.env` file with should be present in the root of this project.

```bash
# messaging
MQTT_SERVER_NAME=localhost
MQTT_PORT=1883
MQTT_TLS=false
MQTT_USERNAME=
MQTT_PASSWORD=
MQTT_MAX_CONNECT_RETRIES=5
MQTT_RECONNECT_TIMEOUT=30
MQTT_CLIENT_ID=ab444537-cf67-47aa-a5ef-3548292e225b
MQTT_PUBLISH_QOS=1
MQTT_TOPIC_PUBLISHER=publisher
MQTT_TOPIC_PUBLISHER_STATE=state

# core
MQTT_TOPIC_SENSORS=sensors
MQTT_TOPIC_SENSORS_DATA=data

# web
WEB_HOSTNAME=localhost
```

## Prerequisites

Sebastian Böhm's avatar
Sebastian Böhm committed
66
* Make sure that you installed `python3, python3-venv, python3-pip` on your system
67
68
69
70
* Make sure that you install `docker` on your system

## Run the project locally

Sebastian Böhm's avatar
Sebastian Böhm committed
71
72
73
* Install necessary packages: `sudo apt-get install python3-flask python3-venv python3-wheel`
* Clone/Pull this repository: `git clone https://gitlab.rz.uni-bamberg.de/sebastian.boehm/edge-iot-simulator`
* Go into the root directory of the repository, switch then to (`cd edge_iot_simulator`)
74
75
76
* Create the env file `.env` with the contents above and change the values according to your needs
* Create a virtual environment at first: `python3 -m venv venv`
* Change to the virtual environment: `source venv/bin/activate`
Sebastian Böhm's avatar
Sebastian Böhm committed
77
* Update pip: `pip3 install --upgrade pip`
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
* Install the necessary dependencies: `pip3 install -r requirements.txt`
* Change the working directory: `cd edge_iot_simulator`
* Run:`python3 main.py`

You should see the following output:

```bash
11/11/2021 02:48:48 PM main Please any key to interrupt...
11/11/2021 02:48:48 PM mqtt_publisher Successfully started mqtt publisher...
11/11/2021 02:48:48 PM temperature_svc Successfully started temperature sensor...
11/11/2021 02:48:48 PM temperature_svc New value measured {"timestamp": 1636638528.2080338, "value": 26, "unit": "celsius"}
11/11/2021 02:48:48 PM app Successfully started WebbApp...
11/11/2021 02:48:48 PM mqtt_publisher Error establishing connection to localhost:1883[Errno 111] Connection refused
11/11/2021 02:48:48 PM mqtt_publisher mqtt publisher received shutdown signal
11/11/2021 02:48:51 PM temperature_svc New value measured {"timestamp": 1636638531.2109501, "value": 20, "unit": "celsius"}
11/11/2021 02:48:54 PM temperature_svc New value measured {"timestamp": 1636638534.2141488, "value": 7, "unit": "celsius"}
11/11/2021 02:48:57 PM temperature_svc New value measured {"timestamp": 1636638537.2166193, "value": 59, "unit": "celsius"}
11/11/2021 02:48:58 PM main Unknown error occurred: Could not establish the connection to the mqtt broker
11/11/2021 02:48:58 PM mqtt_publisher mqtt publisher received shutdown signal
11/11/2021 02:48:58 PM temperature_svc Temperature service received shutdown signal...
11/11/2021 02:48:58 PM app WebApp received shutdown signal....
11/11/2021 02:48:58 PM main Wait for graceful termination...
11/11/2021 02:49:00 PM main Successfully terminated edge-iot simulator
```

We can see the first measurements of the sensor. However, the edge-IoT simulator is terminating because no mqtt message broker is reachable.

Sebastian Böhm's avatar
Sebastian Böhm committed
105
For testing purposes, start a mqtt message broker via docker: `sudo docker run -d -p 1883:1883 -p 9001:9001 eclipse-mosquitto:1.6.15`
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135

Run the application again: `python3 main.py`

You should see the following output:

```bash
11/11/2021 02:55:39 PM main Please any key to interrupt...
11/11/2021 02:55:39 PM mqtt_publisher Successfully started mqtt publisher...
11/11/2021 02:55:39 PM temperature_svc Successfully started temperature sensor...
11/11/2021 02:55:39 PM temperature_svc New value measured {"timestamp": 1636638939.4815965, "value": 46, "unit": "celsius"}
11/11/2021 02:55:39 PM app Successfully started WebbApp...
11/11/2021 02:55:39 PM mqtt_publisher Publisher connected with result code 0
11/11/2021 02:55:39 PM mqtt_publisher Successfully published message {"timestamp": 1636638939.4815965, "value": 46, "unit": "celsius"}
11/11/2021 02:55:42 PM temperature_svc New value measured {"timestamp": 1636638942.4844544, "value": 14, "unit": "celsius"}
11/11/2021 02:55:42 PM mqtt_publisher Successfully published message {"timestamp": 1636638942.4844544, "value": 14, "unit": "celsius"}
11/11/2021 02:55:45 PM temperature_svc New value measured {"timestamp": 1636638945.4878645, "value": 36, "unit": "celsius"}
11/11/2021 02:55:45 PM mqtt_publisher Successfully published message {"timestamp": 1636638945.4878645, "value": 36, "unit": "celsius"}
^C11/11/2021 02:55:48 PM main Received user's shutdown signal...
11/11/2021 02:55:48 PM mqtt_publisher mqtt publisher received shutdown signal
11/11/2021 02:55:48 PM temperature_svc New value measured {"timestamp": 1636638948.4884603, "value": 1, "unit": "celsius"}
11/11/2021 02:55:48 PM mqtt_publisher Successfully published message {"timestamp": 1636638948.4884603, "value": 1, "unit": "celsius"}
11/11/2021 02:55:48 PM mqtt_publisher mqtt publisher received shutdown signal
11/11/2021 02:55:48 PM temperature_svc Temperature service received shutdown signal...
11/11/2021 02:55:48 PM app WebApp received shutdown signal....
11/11/2021 02:55:48 PM main Wait for graceful termination...
11/11/2021 02:55:51 PM main Successfully terminated edge-iot simulator
```

Now the edge-IoT simulator is connected to the local mqtt message broker.

Sebastian Böhm's avatar
Sebastian Böhm committed
136
137
Network architecture:

138
139
```bash
-----------------------localhost----------------------
Sebastian Böhm's avatar
Sebastian Böhm committed
140
edge-IoT simulator <-> docker daemon <-> mqtt message broker
141
142
143
144
145
146
147
148
------------------------------------------------------
```

## Run with docker

To run edge-IoT simulator with docker, do the follpwing:

* Change to the root directory of this repository
Sebastian Böhm's avatar
Sebastian Böhm committed
149
* Build the image: `docker build -t edge-iot-simulator:1.0 .`
150
* Change the working directory to the directory: `cd edge_iot_simulator`
Sebastian Böhm's avatar
Sebastian Böhm committed
151
* Create and adjust the `.env` file! **Make sure that you take the right IP address for the message broker. If you are running the mqtt broker on the same system (physical host), obtain the broker's ip address via `docker inspect`**.
Sebastian Böhm's avatar
Sebastian Böhm committed
152
* Run the image: `docker run -p 5000:5000 --env-file=.env edge-iot-simulator:1.0`, choose `docker run -d -p 5000:5000 --env-file=.env edge-iot-simulator:1.0`
153

Sebastian Böhm's avatar
Sebastian Böhm committed
154
155
156
157
```bash
-----------------------localhost----------------------
docker daemon <-> edge-IoT-simulator <-> mqtt message broker 
------------------------------------------------------
Sebastian Böhm's avatar
Sebastian Böhm committed
158
```