-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlinkpulse.pde
215 lines (198 loc) · 5.33 KB
/
linkpulse.pde
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
#include <Ethernet.h>
#include "Dhcp.h" // uses DHCP code from: http://blog.jordanterrell.com/post/Arduino-DHCP-Library-Version-04.aspx
#include <TextFinder.h>
byte mac[] = {
0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte server[] = {
87,238,48,70 }; // lp1.linkpulse.com
char* channelIDs[]={
"69fc88066df1d55f","7e26f1a83f4c072f","7aab030d8f1f4c81","69fc88066df1d55f"};
long waitTill[4] = {
0, 0, 0, 0};
long stateclicks[4] = {
-1, -1, -1, -1};
long prevStateclicks[4] = {
-1, -1, -1, -1};
long channelWaitTill = 0;
boolean channelSelect = true;
int channel = 0;
long lastReadingAt = 0;
long delayT = 0;
int lastChannel = -1;
Client client(server, 80);
TextFinder finder( client );
void setup()
{
pinMode(2, OUTPUT);
pinMode(3, OUTPUT);
Serial.begin(2400);
welcomeScreen();
if(Dhcp.beginWithDHCP(mac) == 1) { // begin method returns 1 if successful
Serial.println("got IP address, connecting...");
delay(5000);
}
else {
Serial.println("unable to acquire ip address!");
while(true)
; // do nothing
}
}
void sendCommand(int address, int command, int parameter) {
Serial.print(13,BYTE);
Serial.print(address,BYTE);
Serial.print(command,BYTE);
Serial.print(parameter,BYTE);
Serial.print(256-(13+address+command+parameter) % 256,BYTE);
Serial.print(13,BYTE);
Serial.print(address,BYTE);
Serial.print(command,BYTE);
Serial.print(parameter,BYTE);
Serial.print(256-(13+address+command+parameter) % 256,BYTE);
Serial.print(13,BYTE);
Serial.print(address,BYTE);
Serial.print(command,BYTE);
Serial.print(parameter,BYTE);
Serial.print(256-(13+address+command+parameter) % 256,BYTE);
Serial.print(13,BYTE);
Serial.print(address,BYTE);
Serial.print(command,BYTE);
Serial.print(parameter,BYTE);
Serial.print(256-(13+address+command+parameter) % 256,BYTE);
}
void show(int number) {
sendCommand(4,'A',(number/1000)+48);
sendCommand(3,'A',((number/100) % 10 )+48);
sendCommand(2,'A',((number/10) % 10 )+48);
sendCommand(1,'A',(number % 10)+48);
strobe();
}
void strobe() {
delay(100);
sendCommand(8,'S',1);
}
void welcomeScreen() {
sendCommand(4,'B',79); // H
sendCommand(3,'B',111); // A
sendCommand(2,'B',193); // L
sendCommand(1,'B',193); // L
strobe();
}
long getStateclicks(int channel) {
long stateclicks = 0;
long time = 0;
boolean ok = false;
if (client.connect()) {
client.print("GET /lpFeeder/d9c853a68e27d39c/");
client.print(channelIDs[channel]);
client.println(" HTTP/1.0");
client.println("Host: lp1.linkpulse.com");
client.println();
}
else {
Serial.println(" connection failed");
}
if (client.connected()) {
time = millis();
if(finder.find("<description>stateclicks: ") )
{
stateclicks = finder.getValue();
Serial.print("Stateclicks are "); // and echo it to the serial port.
Serial.println(stateclicks);
ok = true;
}
else
Serial.print("Could not find stateclicks field");
}
else {
Serial.println("Disconnected");
}
Serial.println(millis()-time);
client.stop();
client.flush();
if(ok)
return stateclicks;
else
return -1;
}
void setPoint(int channel) {
sendCommand(1,'P',0);
sendCommand(2,'P',0);
sendCommand(3,'P',0);
sendCommand(4,'P',0);
sendCommand(4-channel,'P',255);
strobe();
Serial.print("Channel no ");
Serial.print(channel+1);
Serial.println();
}
int getChannelReading() {
channel = map(analogRead(0),0,1023,0,4); // read the value from the sensor
if(channel==4)
channel=3;
return channel;
}
void showMinus() {
sendCommand(1,'B',2);
sendCommand(2,'B',2);
sendCommand(3,'B',2);
sendCommand(4,'B',2);
strobe();
}
void showChannelNo(int channel) {
sendCommand(4,'B',225); // C
sendCommand(3,'B',75); // h
sendCommand(2,'B',0); // (space)
sendCommand(1,'A',49+channel); // (number)
strobe();
}
void showTrend(long now, long prev) {
Serial.println("showTrend");
digitalWrite(2, LOW);
digitalWrite(3, LOW);
if(now == -1 || prev == -1)
return;
if(now > prev) {
digitalWrite(2, HIGH);
digitalWrite(3, LOW);
Serial.println("Trend up");
}
else if(now < prev)
{
digitalWrite(2, LOW);
digitalWrite(3, HIGH);
Serial.println("Trend down");
}
}
void loop()
{
channel = getChannelReading();
if(channel != lastChannel)
{
setPoint(channel);
Serial.println(channel+1);
lastChannel = channel;
showChannelNo(channel);
channelSelect = true;
channelWaitTill = millis()+3500;
}
if(channelSelect && millis()>channelWaitTill) {
if(stateclicks[channel] == -1)
showMinus();
else {
show(stateclicks[channel]/10);
}
showTrend(stateclicks[channel], prevStateclicks[channel]);
channelSelect = false;
}
if(!channelSelect && millis()>waitTill[channel]) {
stateclicks[channel] = getStateclicks(channel);
if(stateclicks[channel]>-1) {
show(stateclicks[channel]/10);
showTrend(stateclicks[channel], prevStateclicks[channel]);
prevStateclicks[channel] = stateclicks[channel];
waitTill[channel] = millis() + 300000;
}
else
waitTill[channel] = millis() + 30000;
}
}