· 6 years ago · Nov 26, 2019, 06:30 AM
1
2#include <SPI.h>
3#include <WiFi.h>
4#include <HTTPClient.h>
5#include <ArduinoJson.h>
6#include <Adafruit_NeoPixel.h>
7
8// Which pin on the Arduino is connected to the NeoPixels?
9// On a Trinket or Gemma we suggest changing this to 1:
10#define LED_PIN 17
11
12// How many NeoPixels are attached to the Arduino?
13#define LED_COUNT 18
14
15// NeoPixel brightness, 0 (min) to 255 (max)
16#define BRIGHTNESS 50
17Adafruit_NeoPixel strip(LED_COUNT, LED_PIN, NEO_GRB + NEO_KHZ800);
18
19//Bool to determine if i'm connecting to university wifi or home
20const bool UNI_WIFI = false;
21
22
23char ssid[] = "IllinoisNet_Guest"; //UIUC wifi
24int status = WL_IDLE_STATUS; // the Wifi radio's status
25const char *homessid = "VMKI"; //home wifi
26const char *password = "16characters"; //home wifi password (changed before posting this publicly :P )
27
28void setup() {
29 //Initialize serial and wait for port to open:
30 Serial.begin(115200);
31 strip.begin(); // INITIALIZE NeoPixel strip object (REQUIRED)
32 strip.show(); // Turn OFF all pixels ASAP
33 strip.setBrightness(50); // Set BRIGHTNESS to about 1/5 (max = 255)
34 for(int i = 0; i < LED_COUNT; i++){
35 strip.setPixelColor(i, strip.Color(0, 0, 0));
36 }
37 strip.show();
38 while (!Serial) {
39 ; // wait for serial port to connect. Needed for native USB port only
40 }
41
42
43 //TODO: Remove for better testing later
44 /*
45 strip.setPixelColor(14, strip.Color(0, 255, 0));
46 strip.setPixelColor(8, strip.Color(0, 255, 0));
47 strip.setPixelColor(3, strip.Color(255, 255, 0));
48 strip.setPixelColor(4, strip.Color(204, 204, 204));
49 strip.show(); */
50
51
52 // attempt to connect to Wifi network:
53 if(UNI_WIFI){
54 while (status != WL_CONNECTED) {
55 Serial.print("Attempting to connect to open SSID: ");
56 Serial.println(ssid);
57 status = WiFi.begin(ssid);
58
59 // wait 10 seconds for connection:
60 delay(10000);
61 }
62 }
63 else {
64 while (status != WL_CONNECTED) {
65 Serial.print("Attempting to connect to open SSID: ");
66 Serial.println(homessid);
67 status = WiFi.begin(homessid, password);
68
69 // wait 10 seconds for connection:
70 delay(10000);
71 }
72 }
73
74 // you're connected now, so print out the data:
75 Serial.print("You're connected to the network");
76
77}
78void loop() {
79
80 //Setup API call to MTD servers
81 String payload;
82 const String endpoint = "https://developer.cumtd.com/api/v2.2/json/getroutes?key=";
83 const String key = "61d24499d0f44e5a990f15a594067470";
84 HTTPClient http;
85
86 http.begin("https://developer.cumtd.com/api/v2.2/json/getvehiclesbyroute?key=61d24499d0f44e5a990f15a594067470&route_id=10W%20GOLD%20ALT;GOLD;GOLD%20ALT;GOLDHOPPER;5E%20GREEN%20EXPRESS%201%20ALT;5E%20GREEN%20EXPRESS%20ALT;5W%20GREEN%20ALT%202;5W%20GREEN%20EXPRESS%202;GREEN;GREEN%20ALT;GREEN%20EVENING;GREEN%20EVENING%20SATURDAY;GREEN%20EXPRESS;GREEN%20EXPRESS%20ALT;GREEN%20LATE%20NIGHT%20SATURDAY;GREEN%20LATE%20NIGHT%20SUNDAY;GREEN%20SATURDAY;GREEN%20SUNDAY;GREENHOPPER%20EVENING%20SATURDAY;GREENHOPPER%20EVENING;GREENHOPPER;SILVER;SILVER%20EVENING;SILVER%20EVENING%20SATURDAY;SILVER%20EVENING%20SUNDAY;SILVER%20LATE%20NIGHT;SILVER%20LIMITED%20EVENING;SILVER%20LIMITED%20EVENING%20SATURDAY;SILVER%20LIMITED%20SATURDAY;SILVER%20LIMITED%20SUNDAY;SILVER%20SATURDAY;SILVER%20SUNDAY");
87 int httpCode = http.GET(); //Make the request
88
89 if (httpCode > 0) { //Check for the returning code
90 payload = http.getString();
91 Serial.println(httpCode);
92 Serial.println(payload);
93 Serial.println("testing");
94 }
95 else {
96 Serial.println("Error on HTTP request");
97 }
98 http.end(); //Free the resources
99
100 //Process API result (in JSON)
101 DynamicJsonDocument doc(10000);
102 DeserializationError err = deserializeJson(doc, payload);
103 if (err) {
104 Serial.print(F("deserializeJson() failed with code "));
105 Serial.println(err.c_str());
106 }
107
108
109 //Update NeoPixels to match API call results
110 for(int i = 0; i < LED_COUNT; i++){
111 strip.setPixelColor(i, strip.Color(0, 0, 0));
112 }
113 for(int i = 0; i < doc["vehicles"].size(); i++){
114 auto last_location = doc["vehicles"][i]["previous_stop_id"].as<char*>();
115 auto route = doc["vehicles"][i]["trip"]["route_id"].as<String>();
116 int lightPos = mapToLight(last_location);
117 Serial.println(lightPos);
118 if(lightPos >= 0){
119 if(route.indexOf("GREEN") >= 0){
120 strip.setPixelColor(lightPos, strip.Color(0, 255, 0));
121 Serial.println("Green bus found! LED enabled!");
122 }
123 else if(route.indexOf("GOLD") >= 0){
124 strip.setPixelColor(lightPos, strip.Color(255, 255, 0));
125 }
126 else if(route.indexOf("SILVER") >= 0){
127 strip.setPixelColor(lightPos, strip.Color(204, 204, 204));
128 Serial.println("Silver bus found! LED enabled!");
129 }
130
131 }
132 }
133 strip.show();
134
135
136 // check the network connection once every 30 seconds:
137 delay(5000);
138
139
140}
141
142//Takes a stop_id as input, and determines the index of the corresponding neopixel
143//returns -1 if no valid location exists.
144int mapToLight(String input){
145 Serial.print("input: " + input + " --> ");
146 if(input.indexOf("RACEMN") >= 0){
147 return 0;
148 }
149 else if(input.indexOf("GRNRACE") >= 0){
150 return 16;
151 }
152 else if(input.indexOf("GRNCDR") >= 0){
153 return 15;
154 }
155 else if(input.indexOf("GRNBRCH") >= 0){
156 return 14;
157 }
158 else if(input.indexOf("GRNORCH") >= 0){
159 return 12;
160 }
161 else if(input.indexOf("GRNBUSEY") >= 0){
162 return 9;
163 }
164 else if(input.indexOf("GRNGRGST") >= 0){
165 return 8;
166 }
167 else if(input.indexOf("LSE") >= 0){
168 return 17;
169 }
170 else if(input.indexOf("SPFLDBRCH") >= 0){
171 return 1;
172 }
173 else if(input.indexOf("SPFLDPRC") >= 0){
174 return 3;
175 }
176 else if(input.indexOf("SPFLDBSY") >= 0){
177 return 4;
178 }
179 else if(input.indexOf("SPFLDGRGST") >= 0){
180 return 5;
181 }
182 else if(input.indexOf("SPFLDHVY") >= 0){
183 return 6;
184 }
185 else {
186 return -1;
187 }
188
189}