Thanks Here’s the full code just in case I’m doing something really stupid.
Some redundant/innefficient stuff in here as I’m still learning
I had a few memory warnings early on when I was logging a lot of tweets, could it be memory related ? Thanks for you help
(John) wheable
`// Use #require to load the Twitter library
#require “Twitter.class.nut:1.2.0”
s1<-“fred astaire”.toupper();
s2<-“david cameron”.toupper();
s3<-“strictly come dancing”.toupper();
s4<-“taylor swift”.toupper();
s5<-“david beckham”.toupper();
s6<-“mad museum”.toupper();
search_string<- s1+","+s2+","+s3+","+s4+","+s5+","+s6;
local count=0;
// Instantiate a Twitter object (enter your own Twitter app secrets)
local twitter = Twitter(“", "", "******","” );
// Start searching Twitter stream with default string
function onTweet(tweetData)
{
//search tweetdata for text and flash appropriate lights
local result=tweetData.text.toupper();
local leds=0;
if(result.find(s1)!=null)
{
server.log(format("Found: %s ",s1));
leds=leds|(0x01);
}
if(result.find(s2)!=null)
{
server.log(format("Found: %s ",s2));
leds=leds|(0x02);
}
if(result.find(s3)!=null)
{
server.log(format("Found: %s ",s3));
leds=leds|(0x04);
}
if(result.find(s4)!=null)
{
server.log(format("Found: %s ",s4));
leds=leds|(0x08);
}
if(result.find(s5)!=null)
{
server.log(format("Found: %s ",s5));
leds=leds|(0x10);
}
if(result.find(s6)!=null)
{
server.log(format("Found: %s ",s6));
leds=leds|(0x20);
}
if(leds!=0)
{
device.send("tweet", leds);
//server.log(format("%s - %s - %s", tweetData.text, tweetData.user.screen_name, tweetData.id_str));
}
else
{
// server.log(format("Tweet but no match in text : %s - %s - %s", tweetData.text, tweetData.user.screen_name, tweetData.id_str));
}
local tweetTable =
{
"in_reply_to_status_id" : tweetData.id_str,
"status" : format("@%s Thanks for saying hello! (tweeted at %i)", tweetData.user.screen_name, time())
};
//twitter.tweet(tweetTable);//disabled for the moment
}
function onError(errors)
{
// Log all the error messages
foreach (err in errors)
{
server.error(err.code + ":-- " + err.message);
}
// Close the stream then re-open it
twitter.closeStream();
device.send("reset_count",1);
twitter.stream(search_string, onTweet,onError);
}
function myFind(match_string,string)
{
//
local lm=match_string.len();
local ls=string.len();
local m=0;
local s=0;
local match=false;
while((s<ls)&&(!match))
{
// server.log(format(" %s %s ",match_string.slice(m,m+1),string.slice(s,s+1)));
if(match_string.slice(m,m+1)==string.slice(s,s+1))
{
m++;
if(m==lm) match=true;
}
else
{
m=0;
}
s++;
}
return match;
}
function requestHandler(request, response) {
server.log(“receieved http request”);
try {
if (“setting” in request.query) {
// ‘setting’ is a URL-encoded parameter, ie. '/setting=4’
local settingValue = request.query.setting;
server.log (request.query.setting);
// Use the ‘response’ object to acknowledge reception of the request
// to the request’s source. ‘200’ is HTTP status code for 'OK’
response.send(200, “Setting received and applied”);
}
} catch (error) {
// Something went wrong; inform the source of the request
// ‘500’ is HTTP status code for 'Internal Server Error’
response.send(500, error);
}
}
// Register the handler function as a callback
http.onrequest(requestHandler);
twitter.stream(search_string, onTweet,onError);
imp code
// Create a global variabled called ‘led’ and assign the ‘pin9’ object to it
// The <- is Squirrel’s way of creating a global variable and assigning its initial value
count <- 0;
led1 <- hardware.pin1;
led2 <- hardware.pin2;
led3 <- hardware.pin5;
led4 <- hardware.pin7;
led5 <- hardware.pin8;
led6 <- hardware.pin9;
// Configure pin
// Configure ‘led’ to be a digital output with a starting value of digital 0 (low, 0V)
led1.configure(DIGITAL_OUT, 0);
led2.configure(DIGITAL_OUT, 0);
led3.configure(DIGITAL_OUT, 0);
led4.configure(DIGITAL_OUT, 0);
led5.configure(DIGITAL_OUT, 0);
led6.configure(DIGITAL_OUT, 0);
// Function called to turn the LED on or off
// according to the value passed in (1 = on, 0 = off)
function setLedState(state) {
//server.log("Set LED to state: " + state);
if (state&0x01) led1.write(1);else led1.write(0);
if (state&0x02) led2.write(1);else led2.write(0);
if (state&0x04) led3.write(1);else led3.write(0);
if (state&0x08) led4.write(1);else led4.write(0);
if (state&0x10) led5.write(1);else led5.write(0);
if (state&0x20) led6.write(1);else led6.write(0);
}
function reset_count(parameter)
{
count=0;
}
function poll()
{
// Read the pin and log its value
count++;
//server.log(count + " seconds since last error");
// Wake up in 0.1 seconds and do it again
setLedState(0);
imp.wakeup(0.5, poll);
}
// Register a handler for incoming “set.led” messages from the agent
agent.on(“tweet”, setLedState);
agent.on(“reset_count”,reset_count);
poll();
`