mirror of
https://github.com/arduino/Arduino.git
synced 2025-02-20 14:54:31 +01:00
Fix compilation errors on examples and adapt API to new Ethernet functions
This commit is contained in:
parent
4f7e2f1201
commit
0b8d3f6421
@ -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();
|
||||
|
@ -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();
|
||||
|
@ -1,9 +1,11 @@
|
||||
#include <string.h>
|
||||
#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)
|
||||
{
|
||||
|
@ -1,5 +1,5 @@
|
||||
#ifndef Server_h
|
||||
#define Server_h
|
||||
#ifndef server_h
|
||||
#define server_h
|
||||
|
||||
extern "C" {
|
||||
#include "utility/wl_definitions.h"
|
||||
|
@ -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;
|
||||
}
|
||||
|
||||
|
138
WiFi/examples/PachubeClientString/PachubeClientString.ino
Normal file
138
WiFi/examples/PachubeClientString/PachubeClientString.ino
Normal file
@ -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 <WiFi.h>
|
||||
#include <IPAddress.h>
|
||||
|
||||
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");
|
||||
}
|
||||
}
|
@ -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");
|
@ -1,6 +1,6 @@
|
||||
#include "server_drv.h"
|
||||
|
||||
#include "WProgram.h"
|
||||
#include "Arduino.h"
|
||||
#include "spi_drv.h"
|
||||
|
||||
#define _DEBUG_
|
||||
|
@ -1,5 +1,5 @@
|
||||
|
||||
#include "WProgram.h"
|
||||
#include "Arduino.h"
|
||||
#include "spi_drv.h"
|
||||
#include "pins_arduino.h"
|
||||
#define _DEBUG_
|
||||
|
@ -2,7 +2,7 @@
|
||||
#include <string.h>
|
||||
#include <stdint.h>
|
||||
|
||||
#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()
|
||||
|
@ -3,6 +3,7 @@
|
||||
|
||||
#include <inttypes.h>
|
||||
#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();
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user