1
0
mirror of https://github.com/arduino/Arduino.git synced 2024-11-29 10:24:12 +01:00

removing TwitterClient since it does not include OAuth, which is now needed for Twitter logins. Adding TelnetClient examexample.

This commit is contained in:
Tom Igoe 2010-09-17 13:22:32 +00:00
parent 7392f8514d
commit ae0c8770ac
5 changed files with 99 additions and 144 deletions

View File

@ -11,7 +11,7 @@
* Ethernet shield attached to pins 10, 11, 12, 13
created 15 March 2010
updated 25 July 2010
updated 4 Sep 2010
by Tom Igoe
http://www.tigoe.net/pcomp/code/category/arduinowiring/873
@ -55,7 +55,7 @@ void setup() {
void loop() {
// read the analog sensor:
int sensorReading = analogRead(0);
int sensorReading = analogRead(A0);
// if there's incoming data from the net connection.
// send it out the serial port. This is for debugging

View File

@ -14,7 +14,7 @@
* Ethernet shield attached to pins 10, 11, 12, 13
created 15 March 2010
updated 25 July 2010
updated 4 Sep 2010
by Tom Igoe
This code is in the public domain.
@ -57,13 +57,13 @@ void setup() {
void loop() {
// read the analog sensor:
int sensorReading = analogRead(0);
int sensorReading = analogRead(A0);
// convert the data to a String to send it:
String dataString = String(sensorReading);
// you can append multiple readings to this String if your
// pachube feed is set up to handle multiple values:
int otherSensorReading = analogRead(1);
int otherSensorReading = analogRead(A1);
dataString += ",";
dataString += String(otherSensorReading);

View File

@ -0,0 +1,89 @@
/*
Telnet client
This sketch connects to a a telnet server (http://www.google.com)
using an Arduino Wiznet Ethernet shield. You'll need a telnet server
to test this with.
Processing's ChatServer example (part of the network library) works well,
running on port 10002. It can be found as part of the examples
in the Processing application, available at
http://processing.org/
Circuit:
* Ethernet shield attached to pins 10, 11, 12, 13
created 14 Sep 2010
by Tom Igoe
*/
#include <SPI.h>
#include <Ethernet.h>
// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[] = {
0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip[] = {
192,168,1,177 };
// Enter the IP address of the server you're connecting to:
byte server[] = {
1,1,1,1 };
// Initialize the Ethernet client library
// with the IP address and port of the server
// that you want to connect to (port 23 is default for telnet;
// if you're using Processing's ChatServer, use port 10002):
Client client(server, 10002);
void setup() {
// start the Ethernet connection:
Ethernet.begin(mac, ip);
// start the serial library:
Serial.begin(9600);
// give the Ethernet shield a second to initialize:
delay(1000);
Serial.println("connecting...");
// if you get a connection, report back via serial:
if (client.connect()) {
Serial.println("connected");
}
else {
// if you didn't get a connection to the server:
Serial.println("connection failed");
}
}
void loop()
{
// if there are incoming bytes available
// from the server, read them and print them:
if (client.available()) {
char c = client.read();
Serial.print(c);
}
// as long as there are bytes in the serial queue,
// read them and send them out the socket if it's open:
while (Serial.available() > 0) {
char inChar = Serial.read();
if (client.connected()) {
client.print(inChar);
}
}
// if the server's disconnected, stop the client:
if (!client.connected()) {
Serial.println();
Serial.println("disconnecting.");
client.stop();
// do nothing:
while(true);
}
}

View File

@ -1,136 +0,0 @@
/*
Twitter client
This sketch connects to Twitter (http://www.twitter.com)
using a Wiznet Ethernet shield. You can use the Arduino Ethernet shield, or
the Adafruit Ethernet shield, either one will work, as long as it's got
a Wiznet Ethernet module on board.
Circuit:
* Switch connected to digital pin 2
* Ethernet shield attached to pins 10, 11, 12, 13
created 15 March 2010
modified 23 July 2010
by Tom Igoe
http://www.tigoe.net/pcomp/code/category/arduinowiring/873
This code is in the public domain.
*/
#include <SPI.h>
#include <Ethernet.h>
// pin that the pushButton is connected to:
const int buttonPin = 2;
// assign a MAC address for the ethernet controller.
// fill in your address here:
byte mac[] = {
0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};
// assign an IP address for the controller:
byte ip[] = {
192,169,1,20 };
byte gateway[] = {
192,168,1,1};
byte subnet[] = {
255, 255, 255, 0 };
// The server you want to connect to (twitter.com)
byte server[] = {
168,143,162,68};
// initialize the library instance:
Client client(server, 80);
int lastButtonState = LOW; // last state of the pushbutton
boolean connectedLastTime = false; // state of the connection last time through the main loop
void setup() {
// make the pushbutton's pin an input:
pinMode(buttonPin, INPUT);
// start the ethernet connection and serial port:
Ethernet.begin(mac, ip);
Serial.begin(9600);
// give the ethernet module time to boot up:
delay(1000);
}
void loop() {
// if there's incoming data from the net connection.
// send it out the serial port. This is for debugging
// purposes only:
if (client.available()) {
char c = client.read();
Serial.print(c);
}
// if there's no net connection, but there was one last time
// through the loop, then stop the client:
if (!client.connected() && connectedLastTime) {
Serial.println("disconnecting.");
client.stop();
}
// read the pushbutton input pin:
int buttonState = digitalRead(buttonPin);
// make a connection only when the button goes from LOW to HIGH:
if ((buttonState != lastButtonState) && (buttonState == HIGH)) {
// if you're not connected, then connect:
if(!client.connected()) {
sendData();
}
// save the current button state as the last state,
//for next time through the loop
}
lastButtonState = buttonState;
// store the state of the connection for next time through
// the loop:
connectedLastTime = client.connected();
}
// this method makes a HTTP connection to the server:
void sendData() {
// if there's a successful connection:
if (client.connect()) {
Serial.println("connecting...");
// send the HTTP POST request:
client.print("POST http://twitter.com/statuses/update.json HTTP/1.1\n");
client.print("Host: twitter.com\n");
// fill in your twitter login here. It needs to be
// formatted like this: username:password
// then it needs to be base64_encoded.
// you can do that online at many sites, including this one:
// http://www.tools4noobs.com/online_php_functions/base64_encode/
// once encoded, it'll look like a random string of characters
client.print("Authorization: Basic XXXXXXXXXXXXXXXXXXXXXXXX\n");
client.print("Content-type: application/x-www-form-urlencoded\n");
// content length of the status message that follows below:
client.print("Content-Length: 26\n");
client.println("Connection: Close\n");
// generate a random number to add to the status message
// to avoid duplicate status messages. This is a hack to get around
// Twitter's requirement of unique status messages:
int randomNumber = random(9);
// here's the status message:
client.print("status=Hello from Arduino");
client.print(randomNumber);
}
else {
// if you couldn't make a connection:
Serial.println("connection failed");
}
}

View File

@ -10,6 +10,8 @@
created 18 Dec 2009
by David A. Mellis
modified 4 Sep 2010
by Tom Igoe
*/
@ -53,11 +55,11 @@ void loop()
client.println();
// output the value of each analog input pin
for (int i = 0; i < 6; i++) {
for (int analogChannel = 0; analogChannel < 6; analogChannel++) {
client.print("analog input ");
client.print(i);
client.print(analogChannel);
client.print(" is ");
client.print(analogRead(i));
client.print(analogRead(analogChannel));
client.println("<br />");
}
break;