-
Notifications
You must be signed in to change notification settings - Fork 80
/
Copy pathArduinoIoTCloudTCP.h
127 lines (100 loc) · 3.9 KB
/
ArduinoIoTCloudTCP.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
/*
This file is part of ArduinoIoTCloud.
Copyright 2019 ARDUINO SA (http://www.arduino.cc/)
This software is released under the GNU General Public License version 3,
which covers the main part of arduino-cli.
The terms of this license can be found at:
https://www.gnu.org/licenses/gpl-3.0.en.html
You can be released from the requirements of the above licenses by purchasing
a commercial license. Buying such a license is mandatory if you want to modify or
otherwise use the software for commercial activities involving the Arduino
software without disclosing the source code of your own applications. To purchase
a commercial license, send an email to [email protected].
*/
#ifndef ARDUINO_IOT_CLOUD_TCP_H
#define ARDUINO_IOT_CLOUD_TCP_H
#include <ArduinoIoTCloud.h>
#include <Arduino_ConnectionHandler.h>
#ifdef BOARD_HAS_ECCX08 /
#include <ArduinoBearSSL.h>
#elif defined(BOARD_ESP)
#include <WiFiClientSecure.h>
#endif
#include <ArduinoMqttClient.h>
static char const DEFAULT_BROKER_ADDRESS_SECURE_AUTH[] = "mqtts-sa.iot.arduino.cc";
static uint16_t const DEFAULT_BROKER_PORT_SECURE_AUTH = 8883;
static char const DEFAULT_BROKER_ADDRESS_USER_PASS_AUTH[] = "mqtts-up.iot.arduino.cc";
static uint16_t const DEFAULT_BROKER_PORT_USER_PASS_AUTH = 8884;
class ArduinoIoTCloudTCP: public ArduinoIoTCloudClass {
public:
ArduinoIoTCloudTCP();
~ArduinoIoTCloudTCP();
int connect();
bool disconnect();
int connected();
void update();
void connectionCheck();
void printDebugInfo();
#ifdef BOARD_HAS_ECCX08
int begin(TcpIpConnectionHandler & connection, String brokerAddress = DEFAULT_BROKER_ADDRESS_SECURE_AUTH, uint16_t brokerPort = DEFAULT_BROKER_PORT_SECURE_AUTH);
#else
int begin(TcpIpConnectionHandler & connection, String brokerAddress = DEFAULT_BROKER_ADDRESS_USER_PASS_AUTH, uint16_t brokerPort = DEFAULT_BROKER_PORT_USER_PASS_AUTH);
#endif
int begin(String brokerAddress = DEFAULT_BROKER_ADDRESS_SECURE_AUTH, uint16_t brokerPort = DEFAULT_BROKER_PORT_SECURE_AUTH);
// Class constant declaration
static const int MQTT_TRANSMIT_BUFFER_SIZE = 256;
#ifdef BOARD_ESP
inline void setBoardId(String const device_id) {
_device_id = device_id;
}
inline void setSecretDeviceKey(String const password) {
_password = password;
}
#endif
inline TcpIpConnectionHandler * getConnection() {
return _connection;
}
String getBrokerAddress() {
return _brokerAddress;
}
uint16_t getBrokerPort() {
return _brokerPort;
}
// Clean up existing Mqtt connection, create a new one and initialize it
int reconnect();
protected:
friend class CloudSerialClass;
// Used to initialize MQTTClient
void mqttClientBegin();
// Function in charge of perform MQTT reconnection, basing on class parameters(retries,and timeout)
bool mqttReconnect(int const maxRetries, int const timeout);
// Used to retrieve last values from _shadowTopicIn
int writeStdout(const byte data[], int length);
int writeProperties(const byte data[], int length);
int writeShadowOut(const byte data[], int length);
void requestLastValue();
private:
TcpIpConnectionHandler * _connection;
String _brokerAddress;
uint16_t _brokerPort;
#ifdef BOARD_HAS_ECCX08
BearSSLClient* _sslClient;
#elif defined(BOARD_ESP)
WiFiClientSecure* _sslClient;
String _password;
#endif
MqttClient* _mqttClient;
// Class attribute to define MTTQ topics 2 for stdIn/out and 2 for data, in order to avoid getting previous pupblished payload
String _stdinTopic;
String _stdoutTopic;
String _shadowTopicOut;
String _shadowTopicIn;
String _dataTopicOut;
String _dataTopicIn;
String _otaTopic;
static void onMessage(int length);
void handleMessage(int length);
void sendPropertiesToCloud();
};
extern ArduinoIoTCloudTCP ArduinoCloud;
#endif