// Tweet Switch by http://www.brianmoreau.com/projects/social-devices/arduino-tweet-switch.php // Updates your Twitter Status when switch is made const int buttonPin = 2; // the number of the pushbutton pin const int ledPin = 8; // the number of the LED tweet pin int buttonState = 0; // variable for reading the pushbutton status // load SPI COMM and wifi library #include #include // Your wifi network name and password // char ssid[] = "YOUR NETWORK NAME"; // your network SSID (name) //char pass[] = "PASSWORD or WEB KEY"; // your network password (use for WPA, or use as key for WEP) int keyIndex = 0; // your network key Index number (needed only for WEP) int status = WL_IDLE_STATUS; // status of the wifi connection char myStatus = 0; // status when tween sent // Initialize the Wifi client library WiFiClient client; // server to connect to : This is the domain where your PHP script is stored char server[] = "www.brianmoreau.com"; // tweet: This is the status or tweet mesage you want to send to twitter, use %20 for spaces as the message should be URL encoded String tweet = "TEST%20TWEET%20from%20Arduino%20WiFi"; // tweet command url: modify the dirname to match the location url of yoru PHP script // change the value of k= to match the key in your PHP script to prevent unauthorised access String url = "GET /dirname/twitterapi.php?k=123456&c=TWEET&v=" + tweet + " HTTP/1.1"; void setup() { // initialize the pushbutton pin as an input: pinMode(buttonPin, INPUT); // initialize the LED pin as an output: pinMode(ledPin, OUTPUT); //Initialize serial and wait for port to open: Serial.begin(9600); while (!Serial) { ; // wait for serial port to connect. Needed for Leonardo only } // check for the presence of the shield: if (WiFi.status() == WL_NO_SHIELD) { Serial.println("WiFi shield not present, try reset."); // don't continue: while(true); } // attempt to connect to Wifi network: while ( status != WL_CONNECTED) { Serial.print("Attempting to connect to SSID: "); Serial.println(ssid); // Connect to WPA/WPA2 network. Change this line if using open or WEP network: status = WiFi.begin(ssid, pass); // wait 5 seconds for next try delay(5000); } // show led connected status digitalWrite(ledPin2, HIGH); // you're connected now, so print device and network info: printWifiStatus(); } // end setup void loop() { // print the output wifi buffer if data exists while (client.available()) { char c = client.read(); Serial.write(c); } // read the state of the pushbutton value: buttonState = digitalRead(buttonPin); if (buttonState == HIGH) { // turn tweet LED on: digitalWrite(ledPin, HIGH); sendTweet(); } else { // switch not closed // turn tweet LED off: digitalWrite(ledPin, LOW); } } // end loop void printWifiStatus() { Serial.print("Firmware V: "); Serial.println(WiFi.firmwareVersion()); // print the SSID of the network you're attached to: Serial.print("SSID: "); Serial.println(WiFi.SSID()); // print your WiFi shield's IP address: IPAddress ip = WiFi.localIP(); Serial.print("IP Address: "); Serial.println(ip); // print the MAC address of the router you're attached to: byte bssid[6]; WiFi.BSSID(bssid); Serial.print("BSSID: "); Serial.print(bssid[5],HEX); Serial.print(":"); Serial.print(bssid[4],HEX); Serial.print(":"); Serial.print(bssid[3],HEX); Serial.print(":"); Serial.print(bssid[2],HEX); Serial.print(":"); Serial.print(bssid[1],HEX); Serial.print(":"); Serial.println(bssid[0],HEX); // print your subnet mask: IPAddress subnet = WiFi.subnetMask(); Serial.print("NetMask: "); Serial.println(subnet); // print your gateway address: IPAddress gateway = WiFi.gatewayIP(); Serial.print("Gateway: "); Serial.println(gateway); // print the received signal strength: long rssi = WiFi.RSSI(); Serial.print("signal strength (RSSI):"); Serial.print(rssi); Serial.println(" dBm"); // print the encryption type: byte encryption = WiFi.encryptionType(); Serial.print("Encryption Type:"); Serial.println(encryption,HEX); // TKIP (WPA) = 2 // WEP = 5 // CCMP (WPA) = 4 // NONE = 7 // AUTO = 8 } // this function makes the HTTP connection to the server: void sendTweet() { // if tweet not sent while (myStatus == 0) { // attempt to connect to web adderss server if (client.connect(server, 80)) { Serial.println(); Serial.print("Connecting to "); Serial.println(server); client.println(); // send the HTTP PUT / GET request // not secure, look at using POST client.println(url); client.println("Host: www.brianmoreau.com"); client.println("User-Agent: arduino-ethernet"); client.println("Connection: close"); client.println(); myStatus = 1; } else { // fail http req // note not a fail if page not found as get 404 Serial.println("http failed"); client.println(); } } // end while not sent // reset the sent status myStatus = 0; // wait 10 seconds to avoid continious tweeting because of button bounce delay(10000); }