mirror of
https://github.com/arduino/Arduino.git
synced 2024-12-02 13:24:12 +01:00
Added Client connection and WebClient example
This commit is contained in:
parent
ca1a1d8a9d
commit
c10210a881
@ -22,19 +22,14 @@ Client::Client(IPAddress& ip, uint16_t port) : _ip(ip), _port(port), _sock(MAX_S
|
||||
|
||||
uint8_t Client::connect() {
|
||||
_sock = getFirstSocket();
|
||||
//TODO implementation
|
||||
_socket = socket(TCP_SOCKET);
|
||||
if (_socket<0)
|
||||
if (_sock != NO_SOCKET_AVAIL)
|
||||
{
|
||||
return 0;
|
||||
ServerDrv::StartClient(uint32_t(_ip), _port, _sock);
|
||||
WiFiClass::_state[_sock] = _socket;
|
||||
}else{
|
||||
WiFiClass::_state[_sock] = _socket;
|
||||
return 0;
|
||||
}
|
||||
//
|
||||
// if (!::connect(_socket, _ip, _port)) {
|
||||
// return 0;
|
||||
// }
|
||||
return 1;
|
||||
return 1;
|
||||
}
|
||||
|
||||
void Client::write(uint8_t b) {
|
||||
@ -138,7 +133,7 @@ Client::operator bool() {
|
||||
uint8_t Client::getFirstSocket()
|
||||
{
|
||||
for (int i = 0; i < MAX_SOCK_NUM; i++) {
|
||||
if (WiFiClass::_state[i] < 0)
|
||||
if (WiFiClass::_state[i] == 0)
|
||||
{
|
||||
return i;
|
||||
}
|
||||
|
89
WiFi/examples/WebClient/WebClient.pde
Normal file
89
WiFi/examples/WebClient/WebClient.pde
Normal file
@ -0,0 +1,89 @@
|
||||
/*
|
||||
Web client
|
||||
|
||||
This sketch connects to a website (http://www.google.com)
|
||||
using a WiFi shield.
|
||||
|
||||
Circuit:
|
||||
* WiFi shield attached
|
||||
* Analog inputs attached to pins A0 through A5 (optional)
|
||||
|
||||
created 13 July 2010
|
||||
by Domenico La Fauci
|
||||
*/
|
||||
|
||||
|
||||
#include <WiFi.h>
|
||||
#include <IPAddress.h>
|
||||
|
||||
char ssid[32] = { 0 };
|
||||
int status = WL_IDLE_STATUS;
|
||||
IPAddress server(74,125,232,115); // Google
|
||||
//byte server[] = { 173,194,33,104 }; // Google
|
||||
|
||||
// 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(server, 80);
|
||||
|
||||
int startWiFiWpa()
|
||||
{
|
||||
Serial.println("\nSetup WiFi Wpa...");
|
||||
//strcpy(ssid, "AndroidAP9647");
|
||||
strcpy(ssid, "Cariddi");
|
||||
Serial.print("SSID: ");
|
||||
Serial.println(ssid);
|
||||
const char *pass = "1234567890";
|
||||
status = WiFi.begin(ssid, pass);
|
||||
if ( status != WL_CONNECTED)
|
||||
{
|
||||
Serial.println("Connection Failed");
|
||||
}
|
||||
return status;
|
||||
}
|
||||
|
||||
|
||||
void setup() {
|
||||
// start the WiFi connection and the server:
|
||||
Serial.begin(9600);
|
||||
Serial.println("*** Start WebClient WiFi example ***");
|
||||
|
||||
int _status = startWiFiWpa();
|
||||
if ( _status == WL_CONNECTED)
|
||||
{
|
||||
Serial.println("\nStarting connection...");
|
||||
// if you get a connection, report back via serial:
|
||||
if (client.connect()) {
|
||||
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:
|
||||
for(;;)
|
||||
;
|
||||
}
|
||||
}
|
||||
|
@ -33,6 +33,29 @@ void ServerDrv::StartServer(uint16_t port, uint8_t sock)
|
||||
SpiDrv::spiSlaveDeselect();
|
||||
}
|
||||
|
||||
// Start server TCP on port specified
|
||||
void ServerDrv::StartClient(uint32_t ipAddress, uint16_t port, uint8_t sock)
|
||||
{
|
||||
WAIT_FOR_SLAVE_SELECT();
|
||||
INFO2(ipAddress,port);
|
||||
// Send Command
|
||||
SpiDrv::sendCmd(START_CLIENT_TCP_CMD, PARAM_NUMS_3);
|
||||
SpiDrv::sendParam((uint8_t*)&ipAddress, sizeof(ipAddress));
|
||||
SpiDrv::sendParam(port);
|
||||
SpiDrv::sendParam(&sock, 1, LAST_PARAM);
|
||||
|
||||
//Wait the reply elaboration
|
||||
SpiDrv::waitForSlaveReady();
|
||||
|
||||
// Wait for reply
|
||||
uint8_t _data = 0;
|
||||
uint8_t _dataLen = 0;
|
||||
if (!SpiDrv::waitResponseCmd(START_CLIENT_TCP_CMD, PARAM_NUMS_1, &_data, &_dataLen))
|
||||
{
|
||||
WARN("error waitResponse");
|
||||
}
|
||||
SpiDrv::spiSlaveDeselect();
|
||||
}
|
||||
|
||||
uint8_t ServerDrv::getState(uint8_t sock)
|
||||
{
|
||||
|
@ -9,6 +9,8 @@ class ServerDrv
|
||||
public:
|
||||
// Start server TCP on port specified
|
||||
static void StartServer(uint16_t port, uint8_t sock);
|
||||
|
||||
static void StartClient(uint32_t ipAddress, uint16_t port, uint8_t sock);
|
||||
|
||||
static uint8_t getState(uint8_t sock);
|
||||
|
||||
|
@ -426,7 +426,6 @@ void SpiDrv::sendParamLen8(uint8_t param_len)
|
||||
spiTransfer(param_len);
|
||||
}
|
||||
|
||||
|
||||
void SpiDrv::sendParamLen16(uint16_t param_len)
|
||||
{
|
||||
// Send Spi paramLen
|
||||
@ -434,7 +433,6 @@ void SpiDrv::sendParamLen16(uint16_t param_len)
|
||||
spiTransfer((uint8_t)(param_len & 0xff));
|
||||
}
|
||||
|
||||
|
||||
uint8_t SpiDrv::readParamLen8(uint8_t* param_len)
|
||||
{
|
||||
uint8_t _param_len = spiTransfer(DUMMY_DATA);
|
||||
|
@ -38,6 +38,7 @@ enum {
|
||||
DATA_SENT_TCP_CMD = 0x2A,
|
||||
AVAIL_DATA_TCP_CMD = 0x2B,
|
||||
GET_DATA_TCP_CMD = 0x2C,
|
||||
START_CLIENT_TCP_CMD= 0x2D,
|
||||
|
||||
DISCONNECT_CMD = 0x30,
|
||||
// All command with DATA_FLAG 0x40 send a 16bit Len
|
||||
@ -113,4 +114,22 @@ typedef struct __attribute__((__packed__))
|
||||
unsigned char nParam;
|
||||
}tSpiHdr;
|
||||
|
||||
typedef struct __attribute__((__packed__))
|
||||
{
|
||||
uint8_t paramLen;
|
||||
uint32_t param;
|
||||
}tLongParam;
|
||||
|
||||
typedef struct __attribute__((__packed__))
|
||||
{
|
||||
uint8_t paramLen;
|
||||
uint16_t param;
|
||||
}tIntParam;
|
||||
|
||||
typedef struct __attribute__((__packed__))
|
||||
{
|
||||
uint8_t paramLen;
|
||||
uint8_t param;
|
||||
}tByteParam;
|
||||
|
||||
#endif
|
||||
|
Loading…
Reference in New Issue
Block a user