Salve a tutti. Ho acquistato una shield wifi con WiFly RN 171. Ho scaricato le librerie ed effettuato le prove di trasmissione. Funziona tutto perfettamente. Il progetto che avevo in mente è quello di installare una sonda amperometrica (sct013) ed inviare la letura alla piattaforma emon (emon.org).
Non riesco a scrivere il codice per effettuare questa semplice operazione di get request attraverso la suddetta shield.
Dovrei creare qualcosa del genere:
// this method makes a HTTP connection to the server:
void sendData() {
// if there's a successful connection:
if (client.connect(server, 80)) {
Serial.println("Connecting...");
// send the HTTP GET request:
client.print("GET /api/post?apikey=");
client.print(apikey);
if (node > 0) {
client.print("&node=");
client.print(node);
}
client.print("&json={power:");
client.print(Irms*volt);
client.println("} HTTP/1.1");
client.println("Host:emoncms.org");
client.println("User-Agent: Arduino-ethernet");
client.println("Connection: close");
client.println();
Ma non riesco anche avendo sotto mano il codice di test.
Questo il codice di test che funge ![]()
/*
#include <Arduino.h>
#include <Time.h>
#include <SoftwareSerial.h>
#include <Streaming.h>
#include <PString.h>
#include <WiFlySerial.h>
#include "Credentials.h"
#include "MemoryFree.h"
#define ARDUINO_RX_PIN 2
#define ARDUINO_TX_PIN 3
// Set NTP server, update frequency,
wifi.setNTP(ntp_server);
wifi.setNTP_Update_Frequency(" 15");
// don't send *HELLO* on http traffic
// close idle connections after n seconds
// give enough time for packet data to arrive
// make data packet size sufficiently large
// send data packet when a \t appears in stream
// force time resync.
// Configure application-specific settings
Serial << GetBuffer_P(IDX_WT_MSG_APP_SETTINGS, bufTemp, TEMP_BUFFER_SIZE) << endl;
for (int i = 0; i< 7 ; i++) {
wifi.SendCommand(GetBuffer_P(IDX_WT_SETUP_01 + i,bufTemp,TEMP_BUFFER_SIZE),">",bufRequest, REQUEST_BUFFER_SIZE);
}
wifi.getDeviceStatus();
setTime( wifi.getTime() );
delay(1000);
setSyncProvider( GetSyncTime );
// reboot if not working right yet.
iTrack++;
if ( iTrack > 5 ) {
wifi.reboot();
iTrack = 0;
}
}
// Arduino Setup routine.
void setup() {
Serial.begin(9600);
Serial << GetBuffer_P(IDX_WT_MSG_START_WEBCLIENT,bufTemp,TEMP_BUFFER_SIZE) << endl << GetBuffer_P(IDX_WT_MSG_RAM,bufTemp,TEMP_BUFFER_SIZE) << freeMemory() << endl
<< GetBuffer_P(IDX_WT_MSG_WIRE_RX,bufTemp,TEMP_BUFFER_SIZE) << ARDUINO_RX_PIN << endl << GetBuffer_P(IDX_WT_MSG_WIRE_TX,bufTemp,TEMP_BUFFER_SIZE) << ARDUINO_TX_PIN << endl;
wifi.begin();
Serial << F("Starting WebClientGetPost...") << wifi.getLibraryVersion(bufRequest, REQUEST_BUFFER_SIZE) << " RAM:" << freeMemory() << endl;
// get MAC
Serial << F("MAC: ") << wifi.getMAC(bufRequest, REQUEST_BUFFER_SIZE) << endl;
Reconnect();
// Set timezone adjustment: PST is -8h. Adjust to your local timezone.
adjustTime( (long) (-8 * 60 * 60) );
Serial << F("DateTime:") << year() << "-" << month() << "-" << day() << " " << hour() << ":" << minute() << ":" << second() << endl;
Serial << GetBuffer_P(IDX_WT_MSG_WIFI,bufTemp,TEMP_BUFFER_SIZE) << endl
<< F("IP: ") << wifi.getIP(bufRequest, REQUEST_BUFFER_SIZE) << endl
<< F("Netmask: ") << wifi.getNetMask(bufRequest, REQUEST_BUFFER_SIZE) << endl
<< F("Gateway: ") << wifi.getGateway(bufRequest, REQUEST_BUFFER_SIZE) << endl
<< F("DNS: ") << wifi.getDNS(bufRequest, REQUEST_BUFFER_SIZE) << endl
<< F("RSSI: ") << wifi.getRSSI(bufRequest, REQUEST_BUFFER_SIZE) << endl
<< F("battery: ") << wifi.getBattery(bufRequest, REQUEST_BUFFER_SIZE) << endl;
memset (bufRequest,'\0',REQUEST_BUFFER_SIZE);
// close any open connections
wifi.closeConnection();
Serial << F("After Setup RAM:") << freeMemory() << endl ;
}
void loop() {
Serial << F("Beginning of Loop RAM:") << freeMemory() << endl ;
// Perform GET example
float fSampleValue = iLoopCounter + (iLoopCounter / 10 );
Do_GET_Example( iLoopCounter, fSampleValue );
// Perform POST example
Do_POST_Example ( iLoopCounter, fSampleValue );
// Serial << "Clear leftovers" << endl;
// flush the wifi buffer by reading and showing anything left around.
while ((chMisc = wifi.read()) > -1) {
Serial << chMisc;
}
iLoopCounter++;
// wait a bit
Serial << F("Waiting a moment..") << endl;
delay(2000);
}
int Do_GET_Example( int iLoopCounter, float fValue ) {
char bufRequest[REQUEST_BUFFER_SIZE];
char bufTemp[TEMP_BUFFER_SIZE];
PString strRequest(bufRequest, REQUEST_BUFFER_SIZE);
// Build GET expression
strRequest << F("GET ") << MY_SERVER_GET_URL << F("?counter=") << iLoopCounter << F("&value=") << fValue
<< F(" HTTP/1.1") << "\n"
<< F("Host: ") << MY_SERVER_GET << "\n"
<< F("Connection: close") << "\n"
<< "\n\n";
// send data via request
// close connection
Serial << F("GET request:") << strRequest << endl << F("RAM: ") << freeMemory() << endl;
// Open connection, then sent GET Request, and display response.
if (wifi.openConnection( MY_SERVER_GET ) ) {
wifi << (const char*) strRequest << endl;
// Show server response
unsigned long TimeOut = millis() + 4000;
while ( TimeOut > millis() && wifi.isConnectionOpen() ) {
if ( wifi.available() > 0 ) {
Serial << (char) wifi.read();
}
}
// Force-close connection
wifi.closeConnection();
} else {
// Failed to open connection
Serial << GetBuffer_P(IDX_WT_MSG_FAIL_OPEN,bufTemp,TEMP_BUFFER_SIZE) << MY_SERVER_GET << endl;
}
wifi.setDebugChannel( NULL );
return 0;
}
int Do_POST_Example( int iLoopCounter, float fValue ) {
Serial << F("POST Example RAM: ") << freeMemory() << endl;
char bufRequest[REQUEST_BUFFER_SIZE];
char bufTemp[TEMP_BUFFER_SIZE];
char bufPayLoad[TEMP_BUFFER_SIZE];
memset (bufPayLoad,'\0', TEMP_BUFFER_SIZE);
PString strRequest(bufRequest, REQUEST_BUFFER_SIZE);
PString strPayLoad(bufPayLoad, TEMP_BUFFER_SIZE);
// Build POST expression
strPayLoad << F("counter=") << iLoopCounter << F("&value=") << fValue;
<< strPayLoad << "\n\n";
strRequest << F("POST ") << MY_SERVER_POST_URL << " " << GetBuffer_P(IDX_WT_POST_HEAD_01,bufTemp,TEMP_BUFFER_SIZE)
<< F("Host: ") << MY_SERVER_POST << "\n"
<< GetBuffer_P(IDX_WT_POST_HEAD_02,bufTemp,TEMP_BUFFER_SIZE)
<< GetBuffer_P(IDX_WT_POST_HEAD_03,bufTemp,TEMP_BUFFER_SIZE) << strPayLoad.length() << "\n"
<< GetBuffer_P(IDX_WT_POST_HEAD_04,bufTemp,TEMP_BUFFER_SIZE)
<< strPayLoad << "\n\n" ;