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 pathSimpleInternetThing.h
79 lines (63 loc) · 2.24 KB
/
SimpleInternetThing.h
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
/*
SimpleInternetThing.h - Simplifies building an IoT thing.
*/
#ifndef SimpleInternetThing_h
#define SimpleInternetThing_h
#include "Arduino.h"
#include <WiFi.h>
#include <WiFiClientSecure.h>
#include <PubSubClient.h> // https://github.com/rovale/pubsubclient, a fork with some changes enabling MQTT OTA updates.
#include <ArduinoJson.h> // Take a version 5, not the version 6 beta
#define SIMPLE_INTERNET_THING_RECONNECT_DELAY 10000
#define SIMPLE_INTERNET_THING_SYSTEM_MESSAGE_INTERVAL 60000
class SimpleInternetThing
{
public:
SimpleInternetThing(
const char *mqttTopicBase,
const char *thingId, const char *thingName, const char *version,
const char *wiFiSsid, const char *wiFiPassword,
const char *mqttServer, uint16_t mqttPort,
const char *caCert,
const char *mqttUsername, const char *mqttPassword,
int indicatorLedPin);
void setup();
void loop();
void publishData(const char *subject, String data);
void onCommand(std::function<void(String, JsonObject &)> callback);
void inverseIndicatorLed();
private:
const char *_wiFiSsid;
const char *_wiFiPassword;
const char *_mqttServer;
uint16_t _mqttPort;
const char *_caCert;
const char *_thingId;
const char *_thingName;
const char *_mqttUsername;
const char *_mqttPassword;
const char *_mqttTopicBase;
int _indicatorLedPin;
bool _inverseIndicatorLed;
const char *_version;
WiFiClientSecure _wiFiClient;
PubSubClient _mqttClient;
String createTopic(const char *subject);
unsigned long _lastReconnectAttemptAt;
void stayConnected();
unsigned long _sequenceNumber = 0;
unsigned long _wiFiDisconnects = -1;
unsigned long _mqttDisconnects = -1;
String createStatusMessage(bool isOnline);
void turnIndicatorLedOn();
void turnIndicatorLedOff();
void subscribe(String topic, int qos);
void publish(String topic, String message, bool retain);
std::function<void(String, JsonObject &)> _commandCallback;
void onReceive(char *topic, unsigned long length);
void handleOtaUpdate(unsigned long length);
String createOtaUpdateProgressMessage(String message);
unsigned long _lastSystemMessageAt;
String createSystemMessage();
};
#endif