-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
83 lines (72 loc) · 2.4 KB
/
main.cpp
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#include "MQTT.hpp"
#include "dotenv.hpp"
#include "randint.hpp"
#include <cstdio>
#include <iostream>
#include <stdexcept>
const char *g_clientId{"BBB"};
const char *g_template_topic{"{}/feeds/temperature"};
const int g_publishMessageTimeoutMs{1000};
struct AdafruitEnvs
{
const std::string adafruit_address;
const std::string adafruit_username;
const std::string adafruit_key;
};
static AdafruitEnvs get_adafruit_envs();
int main()
{
try
{
const auto [adafruit_address, adafruit_username, adafruit_key]{get_adafruit_envs()};
if (adafruit_address.empty())
{
/* Even when using a local mqtt broker, the local ip address must be provided */
throw std::logic_error{"Env variable ADAFRUIT_ADDRESS not provided"};
}
std::string topic{g_template_topic};
topic.replace(topic.find("{}"), 2, adafruit_username.empty() ? "user" : adafruit_username);
char temperature[3]; /* 2 chars + null character */
snprintf(temperature, 3, "%d", randint(15, 40));
MQTT::Client client(adafruit_address.c_str(), g_clientId);
client.connect(adafruit_username.empty() ? nullptr : adafruit_username.c_str(),
adafruit_key.empty() ? nullptr : adafruit_key.c_str());
client.publishMessage(topic.c_str(), temperature, g_publishMessageTimeoutMs);
client.disconnect();
}
catch (const MQTT::Error &e)
{
std::cerr << "MQTT Exception:" << e.what() << '\n';
}
catch (const std::exception &e)
{
std::cerr << e.what() << '\n';
}
}
static AdafruitEnvs get_adafruit_envs()
{
const auto envs{dotenv::parse()};
std::string adafruit_address{};
auto adafruit_address_it{envs.find("ADAFRUIT_ADDRESS")};
if (adafruit_address_it != envs.cend())
{
adafruit_address = adafruit_address_it->second;
}
std::string adafruit_username{};
auto adafruit_username_it{envs.find("ADAFRUIT_USERNAME")};
if (adafruit_username_it != envs.cend())
{
adafruit_username = adafruit_username_it->second;
}
std::string adafruit_key{};
auto adafruit_key_it{envs.find("ADAFRUIT_KEY")};
if (adafruit_key_it != envs.cend())
{
adafruit_key = adafruit_key_it->second;
}
return {
.adafruit_address = adafruit_address,
.adafruit_username = adafruit_username,
.adafruit_key = adafruit_key,
};
}