A weird IoT coursework, document for code
It sucks. (Not IoT, but the instructor and the company)
So, this is kinda like the document of that IoT project, but just don’t have too much expectation on this article.
I. Env and tools
Env:
- Linux (should be OK with most of distributions)
- WINDOWS (optinal, because my instance of IoT application is forced to use a simulation app)
Packages:
- paho.mqtt.c-1.3.0
- gcc
- arbitrary text editor or IDE
I forget the rest, but if the compile goes wrong, google it then install the corresponding package works fine
Preparation
- Cannot remember anything that is important.
- maybe open the simulation sofware and the project file to make it easier for test part
II. Configuration
Open editor and goto
smartHome.conf
Change the
uri
(and maybe as well asport
) term into the correspodingip address
where the mqtt server holds.1
2
3
4[mqtt]
uri=tcp://192.168.114.107:1883
[server]
port=8888As I used virtual platform, so the whole thing runs locally. It should work fine with regular network, but the proper configuration of the firewall settings is required, otherwise the connection will be refused.
In
mqtt.c
Change the following terms in
preprocessing derivatives
ADDRESS
MQTT_SUB_TOPIC
1
2
In
main.c
Change the following terms in
preprocessing derivatives
CTRL_PUB_TOPIC
SUB_TOPIC
1
2
This should be the least change to make the code running.
III. Compile and run
To compile the files
1 | make |
Run the executable
1 | ./smart_home |
IV. Customization
Some comments
Estabilish Connection
Inmain.c
mqtt funcions are called to connect to the server and subscribe the topic.The process is mainly done with official means, while in
Callback
function, the originalpublication topic
is set to default, and is not used since. Thus I highly doubt this is the right way.Data Collection
The time interval can be changed as you like, however, the shorter the interval, the more likely some value cannot be updated.
It should be noted that due to some issue, the shorter the interval is, the more likely you’re going to miss data from the very beginning.
Inmqtt.c
, when the functionget_virtual_env()
is not called, every(most)JSON
msg will be captured, and then handled separately with many IFs to store the value into corresponding term in a dedicate structure.
Data Handling
After the data is retrieved into
main
, again accessing the elements one by one to decide corresponding measures, again here, the command can be also preset in preprocessing derivatives for a straight forward code reading experience.Due to the need of showing status and detailed problem, there should be flags to indicate the condition of each sensor values, however, that will be a lot more extra code, so I use a single variable to store the status in sum of powers of 2. To me this is clearer.
Report
I think there’s no need for explaination on this on.