-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathEMAR-RCP.ino
150 lines (115 loc) · 3.54 KB
/
EMAR-RCP.ino
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
/*
Project: Peter Moss COVID-19 AI Research Project
Repository: EMAR Mini, Emergency Assistance Robot
Author: Adam Milton-Barker (AdamMiltonBarker.com)
Contributors:
Title: EMAR Mini Remote Control iotJumpWay Publisher
Description: The EMAR Mini Remote Control iotJumpWay Publisher
receives IR commands from the Remote Control Receiver
via serial and sends them to the iotJumpWay.
License: MIT License
Last Modified: 2020-07-04
Credit: Based on example code from www.elegoo.com
*/
#include <ArduinoJson.h>
#include <ESP8266WiFi.h>
#include <PubSubClient.h>
#include <WiFiClientSecure.h>
static const char hias_cert[] PROGMEM = R"EOF(
-----BEGIN CERTIFICATE-----
-----END CERTIFICATE-----
)EOF";
const char fingerprint[] PROGMEM = "";
const char* ssid = "";
const char* password = "";
const char* mqtt_server = "";
int mqttPort = 8883;
String locationID = "";
String applicationID = "";
char applicationName[] = "";
char mqttUsername[] = "";
char mqttPassword[] = "";
char willTopic[50];
String zoneID = "";
String deviceID = "";
BearSSL::WiFiClientSecure espClient;
PubSubClient client(espClient);
BearSSL::X509List cert(hias_cert);
char charBuf[50];
void setupWiFi() {
delay(10);
Serial.println();
Serial.print("Connecting to WiFi network: ");
Serial.println(ssid);
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected succesfully!");
Serial.print("Device IP address: ");
Serial.println(WiFi.localIP());
}
void callback(char* topic, byte* payload, unsigned int length) {
Serial.println("Unused");
}
void publishToApplicationStatus(const char* data){
String statusTopic = locationID+"/Applications/"+applicationID+"/Status";
statusTopic.toCharArray(charBuf, 50);
client.publish(charBuf, data);
}
void publishToDeviceCommands(const char* data){
String commandTopic = locationID+"/Devices/"+zoneID+"/"+deviceID+"/Commands";
commandTopic.toCharArray(charBuf, 50);
client.publish(charBuf, data);
}
void reconnect() {
while (!client.connected()) {
Serial.println("Attempting connection to HIAS iotJumpWay Broker...");
String willTopicString = locationID+"/Applications/"+applicationID+"/Status";
willTopicString.toCharArray(willTopic, 50);
if (client.connect(applicationName, mqttUsername, mqttPassword, willTopic, 0, 0, "OFFLINE")) {
Serial.println("Connected to HIAS iotJumpWay Broker!");
publishToApplicationStatus("ONLINE");
} else {
Serial.print("Failed to connect to HIAS iotJumpWay Broker, rc=");
Serial.println(client.state());
Serial.println("... trying again in 5 seconds");
delay(5000);
}
}
}
void setup() {
Serial.begin(9600);
setupWiFi();
//espClient.setTrustAnchors(&cert);
espClient.setFingerprint(fingerprint);
client.setServer(mqtt_server, mqttPort);
client.setCallback(callback);
}
void loop() {
if (!client.connected()) {
reconnect();
}
client.loop();
while (Serial.available() > 0)
{
String jsReceived = Serial.readStringUntil('\n');
StaticJsonDocument<200> jsonBuffer;
auto error = deserializeJson(jsonBuffer, jsReceived);
if(error)
{
Serial.println("Command Not Sent");
}
else
{
char jsData[89];
jsReceived.toCharArray(jsData, 89);
publishToDeviceCommands(jsData);
Serial.println("Command Sent");
}
}
delay(1000);
}