-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget_analog_actuator.ino
115 lines (90 loc) · 2.76 KB
/
get_analog_actuator.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
/*
DeviceHUB.net sample code for getting analog actuator state.
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 ACTUATOR_NAME "paste_your_ACTUATOR_NAME_here"
#define DEVICE_UUID "paste_your_DEVICE_UUID_here"
#define sec 1000
// Digital pin 5 is the LED output pin
#define LED 5
char clientId[] = "Arduino_Ethernet";
char actuatorTopic[] = "/a/"API_KEY"/p/"PROJECT_ID"/d/"DEVICE_UUID"/actuator/"ACTUATOR_NAME"/state";
// server mqtt.devicehub.net
char server[] = "mqtt.devicehub.net";
char message_buffer[150];
EthernetClient apiClient;
PubSubClient client(server, 1883, callback, apiClient);
// handles message arrived on subscribed topic
void callback(char* topic, byte* payload, unsigned int length)
{
int i = 0;
// create buffer with null terminator
for(i=0; i<length; i++)
{
message_buffer[i] = payload[i];
}
message_buffer[i] = '\0';
String msgString = String(message_buffer).substring(43,44);
Serial.println("message arrived: " + msgString);
if(msgString <= "50")
{
//if message received is smaller then 50, LED is turned off
digitalWrite(LED,LOW);
}
else
{
//if message received is bigger then 50, LED is turned on
digitalWrite(LED,HIGH);
}
}
void setup(void)
{
// init serial for debugging
Serial.begin(9600);
//initialize the digital pin as an output.
pinMode(LED, OUTPUT);
// light should be off
digitalWrite(LED,LOW);
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))
{
client.subscribe(actuatorTopic);
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);
client.subscribe(actuatorTopic);
}
// MQTT client loop processing
client.loop();
}