-
Notifications
You must be signed in to change notification settings - Fork 0
/
space_weather_lamp.ino
293 lines (248 loc) · 6.75 KB
/
space_weather_lamp.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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
#include <ESP8266WiFi.h>
//Please replace with your wifi network and password.
const char* ssid = "MY_WIFI_NETWORK";
const char* password = "MY_WIFI_PASSWORD";
const char* host = "services.swpc.noaa.gov";
//BULK SPEED PINS
const int BLUEPIN = 12;
const int REDPIN = 15;
const int GREENPIN = 13;
//DENSITY PINS
const int BLUEPIN1 = 0;
const int GREENPIN1 = 16;
const int REDPIN1 = 2;
//ION TEMPERATURE PINS
const int BLUEPIN2 = 14;
const int GREENPIN2 = 5;
const int REDPIN2 = 4;
const float SPEEDMULT = .51;
const float DENSMULT = 20.4;
const float TEMPMULT = 0.051;
void setup() {
pinMode(REDPIN, OUTPUT);
pinMode(GREENPIN, OUTPUT);
pinMode(BLUEPIN, OUTPUT);
pinMode(REDPIN1, OUTPUT);
pinMode(GREENPIN1, OUTPUT);
pinMode(BLUEPIN1, OUTPUT);
pinMode(REDPIN2, OUTPUT);
pinMode(GREENPIN2, OUTPUT);
pinMode(BLUEPIN2, OUTPUT);
Serial.begin(115200);
delay(100);
// connect to wifi network
Serial.println();
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}
void loop() {
delay(5000);
Serial.print("connecting to ");
Serial.println(host);
// Use WiFiClient class to create TCP connections
WiFiClient client;
const int httpPort = 80;
if (!client.connect(host, httpPort)) {
Serial.println("connection failed");
return;
}
// create URI for request
String url = "/text/ace-swepam.txt";
Serial.print("Requesting URL: ");
Serial.println(url);
// send request
client.print(String("GET ") + url + " HTTP/1.1\r\n" +
"Host: " + host + "\r\n" +
"Connection: close\r\n\r\n");
delay(500);
int num = 0;
String trueLine = "";
// print to serial
while (client.available()) {
String line = client.readStringUntil('\n');
num++;
if (num == 156) {
trueLine = line;
}
}
Serial.println(trueLine);
int statusInt = getStatus(trueLine);
float protDensity = getProtDens(trueLine);
float bulkSpeed = getBulkSpeed(trueLine);
float ionTemp = getIonTemp(trueLine);
int ionTempPower = getIonTempPower(trueLine);
ionTempPower = ionTempPower - 2;
int ionTempTrue = ionTemp * pow(10, ionTempPower);
Serial.println("closing connection");
if (statusInt != 0) {
badData();
} else {
/**
* Write the corresponding colors to LED 0 for the Bulk Speed.
*/
int blueVal = getBlueVal(bulkSpeed - 200, SPEEDMULT);
int greenVal = getGreenVal(bulkSpeed - 200, SPEEDMULT);
int redVal = getRedVal(bulkSpeed - 200, SPEEDMULT);
Serial.print("blueval ");
Serial.println(blueVal);
Serial.print("greenval ");
Serial.println(greenVal);
Serial.print("redval ");
Serial.println(redVal);
analogWrite(BLUEPIN, blueVal);
analogWrite(GREENPIN, greenVal);
analogWrite(REDPIN, redVal);
/**
* Write the corresponding colors to LED 2 for ion temperature.
*/
int blueVal2 = getBlueVal(ionTempTrue - 100, TEMPMULT);
int greenVal2 = getGreenVal(ionTempTrue - 100, TEMPMULT);
int redVal2 = getRedVal(ionTempTrue - 100, TEMPMULT);
Serial.print("blueval2 ");
Serial.println(blueVal2);
Serial.print("greenval2 ");
Serial.println(greenVal2);
Serial.print("redval2 ");
Serial.println(redVal2);
analogWrite(BLUEPIN2, blueVal2);
analogWrite(GREENPIN2, greenVal2);
analogWrite(REDPIN2, redVal2);
/**
* Begin the color cycling for the proton density.
*/
rainbow(protDensity);
}//else if its working
}//loop
int getStatus(String orig) {
char statusChar = orig.charAt(36);
int statusInt = statusChar - '0';
Serial.print("Status integer: ");
Serial.println(statusInt);
return statusInt;
}
float getProtDens(String orig) {
String protDens = orig.substring(41, 48);
float protDensI = protDens.toFloat();
Serial.println("proton density: " + protDens);
Serial.print("proton density integer: ");
Serial.println(protDensI);
return protDensI;
}
float getBulkSpeed(String orig) {
String bulkSpeed = orig.substring(52, 59);
float bulkSpeedI = bulkSpeed.toFloat();
Serial.println("bulk speed: " + bulkSpeed);
Serial.print("bulk speed integer: ");
Serial.println(bulkSpeedI);
return bulkSpeedI;
}
float getIonTemp(String orig) {
String ionTemp = orig.substring(63, 72);
String ionTemp1 = ionTemp.substring(0, 5);
float ionTempI = ionTemp1.toFloat();
Serial.println("Ion temperature: " + ionTemp1);
Serial.print("Ion temperature integer: ");
Serial.println(ionTempI);
return ionTempI;
}
int getIonTempPower(String orig) {
String ionTemp = orig.substring(63, 72);
String ionTemp1 = ionTemp.substring(8, 10);
int ionTempI = ionTemp1.toInt();
Serial.println("Ion temperature power: " + ionTemp1);
Serial.print("Ion temperature power integer: ");
Serial.println(ionTempI);
return ionTempI;
}
int getGreenVal(int num, float mult) {
int greenVal = 0;
int newnum = mult * num;
Serial.print("greenvalmethod ");
Serial.println(newnum);
if (newnum < 256) {
greenVal = newnum;
}
if (newnum >= 256) {
greenVal = 510 - newnum;
}
return greenVal;
}//getGreenVal1
int getBlueVal(int num, float mult) {
int blueVal = 0;
int newnum = mult * num;
Serial.print("bluevalmethod ");
Serial.println(newnum);
if (newnum < 256) {
blueVal = 255 - newnum;
}
return blueVal;
}//getBlueVal1
int getRedVal(int num, float mult) {
int redVal = 0;
int newnum = mult * num;
Serial.print("redvalmethod ");
Serial.println(newnum);
if (newnum >= 256) {
redVal = newnum;
}
return redVal;
}
void badData() {
analogWrite(BLUEPIN, 0);
analogWrite(GREENPIN, 0);
analogWrite(BLUEPIN1, 0);
analogWrite(GREENPIN1, 0);
analogWrite(BLUEPIN2, 0);
analogWrite(GREENPIN2, 0);
for (int j = 0; j < 6; j++) {
for (int i = 255; i > 0; i--) {
analogWrite(REDPIN, i);
analogWrite(REDPIN1, i);
analogWrite(REDPIN2, i);
delay(20);
}
for (int i = 0; i < 255; i++) {
analogWrite(REDPIN, i);
analogWrite(REDPIN1, i);
analogWrite(REDPIN2, i);
delay(20);
}
}
}
void rainbow(int val) {
int num = 60000;
if(val!=0){
num = 60000 / val;
}else{
num = 60000;
}
int num1 = num / 510;
analogWrite(REDPIN1, 255);
for (int i=0;i<val;i++) {
for (int i = 0; i < 255; i++) {
analogWrite(GREENPIN1, i);
analogWrite(REDPIN1, 255 - i);
delay(num1);
}
for (int i = 0; i < 255; i++) {
analogWrite(GREENPIN1, 255 - i);
analogWrite(BLUEPIN1, i);
delay(num1);
}
for (int i = 0; i < 255; i++) {
analogWrite(BLUEPIN1, 255 - i);
analogWrite(REDPIN1, i);
delay(num1);
}
}
}