-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathGateway_Ethernet.ino
262 lines (208 loc) · 6.16 KB
/
Gateway_Ethernet.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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
/*
Author: Eric Tsai
License: CC-BY-SA, https://creativecommons.org/licenses/by-sa/2.0/
Date: 7-21-2014
File: Ethernet_Gateway.ino
This sketch takes data struct from I2C and publishes it to
mosquitto broker.
Modifications Needed:
1) Update mac address "mac[]"
2) Update MQTT broker IP address "server[]"
*/
#include <SPI.h>
#include <Ethernet.h>
#include <Wire.h>
#include <PubSubClient.h>
//I2C receive device address
const byte MY_ADDRESS = 42; //I2C comms w/ other Arduino
//Ethernet
byte mac[] = { 0x90, 0xA2, 0xDA, 0x0D, 0x11, 0x11 };
byte server[] = { 192, 168, 1, 51 }; //your MQTT broker IP address
//IPAddress ip(192,168,2,61);
EthernetClient ethClient;
PubSubClient client(server, 1883, callback, ethClient);
unsigned long keepalivetime=0;
unsigned long MQTT_reconnect=0;
//use LED for indicating MQTT connection status.
int led = 13;
bool conn_ok;
void callback(char* topic, byte* payload, unsigned int length) {
// handle message arrived
}
void setup()
{
//ethernet
//Ethernet.begin(mac, ip);
Wire.begin (MY_ADDRESS);
Serial.begin (9600);
Serial.println("starting");
pinMode(led, OUTPUT);
//wait for IP address
while (Ethernet.begin(mac) != 1)
{
Serial.println("Error getting IP address via DHCP, trying again...");
delay(3000);
}
//Ethernet.begin(mac, ip);
Serial.println("ethernet OK");
keepalivetime=millis();
Wire.onReceive (receiveEvent);
while (client.connect("arduinoClient") != 1)
{
Serial.println("Error connecting to MQTT");
delay(3000);
}
MQTT_reconnect = millis();
Serial.println("setup complete");
} // end of setup
volatile struct
{
int nodeID;
int sensorID;
unsigned long var1_usl;
float var2_float;
float var3_float; //
int var4_int;
} SensorNode;
int sendMQTT = 0;
volatile boolean haveData = false;
void loop()
{
//if new data on I2C from RFM gateway received, flag for MQTT publish
if (haveData)
{
Serial.print ("Received Device ID = ");
Serial.println (SensorNode.sensorID);
Serial.print (" Time = ");
Serial.println (SensorNode.var1_usl);
Serial.print (" var2_float ");
Serial.println (SensorNode.var2_float);
sendMQTT = 1;
/*
if (client.connect("arduinoClient"))
{
int varnum = 1;
char buff_topic[6];
char buff_message[6];
sprintf(buff_topic, "%02d%01d%01d", SensorNode.nodeID, SensorNode.sensorID, varnum);
Serial.println(buff_topic);
dtostrf (SensorNode.var2_float, 4, 1, buff_message);
client.publish(buff_topic, buff_message);
}
*/
haveData = false;
} // end if haveData
if (sendMQTT == 1)
{
Serial.println("starting MQTT send");
//check to make sure connected to MQTT before trying to publish data
conn_ok = client.connected();
if (conn_ok==1)
{
digitalWrite(led, HIGH);
Serial.println("MQTT connected OK from MQTT Send");
}
else
{
digitalWrite(led, LOW);
Serial.println("MQTT NOT connected OK from MQTT Send");
}
//no connection, reconnect
if (conn_ok == 0)
{
client.disconnect();
delay(5000);
while (client.connect("arduinoClient") != 1)
{
digitalWrite(led, LOW);
Serial.println("Error connecting to MQTT");
delay(4000);
digitalWrite(led, HIGH);
}
digitalWrite(led, HIGH);
Serial.println("reconnected to MQTT");
MQTT_reconnect = millis();
}
int varnum;
char buff_topic[6];
char buff_message[12];
/*
//send var1_usl
varnum = 2;
buff_topic[6];
buff_message[12];
sprintf(buff_topic, "%02d%01d%01d", SensorNode.nodeID, SensorNode.sensorID, varnum);
Serial.println(buff_topic);
dtostrf (SensorNode.var1_usl, 10, 1, buff_message);
client.publish(buff_topic, buff_message);
*/
//send var2_float
varnum = 2;
buff_topic[6];
buff_message[7];
sprintf(buff_topic, "%02d%01d%01d", SensorNode.nodeID, SensorNode.sensorID, varnum);
Serial.println(buff_topic);
dtostrf (SensorNode.var2_float, 2, 1, buff_message);
client.publish(buff_topic, buff_message);
delay(200);
//send var3_float
varnum = 3;
sprintf(buff_topic, "%02d%01d%01d", SensorNode.nodeID, SensorNode.sensorID, varnum);
Serial.println(buff_topic);
dtostrf (SensorNode.var3_float, 2, 1, buff_message);
client.publish(buff_topic, buff_message);
delay(200);
//send var4_int, RSSI
varnum = 4;
sprintf(buff_topic, "%02d%01d%01d", SensorNode.nodeID, SensorNode.sensorID, varnum);
Serial.println(buff_topic);
sprintf(buff_message, "%04d%", SensorNode.var4_int);
client.publish(buff_topic, buff_message);
sendMQTT = 0;
Serial.println("finished MQTT send");
}//end if sendMQTT
//client.loop needs to run every iteration. Previous version did not. Big opps.
client.loop();
//regularly check MQTT connection (ever x seconds)
if ((millis() - MQTT_reconnect) > 60000)
{
conn_ok = client.connected();
if (conn_ok==1)
{
digitalWrite(led, HIGH);
Serial.println("MQTT connected OK");
}
else
{
digitalWrite(led, LOW);
Serial.println("MQTT NOT connected OK");
}
//no connection, reconnect
if (conn_ok == 0)
{
client.disconnect();
delay(5000);
while (client.connect("arduinoClient") != 1)
{
digitalWrite(led, LOW);
Serial.println("Error connecting to MQTT");
delay(4000);
digitalWrite(led, HIGH);
}
digitalWrite(led, HIGH);
}
Serial.println("reconnected to MQTT");
MQTT_reconnect = millis();
}
} // end of loop
// called by interrupt service routine when incoming data arrives
void receiveEvent (int howMany)
{
if (howMany < sizeof SensorNode)
return;
// read into structure
byte * p = (byte *) &SensorNode;
for (byte i = 0; i < sizeof SensorNode; i++)
*p++ = Wire.read ();
haveData = true;
}