harlequin-tech / WiFlyHQ

WiFly RN-XV Arduino Library
Other
110 stars 68 forks source link

Problem posting to Ubidots #35

Closed rodri16 closed 10 years ago

rodri16 commented 10 years ago

Hi Harlequin, I am trying to post some data to Ubidots (ubidots.com) and to another sites like Thingspeak (thingspeak.com) and Plotly (plot.ly). I am using both Arduino wifi shield and RN-XV shield. I made it work with wifi shield but I am having problems with RN-XV. I get this message: close: failed, no CLOS Could you help me? Here is the code... Thank you. Rodrigo

undef PROGMEM

define PROGMEM attribute(( section(".progmem.data") ))

undef PSTR

define PSTR(s) (extension({static prog_char c[] PROGMEM = (s); &c[0];}))

include

include

SoftwareSerial wifiSerial(8,9);

//#include //AltSoftSerial wifiSerial(8,9);

WiFly wifly;

/* Change these to match your WiFi network */ const char mySSID[] = "MarconiLab"; const char myPassword[] = "marconi-lab";

//Dots configuration

define TOKEN "Df2PxNVLpOdn2phb7BriUTs0JwkVOvv6su2zBPh2QY1FbPYok5OMHQelKFNY" //Replace with your TOKEN

define VARIABLEID "538f3bc676254249aec757d9" //Variable ID

define VARIABLEID2 "5386d9c77625426be003b163" //second variable

//WiFiClient client; char server[] = "things.ubidots.com";

void sendIndex(); void sendGreeting(char *name); void send404();

int resetTimer = 0; char buf[80];

const char site[] = "www.google.it";

void setup() { Serial.begin(115200); Serial.println(F("Starting")); Serial.print(F("Free memory: ")); Serial.println(wifly.getFreeMemory(),DEC);

wifiSerial.begin(9600);
if (!wifly.begin(&wifiSerial, &Serial)) {
    Serial.println(F("Failed to start wifly"));
wifly.terminal();
}

/* Join wifi network if not already associated */
if (!wifly.isAssociated()) {
/* Setup the WiFly to connect to a wifi network */
Serial.println(F("Joining network"));
wifly.setSSID(mySSID);
wifly.setPassphrase(myPassword);
wifly.enableDHCP();
wifly.save();

if (wifly.join()) {
    Serial.println(F("Joined wifi network"));
} else {
    Serial.println(F("Failed to join wifi network"));
    wifly.terminal();
}
} else {
    Serial.println(F("Already joined network"));
}

wifly.setBroadcastInterval(0);  // Turn off UPD broadcast

//wifly.terminal();

Serial.print(F("MAC: "));
Serial.println(wifly.getMAC(buf, sizeof(buf)));
Serial.print(F("IP: "));
Serial.println(wifly.getIP(buf, sizeof(buf)));
Serial.print("Netmask: ");
Serial.println(wifly.getNetmask(buf, sizeof(buf)));
Serial.print("Gateway: ");
Serial.println(wifly.getGateway(buf, sizeof(buf)));

wifly.setDeviceID("Wifly-WebServer");
Serial.print("DeviceID: ");
Serial.println(wifly.getDeviceID(buf, sizeof(buf)));

if (wifly.isConnected()) {
    Serial.println(F("Old connection active. Closing"));
wifly.close();
}

// wifly.setProtocol(WIFLY_PROTOCOLTCP); // if (wifly.getPort() != 80) { // wifly.setPort(80); // /* local port does not take effect until the WiFly has rebooted (2.32) / // wifly.save(); // Serial.println(F("Set port to 80, rebooting to make it work")); // wifly.reboot(); // delay(3000); // } //
if (wifly.open(server, 80)) { Serial.print("Connected to "); Serial.println(server); // /_ Send the request */ wifly.println("GET / HTTP/1.0"); wifly.println(); } else { Serial.println("Failed to connect"); } //
Serial.println(F("Ready")); }

void loop() { //Serial.println("Loop" ); if (wifly.available() > 0) { Serial.println("Trying to send data:" ); writeUbidots(String (analogRead(0)), VARIABLEID ); //Send data in String format to the Ubidots function } }

void writeUbidots(String data, String VARID) { String dataString = "{\"value\":"+ data + "}"; //Prepares the data in JSON format Serial.println("ubi");

if (wifly.open(server, 80)) { //If connection is successful, then send this HTTP Request: Serial.println("Connecting..."); wifly.println("POST /api/v1.6/variables/"); //Specify URL, including the VARIABLE ID wifly.print(VARID); wifly.println("/values HTTP/1.1"); wifly.println("Host: things.ubidots.com"); wifly.print("X-Auth-Token: "); //Specify Authentication Token in headers wifly.println(TOKEN); wifly.print("Content-Length: "); wifly.println(dataString.length()); wifly.println("Content-Type: application/json"); wifly.println("Connection: close"); wifly.println(); //End of HTTP headers

wifly.println(dataString);                                   //This is the actual value to send

wifly.flushRx();
wifly.close();

// return 1; } else { // If the connection wasn't possible, then: resetTimer += 1; Serial.println("Connection failed"); Serial.println("Device will restart after 10 failed attempts, so far:"+String(resetTimer)+" attempts."); Serial.println("Killing sockets and disconnecting..."); wifly.flushRx(); wifly.close(); // return 0; } }