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

  1. Open editor and goto smartHome.conf

    Change the uri (and maybe as well as port) term into the correspoding ip address where the mqtt server holds.

    1
    2
    3
    4
    [mqtt]
    uri=tcp://192.168.114.107:1883
    [server]
    port=8888

    As 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.

  2. In mqtt.c

    Change the following terms in preprocessing derivatives

    • ADDRESS
    • MQTT_SUB_TOPIC
      1
      2
      #define ADDRESS "tcp://192.168.114.107:1883"
      #define MQTT_SUB_TOPIC "1663401252952/AIOTSIM2APP"
  3. In main.c

    Change the following terms in preprocessing derivatives

    • CTRL_PUB_TOPIC
    • SUB_TOPIC
      1
      2
      #define CTRL_PUB_TOPIC "1663401252952/APP2AIOTSIM"
      #define SUB_TOPIC "1663401252952/AIOTSIM2APP"

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

  1. Estabilish Connection

    In main.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 original publication topic is set to default, and is not used since. Thus I highly doubt this is the right way.

  2. 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.



    In mqtt.c, when the function get_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.

    The value handling for number types is simple, however if string (char array) is involved, it should be consider whether the comparison should be handled instantly, or assign new memory for it.


    As both comparison are fulltext, thus the time complexity are both O(N) to either assign and copy the string content, or make a instant comparison. The latter only need to store a single digit to represent the result. And remember to deal with fallback situations, especially for certain kind of msg that will not constantly update its value.


  3. 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.

  4. Report

    I think there’s no need for explaination on this on.

Modification suggestions

  1. Change Structure definition in mqtt.h.

  2. Add Data handdling in transfer_virtual_data()

  3. Add preset behavior in main.c

  4. Change conditions and exception handling in main()

  5. Fix bad posix Don’t waste time on it