In the progressing of internet of things. I plan to start with twitter as a beginning. The prototyping which is just a button connected to pin 2 and I wanna trigger a twit once the button has been pressed, but my code seems doesn’t work.
`// Copyright © 2013 Electric Imp
// This file is licensed under the MIT License
// http://opensource.org/licenses/MIT
// Twitter Keys
const API_KEY = “”;
const API_SECRET = “”;
const AUTH_TOKEN = “”;
const TOKEN_SECRET = “”;
class Twitter {
// OAuth
_consumerKey = null;
_consumerSecret = null;
_accessToken = null;
_accessSecret = null;
// URLs
streamUrl = "https://stream.twitter.com/1.1/";
tweetUrl = "https://api.twitter.com/1.1/statuses/update.json";
// Streaming
streamingRequest = null;
_reconnectTimeout = null;
_buffer = null;
constructor (consumerKey, consumerSecret, accessToken, accessSecret) {
this._consumerKey = consumerKey;
this._consumerSecret = consumerSecret;
this._accessToken = accessToken;
this._accessSecret = accessSecret;
this._reconnectTimeout = 60;
this._buffer = "";
}
/***************************************************************************
* function: Tweet
* Posts a tweet to the user's timeline
*
* Params:
* status - the tweet
* cb - an optional callback
*
* Return:
* bool indicating whether the tweet was successful(if no cb was supplied)
* nothing(if a callback was supplied)
**************************************************************************/
function tweet(status, cb = null) {
local headers = { };
local request = _oAuth1Request(tweetUrl, headers, { "This twit was generated by a button via electric imp as a test": status} );
if (cb == null) {
local response = request.sendsync();
if (response && response.statuscode != 200) {
server.log(format("Error updating_status tweet. HTTP Status Code %i:\\r\
%s", response.statuscode, response.body));
return false;
} else {
return true;
}
} else {
request.sendasync(cb);
}
}
}
// register the HTTP handler
http.onrequest(requestHandler);`
device`// create a global variable called button,
// and assign pin1 to it
button <- hardware.pin2;
// create a callback function for our button
function buttonChanged() {
// software debounce
imp.sleep(0.02);
// read the button
local state = button.read();
// log the reading
server.log(state);
// send a message to the agent
agent.send(“tweet”, !state);
}
// configure button to be a DIGITAL_IN
button.configure(DIGITAL_IN_PULLUP, buttonChanged);`
the device log:
2014-09-25 03:03:02 UTC+10 [Device] 0
2014-09-25 03:03:02 UTC+10 [Device] 1
2014-09-25 03:03:02 UTC+10 [Agent] ERROR: no handler for agent.send()
2014-09-25 03:03:02 UTC+10 [Agent] ERROR: no handler for agent.send()
2014-09-25 03:03:02 UTC+10 [Device] 0
2014-09-25 03:03:02 UTC+10 [Device] 1
2014-09-25 03:03:03 UTC+10 [Agent] ERROR: no handler for agent.send()
2014-09-25 03:03:03 UTC+10 [Device] 0
2014-09-25 03:03:03 UTC+10 [Agent] ERROR: no handler for agent.send()
2014-09-25 03:03:03 UTC+10 [Device] 1
2014-09-25 03:03:43 UTC+10 [Agent] ERROR: the index ‘requestHandler’ does not exist
2014-09-25 03:03:43 UTC+10 [Agent] ERROR: at main:70
2014-09-25 03:03:43 UTC+10 [Status] Device Booting; 1.97% program storage used
2014-09-25 03:03:48 UTC+10 [Device] 0
2014-09-25 03:03:48 UTC+10 [Device] 1
2014-09-25 03:03:53 UTC+10 [Agent] ERROR: the index ‘requestHandler’ does not exist
2014-09-25 03:03:53 UTC+10 [Agent] ERROR: at main:70
2014-09-25 03:04:03 UTC+10 [Agent] ERROR: the index ‘requestHandler’ does not exist
2014-09-25 03:04:03 UTC+10 [Agent] ERROR: at main:70
2014-09-25 03:04:13 UTC+10 [Agent] ERROR: the index ‘requestHandler’ does not exist
2014-09-25 03:04:13 UTC+10 [Agent] ERROR: at main:70
2014-09-25 03:04:23 UTC+10 [Agent] ERROR: the index ‘requestHandler’ does not exist
2014-09-25 03:04:23 UTC+10 [Agent] ERROR: at main:70
2014-09-25 03:04:33 UTC+10 [Agent] ERROR: the index ‘requestHandler’ does not exist
2014-09-25 03:04:33 UTC+10 [Agent] ERROR: at main:70
2014-09-25 03:04:43 UTC+10 [Agent] ERROR: the index ‘requestHandler’ does not exist
2014-09-25 03:04:43 UTC+10 [Agent] ERROR: at main:70
2014-09-25 03:04:53 UTC+10 [Agent] ERROR: the index ‘requestHandler’ does not exist
2014-09-25 03:04:53 UTC+10 [Agent] ERROR: at main:70
2014-09-25 03:05:03 UTC+10 [Agent] ERROR: the index ‘requestHandler’ does not exist
2014-09-25 03:05:03 UTC+10 [Agent] ERROR: at main:70
sincerely seeking for help!
Joseph