-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGSMCode.ino
233 lines (190 loc) · 5.4 KB
/
GSMCode.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
/*
Rui Santos
Complete project details at https://RandomNerdTutorials.com/esp32-cloud-mqtt-broker-sim800l/
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files.
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
*/
// Select your modem:
#define TINY_GSM_MODEM_SIM800 // Modem is SIM800L
// Set serial for debug console (to the Serial Monitor, default speed 115200)
#define SerialMon Serial
// Set serial for AT commands
#define SerialAT Serial1
// Define the serial console for debug prints, if needed
#define TINY_GSM_DEBUG SerialMon
// set GSM PIN, if any
#define GSM_PIN ""
// Your GPRS credentials, if any
const char apn[] = "internet.ng.airtel.com"; // APN (example: internet.vodafone.pt) use https://wiki.apnchanger.org
const char gprsUser[] = "";
const char gprsPass[] = "";
// SIM card PIN (leave empty, if not defined)
const char simPIN[] = "";
// MQTT details
const char *broker = "broker.hivemq.com"; // Public IP address or domain name
const char *mqttUsername = "Changeover1"; // MQTT username
const char *mqttPassword = "frankyboo"; // MQTT password
const char *topicOutput1 = "frankyboo/Changeover1";
//const char *topicOutput2 = "esp/output2";
const char *topicTemperature = "frankyboo/temperature";
// Define the serial console for debug prints, if needed
//#define DUMP_AT_COMMANDS
#include <Wire.h>
#include <TinyGsmClient.h>
#ifdef DUMP_AT_COMMANDS
#include <StreamDebugger.h>
StreamDebugger debugger(SerialAT, SerialMon);
TinyGsm modem(debugger);
#else
TinyGsm modem(SerialAT);
#endif
#include <PubSubClient.h>
TinyGsmClient client(modem);
PubSubClient mqtt(client);
// TTGO T-Call pins
#define MODEM_RST 7
//#define MODEM_PWKEY 4
//#define MODEM_POWER_ON 23
#define MODEM_TX 9
#define MODEM_RX 8
#define OUTPUT_1 2
#define OUTPUT_2 15
uint32_t lastReconnectAttempt = 0;
float temperature = 0;
float humidity = 0;
long lastMsg = 0;
void mqttCallback(char *topic, byte *message, unsigned int len)
{
Serial.print("Message arrived on topic: ");
Serial.print(topic);
Serial.print(". Message: ");
String messageTemp;
for (int i = 0; i < len; i++)
{
Serial.print((char)message[i]);
messageTemp += (char)message[i];
}
Serial.println();
// Feel free to add more if statements to control more GPIOs with MQTT
// If a message is received on the topic esp/output1, you check if the message is either "true" or "false".
// Changes the output state according to the message
if (String(topic) == "frankyboo/Changeover1")
{
Serial.print("Changing output to ");
if (messageTemp == "true")
{
Serial.println("true");
digitalWrite(OUTPUT_1, HIGH);
}
else if (messageTemp == "false")
{
Serial.println("false");
digitalWrite(OUTPUT_1, LOW);
}
}
}
boolean mqttConnect()
{
SerialMon.print("Connecting to ");
SerialMon.print(broker);
// Connect to MQTT Broker without username and password
//boolean status = mqtt.connect("GsmClientN");
// Or, if you want to authenticate MQTT:
boolean status = mqtt.connect("GsmClientN", mqttUsername, mqttPassword);
if (status == false)
{
SerialMon.println(" fail");
ESP.restart();
return false;
}
SerialMon.println(" success");
mqtt.subscribe(topicOutput1);
mqtt.subscribe(topicOutput2);
return mqtt.connected();
}
void setup()
{
// Set console baud rate
SerialMon.begin(115200);
delay(10);
// Set modem reset, enable, power pins
// pinMode(MODEM_PWKEY, OUTPUT);
pinMode(MODEM_RST, OUTPUT);
// pinMode(MODEM_POWER_ON, OUTPUT);
// digitalWrite(MODEM_PWKEY, LOW);
digitalWrite(MODEM_RST, HIGH);
// digitalWrite(MODEM_POWER_ON, HIGH);
// pinMode(OUTPUT_1, OUTPUT);
// pinMode(OUTPUT_2, OUTPUT);
SerialMon.println("Wait...");
// Set GSM module baud rate and UART pins
SerialAT.begin(115200, SERIAL_8N1, MODEM_RX, MODEM_TX);
delay(6000);
// Restart takes quite some time
// To skip it, call init() instead of restart()
SerialMon.println("Initializing modem...");
modem.restart();
// modem.init();
String modemInfo = modem.getModemInfo();
SerialMon.print("Modem Info: ");
SerialMon.println(modemInfo);
// Unlock your SIM card with a PIN if needed
if (GSM_PIN && modem.getSimStatus() != 3)
{
modem.simUnlock(GSM_PIN);
}
SerialMon.print("Connecting to APN: ");
SerialMon.print(apn);
if (!modem.gprsConnect(apn, gprsUser, gprsPass))
{
SerialMon.println(" fail");
ESP.restart();
}
else
{
SerialMon.println(" OK");
}
if (modem.isGprsConnected())
{
SerialMon.println("GPRS connected");
}
// MQTT Broker setup
mqtt.setServer(broker, 1883);
mqtt.setCallback(mqttCallback);
}
void loop()
{
if (!mqtt.connected())
{
SerialMon.println("=== MQTT NOT CONNECTED ===");
// Reconnect every 10 seconds
uint32_t t = millis();
if (t - lastReconnectAttempt > 10000L)
{
lastReconnectAttempt = t;
if (mqttConnect())
{
lastReconnectAttempt = 0;
}
}
delay(100);
return;
}
long now = millis();
if (now - lastMsg > 30000)
{
lastMsg = now;
// Convert the value to a char array
char humString[8];
dtostrf(22, 1, 2, humString);
Serial.print("Humidity: ");
Serial.println(humString);
mqtt.publish(topicHumidity, humString);
}
mqtt.loop();
}
{
"mode" : "full", "isActive" : false
}