This repository has been archived by the owner on Sep 21, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSimpleSensorBox.cpp
102 lines (85 loc) · 2.94 KB
/
SimpleSensorBox.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#include <SimpleInternetThing.h> // SimpleInternetThing library (https://github.com/rovale/SimpleInternetThing).
#include <Secrets.h> // Add a secrets file.
/* Example of Secrets.h:
const char ssid[] = "SomeSsid";
const char wiFiPassword[] = "SomeWiFiPassword";
const char mqttServer[] = "SomeMqttServer";
const int mqttPort = 8883;
const char mqttUsername[] = "SomeMqttUserName";
const char mqttPassword[] = "SomeMqttPassword";
const char caCert[] =
"-----BEGIN CERTIFICATE-----\n"
"MIIC8jCCAdqgAwIBAgIJALg8W9O6YOtfMA0GCSqGSIb3DQEBCwUAMA0xCzAJBgNV\n"
"BAYTAk5MMCAXDTE5MDEyNjIzMDMwM1oYDzIxMDEwMzE3MjMwMzAzWjANMQswCQYD\n"
"................................................................\n"
"LQTk2zh0r+SAiN+6B9LcKyqpf9zWQRlirNHAWeDJu8wiHBgqW2qOEkcDCNVpORC4\n"
"J2XE53jAa5TLsaTrHjyxr9ztVdzVTqwvUEQPiFGn7zw8iq+RkmY=\n"
"-----END CERTIFICATE-----\n";
*/
const char mqttTopicBase[] = "somebuilding/someroom";
const char id[] = "ssb1"; // This is added to the MQTT topic name.
const char name[] = "An example of a simple sensor box";
const char version[] = "0.0.1";
const int indicatorLedPin = 25;
SimpleInternetThing simpleSensorBox(
mqttTopicBase, id, name, version,
ssid, wiFiPassword,
mqttServer, mqttPort, caCert, mqttUsername, mqttPassword,
indicatorLedPin);
const int lightPin = 38;
const int rainPin = 35;
unsigned long telemetryInterval = 30000;
unsigned long lastTelemetryMessageAt = -1 * telemetryInterval;
// Triggered when a command message is sent to 'somebuilding/someroom/ssb1/command'.
// Example: '{"name": "updateSettings", "telemetryInterval": 15000}'.
void onCommand(String commandName, JsonObject &root)
{
if (commandName == "updateSettings")
{
if (root.is<unsigned long>("telemetryInterval"))
{
telemetryInterval = root.get<unsigned long>("telemetryInterval");
Serial.print("Changed telemetry interval to ");
Serial.print(telemetryInterval);
Serial.println(".");
}
}
else
{
Serial.println("Unknown command.");
}
}
void setup()
{
Serial.begin(115200);
Serial.println();
Serial.print("Sensor ");
Serial.print(id);
Serial.print(" version ");
Serial.print(version);
Serial.println(".");
simpleSensorBox.setup();
simpleSensorBox.onCommand(onCommand);
pinMode(lightPin, INPUT);
}
String getClimateMessage()
{
StaticJsonBuffer<400> jsonBuffer;
JsonObject &jsonObject = jsonBuffer.createObject();
jsonObject["light"] = analogRead(lightPin);
jsonObject["rain"] = analogRead(rainPin);
String jsonString;
jsonObject.printTo(jsonString);
return jsonString;
}
void loop()
{
simpleSensorBox.loop();
if (millis() - lastTelemetryMessageAt >= telemetryInterval)
{
lastTelemetryMessageAt = millis();
// Publishes telemetry data to 'somebuilding/someroom/ssb1/telemetry/climate'.
// Example: '{"light":79,"rain":23}'.
simpleSensorBox.publishData("telemetry/climate", getClimateMessage());
}
}