Jump to content









Photo

Wireless Gardening with Arduino + CC3000 WiFi Modules

IoT Garden

  • Please log in to reply
39 replies to this topic

#21 PineappleChili

PineappleChili

    Newbie

  • Members
  • Pip
  • 5 posts

Posted 19 July 2014 - 06:35 PM

Hey again! I switched over a Mega for the project and sadly the problem persists. It repeated the loop for a little over 12 hours then stopped uploading again.



#22 jpastor

jpastor

    Development leader

  • Administrators
  • 159 posts

Posted 21 July 2014 - 07:35 AM

Hi PineappleChili

 

It's strange that it works for 12 hours and then suddenly stop working.

Normally we use this kind of boards for prototyping, but not for production environments. Being a HW problem and no a platform problem is difficult that we can give you a solution, but we will do some tests.



#23 PineappleChili

PineappleChili

    Newbie

  • Members
  • Pip
  • 5 posts

Posted 31 July 2014 - 02:58 PM

Hey again!

 

Wanted to follow up and say that I was able to solve the problem with help from a few different sources. To quickly document it here in the event someone else suffering from the same issue stumbles upon this thread:

 

I updated the CC3000 firmware to version 1.28 which was released recently and addresses some of the stability issues caused by how it handles packets.

I reserved the data string to prevent fragmenting the heap.

 

My router seemed to hate the firmware update and caused the connection to hang at resolving the DHCP after 2 iterations of the loop so I rearranged the sketch to connect to the AP in the Setup. Then I added a check in the loop to make sure the connection still exists before sending the data. If the connection check fails, it reconnects then tries to send again.

 

Finally, and I think this was the major change that helped, I was informed via the Adafruit forums that normal .print and .println functionality on the Arduino goes character by character so sending 400 characters is actually sending 400 individual packets which takes way more traffic than it should. The Adafruit library for the CC3000 has a client.fastrprint and client.fastrprintln function that sends up to 90 bytes in one packet which greatly decreased traffic and thus failures caused by interrupting the traffic. To make sure each packet was only 90 bytes, I had to use chars over strings and instead of concatenating the data string and sending it as one giant thing, I sent it as 5 separate packets and let Carriots concatenate it.

 

All in all, seems like the problems are solved though! I was having difficulty getting it to run 8 hours straight, sending every 10 minutes. To test the changes, I was able to get it to run for a little under 48 hours straight sending every 2 minutes. Some basic error logging showed me what caused that crash and a simple connection check - reconnect on failure has solved that. I'd be happy to post the new code if anyone is interested but don't want to spam the forum with the 200 lines of code otherwise. :)



#24 mcastillo

mcastillo

    Carriots CEO

  • Administrators
  • 34 posts
  • LocationMadrid

Posted 31 July 2014 - 03:41 PM

Impressive debugging work! You are welcomed to post the code here. We don't consider it SPAM. You can also release it on GitHub where there is repository of this project. I hoped you liked using Carriots despite these problems with your device. BR.

#25 hotnanas

hotnanas

    Newbie

  • Members
  • Pip
  • 1 posts

Posted 12 August 2014 - 05:53 AM

I'd be interested in seeing the code. I currently can connect, and I have once sent temperature and humidity data read from a DHT 11 sensor. Therefore, I know it works. Most of the time I get a connection failed.

#26 PineappleChili

PineappleChili

    Newbie

  • Members
  • Pip
  • 5 posts

Posted 14 August 2014 - 12:35 AM

Here ya go!

Looking over it again, I should really create a function for the reconnect since I use those same lines like 5 times. Ah well, some work for this weekend. :)

 

/*Fred's Plant Sensor v1.0
Receives humidity and temperature input from SHT-10 soil Sensor
Receives ambient temperature from DS18B20 temperature sensor
Displays data from sensors to LCD
Uploads data to Carriot via Adafruit CC3000 wifi shield
Refreshes every 10 minutes 
Based on code from M.Schwartz*/

//setting up the LCD screen
#include <LiquidCrystal.h>
// (rs, en, db4, db5, db6, db7)
LiquidCrystal dataDisplay(A0, A1, A2, A3, A4, A5);

//setting up WiFi
#include <Adafruit_CC3000.h>
#include <ccspi.h>
#include <SPI.h>
#include <string.h>
//#include "utility/debug.h"
//#include <stdlib.h>

#define ADAFRUIT_CC3000_IRQ   3
#define ADAFRUIT_CC3000_VBAT  5
#define ADAFRUIT_CC3000_CS    10
Adafruit_CC3000 cc3000 = Adafruit_CC3000(ADAFRUIT_CC3000_CS, ADAFRUIT_CC3000_IRQ, ADAFRUIT_CC3000_VBAT, SPI_CLOCK_DIVIDER);
#define WLAN_SSID "xxx"
#define WLAN_PASS "xxx"
#define WLAN_SECURITY WLAN_SEC_WPA2
uint8_t dhcpTimeout;

//Setting up the humidity sensor
#include <SHT1x.h>
const uint8_t humidData = 8;
const uint8_t humidClock= 9;
float airTemp = 0;
float airHumid = 0;
SHT1x humidity(humidData, humidClock);

//setting up ambient temp
#include <OneWire.h>
#include <DallasTemperature.h>
const uint8_t ambientTemp = 7;
OneWire oneWire(ambientTemp);
DallasTemperature sensors(&oneWire);
float soilF = 0;

char soilTChar[32];
char airTempChar[32];
char airHumidChar[32];


//setting up Carriots 
#define API_KEY "xxx"
#define DEVICE  "Soil_Sensor@PineappleChili.PineappleChili"
#define WEBSITE "api.carriots.com"
uint32_t ip;
String data;
uint8_t stringLength = 0;


/*
**************************************
*                 SETUP              *
*Defines LCD Pins and loads LCD      *
*Begins Sensors and reserves Strings *
*Initiates CC3000, connects to wifi  *
**************************************
*/
void setup () {
  Serial.begin(115200);

  //setting up LCD pins
  pinMode(A0, OUTPUT); 
  pinMode(A1, OUTPUT); 
  pinMode(A2, OUTPUT); 
  pinMode(A3, OUTPUT); 
  pinMode(A4, OUTPUT); 
  pinMode(A5, OUTPUT);
  
 //initiate sensors
  sensors.begin();
  
 //LCD load screen 
  dataDisplay.begin(4, 20);  
  
  //connect to wifi
  cc3000.begin();
  Serial.println(F("Initializing CC3000"));
  if (!cc3000.begin()) {
    Serial.print(F("Failed"));
    while(1);
  }
  //Reserving data string
  data.reserve(180);
  
  //connecting to network
  beginning: //goto if connection fails
  Serial.print(F("Attempting to connect to "));
  Serial.println(WLAN_SSID);
  cc3000.connectToAP(WLAN_SSID, WLAN_PASS, WLAN_SECURITY);
  Serial.println(F("Connected"));
  
  //Wait for DHCP to complete
  Serial.println(F("Requesting DHCP"));
  while (!cc3000.checkDHCP()) {
    delay(500);
    Serial.print(F("."));
    dhcpTimeout ++;
    if (dhcpTimeout == 60) {
      Serial.println(F(""));
      Serial.println("DHCP Timeout! Attempting to reconnect.");
      dhcpTimeout = 0;
      goto beginning;
    }
  }
  Serial.println(F(""));
  Serial.println(F("DHCP complete"));

 //get website IP
    ip = 0;
  Serial.print(WEBSITE); Serial.print(F(" -> "));
  while (ip == 0) {
    if (! cc3000.getHostByName(WEBSITE, &ip)) {
      Serial.println(F("Couldn't resolve!"));
    }
    delay(500);
  }
  cc3000.printIPdotsRev(ip);
  Serial.println("");
}


/*
**************************************
*                 Loop               *
*Read Sensors and track iterations   *
*Display data to LCD                 *
*Formats data for upload             *
*Calls function to upload            *
**************************************
*/
void loop () {
  beginning:  //goto call in the event cc3000 is reset due to DHCP hangup


  //read sensors
  airTemp = humidity.readTemperatureF();
  airHumid = humidity.readHumidity();
  sensors.requestTemperatures();
  soilF = ((sensors.getTempCByIndex(0) * 9)/5)+32;
  delay (100);

  //display sensors to LCD
  dataDisplay.clear();
  dataDisplay.print("Soil: ");
  dataDisplay.print(soilF);
  dataDisplay.print((char)223);
  dataDisplay.setCursor(0,1);
  dataDisplay.print("Air: ");
  dataDisplay.print(airTemp);
  dataDisplay.print((char) 223);
  dataDisplay.print(", ");
  dataDisplay.print(airHumid);
  dataDisplay.print("%");

  // Convert Floats to Chars in order to send them via fastrprint
  // dtostrf( [Float variable] , [Minimum SizeBeforePoint] , [sizeAfterPoint] , [WhereToStoreIt] )
  dtostrf(soilF, 2, 2, soilTChar);
  dtostrf(airTemp, 2, 2, airTempChar);
  dtostrf(airHumid, 2, 2, airHumidChar);

  //you need to concatenate the data string in order to calculate length as Carriotts requires data length in the payload
  String data = "{\"protocol\":\"v2\",\"device\":\""+String(DEVICE)+"\",\"at\":\"now\",\"data\":{\"Air Temperature\":"+(airTempChar)+",\"Humidity\":"+(airHumidChar)+",\"Soil Temperature\":"+(soilTChar)+"}}";
  stringLength = data.length();
  
  /*
  //This is used for initial troubleshooting. Uncomment if your upload fails then verify content length here versus what is reported back from the client
  Serial.print(F("Data Length: "));
  Serial.print(stringLength);
  Serial.println();
  Serial.println(F("POST /streams HTTP/1.1"));
  Serial.println(F("Host: api.carriots.com"));
  Serial.println(F("Accept: application/json"));
  Serial.println(F("User-Agent: Arduino-Carriots"));
  Serial.println(F("Content-Type: application/json"));
  Serial.println("carriots.apikey: " + String(API_KEY));
  Serial.println("Content-Length: " + String(stringLength));
  Serial.print(F("Connection: close"));
  Serial.println();
  Serial.println(data);
  */

  //check to see if connection still exists. If not, reconnect
  if (!cc3000.checkConnected()) {
    Serial.println(F("Lost connection! Attempting to reconnect"));
    cc3000.connectToAP(WLAN_SSID, WLAN_PASS, WLAN_SECURITY);
    Serial.println(F("Connected"));
    //Request DHCP - If not resolved in 30 seconds, try to reconnect to AP and start over
    Serial.println(F("Requesting DHCP"));
    while (!cc3000.checkDHCP()) {
      delay(500);
      Serial.print(F("."));
      dhcpTimeout ++;
      if (dhcpTimeout == 60) {
        Serial.println(F(""));
        Serial.println("DHCP Timeout! Reboot CC3000 and attempt to reconnect.");
        dhcpTimeout = 0;
        cc3000.reboot();
        goto beginning;  
      }
    }
    Serial.println(F(""));
    Serial.println(F("DHCP complete"));

  }
  
//Send data via client to Carriots  
  Adafruit_CC3000_Client client = cc3000.connectTCP(ip, 80);
  if (client.connected()) {
  Serial.println(F("Connected!"));
  client.fastrprintln(F("POST /streams HTTP/1.1"));
  client.fastrprintln(F("Host: api.carriots.com"));
  client.fastrprintln(F("Accept: application/json"));
  client.fastrprintln(F("User-Agent: Arduino-Carriots"));
  client.fastrprintln(F("Content-Type: application/json"));
  client.fastrprint(F("carriots.apikey: ")); 
  client.fastrprintln(API_KEY);
  client.fastrprint(F("Content-Length: "));
  client.println(stringLength);
  client.fastrprintln(F("Connection: close"));
  client.println();
  client.fastrprint(F("{\"protocol\":\"v2\",\"device\":\""));
  client.fastrprint(DEVICE);
  client.fastrprint(F("\",\"at\":\"now\",\"data\":{\"Air Temperature\":"));
  client.fastrprint(airTempChar);
  client.fastrprint(F(",\"Humidity\":"));
  client.fastrprint(airHumidChar);
  client.fastrprint(F(",\"Soil Temperature\":"));
  client.fastrprint(soilTChar);
  client.fastrprintln(F("}}"));
  } else {
//if unable to send data, reset cc3000, reconnect to AP, resolve DHCP, start at beginning of loop
    Serial.println (F("Connection failed! Attempting reboot"));
    cc3000.reboot();
    Serial.println(F("Lost connection! Attempting to reconnect"));
    cc3000.connectToAP(WLAN_SSID, WLAN_PASS, WLAN_SECURITY);
    Serial.println(F("Connected"));
    //Request DHCP - If not resolved in 30 seconds, try to reconnect to AP and start over
    Serial.println(F("Requesting DHCP"));
    while (!cc3000.checkDHCP()) {
      delay(500);
      Serial.print(F("."));
      dhcpTimeout ++;
      if (dhcpTimeout == 60) {
        Serial.println(F(""));
        Serial.println(F("DHCP Timeout! Reboot CC3000 and attempt to reconnect."));
        dhcpTimeout = 0;
        cc3000.reboot();
        goto beginning;  
        }
      }
    Serial.println(F(""));
    Serial.println(F("DHCP complete"));
    }
  
  Serial.println(F("----------"));
  while (client.connected()) {
    while (client.available()) {
      char c = client.read();
      Serial.print(c);
    }
  }
   
  client.close();
  Serial.println(F(""));
  Serial.println(F("Closing Client"));

  //Two delays between loop - 10 min for normal procedure, 2 min for testing
  delay (600000);
//  delay (120000);

}



#27 sdeancos

sdeancos

    Master of Puppets

  • Members
  • PipPipPip
  • 102 posts
  • LocationMadrid, Spain.

Posted 14 August 2014 - 06:24 AM

Hi!

 

 

Great job!

 

 

Thank you so much for sharing it with the community!



#28 sarmau

sarmau

    Newbie

  • Members
  • Pip
  • 1 posts

Posted 13 January 2015 - 08:10 AM

hi! can I just ask, what does this mean? I'm totally lost..I'm new with this stuff, but I want to learn.

in your project, it says

 

 

"In the loop() part of the sketch, you need to format the data measured by the Arduino

according the Carriots guidelines (http://adafru.it/d53). For example, data sent by our sketch
should have the following form:
 
 
{
    "protocol":"v2",
    "at":"now",
    "device":"yourDevice@yourUserName",
    "data":{
        "Temperature":"21.05",
        "Humidity":"58.50"
    },
    "checksum":""
}
"
 
 
 
where should I do the formatting? where should I add this part? and what should I add? I am trying to read a potentiometer so I can practice and learn it easily..can you help me? please.

 



#29 jpastor

jpastor

    Development leader

  • Administrators
  • 159 posts

Posted 13 January 2015 - 10:55 AM

Hello sarmau

If you are new, the best way to learn is to read these tutorials:

 

https://www.carriots...i/core_concepts

https://www.carriots...agement#streams

https://www.carriots...end_stream/hurl

https://www.carriots...ts/alert_system

 

After reading, if you have any questions, tell us

 

 

Enjoy Carriots!



#30 ltrees

ltrees

    Newbie

  • Members
  • Pip
  • 1 posts

Posted 06 April 2015 - 04:08 AM

I'm also having some issues with this project. I'm using a Mega 2560 and the same Sensirion sensor in the guide, and the CC3000 shield. 

 

It connects to wifi fine and prints that it has sent data to Carriots, but after that it stalls. This is all I get in the serial monitor, and I get no data at Carriots:

 

Request DHCP
api.carriots.com -> 82.223.244.60Data length110

POST /streams HTTP/1.1
Host: api.carriots.com
Accept: application/json
User-Agent: Arduino-Carriots
Content-Type: application/json
carriots.apikey: ***
Content-Length: 110
Connection: close
{"protocol":"v1","device":"soilSensor@ltrees.ltrees","at":"now","data":{"Temperature":22.33,"Humidity":38.43}}
Connected!
-------------------------------------

Here is the code, it's basically unchanged from the guide aside from where I'm supposed to change it:

 

/*************************************************** 
  This is a sketch to interface a soil sensor & Carriots
  using the Adafruit CC3000 breakout board (or WiFi shield)
  
  Written by Marco Schwartz for Open Home Automation
 ****************************************************/

// Libraries
#include <Adafruit_CC3000.h>
#include <ccspi.h>
#include <SPI.h>
#include <string.h>
#include "utility/debug.h"
#include "DHT.h"
#include <stdlib.h>
#include <Sensirion.h>

// Define CC3000 chip pins
#define ADAFRUIT_CC3000_IRQ   3
#define ADAFRUIT_CC3000_VBAT  5
#define ADAFRUIT_CC3000_CS    10

// Soil sensor pins
const uint8_t dataPin  =  22;
const uint8_t clockPin =  23;

// Soil sensor variables
float t;
float h;
float dewpoint;

// Create soil sensor object
Sensirion soilSensor = Sensirion(dataPin, clockPin);

// Create CC3000 instances
Adafruit_CC3000 cc3000 = Adafruit_CC3000(ADAFRUIT_CC3000_CS, ADAFRUIT_CC3000_IRQ, ADAFRUIT_CC3000_VBAT,
                                         SPI_CLOCK_DIV2); // you can change this clock speed
                                
// WLAN parameters
#define WLAN_SSID       "***"
#define WLAN_PASS       "***"
// Security can be WLAN_SEC_UNSEC, WLAN_SEC_WEP, WLAN_SEC_WPA or WLAN_SEC_WPA2
#define WLAN_SECURITY   WLAN_SEC_WPA2

// Carriots parameters
#define WEBSITE  "api.carriots.com"
#define API_KEY "***"
#define DEVICE  "soilSensor@ltrees.ltrees"

uint32_t ip;

void setup(void)
{
  // Initialize
  Serial.begin(115200);
  
  Serial.println(F("\nInitializing..."));
  if (!cc3000.begin())
  {
    Serial.println(F("Couldn't begin()! Check your wiring?"));
    while(1);
  }
 
}

void loop(void)
{
  // Connect to WiFi network
  cc3000.connectToAP(WLAN_SSID, WLAN_PASS, WLAN_SECURITY);
  Serial.println(F("Connected!"));
  
  /* Wait for DHCP to complete */
  Serial.println(F("Request DHCP"));
  while (!cc3000.checkDHCP())
  {
    delay(100);
  }  
 
  // Get the website IP & print it
  ip = 0;
  Serial.print(WEBSITE); Serial.print(F(" -> "));
  while (ip == 0) {
    if (! cc3000.getHostByName(WEBSITE, &ip)) {
      Serial.println(F("Couldn't resolve!"));
    }
    delay(500);
  }
  cc3000.printIPdotsRev(ip);
  
  // Get data & transform to integers
  soilSensor.measure(&t, &h, &dewpoint);
 
  // Convert data to String
  String temperature = doubleToString(t,2);
  String humidity = doubleToString(h,2);
 
  // Prepare JSON for Carriots & get length
  int length = 0;

  String data = "{\"protocol\":\"v1\",\"device\":\""+String(DEVICE)+"\",\"at\":\"now\",\"data\":{\"Temperature\":"+String(temperature)+",\"Humidity\":"+String(humidity)+"}}";
  
  length = data.length();
  Serial.print("Data length");
  Serial.println(length);
  Serial.println();
  
  // Print request for debug purposes
  Serial.println("POST /streams HTTP/1.1");
  Serial.println("Host: api.carriots.com");
  Serial.println("Accept: application/json");
  Serial.println("User-Agent: Arduino-Carriots");
  Serial.println("Content-Type: application/json");
  Serial.println("carriots.apikey: " + String(API_KEY));
  Serial.println("Content-Length: " + String(length));
  Serial.print("Connection: close");
  Serial.println();
  Serial.println(data);
  
  // Send request
  Adafruit_CC3000_Client client = cc3000.connectTCP(ip, 80);
  if (client.connected()) {
    Serial.println("Connected!");
    client.println("POST /streams HTTP/1.1");
    client.println("Host: api.carriots.com");
    client.println("Accept: application/json");
    client.println("User-Agent: Arduino-Carriots");
    client.println("Content-Type: application/json");
    client.println("carriots.apikey: " + String(API_KEY));
    client.println("Content-Length: " + String(length));
    client.println("Connection: close");
    client.println();
    
    client.println(data);
    
  } else {
    Serial.println(F("Connection failed"));    
    return;
  }
  
  Serial.println(F("-------------------------------------"));
  while (client.connected()) {
    while (client.available()) {
      char c = client.read();
      Serial.print(c);
    }
  }
  client.close();
  Serial.println(F("-------------------------------------"));
  
  Serial.println(F("\n\nDisconnecting"));
  cc3000.disconnect();
  
  // Wait 10 seconds until next update
  delay(10000);
   
}

// Convert double to string
String doubleToString(float input,int decimalPlaces){
  if(decimalPlaces!=0){
    String string = String((int)(input*pow(10,decimalPlaces)));
      if(abs(input)<1){
        if(input>0)
          string = "0"+string;
        else if(input<0)
          string = string.substring(0,1)+"0"+string.substring(1);
      }
      return string.substring(0,string.length()-decimalPlaces)+"."+string.substring(string.length()-decimalPlaces);
    }
  else {
    return String((int)input);
  }
}

 

 



#31 jpastor

jpastor

    Development leader

  • Administrators
  • 159 posts

Posted 06 April 2015 - 10:20 AM

Hello Itrees

The first thing that occurs to me is that it is a memory problem. Although the person to answer would be Marc-Olivier (project's author).
You can try to relieve some of the memory burden and see if the problem is solved.

 
 
Tell us your progress!


#32 AgDiscovery

AgDiscovery

    Newbie

  • Members
  • Pip
  • 1 posts

Posted 22 June 2015 - 10:40 PM

It seems that many of us are having similar, if not the same issues. I have scoured the Carriots and Adafruit forums as well as Marco's page and still cannot seem to get this thing to work.  I am using an Arduino Uno R3, with Adafruit CC3000 Wifi breakout with on-board antenna, and SHT10 sensor.  I am running Arduino IDE 1.5.7 (i know it is not the latest version but, on one of the many posts Marco said that there were known issues with the latest IDE and suggested using 1.5.7 so, i reverted to the earlier version).  Also, the machine is windows 7 64 bit system. You can see the hardware setup in the attachment. 

 

Using Hurl.it, i am able to send data to Carriots using the same information that appears in the code below.

 

Here is the information that i receive from the Serial Monitor:

 

Initializing CC3000
Attempting to connect to INTELLINET
Connected
Requesting DHCP
.
DHCP complete
api.carriots.com -> 82.223.244.60
Data Length: 126
POST /streams HTTP/1.1
Host: api.carriots.com
Accept: application/json
User-Agent: Arduino-Carriots
Content-Type: application/json
carriots.apikey: "I used the APIKEY that worked on Hurl.it"
Content-Length: 126
Connection: close
{"protocol":"v2","device":"SoilSensor@AgDiscovery.AgDiscovery","at":"now","data":{"Humidity":-4.65,"Soil Temperature":-40.00}}
Connected!
----------
HTTP/1.1 401 Unauthorized
Date: Mon, 22 Jun 2015 21:57:54 GMT
Content-Type: application/vnd.carriots.api.v2+json
Content-Length: 40
Connection: close
Server: Carriots REST API
Access-Control-Allow-Origin: *
Access-Control-Allow-Credentials: true
Access-Control-Expose-Headers: Date,Server,Allow,Connection,Content-Length,Content-Type,Cache-Control

{ "response": "Not supported payload." }
Closing Client
Data Length: 126
POST /streams HTTP/1.1
Host: api.carriots.com
Accept: application/json
User-Agent: Arduino-Carriots
Content-Type: application/json
carriots.apikey: "I used the APIKEY that worked on Hurl.it"
Content-Length: 126
Connection: close
{"protocol":"v2","device":"SoilSensor@AgDiscovery.AgDiscovery","at":"now","data":{"Humidity":-4.65,"Soil Temperature":-26.18}}
Connected!
----------
HTTP/1.1 401 Unauthorized
Date: Mon, 22 Jun 2015 22:07:56 GMT
Content-Type: application/vnd.carriots.api.v2+json
Content-Length: 40
Connection: close
Server: Carriots REST API
Access-Control-Allow-Origin: *
Access-Control-Allow-Credentials: true
Access-Control-Expose-Headers: Date,Server,Allow,Connection,Content-Length,Content-Type,Cache-Control

{ "response": "Not supported payload." }
Closing Client
Data Length: 125
POST /streams HTTP/1.1
Host: api.carriots.com
Accept: application/json
User-Agent: Arduino-Carriots
Content-Type: application/json
carriots.apikey: "I used the APIKEY that worked on Hurl.it"
Content-Length: 125
Connection: close
{"protocol":"v2","device":"SoilSensor@AgDiscovery.AgDiscovery","at":"now","data":{"Humidity":-4.65,"Soil Temperature":-7.74}}
Connected!
----------
HTTP/1.1 401 Unauthorized
Date: Mon, 22 Jun 2015 22:17:57 GMT
Content-Type: application/vnd.carriots.api.v2+json
Content-Length: 40
Connection: close
Server: Carriots REST API
Access-Control-Allow-Origin: *
Access-Control-Allow-Credentials: true
Access-Control-Expose-Headers: Date,Server,Allow,Connection,Content-Length,Content-Type,Cache-Control

{ "response": "Not supported payload." }
Closing Client

 

Here is the code that i am using adapted from PineappleChilli's code.  According to multiple posts there are real issues pertaining to SRAM, and Mr. Chilli's code seemed to address this issue the best.  I have commented out some of the features, such as the LCD and the Celsius to Fahrenheit conversion, etc.  Maybe i removed something that i should have kept?

/*Fred's Plant Sensor v1.0
Receives humidity and temperature input from SHT-10 soil Sensor
Receives ambient temperature from DS18B20 temperature sensor
Displays data from sensors to LCD
Uploads data to Carriot via Adafruit CC3000 wifi shield
Refreshes every 10 minutes 
Based on code from M.Schwartz*/
 
//setting up the LCD screen
//#include <LiquidCrystal.h>
// (rs, en, db4, db5, db6, db7)
//LiquidCrystal dataDisplay(A0, A1, A2, A3, A4, A5);
 
//setting up WiFi
#include <Adafruit_CC3000.h>
#include <ccspi.h>
#include <SPI.h>
#include <string.h>
  //#include "utility/debug.h" ---originally commented out
  //#include <stdlib.h>   ---originally commented out
 
#define ADAFRUIT_CC3000_IRQ   3
#define ADAFRUIT_CC3000_VBAT  5
#define ADAFRUIT_CC3000_CS    10
Adafruit_CC3000 cc3000 = Adafruit_CC3000(ADAFRUIT_CC3000_CS, ADAFRUIT_CC3000_IRQ, ADAFRUIT_CC3000_VBAT, SPI_CLOCK_DIVIDER);
#define WLAN_SSID "INTELLINET"
#define WLAN_PASS ""
#define WLAN_SECURITY WLAN_SEC_WPA2
uint8_t dhcpTimeout;
 
//Setting up the humidity sensor
#include <SHT1x.h>
const uint8_t humidData = 8;
const uint8_t humidClock= 9;
float airTemp = 0;
float airHumid = 0;
SHT1x humidity(humidData, humidClock);
 
/*
//setting up ambient temp
#include <OneWire.h>
#include <DallasTemperature.h>
const uint8_t ambientTemp = 7;
OneWire oneWire(ambientTemp);
DallasTemperature sensors(&oneWire);
float soilF = 0;
*/
 
//float soilF = 0; 
 
char soilTChar[32];
char airTempChar[32];
char airHumidChar[32];
 
 
//setting up Carriots 
#define API_KEY "I used the APIKEY that worked on Hurl.it"
#define DEVICE  "SoilSensor@AgDiscovery.AgDiscovery"
#define WEBSITE "api.carriots.com"
uint32_t ip;
String data;
uint8_t stringLength = 0;
 
 
/*
**************************************
*                 SETUP              *
*Defines LCD Pins and loads LCD      *
*Begins Sensors and reserves Strings *
*Initiates CC3000, connects to wifi  *
**************************************
*/
void setup () {
  Serial.begin(115200);
 
  /*
  //setting up LCD pins
  pinMode(A0, OUTPUT); 
  pinMode(A1, OUTPUT); 
  pinMode(A2, OUTPUT); 
  pinMode(A3, OUTPUT); 
  pinMode(A4, OUTPUT); 
  pinMode(A5, OUTPUT);
  */
  
 //initiate sensors
 //sensors.begin();
  
 //LCD load screen 
  //dataDisplay.begin(4, 20);  
  
  //connect to wifi
  cc3000.begin();
  Serial.println(F("Initializing CC3000"));
  if (!cc3000.begin()) {
    Serial.print(F("Failed"));
    while(1);
  }
  //Reserving data string
  data.reserve(180);
  
  //connecting to network
  beginning: //goto if connection fails
  Serial.print(F("Attempting to connect to "));
  Serial.println(WLAN_SSID);
  cc3000.connectToAP(WLAN_SSID, WLAN_PASS, WLAN_SECURITY);
  Serial.println(F("Connected"));
  
  //Wait for DHCP to complete
  Serial.println(F("Requesting DHCP"));
  while (!cc3000.checkDHCP()) {
    delay(500);
    Serial.print(F("."));
    dhcpTimeout ++;
    if (dhcpTimeout == 60) {
      Serial.println(F(""));
      Serial.println("DHCP Timeout! Attempting to reconnect.");
      dhcpTimeout = 0;
      goto beginning;
    }
  }
  Serial.println(F(""));
  Serial.println(F("DHCP complete"));
 
 //get website IP
    ip = 0;
  Serial.print(WEBSITE); Serial.print(F(" -> "));
  while (ip == 0) {
    if (! cc3000.getHostByName(WEBSITE, &ip)) {
      Serial.println(F("Couldn't resolve!"));
    }
    delay(500);
  }
  cc3000.printIPdotsRev(ip);
  Serial.println("");
}
 
 
/*
**************************************
*                 Loop               *
*Read Sensors and track iterations   *
*Display data to LCD                 *
*Formats data for upload             *
*Calls function to upload            *
**************************************
*/
void loop () {
  beginning:  //goto call in the event cc3000 is reset due to DHCP hangup
 
 
  //read sensors
  airTemp = humidity.readTemperatureF();    
  airHumid = humidity.readHumidity();
  //sensors.requestTemperatures();
  //soilF = ((sensors.getTempCByIndex(0) * 9)/5)+32;
  delay (100);
 
  /*
  //display sensors to LCD
  dataDisplay.clear();
  dataDisplay.print("Soil: ");
  dataDisplay.print(soilF);
  dataDisplay.print((char)223);
  dataDisplay.setCursor(0,1);
  dataDisplay.print("Air: ");
  dataDisplay.print(airTemp);
  dataDisplay.print((char) 223);
  dataDisplay.print(", ");
  dataDisplay.print(airHumid);
  dataDisplay.print("%");
  */
 
  /*   ********old code used "soilF"*************
  // Convert Floats to Chars in order to send them via fastrprint 
  // dtostrf( [Float variable] , [Minimum SizeBeforePoint] , [sizeAfterPoint] , [WhereToStoreIt] )
  dtostrf(soilF, 2, 2, soilTChar);
  //dtostrf(airTemp, 2, 2, airTempChar);
  dtostrf(airHumid, 2, 2, airHumidChar);
  */
  
  // Convert Floats to Chars in order to send them via fastrprint
  // dtostrf( [Float variable] , [Minimum SizeBeforePoint] , [sizeAfterPoint] , [WhereToStoreIt] )
  dtostrf(airTemp, 2, 2, soilTChar);
  //dtostrf(airTemp, 2, 2, airTempChar);
  dtostrf(airHumid, 2, 2, airHumidChar);
  
 
  //you need to concatenate the data string in order to calculate length as Carriotts requires data length in the payload
  String data = "{\"protocol\":\"v2\",\"device\":\""+String(DEVICE)+"\",\"at\":\"now\",\"data\":{\"Humidity\":"+(airHumidChar)+",\"Soil Temperature\":"+(soilTChar)+"}}";
 
 
  //String data = "{\"protocol\":\"v2\",\"device\":\""+String(DEVICE)+"\",\"at\":\"now\",\"data\":{\"Air Temperature\":"+(airTempChar)+",\"Humidity\":"+(airHumidChar)+",\"Soil Temperature\":"+(soilTChar)+"}}";
  stringLength = data.length();
  
  
  //This is used for initial troubleshooting. Uncomment if your upload fails then verify content length here versus what is reported back from the client
  Serial.print(F("Data Length: "));
  Serial.print(stringLength);
  Serial.println();
  Serial.println(F("POST /streams HTTP/1.1"));
  Serial.println(F("Host: api.carriots.com"));
  Serial.println(F("Accept: application/json"));
  Serial.println(F("User-Agent: Arduino-Carriots"));
  Serial.println(F("Content-Type: application/json"));
  Serial.println("carriots.apikey: " + String(API_KEY));
  Serial.println("Content-Length: " + String(stringLength));
  Serial.print(F("Connection: close"));
  Serial.println();
  Serial.println(data);
  
  
 
  //check to see if connection still exists. If not, reconnect
  if (!cc3000.checkConnected()) {
    Serial.println(F("Lost connection! Attempting to reconnect"));
    cc3000.connectToAP(WLAN_SSID, WLAN_PASS, WLAN_SECURITY);
    Serial.println(F("Connected"));
    //Request DHCP - If not resolved in 30 seconds, try to reconnect to AP and start over
    Serial.println(F("Requesting DHCP"));
    while (!cc3000.checkDHCP()) {
      delay(500);
      Serial.print(F("."));
      dhcpTimeout ++;
      if (dhcpTimeout == 60) {
        Serial.println(F(""));
        Serial.println("DHCP Timeout! Reboot CC3000 and attempt to reconnect.");
        dhcpTimeout = 0;
        cc3000.reboot();
        goto beginning;  
      }
    }
    Serial.println(F(""));
    Serial.println(F("DHCP complete"));
 
  }
  
//Send data via client to Carriots  
  Adafruit_CC3000_Client client = cc3000.connectTCP(ip, 80);
  if (client.connected()) {
  Serial.println(F("Connected!"));
  client.fastrprintln(F("POST /streams HTTP/1.1"));
  client.fastrprintln(F("Host: api.carriots.com"));
  client.fastrprintln(F("Accept: application/json"));
  client.fastrprintln(F("User-Agent: Arduino-Carriots"));
  client.fastrprintln(F("Content-Type: application/json"));
  client.fastrprint(F("carriots.apikey: ")); 
  client.fastrprintln(API_KEY);
  client.fastrprint(F("Content-Length: "));
  client.println(stringLength);
  client.fastrprintln(F("Connection: close"));
  client.println();
  client.fastrprint(F("{\"protocol\":\"v2\",\"device\":\""));
  client.fastrprint(DEVICE);
  client.fastrprint(F("\",\"at\":\"now\",\"data\":{\"Air Temperature\":"));
  client.fastrprint(airTempChar);
  client.fastrprint(F(",\"Humidity\":"));
  client.fastrprint(airHumidChar);
  client.fastrprint(F(",\"Soil Temperature\":"));
  client.fastrprint(soilTChar);
  client.fastrprintln(F("}}"));
  } else {
//if unable to send data, reset cc3000, reconnect to AP, resolve DHCP, start at beginning of loop
    Serial.println (F("Connection failed! Attempting reboot"));
    cc3000.reboot();
    Serial.println(F("Lost connection! Attempting to reconnect"));
    cc3000.connectToAP(WLAN_SSID, WLAN_PASS, WLAN_SECURITY);
    Serial.println(F("Connected"));
    //Request DHCP - If not resolved in 30 seconds, try to reconnect to AP and start over
    Serial.println(F("Requesting DHCP"));
    while (!cc3000.checkDHCP()) {
      delay(500);
      Serial.print(F("."));
      dhcpTimeout ++;
      if (dhcpTimeout == 60) {
        Serial.println(F(""));
        Serial.println(F("DHCP Timeout! Reboot CC3000 and attempt to reconnect."));
        dhcpTimeout = 0;
        cc3000.reboot();
        goto beginning;  
        }
      }
    Serial.println(F(""));
    Serial.println(F("DHCP complete"));
    }
  
  Serial.println(F("----------"));
  while (client.connected()) {
    while (client.available()) {
      char c = client.read();
      Serial.print(c);
    }
  }
   
  client.close();
  Serial.println(F(""));
  Serial.println(F("Closing Client"));
 
  //Two delays between loop - 10 min for normal procedure, 2 min for testing
  delay (600000);
//  delay (120000);
 
}

I apologize for the long post, hopefully this information will help identify the issue(s).  As mentioned, I have looked over A LOT of forums at multiple sites but if you have something else to try or read, please let me know.  Thanks!

Attached Files



#33 whittaker9

whittaker9

    Newbie

  • Members
  • Pip
  • 8 posts

Posted 25 June 2015 - 02:14 PM

I see that your payload length is 126 characters. Try to keep your payload length under 100 characters.  Some names could be much shorter. For example, "Air Temperature" could be changed to "T". "Soil Temperature" could be "ST".  The space between Air and Temperature may cause a problem, Also check the space between Soil and Temperature. 

//String data = "{\"protocol\":\"v2\",\"device\":\""+String(DEVICE)+"\",\"at\":\"now\",\"data\":{\"Air Temperature\":"+(airTempChar)+",\"Humidity\":"+(airHumidChar)+",\"Soil Temperature\":"+(soilTChar)+"}}";
  stringLength = data.length();

 client.fastrprint(F("\",\"at\":\"now\",\"data\":{\"Air Temperature\":"));   client.fastrprint(airTempChar);   client.fastrprint(F(",\"Humidity\":"));   client.fastrprint(airHumidChar);   client.fastrprint(F(",\"Soil Temperature\":"));   client.fastrprint(soilTChar);   


#34 billwatson

billwatson

    Member

  • Members
  • PipPip
  • 14 posts

Posted 05 September 2015 - 02:07 AM

Hi Marco and PineappleChilli!

 

With your help I'm managed to get a working stream to Carriots using a CC3000, uno and basic LDR sensor

 

It seems as thought I'm having the same issue as you Pineapple with crashing/hanging after a certain time, approximately 6 hours.

 

I was wondering if you had discovered any solutions to this issue.

 

Secondly, I was wondering if you'd had any success using a GSM shield as the connection for the rest api stream?

 

Thanks for all your posts, and I look forward to hearing back from you



#35 jefbelgium

jefbelgium

    Newbie

  • Members
  • Pip
  • 1 posts

Posted 17 September 2015 - 08:21 AM

Hello all,

 

Thanks for the fantastic work. I have adapted PineappleChili's code for the DHT22.

I edited out the soil Temp Probe and the LCD, since I am not using them.

I am planning to add a water temp probe an a PH probe for my greenhouse. Will keep you posted.

 

 

/*Fred's Plant Sensor v1.0
Receives humidity and temperature input from SHT-10 soil Sensor
Receives ambient temperature from DS18B20 temperature sensor
Displays data from sensors to LCD
Uploads data to Carriot via Adafruit CC3000 wifi shield
Refreshes every 10 minutes 
Based on code from M.Schwartz*/
 
 
//setting up WiFi
#include <Adafruit_CC3000.h>
#include <ccspi.h>
#include <SPI.h>
#include <string.h>
//#include "utility/debug.h"
//#include <stdlib.h>
 
//include DHT library
#include "DHT.h"
 
#define ADAFRUIT_CC3000_IRQ   3
#define ADAFRUIT_CC3000_VBAT  5
#define ADAFRUIT_CC3000_CS    10
Adafruit_CC3000 cc3000 = Adafruit_CC3000(ADAFRUIT_CC3000_CS, ADAFRUIT_CC3000_IRQ, ADAFRUIT_CC3000_VBAT, SPI_CLOCK_DIVIDER);
#define WLAN_SSID "your wifi SSID"
#define WLAN_PASS "your password"
#define WLAN_SECURITY WLAN_SEC_UNSEC
// I opted for UNSEC this time, but you can also use WPA2 or other security protocols
uint8_t dhcpTimeout;
 
// DHT11 sensor pins
#define DHTPIN 7 
#define DHTTYPE DHT22
 
// DHT instance
DHT dht(DHTPIN, DHTTYPE);
 
char TemperatureChar[32];
char HumidityChar[32];
 
// Carriots parameters
#define WEBSITE  "api.carriots.com"
#define API_KEY "your API key"
#define DEVICE  "your device name"
uint32_t ip;
String data;
uint8_t stringLength = 0;
 
/*
**************************************
*                 SETUP              *
*Begins Sensors and reserves Strings *
*Initiates CC3000, connects to wifi  *
**************************************
*/
void setup () {
  Serial.begin(115200);
 
  // Initialize DHT sensor
  dht.begin();
  
  //connect to wifi
  cc3000.begin();
  Serial.println(F("Initializing CC3000"));
  if (!cc3000.begin()) {
    Serial.print(F("Failed"));
    while(1);
  }
  //Reserving data string
  data.reserve(180);
  
  //connecting to network
  beginning: //goto if connection fails
  Serial.print(F("Attempting to connect to "));
  Serial.println(WLAN_SSID);
  cc3000.connectToAP(WLAN_SSID, WLAN_PASS, WLAN_SECURITY);
  Serial.println(F("Connected"));
  
  //Wait for DHCP to complete
  Serial.println(F("Requesting DHCP"));
  while (!cc3000.checkDHCP()) {
    delay(500);
    Serial.print(F("."));
    dhcpTimeout ++;
    if (dhcpTimeout == 60) {
      Serial.println(F(""));
      Serial.println("DHCP Timeout! Attempting to reconnect.");
      dhcpTimeout = 0;
      goto beginning;
    }
  }
  Serial.println(F(""));
  Serial.println(F("DHCP complete"));
 
 //get website IP
    ip = 0;
  Serial.print(WEBSITE); Serial.print(F(" -> "));
  while (ip == 0) {
    if (! cc3000.getHostByName(WEBSITE, &ip)) {
      Serial.println(F("Couldn't resolve!"));
    }
    delay(500);
  }
  cc3000.printIPdotsRev(ip);
  Serial.println("");
}
 
 
/*
**************************************
*                 Loop               *
*Read Sensors and track iterations   *
*Formats data for upload             *
*Calls function to upload            *
**************************************
*/
void loop () {
  beginning:  //goto call in the event cc3000 is reset due to DHCP hangup
 
  // Measure the humidity & temperature
  float Humidity = dht.readHumidity();
  float Temperature = dht.readTemperature();
  delay(100);
 
  // Convert Floats to Chars in order to send them via fastrprint
  // dtostrf( [Float variable] , [Minimum SizeBeforePoint] , [sizeAfterPoint] , [WhereToStoreIt] )
  dtostrf(Humidity, 2, 2, HumidityChar);
  dtostrf(Temperature, 2, 2, TemperatureChar);
 
  //you need to concatenate the data string in order to calculate length as Carriotts requires data length in the payload
  String data = "{\"protocol\":\"v2\",\"device\":\""+String(DEVICE)+"\",\"at\":\"now\",\"data\":{\"Humidity\":"+(HumidityChar)+",\"Temperature\":"+(TemperatureChar)+"}}";
  stringLength = data.length();
  
  /*
  //This is used for initial troubleshooting. Uncomment if your upload fails then verify content length here versus what is reported back from the client
  Serial.print(F("Data Length: "));
  Serial.print(stringLength);
  Serial.println();
  Serial.println(F("POST /streams HTTP/1.1"));
  Serial.println(F("Host: api.carriots.com"));
  Serial.println(F("Accept: application/json"));
  Serial.println(F("User-Agent: Arduino-Carriots"));
  Serial.println(F("Content-Type: application/json"));
  Serial.println("carriots.apikey: " + String(API_KEY));
  Serial.println("Content-Length: " + String(stringLength));
  Serial.print(F("Connection: close"));
  Serial.println();
  Serial.println(data);
  */
 
  //check to see if connection still exists. If not, reconnect
  if (!cc3000.checkConnected()) {
    Serial.println(F("Lost connection! Attempting to reconnect"));
    cc3000.connectToAP(WLAN_SSID, WLAN_PASS, WLAN_SECURITY);
    Serial.println(F("Connected"));
    //Request DHCP - If not resolved in 30 seconds, try to reconnect to AP and start over
    Serial.println(F("Requesting DHCP"));
    while (!cc3000.checkDHCP()) {
      delay(500);
      Serial.print(F("."));
      dhcpTimeout ++;
      if (dhcpTimeout == 60) {
        Serial.println(F(""));
        Serial.println("DHCP Timeout! Reboot CC3000 and attempt to reconnect.");
        dhcpTimeout = 0;
        cc3000.reboot();
        goto beginning;  
      }
    }
    Serial.println(F(""));
    Serial.println(F("DHCP complete"));
 
  }
  
//Send data via client to Carriots  
  Adafruit_CC3000_Client client = cc3000.connectTCP(ip, 80);
  if (client.connected()) {
  Serial.println(F("Connected!"));
  client.fastrprintln(F("POST /streams HTTP/1.1"));
  client.fastrprintln(F("Host: api.carriots.com"));
  client.fastrprintln(F("Accept: application/json"));
  client.fastrprintln(F("User-Agent: Arduino-Carriots"));
  client.fastrprintln(F("Content-Type: application/json"));
  client.fastrprint(F("carriots.apikey: ")); 
  client.fastrprintln(API_KEY);
  client.fastrprint(F("Content-Length: "));
  client.println(stringLength);
  client.fastrprintln(F("Connection: close"));
  client.println();
  client.fastrprint(F("{\"protocol\":\"v2\",\"device\":\""));
  client.fastrprint(DEVICE);
  client.fastrprint(F("\",\"at\":\"now\",\"data\":{\"Humidity\":"));
  client.fastrprint(HumidityChar);
  client.fastrprint(F(",\"Temperature\":"));
  client.fastrprint(TemperatureChar);
  client.fastrprintln(F("}}"));
  } else {
//if unable to send data, reset cc3000, reconnect to AP, resolve DHCP, start at beginning of loop
    Serial.println (F("Connection failed! Attempting reboot"));
    cc3000.reboot();
    Serial.println(F("Lost connection! Attempting to reconnect"));
    cc3000.connectToAP(WLAN_SSID, WLAN_PASS, WLAN_SECURITY);
    Serial.println(F("Connected"));
    //Request DHCP - If not resolved in 30 seconds, try to reconnect to AP and start over
    Serial.println(F("Requesting DHCP"));
    while (!cc3000.checkDHCP()) {
      delay(500);
      Serial.print(F("."));
      dhcpTimeout ++;
      if (dhcpTimeout == 60) {
        Serial.println(F(""));
        Serial.println(F("DHCP Timeout! Reboot CC3000 and attempt to reconnect."));
        dhcpTimeout = 0;
        cc3000.reboot();
        goto beginning;  
        }
      }
    Serial.println(F(""));
    Serial.println(F("DHCP complete"));
    }
  
  Serial.println(F("----------"));
  while (client.connected()) {
    while (client.available()) {
      char c = client.read();
      Serial.print©;
    }
  }
   
  client.close();
  Serial.println(F(""));
  Serial.println(F("Closing Client"));
 
  //Two delays between loop - 10 min for normal procedure, 2 min for testing
  delay (600000);
//  delay (120000);
 
}


#36 GrowFerbi

GrowFerbi

    Newbie

  • Members
  • Pip
  • 1 posts

Posted 02 December 2015 - 12:52 PM

Hi all,

 

I'm having a similar problem. I'm using the code modified only with my project data. I'm trying to send temp = 21.05 and humidity = 21.05, just to check if is connecting properly, but I'm not being able to detect the streams on carriots website.

 

/*************************************************** 
  This is a sketch to interface a soil sensor & Carriots
  using the Adafruit CC3000 breakout board (or WiFi shield)
  
  Written by Marco Schwartz for Open Home Automation
 ****************************************************/

// Libraries
#include <Adafruit_CC3000.h>
#include <ccspi.h>
#include <SPI.h>
#include <string.h>
#include "utility/debug.h"
//#include "DHT.h"
#include<stdlib.h>
//#include <Sensirion.h>

// Define CC3000 chip pins
#define ADAFRUIT_CC3000_IRQ   3
#define ADAFRUIT_CC3000_VBAT  5
#define ADAFRUIT_CC3000_CS    10

// Soil sensor pins
const uint8_t dataPin  =  6;
const uint8_t clockPin =  7;

// Soil sensor variables
float t;
float h;
float dewpoint;

// Create soil sensor object
//Sensirion soilSensor = Sensirion(dataPin, clockPin);

// Create CC3000 instances
Adafruit_CC3000 cc3000 = Adafruit_CC3000(ADAFRUIT_CC3000_CS, ADAFRUIT_CC3000_IRQ, ADAFRUIT_CC3000_VBAT,
                                         SPI_CLOCK_DIV2); // you can change this clock speed
                         
// WLAN parameters
#define WLAN_SSID       "free_wifi"
#define WLAN_PASS       "a12b10c85a"
// Security can be WLAN_SEC_UNSEC, WLAN_SEC_WEP, WLAN_SEC_WPA or WLAN_SEC_WPA2
#define WLAN_SECURITY   WLAN_SEC_WPA2

// Carriots parameters
#define WEBSITE  "api.carriots.com"
#define API_KEY "*****"
#define DEVICE  "defaultDevice@GrowFerbi.GrowFerbi"

uint32_t ip;

void setup(void)
{
  // Initialize
  Serial.begin(115200);
  
  Serial.println(F("\nInitializing..."));
  if (!cc3000.begin())
  {
    Serial.println(F("Couldn't begin()! Check your wiring?"));
    while(1);
  }

  //Serial.print("Got out!");
 
}

void loop(void)
{
  // Connect to WiFi network
  //Serial.println("Got here!");
  cc3000.connectToAP(WLAN_SSID, WLAN_PASS, WLAN_SECURITY);
  Serial.println(F("Connected!"));
  
  /* Wait for DHCP to complete */
  Serial.println(F("Request DHCP"));
  while (!cc3000.checkDHCP())
  {
    delay(100);
  }  
 
  // Get the website IP & print it
  ip = 0;
  Serial.print(WEBSITE); Serial.print(F(" -> "));
  while (ip == 0) {
    if (! cc3000.getHostByName(WEBSITE, &ip)) {
      Serial.println(F("Couldn't resolve!"));
    }
    delay(500);
  }
  cc3000.printIPdotsRev(ip);
  
  // Get data & transform to integers
//  soilSensor.measure(&t, &h, &dewpoint);
  t = 21.05;
  h = 21.05;
  // Convert data to String
  String temperature = doubleToString(t,2);
  String humidity = doubleToString(h,2);

  //temperature = "21.05";
  //humidity = "21.05";
  // Prepare JSON for Carriots & get length
  int length = 0;

  String data = "{\"protocol\":\"v2\",\"device\":\""+String(DEVICE)+"\",\"at\":\"now\",\"data\":{\"Temperature\":"+String(temperature)+",\"Humidity\":"+String(humidity)+"}}";
  
  length = data.length();
  Serial.print("Data length");
  Serial.println(length);
  Serial.println();
  
  // Print request for debug purposes
  Serial.println("POST /streams HTTP/1.1");
  Serial.println("Host: api.carriots.com");
  Serial.println("Accept: application/json");
  Serial.println("User-Agent: Arduino-Carriots");
  Serial.println("Content-Type: application/json");
  Serial.println("carriots.apikey: " + String(API_KEY));
  Serial.println("Content-Length: " + String(length));
  Serial.print("Connection: close");
  Serial.println();
  Serial.println(data);
  
  // Send request
  
  Adafruit_CC3000_Client client = cc3000.connectTCP(ip, 80);
  if (client.connected()) {
    Serial.println("Connected!");
    client.println("POST /streams HTTP/1.1");
    Serial.println("1");
    client.println("Host: api.carriots.com");
    Serial.println("2");

    client.println("Accept: application/json");
    Serial.println("3");
    client.println("User-Agent: Arduino-Carriots");
    Serial.println("4");
    client.println("Content-Type: application/json");
    Serial.println("5");
    client.println("carriots.apikey: " + String(API_KEY));
    Serial.println("6");
    client.println("Content-Length: " + String(length));
    Serial.println("7");
    client.println("Connection: close");
    Serial.println("8");
    client.println();
    Serial.println("9");
    client.println(data);
    Serial.println("Sent!");
    
  } else {
    Serial.println(F("Connection failed"));    
    return;
  }
  
  Serial.println(F("-------------------------------------"));
  while (client.connected()) {
    while (client.available()) {
      char c = client.read();
      Serial.print(c);
    }
  }
  client.close();
  Serial.println(F("-------------------------------------"));
  
  Serial.println(F("\n\nDisconnecting"));
  cc3000.disconnect();
  
  // Wait 10 seconds until next update
  delay(10000);
   
}

// Convert double to string
String doubleToString(float input,int decimalPlaces){
  if(decimalPlaces!=0){
    String string = String((int)(input*pow(10,decimalPlaces)));
      if(abs(input)<1){
        if(input>0)
          string = "0"+string;
        else if(input<0)
          string = string.substring(0,1)+"0"+string.substring(1);
      }
      return string.substring(0,string.length()-decimalPlaces)+"."+string.substring(string.length()-decimalPlaces);
    }
  else {
    return String((int)input);
  }
}

I also used some Serial prints (with numbers) to check where is hanging, and the last number printed is '9', meaning that it's hanging on "client.println(data);"

 

I was unsure if I was suppose to use this

 

#define DEVICE "defaultDevice@GrowFerbi.GrowFerbi"

 
or this:
#define DEVICE  "defaultDevice@GrowFerbi.GrowFerbi"
The Serial output with the latter is different:
 
 
Initializing...
Connected!
Request DHCP
api.carriots.com -> 82.223.244.60Data length103
 
POST /streams HTTP/1.1
Host: api.carriots.com
Accept: application/json
User-Agent: Arduino-Carriots
Content-Type: application/json
carriots.apikey: 9eaf5fdd68228d0f60a8deb18315fa22e7993de93825de0ed7d2730dac737fcd
Content-Length: 103
Connection: close
{"protocol":"v2","device":"defaultDevice@GrowFerbi","at":"now","data":{"Temperature":0.,"Humidity":0.}}
Connected!
9
Sent!
-------------------------------------
HTTP/1.1 401 Unauthorized
Date: Wed, 02 Dec 2015 12:47:58 GMT
Content-Type: application/vnd.carriots.api.v2+json
Content-Length: 40
Connection: close
Server: Carriots REST API
Access-Control-Allow-Origin: *
Access-Control-Allow-Credentials: true
Access-Control-Expose-Headers: Date,Server,Allow,Connection,Content-Length,Content-Type,Cache-Control
 
{ "response": "Not supported payload." }-------------------------------------
 
Disconnecting
 
What am I missing? 
 
Thanks!
 


#37 whittaker9

whittaker9

    Newbie

  • Members
  • Pip
  • 8 posts

Posted 04 December 2015 - 02:04 AM

I see that the Content-Length is 103 characters from your data stream. Try to get it below below 100 characters. See my post #33 above.

#38 erios_arduino

erios_arduino

    Newbie

  • Members
  • Pip
  • 3 posts

Posted 23 February 2016 - 04:35 PM

for those working with arduino. The problem solved by placing a "+1" in the lengt line

Serial.println("Content-Length: " + String(length+1));

 

and leaving the "client line intact

client.println("Content-Length: " + String(length));

 

i dont know why. This is so crazy



#39 uiucphoenix

uiucphoenix

    Newbie

  • Members
  • Pip
  • 1 posts

Posted 06 April 2016 - 08:08 PM

Hi all-

 

Little to no Arduino coding experience here, so please be nice!

 

Has anyone adapted the sketch to work on an Arduino Leonardo with Wifi Shield?

 

 

 

Thank you!



#40 billwatson

billwatson

    Member

  • Members
  • PipPip
  • 14 posts

Posted 10 April 2016 - 09:50 PM

yooo Erios,

 

Do you mean this a solution to the program hanging?

Can't wait to try this out.

 

thanks for sharing!






2 user(s) are reading this topic

0 members, 2 guests, 0 anonymous users