Please enable JavaScript to view this site.

Waijung 2 User Guide

Navigation: ESP32 target > Tutorials > Model-Based Design (MBD) & Internet of Things (IoT) tutorials

Project 11: Complete web-enabled embedded controller and data storage using Aimagin Analytics

Scroll Prev Top Next More

Project description

This project will be achieving the same result as Project 10 but we will teach you how to implement the web application using drag and drop tools with minimum code, instead of needing to write a lot of HTML/CSS/JS. We will also teach you how to store the recorded data in a server.

Learning objectives

In this tutorial you will learn:

1.Understand how to implement a web application using drag and drop tools (Aimagin Connect) to control a device.

2.Understand how to configure a server (Aimagin Analytics) to receive data for secure storage and analysis.

 

Required hardware

No.

Item

Picture

Quantity

1

FiO Glide ESP32 Starter Kit

fio_glide_starter_kit

1

2

Breadboard (830 points)

breadboard

1

3

Jumper Wire Male-Female

jumper_wire

4

4

Red 5mm LED

red_led

1

5

Resistor 220 Ohms

resistor

1

6

Resistor 2k Ohms

resistor

1

7

LDR sensor

Project_LDR

1

8

USB Wi-Fi dongle (only necessary if WiFi AP mode is used)

wifi_dongle

1

9

Micro SD card reader (Example)

micro_sd_card_reader

1

 

Hardware setup

project7_hardware_setup

 

 

Let's get started on building the web application with Aimagin Connect first.

 

What is Aimagin Connect?

Aimagin Connect can be used create web applications using drag and drop methodologies for both enterprise and embedded web servers.

 

PART 1: DESIGN THE WEB APPLICATION

Note: You can download the completed file here, or you can follow the steps and do it yourself as well.

 

 

1.Go to https://bidev.aimagin.com/ and sign in to your Aimagin Analytics account.

Credentials needed: Organization ID, Username, Password

Note: Credentials can only be generated by Aimagin.

project11_1

2.Successfully signed in

project11_2

3.Click on Aimagin Connect

project11_3

4.Name the project

project11_4
project11_5

5.Add a new page

project11_6

6.Expand the page

project11_7

7.Add a Break line by dragging and dropping

project11_8
project11_9

8.Add a Flex and change the background color

project11_10
project11_11

9.Change the Flex Property

project11_23
project11_24

10.Expand the flex to add components under this flex

project11_12

11. Add two columns under this flex

project11_13
project11_14

 

12. Expand both columns

project11_16

13. Drag and drop two Labels, a Rocker Switch, and a Circular Gauge under the first column.

project11_17
project11_18
project11_19
project11_20

14. Give both labels a name.

project11_21
project11_22

15. Save the file and preview (Please do this after every 5-10 steps. If there is a problem with your internet connection, the server, or you refresh the page, all unsaved work will be lost). Previewing the file will also help you see your progress.

project11_159

 

16. You can view the saved file under Project. If there is ever a problem with internet connection / server / page refreshing, your saved file will be found under Project and you can open it and continue your work.

project11_160
project11_161

17.Configure the Rocker Switch.

project11_25
project11_26
project11_27

18. Configure the Circular Gauge.

project11_28
project11_29

19. Drag and drop a label and Multi Axis Chart under the second column.

project11_30
project11_31

20. Change Label name.

project11_32

21. Configure the Multi Axis Chart.

project11_33
project11_34
project11_35
project11_36
project11_37
project11_38

22. Add another Flex, change the background color, and edit the Flex Property.

project11_39
project11_40
project11_41

23. Add six columns under this Flex.

project11_42

24. Under the first and fifth columns, add a Label and a Slidebar component. Leave the other columns empty.

project11_43
project11_44

25. Change the column size of the first and fifth columns to 33.3% on all screen size. This will help with sizing and spacing of the Slidebars in addition to the appropriate alignment of the webpage.

project11_51
project11_52

26. Change the label names.

project11_45
project11_46

27. Configure the two Slidebars.

project11_47
project11_48

28. Add two Break lines after the Flex.

project11_49

29. Change ID of some components.

Slidebar 1 ID: threshold_slider

Slidebar 2 ID: brightness_slider

Multi Axis Chart ID: chart

Circular Gauge ID: gauge

Rocker Switch ID: rocker_switch

project11_50

30. Save the file and preview.

project11_53
project11_54

 

Design of the web application is complete. Now the next step is to make it functional.

 

PART 2: ADD FUNCTIONALITY TO WEB APPLICATION

 

1.Add callback code. Download this file and open it.

project11_55

2.Copy the code.

project11_56

3.Paste the code in the "init" section and save.

project11_57
project11_58

4.Give the page a name and save the file.

project11_59

5.Click on "Save as Web app" and give a name.

project11_60
project11_61

6.System settings tab (in the left menu) > Right click on Web app > Open in new window

project11_62

7.Create a web app to publish.

project11_63

8.Give a name, select the file you just saved as a web app in Step 5, and click on Update.

project11_64
project11_65

9.Web app will now be published. Even though it will show as published in the table, it will be shown in the menu bar on the left only if Aimagin has granted the permission to do so. If you cannot view it in the left menu, then please contact us before continuing the next steps.

project11_66
project11_67

10. System settings tab > Export > Add webapp > Select your web app that you just created and published > Click Ok

project11_68
project11_69
project11_70

11. Click on Generate ESP32 Webapp Package. This will download the created webapp and related Aimagin Connect files as a .zip file.

project11_71
project11_72

12. Open the .zip file, double click on Server folder, and extract the contents to a SD card.

project11_73
project11_74

13. Insert the SD card into ESP32.

 

The web application side is now complete. We will now move on to Simulink to design the firmware that will allow ESP32 to collect data and publish to the web application, as well as allow the web application to control the ESP32.

 

PART 3: DESIGN FIRMWARE ON MATLAB/SIMULINK (PART A)

Note: You can download the completed file here, or you can follow the steps and do it yourself as well.

 

1.Open MATLAB

Project1_2

2.Open Simulink

Project1_3

3.Create a blank model

Project1_4

4.Save the file in a folder and give both the folder and the file a name without any spaces. Make sure the file path does not have any spaces either.

project11_77

5.In MATLAB homepage, navigate to the folder where the file is stored.

project11_78

6.Go back to the Simulink model. Double-click in the blank area, search for Waijung 2 Target Setup block and import it.

project11_75
project11_76

7.Double click on the block and configure as follows.

project11_79
project11_80

8.Import the following blocks. All block names can be found below the block.

project11_81

9.Connect them as shown.

project11_82

10. Double click the Step block and change sample time to 1.

project11_83

 

11. In the two String Processing blocks, enter the SSID and Password (in single quotes) of the router you want your ESP32 to connect to. You can use the same router / access point that your computer is using to connect to the internet. We will be using WiFi Station mode, since later on we need to send data acquired by ESP32 to the server via MQTT. That can only be done in WiFi Station mode.  

Note: All ESP32 boards only support 2.4GHz bandwidth (except the recently released ESP32-C5 which also supports 5GHz), so make sure the Access Point you connect your ESP32 board to in WiFi STA mode lies in the 2.4GHz frequency range.

project11_84

 

12. For the IP address, Gateway, and Subnet mask, search for "command prompt" or "cmd" and type "ipconfig" to find the configurations and input them into the WiFi Setup block.

Note: For the IP address, choose any number between 0 to 255 for the last 3 digits, except the IPv4 address listed in command prompt (since that is the IP address assigned to your computer). That will then be the IP address assigned to your ESP32 from the router you are connecting to.

project1_STA_mode_1

project11_85

 

13. Open the Enabled Subsystem, which will only be triggered when WiFi connection status is successful. Copy the Stateflow control algorithm from Exercise 6 and paste it in the Enabled Subsystem.

project11_86
project11_87

14. Go to MODELING tab > Model Explorer and change data type of LED to uint32.

project11_116
project11_117

15. Close the Model Explorer and save the file. That is the only way to apply the changes made in the Model Explorer.

16.Import the following blocks. Block names are below each block.

Note: Please import the UIDREAD block only if you have a Waijung 2 Professional or Commercial License.

project11_88

 

17. Configure ADC block as shown.

project11_90

18. Configure DAC block as shown.

project11_91

19. Configure Data Store Memory and Data Store Read blocks.

project11_99
project11_92

20. Configure String Processing block.

Paste the following into the Format text field: '{\"res\":true,\"data\":{\"status\":true,\"res\":\"success\",\"data\":{\"adc\":\"%d\"}}}'

project11_93

21. Configure String Processing block.

Note:

Paste the following into the Format text field: 'mode = %d , threshold = %d , brightness = %d , mac = %s\n' if you have the UIDREAD block imported.

Paste the following into the Format text field: 'mode = %d , threshold = %d , brightness = %d\n' if you do not have the UIDREAD block imported.

project11_94

22. Configure Print block.

project11_102

23.Configure Demux block.

project11_95

24. Configure Aimagin Connect Custom mode block as shown.

Use the following modes in this order: set_brightness, set_threshold, set_mode, get_gauge

project11_96

25. Rename the 4 function-call subsystems to the same name as modes.

project11_97

26. Connect all blocks as shown.

project11_98

27. Enter the get_gauge function call subsystem, import the Aimagin Connect Write Request block, configure it and connect the input port coming from the String Processing block.

project11_100

28. Enter the set_mode function call subsystem and import the following blocks.

project11_101
project11_103

29. Configure the String Processing block.

Paste the following into the Format text field: 'set_mode results from connect: %s\n'

project11_104

30.Configure the String Processing block.

Paste the following into the Format text field: '\"mode\":%d'

project11_105

31.Configure the String Processing block.

Paste the following into the Format text field: '{\"res\":true, \"data\": {\"status\": true, \"res\": \"success\", \"data\": {}}}'

project11_106

32.Configure the String Processing block.

Paste the following into the Format text field: 'set_mode sscanf results: mode = %d\n'

project11_107

33. Configure both Print blocks.

project11_108

34. Configure Data Store Write block.

project11_109

35. Connect all blocks as shown below.

project11_110

36. Copy all the blocks (except function block) from set_mode subsystem, go to set_threshold subsystem and paste it there.

project11_111
project11_112

37. Repeat steps performed with the String Processing blocks in the set_mode function-call subsystem with some modifications:

'set_mode results from connect: %s\n' --> 'set_threshold results from connect: %s\n'

'\"mode\":%d' --> '\"threshold\":\"%u\"'

'set_mode sscanf results: mode = %d\n' --> 'set_threshold sscanf results: threshold = %d\n'

38. Change Data Store Write block from mode to threshold.

project11_113

39. Copy all the blocks (except function block) from set_threshold subsystem, go to set_brightness subsystem and paste it there.

40. Repeat steps performed with the String Processing blocks in the set_threshold function-call subsystem with some modifications:

'set_threshold results from connect: %s\n' --> 'set_brightness results from connect: %s\n'

'\"threshold\":\"%u\"' --> '\"brightness\":\"%u\"'

'set_threshold sscanf results: threshold = %d\n' --> 'set_brightness sscanf results: brightness = %d\n'

41. Change Data Store Write block from threshold to brightness

project11_114

42. Connect your ESP32 to your computer. After connecting, check the Waijung 2 Target Setup block to make sure the COM port selected is of the ESP32.

project11_115

43. Make sure your SD card is also plugged into the ESP32.

44.Go under ‘APPS’ and click ‘Embedded Coder’.

project11_120

45. Click ‘Build’.

project11_121

46. Once build is successful, serial monitor will open automatically. Copy the MAC address of your ESP32 shown in the serial monitor and store it somewhere. We will use this later.

project11_118
project11_119

47.Go to the IP address that you set in the WiFi Setup block and login with your credentials.

project11_122

48. You will see your webapp published in the left menu bar. Click it to view your control interface. ESP32 is now acting as a stand-alone data acquisition device as well as a web-based controller. Enjoy playing around..

project11_123
project11_124

 

We have so far been able to design a web application using Aimagin Connect, which then runs in ESP32's embedded web server to both control ESP32 and acquire data from it simultaneously. Now we will move on to configuring the Aimagin Analytics server (Aimagin Connect is a part of Aimagin Analytics) such that it can receive data from ESP32 and store it.

 

PART 4: CONFIGURING AIMAGIN ANALYTICS SERVER

Note: To understand this section and the steps, it is important to understand what MQTT protocol is and how it works. We have tried to explain it as much as possible through the steps that will be shown but more background research will help you.

 

1.Go to https://bidev.aimagin.com/ and sign in to your Aimagin Analytics account using your credentials.

2.Create a new table under Analytical database.

project11_125
project11_126

3.Give a name to the table, import this table structure file after downloading it, and create the table.

project11_127
project11_128

4.Create a topic to publish data to.

project11_129

5.Set up the topic.

project11_130
project11_131
project11_132

6.Do data mapping.

project11_133
project11_134

7.Topic is created.

project11_135

8.Set permission for the topic created by going to Edge Device group.

project11_136

9.Create a new group and set permissions

project11_137
project11_138

10. Register the device (ESP32) by going to Edge devices and creating a new device.

project11_139

11. Enter device details. Enter the MAC address saved earlier. Create a username and password and save it, you will need this later.

project11_140

12. Device created.

project11_141

 

Server configuration is now complete. The Aimagin Analytics Server includes the MQTT broker and also acts as a Client by subscribing to the topic the ESP32 (also a Client) will be sending information to. A lot of the information entered in this section to configure the server to receive information will be used in the next section, which will also be the final section of this project.

 

PART 5: DESIGN FIRMWARE ON MATLAB/SIMULINK (PART B)

Note: You can download the completed file here, or you can follow the steps and do it yourself as well.

 

We will be adding a few blocks to the Simulink model designed in Part 3 of this project. These blocks will be related to adding MQTT functionality to the model so that the ESP32 can communicate with the server and send data.

 

1.Open the model saved in Part 3 of this project.

project11_142

2.Import the blocks shown. Block names are below each block. To import the 'convert' block, search for 'Data Type Conversion'.

project11_143

3.Refer back to this step of configuring the server. Recall that out of the 7 columns in our database table, 6 of them will be updated with information coming from the server. It is only the LDR value that will be sent from the ESP32. We need to send the LDR value in JSON format, and we have defined the "key" on the server as 'LDR_value'. The key entered here should match the key sent from ESP32.

project11_133

4.Double click the JSON block and configure as shown. We are configuring the data type column to be int32 because the LDR value this block receives will be from the ADC block, which will output values between 0 and 4095, making it an int32 type.

project11_144

5.Enter the JSON key in String Processing block and connect to JSON block.

project11_145

6.Double click the Data Type Conversion block and change it to int32 type.

project11_146

7.Connect ADC block output to it and then connect that to JSON block.

project11_147

8.Double click MQTT Setup block and configure according to information entered in the server under Edge Devices.

Host: bidev.aimagin.com

Port: 1883

MQTT Client ID: MAC address of ESP32

Keep Alive Time: 90

Username: Need to add "esp32-" before adding username.

Password: If you haven't noted down the password when you were first setting up the Edge Device page, you will need to generate a new password. Make sure to update the Edge Device page after you generate a new password and copy that to the Password field in the MQTT Setup block.

project11_148
project11_149

9.Double click MQTT Publish block and configure according to information entered in the server under Topics. We will be publishing data to server every 10 seconds.

project11_150

10. Connect blocks as shown.

project11_151

11. Configure String Processing block.

Format: 'MQTT publish status = %s'

project11_152

12. Configure Print block.'

project11_153

13. Connect blocks as shown.

project11_154

14. Save the file.

15. Go under ‘APPS’ and click ‘Embedded Coder’.

project11_120

16. Click ‘Build’.

project11_121

17. Once Build is successful, go to the IP address that you set in the WiFi Setup block, login with your credentials, open your web application.

project11_155

18. Go to https://bidev.aimagin.com/ and open Analytical Database.

project11_156

19. Go to Data tab > Select the table you created.

project11_157

20. Watch information entering database every 10 seconds (as we configured) along with real-time web control and acquisition.

project11_158

 

 

This is the end of Project 11.

Copyright 2024 Aimagin Co.,Ltd. Rev.1680