Hey Y’all. I am making a light box that changes color (red, orange, or yellow) based off alerts from the on-campus police twitter page. When there isn’t a new alert or the box has been reset (using a toggle switch), then the box alternates between blue and white. I am using a Freakduino board, which is compatible with arduino software, it just has built in wifi. I am using the code from the World-Mood-In-A-Box instructable (http://www.instructables.com/id/Twitter-Mood-Light-The-Worlds-Mood-in-a-Box) but trying to tweak it to fit my needs. I have almost nil experience coding and could use help making this code work. Almost all the code is below, I have started to tweak some of it, the final part of it can be found in step 11 of the instructables. Thanks!
// LED setup - only some pins provide 8-bit PWM (Pulse-width modulation)
// output with the analogWrite() function.
// http://www.arduino.cc/en/Main/ArduinoBoardDuemilanove
// PWM: 3,5,6,9,10,11
#define redPin (3)
#define greenPin (5)
#define bluePin (6)
// delay in ms between fade updates
// max fade time = 255 * 15 = 3.825s
#define fadeDelay (15)
// Wifi setup
#define network ([your network])
#define username ([your network id])
#define password ([your network password])
#define remoteServer ("twitter.com/#!/uwalert")
const char* moodNames[NUM_MOOD_TYPES] = {
"Immediate Emergencies",
"Incidents on Campus",
"Weather warnings",
};
//const char* moodIntensityNames[NUM_MOOD_INTENSITY] = {
//"mild",
// "considerable",
//"extreme",
//};
#define SLEEP_TIME_BETWEEN_SEARCHES (1000 * 5)
// Store search strings in flash (program) memory instead of SRAM.
// http://www.arduino.cc/en/Reference/PROGMEM
// edit TWEETS_PER_PAGE if changing the rpp value
prog_char string_0[] PROGMEM = "GET /search.json?q=\"be+alert\"+OR+\"armed+robbery\"+OR+\"stay+clear+of+area\"+OR+\”\"+OR+\"shoot\"+OR+\"shooting\"+OR+\"assault\"+OR+\"stay+alert\"+OR+\"earthquake\"+OR+\"suspended\"+OR+\"closure\"+OR+\"canceled+classes\"&rpp=30&result_type=recent";
prog_char string_1[] PROGMEM = "GET /search.json?q=\"fire\"+OR+\"chemical+spill\"+OR+\"evacuate\"+OR+\"evacuation\"+OR+\"power+outage\"+OR+\"all+clear\"&rpp=30&result_type=recent";
prog_char string_2[] PROGMEM = "GET /search.json?q=\"weather\"&rpp=30&result_type=recent";
// be sure to change this if you edit the rpp value above
#define TWEETS_PER_PAGE (30)
PROGMEM const char *searchStrings[] =
{
string_0,
string_1,
string_2,
};
void setup()
{
Serial.begin(9600);
delay(100);
}
void loop()
{
// create and initialise the subsystems
WiFly wifly(network, password, SLEEP_TIME_BETWEEN_SEARCHES, Serial);
WorldMood worldMood(Serial, emotionSmoothingFactor, moodSmoothingFactor, moderateMoodThreshold,extremeMoodThreshold, tempramentRatios);
LED led(Serial, redPin, greenPin, bluePin, fadeDelay);
TwitterParser twitterSearchParser(Serial, TWEETS_PER_PAGE);
wifly.Reset();
char searchString[160];
while (true)
{
for (int i = 0; i < NUM_MOOD_TYPES; i++)
{
twitterSearchParser.Reset();
// read in new search string to SRAM from flash memory
strcpy_P(searchString, (char*)pgm_read_word(&(searchStrings[i])));
bool ok = false;
int retries = 0;
// some recovery code if the web request fails
while (!ok)
{
ok = wifly.HttpWebRequest(remoteServer, searchString, &twitterSearchParser);
if (!ok)
{
Serial.println("HttpWebRequest failed");
retries++;
if (retries > 3)
{
wifly.Reset();
retries = 0;
}
}
}
float tweetsPerMinute = twitterSearchParser.GetTweetsPerMinute();
// debug code
Serial.println("");
Serial.print(moodNames[i]);
Serial.print(": tweets per min = ");
Serial.println(tweetsPerMinute);
worldMood.RegisterTweets(i, tweetsPerMinute);
}
MOOD_TYPE newMood = worldMood.ComputeCurrentMood();
MOOD_INTENSITY newMoodIntensity = worldMood.ComputeCurrentMoodIntensity();
Serial.print("The Mood of the World is ... ");
Serial.print(moodIntensityNames[(int)newMoodIntensity]);
Serial.print(" ");
Serial.println(moodNames[(int)newMood]);
led.SetColor((int)newMood, (int)newMoodIntensity);
// save the battery
wifly.Sleep();
delay(SLEEP_TIME_BETWEEN_SEARCHES);
Serial.println("");
}
}
/*
Send the correct commands to connect to a wireless network using the parameters used on construction
*/
void WiFly::AutoConnect()
{
delay(DEFAULT_TIME_TO_READY);
FlushRX();
// Enter command mode
EnterCommandMode();
// Reboot to get device into known state
WriteToWiFlyCR("reboot");
WaitUntilReceived("*Reboot*");
WaitUntilReceived("*READY*");
FlushRX();
EnterCommandMode();
// turn off auto joining
WriteToWiFlyCR("set wlan join 0");
WaitUntilReceived(AOK, ERR);
// Set authentication level to
WriteToWiFly("set w a ");
WriteToWiFlyCR(auth_level);
WaitUntilReceived(AOK, ERR);
// Set authentication phrase to
WriteToWiFly("set w p ");
WriteToWiFlyCR(m_password);
WaitUntilReceived(AOK, ERR);
// Set localport to
WriteToWiFly("set i l ");
WriteToWiFlyCR(port_listen);
WaitUntilReceived(AOK, ERR);
// Deactivate remote connection automatic message
WriteToWiFlyCR("set comm remote 0");
WaitUntilReceived(AOK, ERR);
// Join wireless network
WriteToWiFly("join ");
WriteToWiFlyCR(m_network);
delay(DEFAULT_TIME_TO_JOIN);
bool ok = WaitUntilReceived("IP=");
delay(DEFAULT_TIME_TO_WAIT);
FlushRX();
if(ok == false)
{
m_printer->print("Failed to associate with '");
m_printer->print(m_network);
m_printer->println("'\n\rRetrying...");
FlushRX();
AutoConnect();
}
else
{
m_printer->println("Associated!");
ExitCommandMode();
}
// TODO save this configuration
}
/*
Enter command mode by sending: $$
Characters are passed until this exact sequence is seen. If any bytes are seen before these chars, or
after these chars, in a 1 second window, command mode will not be entered and these bytes will be passed
on to other side.
*/
void WiFly::EnterCommandMode()
{
FlushRX();
delay(1000); // wait 1s as instructed above
m_printer->println("Entering command mode.");
WriteToWiFly("$$");
WaitUntilReceived("CMD");
}
/*
exit command mode
send the "exit" command and await the confirmation result "EXIT"
*/
void WiFly::ExitCommandMode()
{
WriteToWiFlyCR("exit");
WaitUntilReceived("EXIT");
}
/*
Parameters: The server to telnet into, the get command that needs to be sent, a custom HtmlParser that
is called every time a character is received. The parser is responsible for processing the HTML
that is returned.
*/
bool WiFly::HttpWebRequest(const char* server, const char* getCommand, HtmlParser* parser)
{
m_printer->println(getCommand);
FlushRX();
FlushRX();
// Enter command mode
EnterCommandMode();
FlushRX();
// open a TCP connection, port 80 for HTTP
WriteToWiFly("open ");
WriteToWiFly(server);
WriteToWiFlyCR(" 80");
bool openOK = WaitUntilReceived(COMM_OPEN);
if (openOK == false)
{
m_printer->println("open port failed!");
delay(1000);
WriteToWiFlyCR("close");
WaitUntilReceived(COMM_CLOSE);
ExitCommandMode();
return false;
}
// eg. "GET /search.json?q=foo HTTP/1.1\r\n"
WriteToWiFlyCRLF(getCommand);
// eg. "Host: search.twitter.com\r\n"
WriteToWiFly("Host: ");
WriteToWiFlyCRLF(server);
// "\r\n"
WriteToWiFlyCRLF("");
// now wait for the response
int timeOut = 0;
bool ok = false;
while(timeOut < 5000)// timeout after 5 seconds
{
if((ReadCharFromWiFly(LSR) & 0x01))
{
char incoming_data = ReadCharFromWiFly(RHR);
m_printer->print(incoming_data,BYTE);
bool done = parser->Parse(incoming_data);
if (done)
{
ok = true;
break;
}
timeOut = 0; //reset the timeout
}
else
{
delay(1);
timeOut++;
}
}
FlushRX();
// disconnect TCP connection.
WriteToWiFlyCR("close");
WaitUntilReceived(COMM_CLOSE);
ExitCommandMode();
return ok;
}