/
MKR1KWIv3
282 lines (220 loc) · 7.91 KB
/
MKR1KWIv3
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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
*
Test HTTP Client
Context: Arduino, with WINC1500 module
*/
// include required libraries and config files
#include <SPI.h>
#include <WiFi101.h>
//#include <ESP8266WiFi.h> // use this instead of WiFi101 for ESP8266 modules
#include <ArduinoHttpClient.h>
#include <LiquidCrystal.h>
#include <ArduinoJson.h>
#include <Servo.h>
WiFiClient netSocket; // network socket to server
//const char server[] = "104.131.5.90:5000"; // server name
const char server[] = "104.131.5.90"; // server name
// if at ITP, your network SSID (name)
char ssid[] = "itpsandbox";
char pass[] = "NYU+s0a!+P?";
// if at HOME, your network SSID (name)
//char ssid[] = "x???x"; // your network SSID (name)
//char pass[] = "x???x";
//String route = "/price"; // API route
//String route = "/blocks"; // API route
String route = "";
String json_field = "";
String rawResult = "";
long resultValue = 0;
// initialize the library by associating any needed LCD interface pin
// with the arduino pin number it is connected to
const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
int timesPressed = 0;
// https://www.arduino.cc/en/Tutorial/Button
const int redButtonPin = 6; // the number of the pushbutton pin
const int greyButtonPin = 7; // the number of the pushbutton pin
const int blackButtonPin = 9; // the number of the pushbutton pin
const int PRICE = 1;
const int BLOCKS = 2;
const int MININGTIME = 3;
int lastPressed = -1;
int buttonChanged = 0;
// variables will change:
int redButtonState = 0; // variable for reading the pushbutton status
int greyButtonState = 0; // variable for reading the pushbutton status
int blackButtonState = 0; // variable for reading the pushbutton status
int doWebRequest = 0; // should we do a web request?
// create servo object to control a servo
Servo myservo;
int pos = 0; // variable to store the servo position
void setup() {
Serial.begin(9600); // initialize serial communication
Serial.println("in status");
// while you're not connected to a WiFi AP,
while ( WiFi.status() != WL_CONNECTED) {
Serial.print("Attempting to connect to Network named: ");
Serial.println(ssid); // print the network name (SSID)
WiFi.begin(ssid, pass); // try to connect
delay(2000);
}
// When you're connected, print out the device's network status:
IPAddress ip = WiFi.localIP();
Serial.print("IP Address: ");
Serial.println(ip);
// set up the LCD's number of columns and rows:
lcd.begin(16, 2);
// Print a message to the LCD.
lcd.clear();
lcd.print("Blockchain!");
lcd.setCursor(0, 1);
lcd.print("Press to begin");
// initialize the pushbutton pin as an input:
pinMode(redButtonPin, INPUT);
pinMode(greyButtonPin, INPUT);
pinMode(blackButtonPin, INPUT);
// attaches the servo on pin 8 to the servo object
myservo.attach(8);
}
void loop() {
//delay(1000);
//HttpClient http(netSocket, server, 8080); // make an HTTP client
//delay(500); // wait 10 seconds
// set the cursor to column 0, line 1
// (note: line 1 is the second row, since counting begins with 0):
//lcd.setCursor(0, 1);
//////////////////
// determine the state of the buttons
//////////////////
// read the state of the pushbutton value:
greyButtonState = digitalRead(greyButtonPin);
redButtonState = digitalRead(redButtonPin);
blackButtonState = digitalRead(blackButtonPin);
// check if the pushbutton is pressed. If it is, the buttonState is HIGH:
if (greyButtonState == HIGH) {
// turn LED on:
timesPressed--;
if (lastPressed != PRICE){
buttonChanged = 1;
doWebRequest = 1;
Serial.println("button changed to: PRICE");
} else {
buttonChanged = 0;
}
lastPressed = PRICE;
route = "/price";
json_field = "price";
} else if (redButtonState == HIGH) {
// turn LED on:
timesPressed++;
if (lastPressed != BLOCKS){
buttonChanged = 1;
doWebRequest = 1;
Serial.println("button changed to: BLOCKS");
} else {
buttonChanged = 0;
}
lastPressed = BLOCKS;
route = "/blocks";
json_field = "height";
} else if (blackButtonState == HIGH) {
// turn LED on:
timesPressed++;
if (lastPressed != MININGTIME){
buttonChanged = 1;
doWebRequest = 1;
Serial.println("button changed to: MININGTIME");
} else {
buttonChanged = 0;
}
lastPressed = MININGTIME;
route = "/minetime";
json_field = "miningtime";
} else {
buttonChanged = 0;
// turn LED off:
//digitalWrite(ledPin, LOW);
}
// print the number of seconds since reset:
//lcd.print(millis() / 1000);
//lcd.print(timesPressed);
if (buttonChanged){
//////////////////
// do web stuff
//////////////////
if (doWebRequest){
HttpClient http(netSocket, server, 5000); // make an HTTP client
http.get(route); // make a GET request
while (http.connected()) { // while connected to the server,
if (http.available()) { // if there is a response from the server,
/*
---------------------
HTTP/1.1 200 OK
X-Powered-By: Express
Content-Type: application/json; charset=utf-8
Content-Length: 17
ETag: W/"11-8s4y7w16weFOLxpEe4ro18AKdZw"
Date: Sun, 26 Nov 2017 08:17:24 GMT
Connection: close
{"height":495370}
---------------------
*/
// skip the headers
char endOfHeaders[] = "\r\n\r\n";
http.find(endOfHeaders);
rawResult = http.readString(); // read it
Serial.println(rawResult); // and print it
// parse result
// https://arduinojson.org/example/parser/
// https://github.com/bblanchon/ArduinoJson/blob/master/examples/JsonHttpClient/JsonHttpClient.ino
StaticJsonBuffer<200> jsonBuffer;
JsonObject& root = jsonBuffer.parseObject(rawResult);
if (!root.success()) {
Serial.println("parseObject() failed");
return;
}
resultValue = root[json_field];
Serial.println(json_field + ": " + resultValue); // and print it
}
}
// when there's nothing left to the response,
http.stop(); // close the request
}
//////////////////
// print results
//////////////////
if(lastPressed == BLOCKS){
lcd.clear();
lcd.print("Blocks: ");
//lcd.print(timesPressed);
lcd.print(resultValue);
int maxBlocks = 1000000;
int servoPos = (resultValue * 180) / maxBlocks;
myservo.write(servoPos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
} else if(lastPressed == PRICE){
lcd.clear();
lcd.print("Price: ");
lcd.print(resultValue);
int maxPrice = 10000;
int servoPos = (resultValue * 180) / maxPrice;
myservo.write(servoPos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
} else if(lastPressed == MININGTIME){
lcd.clear();
lcd.print("Mining Time: ");
lcd.print(resultValue);
int maxMiningtime = 100;
int servoPos = (resultValue * 180) / maxMiningtime;
myservo.write(servoPos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
} else {
lcd.clear();
lcd.print("Blockchain!");
lcd.setCursor(0, 1);
lcd.print("Press to begin");
}
}
delay(10);
// print the number of seconds since reset:
//lcd.print(millis() / 1000);
}