mirror of
https://github.com/esp8266/Arduino.git
synced 2026-01-06 05:22:30 +03:00
Merge branch 'master' into wifly_integration
This commit is contained in:
@@ -5,7 +5,7 @@ extern "C" {
|
||||
#include "string.h"
|
||||
}
|
||||
|
||||
#include "WProgram.h"
|
||||
#include "Arduino.h"
|
||||
|
||||
#include "Ethernet.h"
|
||||
#include "Client.h"
|
||||
@@ -70,19 +70,24 @@ int Client::connect(IPAddress ip, uint16_t port) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
void Client::write(uint8_t b) {
|
||||
if (_sock != MAX_SOCK_NUM)
|
||||
send(_sock, &b, 1);
|
||||
size_t Client::write(uint8_t b) {
|
||||
return write(&b, 1);
|
||||
}
|
||||
|
||||
void Client::write(const char *str) {
|
||||
if (_sock != MAX_SOCK_NUM)
|
||||
send(_sock, (const uint8_t *)str, strlen(str));
|
||||
size_t Client::write(const char *str) {
|
||||
return write((const uint8_t *) str, strlen(str));
|
||||
}
|
||||
|
||||
void Client::write(const uint8_t *buf, size_t size) {
|
||||
if (_sock != MAX_SOCK_NUM)
|
||||
send(_sock, buf, size);
|
||||
size_t Client::write(const uint8_t *buf, size_t size) {
|
||||
if (_sock == MAX_SOCK_NUM) {
|
||||
setWriteError();
|
||||
return 0;
|
||||
}
|
||||
if (!send(_sock, buf, size)) {
|
||||
setWriteError();
|
||||
return 0;
|
||||
}
|
||||
return size;
|
||||
}
|
||||
|
||||
int Client::available() {
|
||||
@@ -156,18 +161,8 @@ uint8_t Client::status() {
|
||||
return W5100.readSnSR(_sock);
|
||||
}
|
||||
|
||||
// the next three functions are a hack so we can compare the client returned
|
||||
// by Server::available() to null, or use it as the condition in an
|
||||
// if-statement. this lets us stay compatible with the Processing network
|
||||
// library.
|
||||
|
||||
uint8_t Client::operator==(int p) {
|
||||
return _sock == MAX_SOCK_NUM;
|
||||
}
|
||||
|
||||
uint8_t Client::operator!=(int p) {
|
||||
return _sock != MAX_SOCK_NUM;
|
||||
}
|
||||
// the next function allows us to use the client returned by
|
||||
// Server::available() as the condition in an if-statement.
|
||||
|
||||
Client::operator bool() {
|
||||
return _sock != MAX_SOCK_NUM;
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
#ifndef client_h
|
||||
#define client_h
|
||||
#include "WProgram.h"
|
||||
#include "Arduino.h"
|
||||
#include "Print.h"
|
||||
#include "NetClient.h"
|
||||
#include "IPAddress.h"
|
||||
@@ -14,9 +14,9 @@ public:
|
||||
uint8_t status();
|
||||
virtual int connect(IPAddress ip, uint16_t port);
|
||||
virtual int connect(const char *host, uint16_t port);
|
||||
virtual void write(uint8_t);
|
||||
virtual void write(const char *str);
|
||||
virtual void write(const uint8_t *buf, size_t size);
|
||||
virtual size_t write(uint8_t);
|
||||
virtual size_t write(const char *str);
|
||||
virtual size_t write(const uint8_t *buf, size_t size);
|
||||
virtual int available();
|
||||
virtual int read();
|
||||
virtual int read(uint8_t *buf, size_t size);
|
||||
|
||||
@@ -6,7 +6,7 @@
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include "Dhcp.h"
|
||||
#include "wiring.h"
|
||||
#include "Arduino.h"
|
||||
#include "util.h"
|
||||
|
||||
int DhcpClass::beginWithDHCP(uint8_t *mac, unsigned long timeout, unsigned long responseTimeout)
|
||||
@@ -271,11 +271,19 @@ uint8_t DhcpClass::parseDHCPResponse(unsigned long responseTimeout, uint32_t& tr
|
||||
case routersOnSubnet :
|
||||
opt_len = _dhcpUdpSocket.read();
|
||||
_dhcpUdpSocket.read(_dhcpGatewayIp, 4);
|
||||
for (int i = 0; i < opt_len-4; i++)
|
||||
{
|
||||
_dhcpUdpSocket.read();
|
||||
}
|
||||
break;
|
||||
|
||||
case dns :
|
||||
opt_len = _dhcpUdpSocket.read();
|
||||
_dhcpUdpSocket.read(_dhcpDnsServerIp, 4);
|
||||
for (int i = 0; i < opt_len-4; i++)
|
||||
{
|
||||
_dhcpUdpSocket.read();
|
||||
}
|
||||
break;
|
||||
|
||||
case dhcpServerIdentifier :
|
||||
|
||||
@@ -9,7 +9,7 @@
|
||||
#include "Dns.h"
|
||||
#include <string.h>
|
||||
//#include <stdlib.h>
|
||||
#include "wiring.h"
|
||||
#include "Arduino.h"
|
||||
|
||||
|
||||
#define SOCKET_NONE 255
|
||||
|
||||
@@ -33,27 +33,37 @@ int EthernetClass::begin(uint8_t *mac_address)
|
||||
}
|
||||
|
||||
void EthernetClass::begin(uint8_t *mac_address, IPAddress local_ip)
|
||||
{
|
||||
// Assume the DNS server will be the machine on the same network as the local IP
|
||||
// but with last octet being '1'
|
||||
IPAddress dns_server = local_ip;
|
||||
dns_server[3] = 1;
|
||||
begin(mac_address, local_ip, dns_server);
|
||||
}
|
||||
|
||||
void EthernetClass::begin(uint8_t *mac_address, IPAddress local_ip, IPAddress dns_server)
|
||||
{
|
||||
// Assume the gateway will be the machine on the same network as the local IP
|
||||
// but with last octet being '1'
|
||||
IPAddress gateway = local_ip;
|
||||
gateway[3] = 1;
|
||||
begin(mac_address, local_ip, gateway);
|
||||
begin(mac_address, local_ip, dns_server, gateway);
|
||||
}
|
||||
|
||||
void EthernetClass::begin(uint8_t *mac_address, IPAddress local_ip, IPAddress gateway)
|
||||
void EthernetClass::begin(uint8_t *mac_address, IPAddress local_ip, IPAddress dns_server, IPAddress gateway)
|
||||
{
|
||||
IPAddress subnet(255, 255, 255, 0);
|
||||
begin(mac_address, local_ip, gateway, subnet);
|
||||
begin(mac_address, local_ip, dns_server, gateway, subnet);
|
||||
}
|
||||
|
||||
void EthernetClass::begin(uint8_t *mac, IPAddress local_ip, IPAddress gateway, IPAddress subnet)
|
||||
void EthernetClass::begin(uint8_t *mac, IPAddress local_ip, IPAddress dns_server, IPAddress gateway, IPAddress subnet)
|
||||
{
|
||||
W5100.init();
|
||||
W5100.setMACAddress(mac);
|
||||
W5100.setIPAddress(local_ip._address);
|
||||
W5100.setGatewayIp(gateway._address);
|
||||
W5100.setSubnetMask(subnet._address);
|
||||
_dnsServerAddress = dns_server;
|
||||
}
|
||||
|
||||
IPAddress EthernetClass::localIP()
|
||||
|
||||
@@ -20,8 +20,9 @@ public:
|
||||
// Returns 0 if the DHCP configuration failed, and 1 if it succeeded
|
||||
int begin(uint8_t *mac_address);
|
||||
void begin(uint8_t *mac_address, IPAddress local_ip);
|
||||
void begin(uint8_t *mac_address, IPAddress local_ip, IPAddress gateway);
|
||||
void begin(uint8_t *mac_address, IPAddress local_ip, IPAddress gateway, IPAddress subnet);
|
||||
void begin(uint8_t *mac_address, IPAddress local_ip, IPAddress dns_server);
|
||||
void begin(uint8_t *mac_address, IPAddress local_ip, IPAddress dns_server, IPAddress gateway);
|
||||
void begin(uint8_t *mac_address, IPAddress local_ip, IPAddress dns_server, IPAddress gateway, IPAddress subnet);
|
||||
|
||||
IPAddress localIP();
|
||||
IPAddress subnetMask();
|
||||
|
||||
@@ -67,18 +67,20 @@ Client Server::available()
|
||||
return Client(MAX_SOCK_NUM);
|
||||
}
|
||||
|
||||
void Server::write(uint8_t b)
|
||||
size_t Server::write(uint8_t b)
|
||||
{
|
||||
write(&b, 1);
|
||||
}
|
||||
|
||||
void Server::write(const char *str)
|
||||
size_t Server::write(const char *str)
|
||||
{
|
||||
write((const uint8_t *)str, strlen(str));
|
||||
}
|
||||
|
||||
void Server::write(const uint8_t *buffer, size_t size)
|
||||
size_t Server::write(const uint8_t *buffer, size_t size)
|
||||
{
|
||||
size_t n = 0;
|
||||
|
||||
accept();
|
||||
|
||||
for (int sock = 0; sock < MAX_SOCK_NUM; sock++) {
|
||||
@@ -86,7 +88,9 @@ void Server::write(const uint8_t *buffer, size_t size)
|
||||
|
||||
if (EthernetClass::_server_port[sock] == _port &&
|
||||
client.status() == SnSR::ESTABLISHED) {
|
||||
client.write(buffer, size);
|
||||
n += client.write(buffer, size);
|
||||
}
|
||||
}
|
||||
|
||||
return n;
|
||||
}
|
||||
|
||||
@@ -14,9 +14,9 @@ public:
|
||||
Server(uint16_t);
|
||||
Client available();
|
||||
virtual void begin();
|
||||
virtual void write(uint8_t);
|
||||
virtual void write(const char *str);
|
||||
virtual void write(const uint8_t *buf, size_t size);
|
||||
virtual size_t write(uint8_t);
|
||||
virtual size_t write(const char *str);
|
||||
virtual size_t write(const uint8_t *buf, size_t size);
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
@@ -102,21 +102,22 @@ int UDP::endPacket()
|
||||
return sendUDP(_sock);
|
||||
}
|
||||
|
||||
void UDP::write(uint8_t byte)
|
||||
size_t UDP::write(uint8_t byte)
|
||||
{
|
||||
write(&byte, 1);
|
||||
return write(&byte, 1);
|
||||
}
|
||||
|
||||
void UDP::write(const char *str)
|
||||
size_t UDP::write(const char *str)
|
||||
{
|
||||
size_t len = strlen(str);
|
||||
write((const uint8_t *)str, len);
|
||||
return write((const uint8_t *)str, len);
|
||||
}
|
||||
|
||||
void UDP::write(const uint8_t *buffer, size_t size)
|
||||
size_t UDP::write(const uint8_t *buffer, size_t size)
|
||||
{
|
||||
uint16_t bytes_written = bufferData(_sock, _offset, buffer, size);
|
||||
_offset += bytes_written;
|
||||
return bytes_written;
|
||||
}
|
||||
|
||||
int UDP::parsePacket()
|
||||
|
||||
@@ -67,11 +67,11 @@ public:
|
||||
// Returns 1 if the packet was sent successfully, 0 if there was an error
|
||||
int endPacket();
|
||||
// Write a single byte into the packet
|
||||
virtual void write(uint8_t);
|
||||
virtual size_t write(uint8_t);
|
||||
// Write a string of characters into the packet
|
||||
virtual void write(const char *str);
|
||||
virtual size_t write(const char *str);
|
||||
// Write size bytes from buffer into the packet
|
||||
virtual void write(const uint8_t *buffer, size_t size);
|
||||
virtual size_t write(const uint8_t *buffer, size_t size);
|
||||
|
||||
// Start processing the next available incoming packet
|
||||
// Returns the size of the packet in bytes, or 0 if no packets are available
|
||||
|
||||
@@ -0,0 +1,54 @@
|
||||
/*
|
||||
DHCP-based IP printer
|
||||
|
||||
This sketch uses the DHCP extensions to the Ethernet library
|
||||
to get an IP address via DHCP and print the address obtained.
|
||||
using an Arduino Wiznet Ethernet shield.
|
||||
|
||||
Circuit:
|
||||
* Ethernet shield attached to pins 10, 11, 12, 13
|
||||
|
||||
created 12 April 2011
|
||||
by Tom Igoe
|
||||
|
||||
*/
|
||||
|
||||
#include <SPI.h>
|
||||
#include <Ethernet.h>
|
||||
|
||||
// Enter a MAC address for your controller below.
|
||||
// Newer Ethernet shields have a MAC address printed on a sticker on the shield
|
||||
byte mac[] = {
|
||||
0x00, 0xAA, 0xBB, 0xCC, 0xDE, 0x02 };
|
||||
|
||||
// Initialize the Ethernet client library
|
||||
// with the IP address and port of the server
|
||||
// that you want to connect to (port 80 is default for HTTP):
|
||||
Client client;
|
||||
|
||||
void setup() {
|
||||
// start the serial library:
|
||||
Serial.begin(9600);
|
||||
// start the Ethernet connection:
|
||||
Serial.println("Trying to get an IP address using DHCP");z
|
||||
if (Ethernet.begin(mac) == 0) {
|
||||
Serial.println("Failed to configure Ethernet using DHCP");
|
||||
// no point in carrying on, so do nothing forevermore:
|
||||
while(true);
|
||||
}
|
||||
// print your local IP address:
|
||||
Serial.print("My IP address: ");
|
||||
IPAddress myIPAddress = Ethernet.localIP();
|
||||
for (byte thisByte = 0; thisByte < 4; thisByte++) {
|
||||
// print the value of each byte of the IP address:
|
||||
Serial.print(myIPAddress[thisByte], DEC);
|
||||
Serial.print(".");
|
||||
}
|
||||
Serial.println();
|
||||
}
|
||||
|
||||
void loop() {
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,53 @@
|
||||
/*
|
||||
DHCP-based IP printer
|
||||
|
||||
This sketch uses the DHCP extensions to the Ethernet library
|
||||
to get an IP address via DHCP and print the address obtained.
|
||||
using an Arduino Wiznet Ethernet shield.
|
||||
|
||||
Circuit:
|
||||
* Ethernet shield attached to pins 10, 11, 12, 13
|
||||
|
||||
created 12 April 2011
|
||||
by Tom Igoe
|
||||
|
||||
*/
|
||||
|
||||
#include <SPI.h>
|
||||
#include <Ethernet.h>
|
||||
|
||||
// Enter a MAC address for your controller below.
|
||||
// Newer Ethernet shields have a MAC address printed on a sticker on the shield
|
||||
byte mac[] = {
|
||||
0x00, 0xAA, 0xBB, 0xCC, 0xDE, 0x02 };
|
||||
|
||||
// Initialize the Ethernet client library
|
||||
// with the IP address and port of the server
|
||||
// that you want to connect to (port 80 is default for HTTP):
|
||||
Client client;
|
||||
|
||||
void setup() {
|
||||
// start the serial library:
|
||||
Serial.begin(9600);
|
||||
// start the Ethernet connection:
|
||||
if (Ethernet.begin(mac) == 0) {
|
||||
Serial.println("Failed to configure Ethernet using DHCP");
|
||||
// no point in carrying on, so do nothing forevermore:
|
||||
for(;;)
|
||||
;
|
||||
}
|
||||
// print your local IP address:
|
||||
Serial.print("My IP address: ");
|
||||
for (byte thisByte = 0; thisByte < 4; thisByte++) {
|
||||
// print the value of each byte of the IP address:
|
||||
Serial.print(Ethernet.localIP()[thisByte], DEC);
|
||||
Serial.print(".");
|
||||
}
|
||||
Serial.println();
|
||||
}
|
||||
|
||||
void loop() {
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,80 @@
|
||||
/*
|
||||
DHCP Chat Server
|
||||
|
||||
A simple server that distributes any incoming messages to all
|
||||
connected clients. To use telnet to your device's IP address and type.
|
||||
You can see the client's input in the serial monitor as well.
|
||||
Using an Arduino Wiznet Ethernet shield.
|
||||
|
||||
THis version attempts to get an IP address using DHCP
|
||||
|
||||
Circuit:
|
||||
* Ethernet shield attached to pins 10, 11, 12, 13
|
||||
|
||||
created 21 May 2011
|
||||
by Tom Igoe
|
||||
Based on ChatServer example by David A. Mellis
|
||||
|
||||
*/
|
||||
|
||||
#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.
|
||||
// gateway and subnet are optional:
|
||||
byte mac[] = {
|
||||
0x00, 0xAA, 0xBB, 0xCC, 0xDE, 0x02 };
|
||||
IPAddress ip(192,168,1, 177);
|
||||
IPAddress gateway(192,168,1, 1);
|
||||
IPAddress subnet(255, 255, 0, 0);
|
||||
|
||||
// telnet defaults to port 23
|
||||
Server server(23);
|
||||
boolean gotAMessage = false; // whether or not you got a message from the client yet
|
||||
|
||||
void setup() {
|
||||
// open the serial port
|
||||
Serial.begin(9600);
|
||||
// start the Ethernet connection:
|
||||
Serial.println("Trying to get an IP address using DHCP");
|
||||
if (Ethernet.begin(mac) == 0) {
|
||||
Serial.println("Failed to configure Ethernet using DHCP");
|
||||
// initialize the ethernet device not using DHCP:
|
||||
Ethernet.begin(mac, ip, gateway, subnet);
|
||||
}
|
||||
// print your local IP address:
|
||||
Serial.print("My IP address: ");
|
||||
ip = Ethernet.localIP();
|
||||
for (byte thisByte = 0; thisByte < 4; thisByte++) {
|
||||
// print the value of each byte of the IP address:
|
||||
Serial.print(ip[thisByte], DEC);
|
||||
Serial.print(".");
|
||||
}
|
||||
Serial.println();
|
||||
// start listening for clients
|
||||
server.begin();
|
||||
|
||||
}
|
||||
|
||||
void loop() {
|
||||
// wait for a new client:
|
||||
Client client = server.available();
|
||||
|
||||
// when the client sends the first byte, say hello:
|
||||
if (client) {
|
||||
if (!gotAMessage) {
|
||||
Serial.println("We have a new client");
|
||||
client.println("Hello, client!");
|
||||
gotAMessage = true;
|
||||
}
|
||||
|
||||
// read the bytes incoming from the client:
|
||||
char thisChar = client.read();
|
||||
// echo the bytes back to the client:
|
||||
server.write(thisChar);
|
||||
// echo the bytes to the server as well:
|
||||
Serial.print(thisChar);
|
||||
}
|
||||
}
|
||||
|
||||
76
libraries/Ethernet/examples/DnsWebClient/DnsWebClient.pde
Normal file
76
libraries/Ethernet/examples/DnsWebClient/DnsWebClient.pde
Normal file
@@ -0,0 +1,76 @@
|
||||
/*
|
||||
DNS and DHCP-based Web client
|
||||
|
||||
This sketch connects to a website (http://www.google.com)
|
||||
using an Arduino Wiznet Ethernet shield.
|
||||
|
||||
Circuit:
|
||||
* Ethernet shield attached to pins 10, 11, 12, 13
|
||||
|
||||
created 18 Dec 2009
|
||||
by David A. Mellis
|
||||
modified 12 April 2011
|
||||
by Tom Igoe, based on work by Adrian McEwen
|
||||
|
||||
*/
|
||||
|
||||
#include <SPI.h>
|
||||
#include <Ethernet.h>
|
||||
|
||||
// Enter a MAC address for your controller below.
|
||||
// Newer Ethernet shields have a MAC address printed on a sticker on the shield
|
||||
byte mac[] = { 0x00, 0xAA, 0xBB, 0xCC, 0xDE, 0x02 };
|
||||
char serverName[] = "www.google.com";
|
||||
|
||||
// Initialize the Ethernet client library
|
||||
// with the IP address and port of the server
|
||||
// that you want to connect to (port 80 is default for HTTP):
|
||||
Client client;
|
||||
|
||||
void setup() {
|
||||
// start the serial library:
|
||||
Serial.begin(9600);
|
||||
// start the Ethernet connection:
|
||||
if (Ethernet.begin(mac) == 0) {
|
||||
Serial.println("Failed to configure Ethernet using DHCP");
|
||||
// no point in carrying on, so do nothing forevermore:
|
||||
while(true);
|
||||
}
|
||||
// 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(serverName, 80)) {
|
||||
Serial.println("connected");
|
||||
// Make a HTTP request:
|
||||
client.println("GET /search?q=arduino HTTP/1.0");
|
||||
client.println();
|
||||
}
|
||||
else {
|
||||
// kf 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);
|
||||
}
|
||||
|
||||
// if the server's disconnected, stop the client:
|
||||
if (!client.connected()) {
|
||||
Serial.println();
|
||||
Serial.println("disconnecting.");
|
||||
client.stop();
|
||||
|
||||
// do nothing forevermore:
|
||||
while(true);
|
||||
}
|
||||
}
|
||||
|
||||
124
libraries/Ethernet/examples/TwitterClient/TwitterClient.ino
Normal file
124
libraries/Ethernet/examples/TwitterClient/TwitterClient.ino
Normal file
@@ -0,0 +1,124 @@
|
||||
/*
|
||||
Twitter Client with Strings
|
||||
|
||||
This sketch connects to Twitter using an Ethernet shield. It parses the XML
|
||||
returned, and looks for <text>this is a tweet</text>
|
||||
|
||||
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.
|
||||
|
||||
This example uses the DHCP routines in the Ethernet library which is part of the
|
||||
Arduino core from version 1.0 beta 1
|
||||
|
||||
This example uses the String library, which is part of the Arduino core from
|
||||
version 0019.
|
||||
|
||||
Circuit:
|
||||
* Ethernet shield attached to pins 10, 11, 12, 13
|
||||
|
||||
created 21 May 2011
|
||||
by Tom Igoe
|
||||
|
||||
This code is in the public domain.
|
||||
|
||||
*/
|
||||
#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[] = {
|
||||
0x00, 0xAA, 0xBB, 0xCC, 0xDE, 0x01 };
|
||||
IPAddress ip(192,168,1,20);
|
||||
|
||||
// initialize the library instance:
|
||||
Client client;
|
||||
|
||||
const int requestInterval = 60000; // delay between requests
|
||||
|
||||
char serverName[] = "api.twitter.com"; // twitter URL
|
||||
|
||||
boolean requested; // whether you've made a request since connecting
|
||||
long lastAttemptTime = 0; // last time you connected to the server, in milliseconds
|
||||
|
||||
String currentLine = ""; // string to hold the text from server
|
||||
String tweet = ""; // string to hold the tweet
|
||||
boolean readingTweet = false; // if you're currently reading the tweet
|
||||
|
||||
void setup() {
|
||||
// reserve space for the strings:
|
||||
currentLine.reserve(256);
|
||||
tweet.reserve(150);
|
||||
|
||||
// initialize serial:
|
||||
Serial.begin(9600);
|
||||
// attempt a DHCP connection:
|
||||
if (!Ethernet.begin(mac)) {
|
||||
// if DHCP fails, start with a hard-coded address:
|
||||
Ethernet.begin(mac, ip);
|
||||
}
|
||||
// connect to Twitter:
|
||||
connectToServer();
|
||||
}
|
||||
|
||||
|
||||
|
||||
void loop()
|
||||
{
|
||||
if (client.connected()) {
|
||||
if (client.available()) {
|
||||
// read incoming bytes:
|
||||
char inChar = client.read();
|
||||
|
||||
// add incoming byte to end of line:
|
||||
currentLine += inChar;
|
||||
|
||||
// if you get a newline, clear the line:
|
||||
if (inChar == '\n') {
|
||||
currentLine = "";
|
||||
}
|
||||
// if the current line ends with <text>, it will
|
||||
// be followed by the tweet:
|
||||
if ( currentLine.endsWith("<text>")) {
|
||||
// tweet is beginning. Clear the tweet string:
|
||||
readingTweet = true;
|
||||
tweet = "";
|
||||
}
|
||||
// if you're currently reading the bytes of a tweet,
|
||||
// add them to the tweet String:
|
||||
if (readingTweet) {
|
||||
if (inChar != '<') {
|
||||
tweet += inChar;
|
||||
}
|
||||
else {
|
||||
// if you got a "<" character,
|
||||
// you've reached the end of the tweet:
|
||||
readingTweet = false;
|
||||
Serial.println(tweet);
|
||||
// close the connection to the server:
|
||||
client.stop();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
else if (millis() - lastAttemptTime > requestInterval) {
|
||||
// if you're not connected, and two minutes have passed since
|
||||
// your last connection, then attempt to connect again:
|
||||
connectToServer();
|
||||
}
|
||||
}
|
||||
|
||||
void connectToServer() {
|
||||
// attempt to connect, and wait a millisecond:
|
||||
Serial.println("connecting to server...");
|
||||
if (client.connect(serverName, 80)) {
|
||||
Serial.println("making HTTP request...");
|
||||
// make HTTP GET request to twitter:
|
||||
client.println("GET /1/statuses/user_timeline.xml?screen_name=arduino&count=1 HTTP/1.1");
|
||||
client.println("HOST: api.twitter.com");
|
||||
client.println();
|
||||
}
|
||||
// note the time of this connect attempt:
|
||||
lastAttemptTime = millis();
|
||||
}
|
||||
@@ -324,6 +324,10 @@ private:
|
||||
inline static void initSS() { DDRB |= _BV(4); };
|
||||
inline static void setSS() { PORTB &= ~_BV(4); };
|
||||
inline static void resetSS() { PORTB |= _BV(4); };
|
||||
#elif defined(__AVR_ATmega32U4__) || defined(__AVR_AT90USB1286__) || defined(__AVR_AT90USB646__) || defined(__AVR_AT90USB162__)
|
||||
inline static void initSS() { DDRB |= _BV(0); };
|
||||
inline static void setSS() { PORTB &= ~_BV(0); };
|
||||
inline static void resetSS() { PORTB |= _BV(0); };
|
||||
#else
|
||||
inline static void initSS() { DDRB |= _BV(2); };
|
||||
inline static void setSS() { PORTB &= ~_BV(2); };
|
||||
|
||||
Reference in New Issue
Block a user