2012/11/05

How to Make "Defcon meter"

SUMMARY


Defcon Meter is a gadget that can be controlled through Twitter.
The hardware is using EhterShield and Arduino.



To read and write Twitter, it is necessary to certify OAuth.

"StewGate" is a great service that will take over the authentication process. It has been provided Accense Technology, by Inc..
"Stewitter" is a library for the Arduino for accessing Twitter via StewGate. It was produced by Mr. Arm22.





Thank you.


BEHAVIOR

This gadget expresses to those who are away from the PC, what you are how much imminent.

I do the following operations.

Sending a message to a dedicated account Twitter lamps out of 5 varies according to the message.


For example, when sent from twitter as "@ defcon_nekotoro defcon4", Defcon meter lamp somewhere in Japan it will be 4.
Warning to ALERT_4 to members in the room.

In this way, through Twitter from smartphones and PC,
You will be able to control the gadget that led to the internet.


 

PRODUCTION


Implemented simply repeated EtherSheld to Arduino, Hardware, we only connect five LED.





Next, prepare the account of Twitter. Http://stewgate.appspot.com/sg1/ to access while logged in, we do the authentication.
Now, I should be able to get the token.

Environment is a library of Stewitter 's blog Arm22 downloaded from,
I reside in the arduino-1.0.1 \ libraries.

After that, I'll write a sketch.

# Include <SPI.h>
# Include <Ethernet.h>
# Include <Stewitter.h>

/ / Todo: Please change to those described in the MAC address of your board.
byte mac [] = {
0x90, 0xA2, 0xDA, 0x0D, 0x53, 0x30};

/ / Todo: please rewrite in your token "YourToken".
Stewitter twitter ("YourToken");

String old_tw_Tick = "0";
String old_tw_Whois = "";

String tw_Date;
String tw_Tick;
String tw_Whois;
String tw_Massage;

char t_msg [140 +1] = "";

# Define LED_D1 9
# Define LED_D2 8
# Define LED_D3 7
# Define LED_D4 6
# Define LED_D5 5

int defcon_level = 5;

boolean set_post = false;
boolean report_post = false;

void setup ()
{

pinMode (LED_D1, OUTPUT);
pinMode (LED_D2, OUTPUT);
pinMode (LED_D3, OUTPUT);
pinMode (LED_D4, OUTPUT);
pinMode (LED_D5, OUTPUT);
delay (1000);

set_led (1);
delay (500);
set_led (2);
delay (500);
set_led (3);
delay (500);
set_led (4);
delay (500);
set_led (5);
delay (3000);

set_led (0);
delay (500);

Ethernet.begin (mac);

Serial.begin (9600);
Serial.println ("connecting ...");

set_led (defcon_level);

set_post = false;
report_post = false;

/ / Connect to twitter, get (messages containing @ yourname) mentions the last

}

void loop ()
{
check_mention ();
set_led (defcon_level);

delay (10000);
}

void set_led (int level)
{
digitalWrite (LED_D1, LOW);
digitalWrite (LED_D2, LOW);
digitalWrite (LED_D3, LOW);
digitalWrite (LED_D4, LOW);
digitalWrite (LED_D5, LOW);

switch (level) {
case 1:
digitalWrite (LED_D1, HIGH);
break;
case 2:
digitalWrite (LED_D2, HIGH);
break;
case 3:
digitalWrite (LED_D3, HIGH);
break;
case 4:
digitalWrite (LED_D4, HIGH);
break;
case 5:
digitalWrite (LED_D5, HIGH);
break;
}
}

void check_mention ()
{
int i;
if (twitter.lastMention ()) {
/ / Wait for the completion
int status = twitter.wait ();
/ / Completed successfully if the 200 status
if (status == 200) {
/ / Display the last mentions
/ / Ex) 20100111082341 | 874453678 | whosaysni | @ yourname whats up?
/ / Serial.println (twitter.response ());

/ / Str = twitter.response ();
String str = twitter.response ();

/ / Serial.println (twitter.response ());
/ / Serial.println (str);

/ / Find date
int s_pos = 0;
int e_pos = 0;

e_pos = str.indexOf ("|", s_pos);
String tw_Date = str.substring (s_pos, e_pos);
/ / Serial.println (tw_Date);
s_pos = e_pos + 1;

/ / Find tw_chick
e_pos = str.indexOf ("|", s_pos);
String tw_Tick = str.substring (s_pos, e_pos);
/ / Serial.println (tw_Tick);
s_pos = e_pos + 1;

/ / Find whoistw
e_pos = str.indexOf ("|", s_pos);
String tw_Whois = str.substring (s_pos, e_pos);
/ / Serial.println (tw_Whois);
s_pos = e_pos + 1;

/ / Find message

String tw_Massage = str.substring (s_pos);
/ / Serial.println (tw_Massage);

if (! tw_Tick.equals (old_tw_Tick)) {

Serial.println (tw_Date);
Serial.println (tw_Tick);
Serial.println (tw_Whois);
Serial.println (tw_Massage);
old_tw_Tick = tw_Tick;

if (tw_Massage.indexOf ("? defcon", 0)! = -1) {

/ / Report_post ();

set_post = false;
report_post = true;
}
if (tw_Massage.indexOf ("defcon1", 0)! = -1) {
Serial.println ("DEFCON LEVEL 1");
defcon_level = 1;
/ / Setlevel_post ();

set_post = true;
report_post = false;
}
if (tw_Massage.indexOf ("defcon2", 0)! = -1) {
Serial.println ("DEFCON LEVEL 2");
defcon_level = 2;
/ / Setlevel_post ();

set_post = true;
report_post = false;
}
if (tw_Massage.indexOf ("defcon3", 0)! = -1) {
Serial.println ("DEFCON LEVEL 3");
defcon_level = 3;
/ / Setlevel_post ();
set_post = true;
report_post = false;
}
if (tw_Massage.indexOf ("defcon4", 0)! = -1) {
Serial.println ("DEFCON LEVEL 4");
defcon_level = 4;
/ / Setlevel_post ();
set_post = true;
report_post = false;
}
if (tw_Massage.indexOf ("defcon5", 0)! = -1) {
Serial.println ("DEFCON LEVEL 5");
defcon_level = 5;
/ / Setlevel_post ();
set_post = true;
report_post = false;
}

if (set_post) {
set_post = false;
          
String msg = "@";
msg + = tw_Whois;
msg + = "SET DEFCON LEVEL";
msg + = defcon_level;
msg + = "/";
msg + = tw_Date;

for (i = 0; i <msg.length (); i + +) {
t_msg [i] = msg [i];
}

/ / Sprintf (t_msg, "% s", msg);
Serial.println (t_msg);
post ();
}

if (report_post) {
report_post = false;
          
String msg = "@";
msg + = tw_Whois;
msg + = "NOW DEFCON LEVEL";
msg + = defcon_level;
msg + = "/";
msg + = tw_Date;

for (i = 0; i <msg.length (); i + +) {
t_msg [i] = msg [i];
}

/ / Sprintf (t_msg, "% s", msg);
Serial.println (t_msg);
post ();
}
}
}
else {
Serial.print ("failed: code");
Serial.println (status);
}
}
else {
Serial.println ("connection failed.");
}
}

void post ()
{
Serial.println ("connecting ...");
if (twitter.post (t_msg)) {
int status = twitter.wait ();
if (status == 200) {
Serial.println ("OK.");
}
else {
Serial.print ("failed: code");
Serial.println (status);
}
}
else {
Serial.println ("connection failed.");
}
delay (1000);
}

What you are doing, the


  1. Once every 10 seconds, I'll check his mentions of whether they are destined to come.
  2. Compared with the mentions had come before, I will update as the latest data was different.
  3. Parses the message, change the LED I were to change command.
  4. Written to the current state of Twitter.


Is.

Currently, I have come to a reaction from anyone in the mentions.
I think it is possible, if necessary using filter "whois".



Thanks


Thanks

"Connecting to the net own gadget", I'm very excited.
I would like to thank "Stewitter" and "StewGate"!

Let's Try to realize your ideas!




0 件のコメント:

コメントを投稿