Skip to content
This repository was archived by the owner on Jan 11, 2020. It is now read-only.

Commit ee3ace6

Browse files
committed
Add mqttGuard-ed example
1 parent e120af0 commit ee3ace6

File tree

1 file changed

+166
-0
lines changed

1 file changed

+166
-0
lines changed
Lines changed: 166 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
1+
//Define DEBUG to get the Output from DEBUG_PRINTLN
2+
#define DEBUG 1
3+
4+
#include <sstream>
5+
6+
//Include Basecamp in this sketch
7+
#include <Basecamp.hpp>
8+
#include <Configuration.hpp>
9+
10+
// Create a new Basecamp instance called iot that will start the ap in secure mode and the webserver ui only in setup mode
11+
Basecamp iot{Basecamp::SetupModeWifiEncryption::secured, Basecamp::ConfigurationUI::accessPoint};
12+
// Uncomment the following line and comment to one above to start the ESP with open wifi and always running config ui
13+
// Basecamp iot;
14+
15+
//Variables for the sensor and the battery
16+
static const int ResetPin = 35;
17+
static const int SensorPin = 32;
18+
static const int BatteryPin = 34;
19+
int sensorValue = 0;
20+
//The batteryLimit defines the point at which the battery is considered empty.
21+
int batteryLimit = 3300;
22+
23+
//This is used to control if the ESP should enter sleep mode or not
24+
bool delaySleep = false;
25+
26+
//Variables for the mqtt packages and topics
27+
uint16_t statusPacketIdSub = 0;
28+
String delaySleepTopic;
29+
String statusTopic;
30+
String batteryTopic;
31+
String batteryValueTopic;
32+
33+
// Reset the configuration to factory defaults (all empty)
34+
void resetToFactoryDefaults()
35+
{
36+
DEBUG_PRINTLN("Resetting to factory defaults");
37+
Configuration config(String{"/basecamp.json"});
38+
config.load();
39+
config.resetExcept({ConfigurationKey::accessPointSecret, });
40+
config.save();
41+
}
42+
43+
void setup() {
44+
//configuration of the battery and sensor pins
45+
pinMode(ResetPin, INPUT_PULLDOWN);
46+
pinMode(SensorPin, INPUT_PULLDOWN);
47+
pinMode(BatteryPin, INPUT);
48+
49+
//read the status of the doorsensor as soon as possible to determine the state that triggered it
50+
sensorValue = digitalRead(SensorPin);
51+
52+
bool resetPressed = (digitalRead(ResetPin) == HIGH);
53+
if (resetPressed)
54+
{
55+
resetToFactoryDefaults();
56+
}
57+
58+
// Initialize Basecamp
59+
iot.begin();
60+
// Alternate example: optional initialization with a fixed ap password for setup-mode:
61+
// iot.begin("yoursecurepassword");
62+
63+
if (resetPressed) {
64+
DEBUG_PRINTLN("**** CONFIG HAS BEEN MANUALLY RESET ****");
65+
}
66+
67+
//Configure the MQTT topics
68+
delaySleepTopic = "cmd/" + iot.hostname + "/delaysleep";
69+
statusTopic = "stat/" + iot.hostname + "/status";
70+
batteryTopic = "stat/" + iot.hostname + "/battery";
71+
batteryValueTopic = "stat/" + iot.hostname + "/batteryvalue";
72+
73+
//Set up the Callbacks for the MQTT instance. Refer to the Async MQTT Client documentation
74+
// TODO: We should do this actually _before_ connecting the mqtt client...
75+
iot.mqtt.onConnect(onMqttConnect);
76+
iot.mqttOnPublish(suspendESP);
77+
iot.mqtt.onMessage(onMqttMessage);
78+
}
79+
80+
81+
//This function is called when the MQTT-Server is connected
82+
void onMqttConnect(bool sessionPresent) {
83+
DEBUG_PRINTLN(__func__);
84+
85+
//Subscribe to the delay topic
86+
iot.mqtt.subscribe(delaySleepTopic.c_str(), 0);
87+
//Trigger the transmission of the current state.
88+
transmitStatus();
89+
}
90+
91+
92+
//This function transfers the state of the sensor. That includes the door status, battery status and level
93+
void transmitStatus() {
94+
DEBUG_PRINTLN(__func__);
95+
96+
if (sensorValue == 0) {
97+
DEBUG_PRINTLN("Door open");
98+
//Transfer the current state of the sensor to the MQTT broker
99+
statusPacketIdSub = iot.mqttPublish(statusTopic.c_str(), 1, true, "open" );
100+
//Configure the wakeup pin to wake if the door is closed
101+
esp_sleep_enable_ext0_wakeup((gpio_num_t)SensorPin, 1);
102+
} else {
103+
DEBUG_PRINTLN("Door closed");
104+
//Transfer the current state of the sensor to the MQTT broker
105+
statusPacketIdSub = iot.mqttPublish(statusTopic.c_str(), 1, true, "closed" );
106+
//Configure the wakeup pin to wake if the door is closed
107+
esp_sleep_enable_ext0_wakeup((gpio_num_t)SensorPin, 0);
108+
}
109+
110+
//Read the current analog battery value
111+
sensorValue = analogRead(BatteryPin);
112+
//sensorC stores the battery value as a char
113+
char sensorC[6];
114+
//convert the sensor value to a string
115+
sprintf(sensorC, "%04i", sensorValue);
116+
//Send the sensor value to the MQTT broker
117+
iot.mqttPublish(batteryValueTopic.c_str(), 1, true, sensorC);
118+
//Check the battery level and publish the state
119+
if (sensorValue < batteryLimit) {
120+
DEBUG_PRINTLN("Battery empty");
121+
iot.mqttPublish(batteryTopic.c_str(), 1, true, "empty" );
122+
} else {
123+
DEBUG_PRINTLN("Battery full");
124+
iot.mqttPublish(batteryTopic.c_str(), 1, true, "full" );
125+
}
126+
DEBUG_PRINTLN("Data published");
127+
}
128+
129+
//This topic is called if an MQTT message is received
130+
void onMqttMessage(char* topic, char* payload, AsyncMqttClientMessageProperties properties, size_t len, size_t index, size_t total) {
131+
DEBUG_PRINTLN(__func__);
132+
133+
//Check if the payload eqals "true" and set delaySleep correspondigly
134+
//Since we only subscribed to one topic, we only have to compare the payload
135+
if (strcmp(payload, "true") == 0) {
136+
delaySleep = true;
137+
} else {
138+
delaySleep = false;
139+
}
140+
}
141+
142+
void suspendESP(uint16_t packetId) {
143+
DEBUG_PRINTLN(__func__);
144+
145+
if (delaySleep) {
146+
DEBUG_PRINTLN("Delaying Sleep for manual keep-alive request");
147+
return;
148+
}
149+
150+
if (!iot.mqttAllSent()) {
151+
std::ostringstream debug;
152+
debug << "Waiting for " << iot.mqttRemainingPackets() << " mqtt packets to be sent before going to sleep.";
153+
DEBUG_PRINTLN(debug.str().c_str());
154+
return;
155+
}
156+
157+
DEBUG_PRINTLN("Entering deep sleep");
158+
//properly disconnect from the MQTT broker
159+
iot.mqtt.disconnect();
160+
//send the ESP into deep sleep
161+
esp_deep_sleep_start();
162+
}
163+
164+
void loop()
165+
{
166+
}

0 commit comments

Comments
 (0)