-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsend_2digital_sensors.ino
122 lines (95 loc) · 3.1 KB
/
send_2digital_sensors.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
/*
DeviceHUB.net sample code for sending two digital sensors.
Download and install PubSubClient library from https://github.com/knolleary/pubsubclient
First open the PubSubClient.h library header file and make the following
modification in that file. (increase the packet size as shown below).
// MQTT_MAX_PACKET_SIZE : Maximum packet size
//#define MQTT_MAX_PACKET_SIZE 128
#define MQTT_MAX_PACKET_SIZE 256
created 26 May 2015
by Alexandru Gheorghe
*/
#include <SPI.h>
#include <Ethernet.h>
#include <PubSubClient.h>
// MAC Address of Arduino Ethernet (Sheild)
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
#define API_KEY "paste_your_API_KEY_here"
#define PROJECT_ID "paste_your_PROJECT_ID_here"
#define DI_SENSOR_NAME1 "paste_your_first_SENSOR_NAME_here"
#define DI_SENSOR_NAME2 "paste_your_second_SENSOR_NAME_here"
#define DEVICE_UUID "paste_your_DEVICE_UUID_here"
#define sec 1000
char clientId[] = "Arduino_Ethernet";
char sensorTopic1[] = "/a/"API_KEY"/p/"PROJECT_ID"/d/"DEVICE_UUID"/sensor/"DI_SENSOR_NAME1"/data";
char sensorTopic2[] = "/a/"API_KEY"/p/"PROJECT_ID"/d/"DEVICE_UUID"/sensor/"DI_SENSOR_NAME2"/data";
// server mqtt.devicehub.net
char server[] = "mqtt.devicehub.net";
char message_buffer1[10];
char message_buffer2[10];
EthernetClient apiClient;
void callback(char* topic, byte* payload, unsigned int length)
{
// handle message arrived
}
PubSubClient client(server, 1883, callback, apiClient);
boolean conn = false;
unsigned long time;
void setup(void)
{
// init serial for debugging
Serial.begin(9600);
if (Ethernet.begin(mac) == 0)
{
Serial.println("Failed to configure Ethernet using DHCP");
Ethernet.begin(mac);
}
Serial.println("\nStarting connection to server...");
if(client.connect(clientId))
{
conn = true;
Serial.println("Successfuly connected and running!");
}
else
{
Serial.println("Connection problem");
}
}
void loop(void)
{
//if client it's not connected or disconnects here we try to reconnect
if (!client.connected())
{
Serial.println("reconnecting ...");
client.connect(clientId);
delay(3*sec);
sendData();
}
sendData();
}
void sendData(void)
{
//simulation for two digital sensors
unsigned int digital_sensor1 = random(0, 2);
unsigned int digital_sensor2 = random(0, 2);
if(conn)
{
//publishing new values every 5 seconds
if (millis() > (time + 5000))
{
time = millis();
String pubString1 = "{\"value\": " + String(digital_sensor1) + "}";
String pubString2 = "{\"value\": " + String(digital_sensor2) + "}";
pubString1.toCharArray(message_buffer1, pubString1.length()+1);
pubString2.toCharArray(message_buffer2, pubString2.length()+1);
Serial.print("Publishing new values: ");
Serial.print(digital_sensor1);
Serial.print(" and ");
Serial.println(digital_sensor2);
client.publish(sensorTopic1, message_buffer1);
client.publish(sensorTopic2, message_buffer2);
}
// MQTT client loop processing
client.loop();
}
}