-
-
Save polluxlabs/e8c4e7caa34400b1eb656178dffc25a0 to your computer and use it in GitHub Desktop.
Compute the world's state with sentiment analysis
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Libraries | |
#include <ArduinoJson.h> | |
#include <WiFi.h> | |
#include <HTTPClient.h> | |
#include <Adafruit_NeoPixel.h> | |
//Pin for the NeoPixel | |
int ledPin = 14; | |
//Initiate NeoPixel | |
Adafruit_NeoPixel pixels = Adafruit_NeoPixel(12, ledPin, NEO_GRB + NEO_KHZ800); | |
//Colors | |
int minus5[] = {255, 0, 0}; | |
int minus4[] = {255, 51, 0}; | |
int minus3[] = {255, 102, 0}; | |
int minus2[] = {255, 153, 0}; | |
int minus1[] = {255, 204, 0}; | |
int neutral[] = {255, 255, 0}; | |
int plus1[] = {204, 255, 0}; | |
int plus2[] = {153, 255, 0}; | |
int plus3[] = {102, 255, 0}; | |
int plus4[] = {51, 255, 0}; | |
int plus5[] = {0, 255, 0}; | |
// WiFi Credentials | |
const char* ssid = "WiFi SSID"; | |
const char* password = "PASSWORD"; | |
String article; | |
int sentiment = 0; | |
void getSentiment() { | |
HTTPClient http; | |
http.begin("http://api.meaningcloud.com/sentiment-2.1?key=<<API-KEY>>&of=json&txt=" + article + "&lang=en"); | |
int httpCode = http.GET(); | |
if (httpCode == 200) { | |
String payload = http.getString(); | |
const size_t capacity = 2*JSON_ARRAY_SIZE(0) + 7*JSON_ARRAY_SIZE(1) + 5*JSON_ARRAY_SIZE(2) + 2*JSON_ARRAY_SIZE(3) + JSON_ARRAY_SIZE(4) + 2*JSON_ARRAY_SIZE(5) + 2*JSON_ARRAY_SIZE(6) + JSON_ARRAY_SIZE(7) + 25*JSON_OBJECT_SIZE(4) + 4*JSON_OBJECT_SIZE(6) + 22*JSON_OBJECT_SIZE(7) + JSON_OBJECT_SIZE(8) + 2*JSON_OBJECT_SIZE(9) + 3*JSON_OBJECT_SIZE(10) + JSON_OBJECT_SIZE(11) + 3500; | |
DynamicJsonDocument doc(capacity); | |
DeserializationError error = deserializeJson(doc, payload, DeserializationOption::NestingLimit(11)); | |
if (error) { | |
Serial.print(F("deserializeJson() Error: ")); | |
Serial.println(error.c_str()); | |
return; | |
} | |
JsonObject status = doc["status"]; | |
String score_tag = doc["score_tag"]; | |
Serial.println(score_tag); | |
if (score_tag == "P+") { | |
sentiment += 2; | |
} | |
else if (score_tag == "P") { | |
sentiment += 1; | |
} | |
else if (score_tag == "N") { | |
sentiment -= 1; | |
} | |
else if (score_tag == "N+") { | |
sentiment -= 2; | |
} | |
else if (score_tag == "NONE") { | |
sentiment += 0; | |
} | |
} | |
else { | |
Serial.println("Bad HTTP Request"); | |
} | |
http.end(); | |
} | |
void setup() { | |
pixels.begin(); | |
pixels.setBrightness(175); | |
pinMode (14, OUTPUT); //LED Pin | |
Serial.begin(115200); | |
WiFi.begin(ssid, password); | |
while (WiFi.status() != WL_CONNECTED) { | |
delay(1000); | |
Serial.println("Connecting..."); | |
} | |
Serial.println("Connected!"); | |
} | |
void () { | |
//reset sentiment count | |
sentiment = 0; | |
if ((WiFi.status() == WL_CONNECTED)) { | |
HTTPClient http; | |
http.begin("http://newsapi.org/v2/top-headlines?country=us&apiKey=<<API-KEY>>"); | |
int httpCode = http.GET(); | |
if (httpCode == 200) { | |
String payload = http.getString(); | |
const size_t capacity = JSON_ARRAY_SIZE(20) + 20 * JSON_OBJECT_SIZE(2) + JSON_OBJECT_SIZE(3) + 20 * JSON_OBJECT_SIZE(8) + 16600; | |
DynamicJsonDocument doc(capacity); | |
DeserializationError error = deserializeJson(doc, payload); | |
if (error) { | |
Serial.print(F("deserializeJson() Error: ")); | |
Serial.println(error.c_str()); | |
return; | |
} | |
//Save number of headlines | |
int noHeadlines = doc["totalResults"]; | |
//Loop through headlines & get sentiment | |
for (int i = 0; i < 15; i++) { | |
//Get Headlines from Newsapi | |
JsonArray articles = doc["articles"]; | |
JsonObject articles_number = articles[i]; | |
const char* articles_number_title = articles_number["title"]; | |
article = String(articles_number_title); | |
Serial.println(article); | |
article.replace(" ", "%20"); | |
Serial.println(article); | |
getSentiment(); | |
} | |
Serial.println("Loop done."); | |
Serial.println(sentiment); | |
//Show state on NeoPixel | |
if (sentiment <= -5) { | |
for (int i = 0; i < 12; i++) { | |
pixels.setPixelColor(i, minus5[0], minus5[1], minus5[2]); | |
pixels.show(); | |
} | |
} else if (sentiment >= 5) { | |
for (int i = 0; i < 12; i++) { | |
pixels.setPixelColor(i, plus5[0], plus5[1], plus5[2]); | |
pixels.show(); | |
} | |
} | |
switch (sentiment) { | |
case -4: | |
for (int i = 0; i < 12; i++) { | |
pixels.setPixelColor(i, minus4[0], minus4[1], minus4[2]); | |
pixels.show(); | |
} | |
break; | |
case -3: | |
for (int i = 0; i < 12; i++) { | |
pixels.setPixelColor(i, minus3[0], minus3[1], minus3[2]); | |
pixels.show(); | |
} | |
break; | |
case -2: | |
for (int i = 0; i < 12; i++) { | |
pixels.setPixelColor(i, minus2[0], minus2[1], minus2[2]); | |
pixels.show(); | |
} | |
break; | |
case -1: | |
for (int i = 0; i < 12; i++) { | |
pixels.setPixelColor(i, minus1[0], minus1[1], minus1[2]); | |
pixels.show(); | |
} | |
break; | |
case 0: | |
for (int i = 0; i < 12; i++) { | |
pixels.setPixelColor(i, neutral[0], neutral[1], neutral[2]); | |
pixels.show(); | |
} | |
break; | |
case 1: | |
for (int i = 0; i < 12; i++) { | |
pixels.setPixelColor(i, plus1[0], plus1[1], plus1[2]); | |
pixels.show(); | |
} | |
break; | |
case 2: | |
for (int i = 0; i < 12; i++) { | |
pixels.setPixelColor(i, plus2[0], plus2[1], plus2[2]); | |
pixels.show(); | |
} | |
break; | |
case 3: | |
for (int i = 0; i < 12; i++) { | |
pixels.setPixelColor(i, plus3[0], plus3[1], plus3[2]); | |
pixels.show(); | |
} | |
break; | |
case 4: | |
for (int i = 0; i < 12; i++) { | |
pixels.setPixelColor(i, plus4[0], plus4[1], plus4[2]); | |
pixels.show(); | |
} | |
break; | |
} | |
} | |
else { | |
Serial.println("Bad HTTP Request"); | |
} | |
http.end(); | |
Serial.println("Connection closed."); | |
} | |
//Wait for 30 minutes until next call | |
delay(1800000); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment