diff --git a/WiFi/Client.cpp b/WiFi/Client.cpp index 82c2b3e7a..afdb31d7c 100755 --- a/WiFi/Client.cpp +++ b/WiFi/Client.cpp @@ -6,8 +6,6 @@ extern "C" { #include "utility/debug.h" } -#include "WProgram.h" - #include "WiFi.h" #include "Client.h" #include "Server.h" @@ -15,18 +13,36 @@ extern "C" { uint16_t Client::_srcport = 1024; +Client::Client() : _sock(MAX_SOCK_NUM) { +} + Client::Client(uint8_t sock) : _sock(sock) { } -Client::Client(IPAddress& ip, uint16_t port) : _ip(ip), _port(port), _sock(MAX_SOCK_NUM) { +int Client::connect(const char* host, uint16_t port) { + /* TODO Add DNS wifi spi function to resolve DNS */ +#if 0 + // Look up the host first + int ret = 0; + DNSClient dns; + IPAddress remote_addr; + + dns.begin(Ethernet.dnsServerIP()); + ret = dns.getHostByName(host, remote_addr); + if (ret == 1) { + return connect(remote_addr, port); + } else { + return ret; + } +#endif } -uint8_t Client::connect() { +int Client::connect(IPAddress ip, uint16_t port) { _sock = getFirstSocket(); if (_sock != NO_SOCKET_AVAIL) { - ServerDrv::StartClient(uint32_t(_ip), _port, _sock); - WiFiClass::_state[_sock] = _socket; + ServerDrv::StartClient(uint32_t(ip), port, _sock); + WiFiClass::_state[_sock] = _sock; }else{ return 0; } @@ -86,6 +102,11 @@ int Client::read(uint8_t* buf, size_t size) { return 0; } +int Client::peek() { + //TODO to be implemented + return 0; +} + void Client::flush() { while (available()) read(); diff --git a/WiFi/Client.h b/WiFi/Client.h index 277476230..c812089c1 100755 --- a/WiFi/Client.h +++ b/WiFi/Client.h @@ -1,23 +1,25 @@ -#ifndef Client_h -#define Client_h - -#include "IPAddress.h" +#ifndef client_h +#define client_h +#include "Arduino.h" #include "Print.h" -class Client : public Print { +class Client : public Stream { + public: + Client(); Client(uint8_t sock); - Client(IPAddress& ip, uint16_t port); uint8_t status(); - uint8_t connect(); + int connect(IPAddress ip, uint16_t port); + 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 int available(); virtual int read(); - virtual int read(uint8_t* buf, size_t size); - void flush(); + virtual int read(uint8_t *buf, size_t size); + virtual int peek(); + virtual void flush(); void stop(); uint8_t connected(); operator bool(); @@ -27,8 +29,6 @@ public: private: static uint16_t _srcport; uint8_t _sock; //not used - IPAddress _ip; - uint16_t _port; uint16_t _socket; uint8_t getFirstSocket(); diff --git a/WiFi/Server.cpp b/WiFi/Server.cpp index 695c55d25..a8e236029 100755 --- a/WiFi/Server.cpp +++ b/WiFi/Server.cpp @@ -1,9 +1,11 @@ #include -#include "Server.h" -#include "Client.h" -#include "WiFi.h" #include "server_drv.h" -#include "wiring.h" + +#include "WiFi.h" +#include "Client.h" +#include "Server.h" + + Server::Server(uint16_t port) { diff --git a/WiFi/Server.h b/WiFi/Server.h index cf586c6ad..34124af09 100755 --- a/WiFi/Server.h +++ b/WiFi/Server.h @@ -1,5 +1,5 @@ -#ifndef Server_h -#define Server_h +#ifndef server_h +#define server_h extern "C" { #include "utility/wl_definitions.h" diff --git a/WiFi/WiFi.cpp b/WiFi/WiFi.cpp index 337c0bb93..5e766e0f5 100755 --- a/WiFi/WiFi.cpp +++ b/WiFi/WiFi.cpp @@ -1,6 +1,5 @@ #include "wifi_drv.h" #include "WiFi.h" -#include "wiring.h" #define _DEBUG_ @@ -125,21 +124,21 @@ uint8_t* WiFiClass::macAddress(uint8_t* mac) IPAddress WiFiClass::localIp() { IPAddress ret; - WiFiDrv::getIpAddress(ret.raw_address()); + WiFiDrv::getIpAddress(ret); return ret; } IPAddress WiFiClass::subnetMask() { IPAddress ret; - WiFiDrv::getSubnetMask(ret.raw_address()); + WiFiDrv::getSubnetMask(ret); return ret; } IPAddress WiFiClass::gatewayIP() { IPAddress ret; - WiFiDrv::getGatewayIP(ret.raw_address()); + WiFiDrv::getGatewayIP(ret); return ret; } diff --git a/WiFi/examples/PachubeClientString/PachubeClientString.ino b/WiFi/examples/PachubeClientString/PachubeClientString.ino new file mode 100644 index 000000000..9a38ec15a --- /dev/null +++ b/WiFi/examples/PachubeClientString/PachubeClientString.ino @@ -0,0 +1,138 @@ +/* + Pachube sensor client with Strings + + This sketch connects an analog sensor to Pachube (http://www.pachube.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. + + This example uses the String library, which is part of the Arduino core from + version 0019. + + Circuit: + * Analog sensor attached to analog in 0 + * Ethernet shield attached to pins 10, 11, 12, 13 + + created 15 March 2010 + updated 4 Sep 2010 + by Tom Igoe + + This code is in the public domain. + + */ + +#include +#include + +char ssid[32] = { 0 }; +int status = WL_IDLE_STATUS; + +// The address of the server you want to connect to (pachube.com): +IPAddress server(173,203,98,29); + +// initialize the library instance: +Client client(server, 80); + +long lastConnectionTime = 0; // last time you connected to the server, in milliseconds +boolean lastConnected = false; // state of the connection last time through the main loop +const int postingInterval = 10000; //delay between updates to Pachube.com + +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("\nWiFi Connected."); + } + + delay(1000); +} + +void loop() { + // read the analog sensor: + 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(A1); + dataString += ","; + dataString += String(otherSensorReading); + + // 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() && lastConnected) { + Serial.println(); + Serial.println("disconnecting."); + client.stop(); + } + + // if you're not connected, and ten seconds have passed since + // your last connection, then connect again and send data: + if(!client.connected() && (millis() - lastConnectionTime > postingInterval)) { + sendData(dataString); + } + // store the state of the connection for next time through + // the loop: + lastConnected = client.connected(); +} + +// this method makes a HTTP connection to the server: +void sendData(String thisData) { + // if there's a successful connection: + if (client.connect()) { + Serial.println("connecting..."); + // send the HTTP PUT request. + // fill in your feed address here: + client.print("PUT /api/http://api.pachube.com/v2/feeds/24196.csv HTTP/1.1\n"); + client.print("Host: www.pachube.com\n"); + // fill in your Pachube API key here: + client.print("X-PachubeApiKey: gw0L2A-J5ACRGQccX59tCYt0IEzyecr-SoiuC47U1-8\n"); + client.print("Content-Length: "); + client.println(thisData.length(), DEC); + + // last pieces of the HTTP PUT request: + client.print("Content-Type: text/csv\n"); + client.println("Connection: close\n"); + + // here's the actual content of the PUT request: + client.println(thisData); + + // note the time that the connection was made: + lastConnectionTime = millis(); + } + else { + // if you couldn't make a connection: + Serial.println("connection failed"); + } +} diff --git a/WiFi/examples/WebClient/WebClient.pde b/WiFi/examples/WebClient/WebClient.ino similarity index 94% rename from WiFi/examples/WebClient/WebClient.pde rename to WiFi/examples/WebClient/WebClient.ino index feabd0b38..8959d7e0b 100644 --- a/WiFi/examples/WebClient/WebClient.pde +++ b/WiFi/examples/WebClient/WebClient.ino @@ -19,12 +19,11 @@ 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); +Client client; int startWiFiWpa() { @@ -53,7 +52,7 @@ void setup() { { Serial.println("\nStarting connection..."); // if you get a connection, report back via serial: - if (client.connect()) { + if (client.connect(server, 80)) { Serial.println("connected"); // Make a HTTP request: client.println("GET /search?q=arduino HTTP/1.0"); diff --git a/WiFi/examples/WebServer/WebServer.pde b/WiFi/examples/WebServer/WebServer.ino similarity index 100% rename from WiFi/examples/WebServer/WebServer.pde rename to WiFi/examples/WebServer/WebServer.ino diff --git a/WiFi/examples/wifi_Server_example/wifi_Server_example.pde b/WiFi/examples/wifi_Server_example/wifi_Server_example.ino similarity index 100% rename from WiFi/examples/wifi_Server_example/wifi_Server_example.pde rename to WiFi/examples/wifi_Server_example/wifi_Server_example.ino diff --git a/WiFi/examples/wifi_Server_example_WEP/wifi_Server_example_WEP.pde b/WiFi/examples/wifi_Server_example_WEP/wifi_Server_example_WEP.ino similarity index 100% rename from WiFi/examples/wifi_Server_example_WEP/wifi_Server_example_WEP.pde rename to WiFi/examples/wifi_Server_example_WEP/wifi_Server_example_WEP.ino diff --git a/WiFi/examples/wifi_Server_example_WPA/wifi_Server_example_WPA.pde b/WiFi/examples/wifi_Server_example_WPA/wifi_Server_example_WPA.ino similarity index 100% rename from WiFi/examples/wifi_Server_example_WPA/wifi_Server_example_WPA.pde rename to WiFi/examples/wifi_Server_example_WPA/wifi_Server_example_WPA.ino diff --git a/WiFi/examples/wifi_WPA_example/wifi_WPA_example.pde b/WiFi/examples/wifi_WPA_example/wifi_WPA_example.ino similarity index 100% rename from WiFi/examples/wifi_WPA_example/wifi_WPA_example.pde rename to WiFi/examples/wifi_WPA_example/wifi_WPA_example.ino diff --git a/WiFi/utility/server_drv.cpp b/WiFi/utility/server_drv.cpp index eee1be243..005ba4c76 100644 --- a/WiFi/utility/server_drv.cpp +++ b/WiFi/utility/server_drv.cpp @@ -1,6 +1,6 @@ #include "server_drv.h" -#include "WProgram.h" +#include "Arduino.h" #include "spi_drv.h" #define _DEBUG_ diff --git a/WiFi/utility/spi_drv.cpp b/WiFi/utility/spi_drv.cpp index 8688b9b56..43c5dd251 100644 --- a/WiFi/utility/spi_drv.cpp +++ b/WiFi/utility/spi_drv.cpp @@ -1,5 +1,5 @@ -#include "WProgram.h" +#include "Arduino.h" #include "spi_drv.h" #include "pins_arduino.h" #define _DEBUG_ diff --git a/WiFi/utility/wifi_drv.cpp b/WiFi/utility/wifi_drv.cpp index a8dffe351..04c44c257 100644 --- a/WiFi/utility/wifi_drv.cpp +++ b/WiFi/utility/wifi_drv.cpp @@ -2,7 +2,7 @@ #include #include -#include "WProgram.h" +#include "Arduino.h" #include "spi_drv.h" #include "wifi_drv.h" @@ -189,22 +189,25 @@ uint8_t* WiFiDrv::getMacAddress() return _mac; } -void WiFiDrv::getIpAddress(uint8_t *ip) +void WiFiDrv::getIpAddress(IPAddress& ip) { getNetworkData(_localIp, _subnetMask, _gatewayIp); - memcpy(ip, _localIp, WL_IPV4_LENGTH); + ip = _localIp; + //memcpy(ip, _localIp, WL_IPV4_LENGTH); } - void WiFiDrv::getSubnetMask(uint8_t *ip) + void WiFiDrv::getSubnetMask(IPAddress& ip) { getNetworkData(_localIp, _subnetMask, _gatewayIp); - memcpy(ip, _subnetMask, WL_IPV4_LENGTH); + ip = _subnetMask; + //memcpy(ip, _subnetMask, WL_IPV4_LENGTH); } - void WiFiDrv::getGatewayIP(uint8_t *ip) + void WiFiDrv::getGatewayIP(IPAddress& ip) { getNetworkData(_localIp, _subnetMask, _gatewayIp); - memcpy(ip, _gatewayIp, WL_IPV4_LENGTH); + ip = _gatewayIp; + //memcpy(ip, _gatewayIp, WL_IPV4_LENGTH); } char* WiFiDrv::getCurrentSSID() diff --git a/WiFi/utility/wifi_drv.h b/WiFi/utility/wifi_drv.h index 661e0e45f..80cc2d8a0 100644 --- a/WiFi/utility/wifi_drv.h +++ b/WiFi/utility/wifi_drv.h @@ -3,6 +3,7 @@ #include #include "wifi_spi.h" +#include "IPAddress.h" #define KEY_IDX_LEN 1 #define WL_DELAY_START_CONNECTION 5000 @@ -40,11 +41,11 @@ public: static uint8_t* getMacAddress(); - static void getIpAddress(uint8_t *ip); + static void getIpAddress(IPAddress& ip); - static void getSubnetMask(uint8_t *ip); + static void getSubnetMask(IPAddress& ip); - static void getGatewayIP(uint8_t *ip); + static void getGatewayIP(IPAddress& ip); static char* getCurrentSSID();