-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathesp8266.ino
166 lines (119 loc) · 3.85 KB
/
esp8266.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
#include <ESP8266WiFi.h>
#include <AsyncMqttClient.h>
//MQTT Settings
AsyncMqttClient mqttClient;
#define MQTT_RELAY1_TOPIC "CMAXMEDIA/SMS"
#define MQTT_RELAY1_TOPIC_1 "CMAXMEDIA/SMS/RECEIVED"
#define MQTT_LASTWILL_TOPIC "CMAXMEDIA/LIGHT/lastwill"
#define MQTT_DEVICE_ID "SMSTerminal"
void setup() {
// put your setup code here, to run once:
Serial.begin( 9600 );
blinkit();
pinMode( LED_BUILTIN, OUTPUT );
//Setup WiFi
WiFi.persistent(false);
WiFi.mode(WIFI_STA);
WiFi.begin(MySSID , MyPSK );
while (WiFi.status() != WL_CONNECTED) {
delay(500);
}
blinkit();
blinkit();
//Setup MQTT - Thnx to Spiess A.
mqttClient.onConnect(onMqttConnect);
mqttClient.onDisconnect(onMqttDisconnect);
mqttClient.onMessage(onMqttMessage);
mqttClient.onPublish(onMqttPublish);
mqttClient.setServer(IPAddress(192, 168, 0, 75), 1883);
mqttClient.setKeepAlive(5).setCleanSession(false).setWill(MQTT_LASTWILL_TOPIC, 2, true, "my wife will get all my money").setCredentials(MyMQTTUser, MyMQTTPass).setClientId( MQTT_DEVICE_ID );
mqttClient.connect();
blinkit();
blinkit();
}
void loop() {
String serialData = readSerial();
if( serialData!="" ) {
//incoming SMS
if( serialData.startsWith( "SMSReceived" ) ) {
blinkfast();
blinkfast();
blinkfast();
//make payload
String part1 = getValue( serialData, "_", 1 );
String part2 = getValue( serialData, "_", 2 );
String part3 = part1 + " von " + part2;
char test[100];
part3.toCharArray( test, 100 );
//publish payload over MQTT
mqttClient.publish(MQTT_RELAY1_TOPIC_1, 1, false, test );
}
}
}
void onMqttConnect(bool sessionPresent) {
mqttClient.subscribe(MQTT_RELAY1_TOPIC, 1);
}
void onMqttDisconnect(AsyncMqttClientDisconnectReason reason) {
mqttClient.connect();
}
void onMqttSubscribe(uint16_t packetId, uint8_t qos) {}
void onMqttUnsubscribe(uint16_t packetId) {}
void onMqttMessage(char* topic, char* payload, AsyncMqttClientMessageProperties properties, size_t len, size_t index, size_t total) {
//incoming topic
if(strcmp( topic, MQTT_RELAY1_TOPIC )==0 ) {
String writeString = "PrepareSMS_";
writeString += payload;
//write data to MKR1400
Serial.println( writeString );
delay( 1000 );
}
}
void onMqttPublish(uint16_t packetId) {
}
//shared functions
String getValue(String data, String separator, int index)
{
int found = 0;
int strIndex[] = { 0, -1 };
int maxIndex = data.length() - 1;
for (int i = 0; i <= maxIndex && found <= index; i++) {
if ( (String) data.charAt(i) == separator || i == maxIndex) {
found++;
strIndex[0] = strIndex[1] + 1;
strIndex[1] = (i == maxIndex) ? i+1 : i;
}
}
return found > index ? data.substring(strIndex[0], strIndex[1]) : "";
}
String readSerial() {
char rc;
String receivedString;
while (Serial.available() > 0) {
delay( 3 );
rc = Serial.read();
receivedString += rc;
}
return receivedString;
}
String readSerial1() {
char rc;
String receivedString;
while (Serial1.available() > 0) {
delay( 3 );
rc = Serial1.read();
receivedString += rc;
}
return receivedString;
}
void blinkit() {
digitalWrite(LED_BUILTIN, LOW); // turn the LED on (HIGH is the voltage level)
delay(1000); // wait for a second
digitalWrite(LED_BUILTIN, HIGH); // turn the LED off by making the voltage LOW
delay(1000); // wait for a second
}
void blinkfast() {
digitalWrite(LED_BUILTIN, LOW); // turn the LED on (HIGH is the voltage level)
delay(150); // wait for a second
digitalWrite(LED_BUILTIN, HIGH); // turn the LED off by making the voltage LOW
delay(150); // wait for a second
}