Read state of an input pin and tweet it

Hi guis I’m really a noob with my IMP and in english too; since last year I was running my IMP with a copy/paste code from whiskeytangohotel (tweet cat door) adjusted to my scope.
EASY JOB :

  1. Read the level on pin1 (high or low)
  2. Post a tweet (Level High or Level low) via a php script on Opensen.se.

As I read on recent post by IJLabs and all you gorgeous forum member: Hugo, Peter, jwehr, brendandawes, odb, beardedinventor the system must be reviewed and I tried many times without success.

I would underline that I don’t need to randomize the text on the tweet message but I need only a tweet “ON” in case of pin1 High and tweet “OFF” in case of pin1 LOW

Attached device code and agent code.

Please let me understand

Hi,

maybe you should try something like this.

Device:
`
//http://electricimp.com/docs/api/hardware/pin/read/
pin <- hardware.pin1;

function readPin() {
local state = pin.read();
//state will be 0 or 1
agent.send(“pinState”,state);
}
//each time the state of the pin changes, the function readPin will be called.
pin.configure(DIGITAL_IN_PULLUP, readPin);
`

Agent:
`
/*
Twitter code by @electricimp
https://github.com/electricimp/reference/tree/master/webservices/twitter
*/

helper <- {
function encode(str) {
return http.urlencode({ s = str }).slice(2);
}
}

class TwitterClient {
consumerKey = null;
consumerSecret = null;
accessToken = null;
accessSecret = null;

baseUrl = "https://api.twitter.com/";

constructor (_consumerKey, _consumerSecret, _accessToken, _accessSecret) {
    this.consumerKey = _consumerKey;
    this.consumerSecret = _consumerSecret;
    this.accessToken = _accessToken;
    this.accessSecret = _accessSecret;
}

function post_oauth1(postUrl, headers, post) {
    local time = time();
    local nonce = time;

    local parm_string = http.urlencode({ oauth_consumer_key = consumerKey });
    parm_string += "&" + http.urlencode({ oauth_nonce = nonce });
    parm_string += "&" + http.urlencode({ oauth_signature_method = "HMAC-SHA1" });
    parm_string += "&" + http.urlencode({ oauth_timestamp = time });
    parm_string += "&" + http.urlencode({ oauth_token = accessToken });
    parm_string += "&" + http.urlencode({ oauth_version = "1.0" });
    parm_string += "&" + http.urlencode({ status = post });
    
    local signature_string = "POST&" + helper.encode(postUrl) + "&" + helper.encode(parm_string)
    
    local key = format("%s&%s", helper.encode(consumerSecret), helper.encode(accessSecret));
    local sha1 = helper.encode(http.base64encode(http.hash.hmacsha1(signature_string, key)));
    
    local auth_header = "oauth_consumer_key=\""+consumerKey+"\", ";
    auth_header += "oauth_nonce=\""+nonce+"\", ";
    auth_header += "oauth_signature=\""+sha1+"\", ";
    auth_header += "oauth_signature_method=\""+"HMAC-SHA1"+"\", ";
    auth_header += "oauth_timestamp=\""+time+"\", ";
    auth_header += "oauth_token=\""+accessToken+"\", ";
    auth_header += "oauth_version=\"1.0\"";
    
    local headers = {
        "Authorization": "OAuth " + auth_header,
    };
    
    local response = http.post(postUrl + "?status=" + helper.encode(post), headers, "").sendsync();
    return response
}

function Tweet(_status) {
    local postUrl = baseUrl + "1.1/statuses/update.json";
    local headers = { };
    
    local response = post_oauth1(postUrl, headers, _status)
    if (response && response.statuscode != 200) {
        server.log("Error updating_status tweet. HTTP Status Code " + response.statuscode);
        server.log(response.body);
        return null;
    } else {
        server.log("Tweet Successful!");
    }
}

}
//
_CONSUMER_KEY <- “MY KEY”
_CONSUMER_SECRET <- “MY SECRET”
_ACCESS_TOKEN <- “MY ACCESS TOKEN”
_ACCESS_SECRET <- "MY ACCESS SECRET"
twitter <- TwitterClient(_CONSUMER_KEY, _CONSUMER_SECRET, _ACCESS_TOKEN, _ACCESS_SECRET);

function sendTweet(data){
twitter.Tweet("@YOUR_TWITTER_HANDLE"+" "+data);
}

device.on(“pinState”,sendTweet);
`

I haven’t tested it though.
Don’t forget to setup a Twitter app.
You’ll have to replace:
“MY KEY”
“MY SECRET”
“MY ACCESS TOKEN”
“MY ACCESS SECRET”
"@YOUR_TWITTER_HANDLE"
with you data.

g.
mezelve

Many Thanks Mezelve. I will try it maybe tonight than I leave a feedback. Twitter app already done hopefully…

working perfectly mezelve!
Thanks again!