Jump to content









Photo

Help sending data and status to Carriots, Arduino UNO CC3000 wifi

Arduino CC3000 wifi temperature humidity DHT

  • Please log in to reply
12 replies to this topic

#1 ascarygnome

ascarygnome

    Newbie

  • Members
  • Pip
  • 1 posts

Posted 23 February 2015 - 08:17 PM

I am extremely new to all this iot stuff and online coding languages, strings, JSON, etc.... I've pieced this program together by copying and pasting.

 

I'm able to send data to Carriots: temperature, humidity, analog readings. However i would like to send ON or OFF, Low or Full status instead of the analog readings.

I've tried sending "ON" as a string (as i handle strings in my program) but it doesnt work. 

Any help, suggestions, anything, would be greatly appreciated.


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


// Define CC3000 chip pins
#define ADAFRUIT_CC3000_IRQ   3
#define ADAFRUIT_CC3000_VBAT  5
#define ADAFRUIT_CC3000_CS    10
// Temperature and humidity sensor: Pin 2
#define DHTPIN 2  
#define DHTTYPE DHT11 
DHT dht(DHTPIN, DHTTYPE);
// 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       "Matias"
#define WLAN_PASS       "yourPassword"
// Security can be WLAN_SEC_UNSEC, WLAN_SEC_WEP, WLAN_SEC_WPA or WLAN_SEC_WPA2
#define WLAN_SECURITY   WLAN_SEC_UNSEC

// Carriots parameters
#define WEBSITE  "api.carriots.com"
#define API_KEY "310ae6892cd96723b3862fb4b143faa423719a2ff19a0570f7b11357abf0c302"
#define DEVICE  "Garden@ascarygnome.ascarygnome"

uint32_t ip;
int vegPhoto = A0;
int bloomPhoto = A1;
int earlyBloomFill = A2;
int lateBloomFill = A3;
int vegFill = A4;


void setup(void)
{
  // Initialize
  Serial.begin(115200);
  dht.begin();
  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);
  
  float t = dht.readTemperature();
  float h = dht.readHumidity();
  float v = analogRead(vegPhoto);
  float w = analogRead(bloomPhoto);
  float x = analogRead(earlyBloomFill);
  float y = analogRead(lateBloomFill);
  float z = analogRead(vegFill);
  
  // Convert data to String
  String temperature = doubleToString(t,0);
  String humidity = doubleToString(h,0);
  String vegPhotoresistor = doubleToString(v,0);
  String bloomPhotoresistor = doubleToString(w,0);
  String earlyBloomFillLevel = doubleToString(x,0);
  String lateBloomFillLevel = doubleToString(y,0);
  String vegFillLevel = doubleToString(z,0);
 
  // Prepare JSON for Carriots & get length
  int length = 0;

  String data = "{\"protocol\":\"v2\",\"device\":\""+String(DEVICE)+"\",\"at\":\"now\",\"data\":{\"T\":"+String(temperature)+",\"H\":"+String(humidity)+",\"VL\":"+String(vegPhotoresistor)+",\"BL\":"+String(bloomPhotoresistor)+",\"EBF\":"+String(earlyBloomFillLevel)+",\"LBF\":"+String(lateBloomFillLevel)+",\"VF\":"+String(vegFillLevel)+"}}";
  
  length = data.length();
  
//  // Print request for debug purposes
//  Serial.println();
//  Serial.println("Data length");
//  Serial.println(length);
//  Serial.println();
//  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);
  

      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(String(length));
  client.fastrprintln(F("Connection: close"));
  client.println();
  client.fastrprint(F("{\"protocol\":\"v2\",\"device\":\""));
  client.fastrprint(DEVICE);
  client.fastrprint(F("\",\"at\":\"now\",\"data\":{\"T\":"));
  client.print(String(temperature));
  client.fastrprint(F(",\"H\":"));
  client.print(String(humidity));
  client.fastrprint(F(",\"VL\":"));
  client.print(String(vegPhotoresistor));
  client.fastrprint(F(",\"BL\":"));
  client.print(String(bloomPhotoresistor));
  client.fastrprint(F(",\"EBF\":"));
  client.print(String(earlyBloomFillLevel));
  client.fastrprint(F(",\"LBF\":"));
  client.print(String(lateBloomFillLevel));
  client.fastrprint(F(",\"VF\":"));
  client.print(String(vegFillLevel));
  client.fastrprintln(F("}}"));
  } 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 1 minute until next update
  delay(60000);
   
}

// 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);
  }
}

Also, in my JSON String data my labels are H,T,VL, BFL etc and would like Humidity, Temperature, Veg Light etc. but when i try these longer names my length comes back as 0 and Carriots won't accept it.... not sure if its my arduino or CC3000 capabilities or if its the way i'm handling all this.

 

Thanks, Anthony



#2 whittaker9

whittaker9

    Newbie

  • Members
  • Pip
  • 8 posts

Posted 09 April 2015 - 01:48 AM

Anthony,

 

I've been running your code for a few days.  The program locks up after it's been running for about a day.  The program stops after printing "Connection failed" . Using the watchdog timer as you suggested seems to be a good way to help the program recover. 

 

I see that there is a  "return" statement after the "Serial.println" statement. I would think that this would cause the program to exit the loop{} and end the program.  Wouldn't it be better to let the loop continue?

 

 

Glen



#3 Florafelt

Florafelt

    Newbie

  • Members
  • Pip
  • 2 posts

Posted 10 June 2015 - 07:38 AM

I'm doing the same thing using Adrino with CC3300 Wifi.

 

Serial Monitor shows activity.

 

But the data doesn't show in my Carriots account feed.

 

Please help...

 

 

 

Serial Monitor:

 

Initializing...
Connected!
Request DHCP
api.carriots.com -> 82.223.244.60Data length116
 
POST /streams HTTP/1.1
Host: api.carriots.com
Accept: application/json
User-Agent: Arduino-Carriots
Content-Type: application/json
carriots.apikey: "myapikey"
Content-Length: 116
Connection: close
{"protocol":"v2","device":"SoilSensor@Florafelt.Florafelt","at":"now","data":{"Temperature":23.83,"Humidity":59.00}}
Connected!
 
 
 

 

Using the following code.

 

/*************************************************** 
  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       "host"
#define WLAN_PASS       "password"
// 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 "my api"
#define DEVICE  "my sensor"
 
uint32_t ip;
 
void setup(void)
{
  // Initialize
  Serial.begin(9600);
  
  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\":\"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");
    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©;
    }
  }
  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);
  }
}


#4 whittaker9

whittaker9

    Newbie

  • Members
  • Pip
  • 8 posts

Posted 10 June 2015 - 12:37 PM

Are you really using an Adafruit CC3300? I'll assume that you are using the correct keys and device name in your sketch.

 

I suspect that the Carriots server is not accepting the payload from your device.  The Adafruit CC3000 doesn't always work well with the println statement.  Try changing the "client.println" statements to  "client.fastrprintln" as shown in sample code below.

 

Also take a look at this forum post:

 

http://forum.carriot...-modules/page-2

 

The "client.println(data)" statement may not be working. It's best to send your data in several "client.fastrprint" statements to keep each message under 90 bytes.

 

 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(String(length));
  client.fastrprintln(F("Connection: close"));
  client.println();
  client.fastrprint(F("{\"protocol\":\"v2\",\"device\":\""));
  client.fastrprint(DEVICE);
  client.fastrprint(F("\",\"at\":\"now\",\"data\":{\"T\":"));
  client.print(String(temperature));
  client.fastrprint(F(",\"H\":"));
  client.print(String(humidity));
  client.fastrprint(F(",\"VL\":"));
  client.print(String(vegPhotoresistor));
  client.fastrprint(F(",\"BL\":"));
  client.print(String(bloomPhotoresistor));
  client.fastrprint(F(",\"EBF\":"));
  client.print(String(earlyBloomFillLevel));
  client.fastrprint(F(",\"LBF\":"));
  client.print(String(lateBloomFillLevel));
  client.fastrprint(F(",\"VF\":"));
  client.print(String(vegFillLevel));
  client.fastrprintln(F("}}"));
  } 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 1 minute until next update
  delay(60000);
   
}


#5 whittaker9

whittaker9

    Newbie

  • Members
  • Pip
  • 8 posts

Posted 10 June 2015 - 12:54 PM

I forgot to mention that it's not a good practice to post your API_KEY and DEVICE names on public forums.



#6 Florafelt

Florafelt

    Newbie

  • Members
  • Pip
  • 2 posts

Posted 10 June 2015 - 03:56 PM

It's working!

 

Thanks for your help.

 

Yes Adafruit CC3000.

 

The fix that made it work was as you mentioned change the Name "Temperature" to "T".

 

String data = "{\"protocol\":\"v2\",\"device\":\""+String(DEVICE)+"\",\"at\":\"now\",\"data\":{\"T\":"+String(temperature)+",\"H\":"+String(humidity)+"}}";
  

Did not have to use client.fastprint as you mentioned.

 

Here is the complete code that worked for me:

 

 

/*************************************************** 
  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       "my wifi name"
#define WLAN_PASS       "my wifi password"
// 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 "my api key"
#define DEVICE  "my sensor name"
 
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\":\"v2\",\"device\":\""+String(DEVICE)+"\",\"at\":\"now\",\"data\":{\"T\":"+String(temperature)+",\"H\":"+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©;
    }
  }
  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);
  }
}


#7 whittaker9

whittaker9

    Newbie

  • Members
  • Pip
  • 8 posts

Posted 10 June 2015 - 05:19 PM

By changing the names "Temperature" to "T" and "Humidity" to "H", you shortened your payload length. The original length was 116 characters. Now it is 99 characters. There is a good chance that you were exceeding the maxium length that the client.print() function or the CC3000 board could handle.



#8 jpastor

jpastor

    Development leader

  • Administrators
  • 159 posts

Posted 11 June 2015 - 06:27 AM

Good advice!!!

Thanks for the contribution
 
 
 


#9 whittaker9

whittaker9

    Newbie

  • Members
  • Pip
  • 8 posts

Posted 11 June 2015 - 12:05 PM

What is the maximum payload length that can be accepted by the Carriots server and where is this documented?



#10 sagowtham14

sagowtham14

    Newbie

  • Members
  • Pip
  • 1 posts

Posted 15 September 2015 - 04:34 PM

I have a similar kind of application. But the data stream in my account is empty. How do I set up Carriots to receive analog sensor readings from 4 sensors? PLEASE HELP. 

 

I get no response from Carriots side. The serial monitor reads value and prints it. 



//including all libraries
#include <Adafruit_CC3000.h>
#include <ccspi.h>
#include <SPI.h>
#include <string.h>
#include <stdlib.h>
#include "utility/debug.h"

//defining global variables
#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); 

                         
//defining global pins
const uint8_t hpPin = 0; 
const uint8_t vibPin = 2;
const uint8_t presPin = 4;
const uint8_t tempPin = 5;
float reading11, reading22, reading33, reading44;


//WLAN parameters
#define WLAN_SSID "WIFI nietwork"
#define WLAN_PASS "password"
#define WLAN_SECURITY WLAN_SEC_WPA2

//Carriots parameters
#define WEBSITE "api.carriots.com"
#define API_KEY "full privilege api key "
#define DEVICE "my device ID"

uint32_t ip;


void setup() 
{
 pinMode(hpPin,INPUT);
 pinMode(vibPin,INPUT);
 pinMode(presPin,INPUT);
 pinMode(tempPin, INPUT);
 Serial.begin(115200);
 Serial.println(F("\n Initializing..."));
 if (!cc3000.begin())
   {
     Serial.println(F("Couldnt beign! Check connections"));
     while(1);
   }
}  

void loop() 
{
//Connecting to Wifi network
Serial.print(F("\nAttempting to connect to ")); Serial.println(WLAN_SSID);
if (!cc3000.connectToAP(WLAN_SSID, WLAN_PASS, WLAN_SECURITY)) 
{
  Serial.println(F("Failed!"));
  while(1);
}
Serial.println(F("Connected!"));

//Waiting for DHCP to complete
Serial.println(F("Request DHCP"));
while(!cc3000.checkDHCP())
{
  delay(100);
}

//if completed print details
ip = 0;
Serial.print(WEBSITE);
Serial.print(F("->"));
while (ip ==0)
{
  if (! cc3000.getHostByName(WEBSITE, &ip))
  {
    Serial.println(F("Couldnt resolve"));
  }
  delay(500);
}

cc3000.printIPdotsRev(ip);
//read the values of sensors
readSensors();

// Convert Floats to Strings.

char TempString[32];  //  A temporary character array to hold data.
  // dtostrf( [Float variable] , [Minimum SizeBeforePoint] , [sizeAfterPoint] , [WhereToStoreIt] )
dtostrf(reading44,2,2,TempString);
String T =  String(TempString);
dtostrf(reading33,2,2,TempString);
String P =  String(TempString);
dtostrf(reading22,2,2,TempString);
String V = String(TempString);
dtostrf(reading11,2,2,TempString);
String H = String(TempString);


//after converting to string, prepare JSON for Carriots & get length
int length = 0;
String bigData = "{\"protocol\":\"v2\",\"device\":\""+String(DEVICE)+"\",\"at\":\"now\",\"data\":{\"Ho\": "+String(H)+", \"Vi\": "+String(V)+",\"Pr\": "+String(P)+",\"Te\": "+String(T)+"}}";
length = bigData.length();
Serial.print("Data Length :");
Serial.println(length);
Serial.println();

//Print all the details for confirmation 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 api key: " + String(API_KEY));
Serial.println("Content length: " + String(length));
Serial.println("Connection: Close");
Serial.println();
Serial.println(T);
Serial.println(P);Serial.println(V);Serial.println(H);





//connect cc3000 to internet via client
Adafruit_CC3000_Client client = cc3000.connectTCP(ip,80);

if (client.connected())
{
  Serial.println("Connecte1d!");
  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 api key: " + String(API_KEY));
  client.println("Content length: " + String(length));
  client.print("Connection: Close");
  client.println();
  client.print(F("{\"protocol\":\"v2\",\"device\":\""));
  client.print(DEVICE);
  client.print(F("\",\"at\":\"now\",\"data\":{\"Horsepower\":"));
  client.print(H);
  client.print(F(",\"Vibration\":"));
  client.print(V);
  client.print(F(",\"Pressure\":"));
  client.print(P);
  client.print(F(",\"Temperature\":"));
  client.print(T);
  client.println(F("}}"));

}
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("\n-------------------------------------"));
  Serial.println(F("Disconnecting\n"));
  cc3000.disconnect();
//wait this time for next update
delay(1000);

}



//readsensors function
void readSensors()
{
  int reading1 = analogRead(hpPin); 
  int reading2 = analogRead(vibPin);
  int reading3 = analogRead(presPin);
  int reading4 = analogRead(tempPin);

  float reading11 = reading1*(5.0/1023.0);
  float reading22 = reading2*(5.0/1023.0);
  float reading33 = reading3*(5.0/1023.0);
  float reading44 = reading4*(5.0/1023.0);
}


#11 NeronZee

NeronZee

    Newbie

  • Members
  • Pip
  • 1 posts

Posted 06 November 2015 - 04:29 PM

I have gone through your code and it is looking that there is a  "return" statement after the "Serial.println" statement. I would think that this would cause the program to exit the loop{} and end the program. 

You don't think that it ll be better to let the loop continue?

Also using the watchdog timer as you suggested seems to be a good way to help the program recover. 

 

pcb assembling



#12 billwatson

billwatson

    Member

  • Members
  • PipPip
  • 14 posts

Posted 10 April 2016 - 09:37 PM

Hi NeronZee, another noob here.

 

 I'm doing a similar project (arduino & cc3000 to carriots) and have had the same issue with the program hanging.  I haven't had any success with watchdog and as a workaround have switched to a mega and used the uno connected to a relay to switch the supply to the mega on and off every half and hour.  I will change to a cheaper Tiny controller so i can get my uno back for other projects.

 

hope this helps.

 

Please let me know if you find a solution to the hanging!



#13 TabbyLevan

TabbyLevan

    Newbie

  • Members
  • Pip
  • 1 posts

Posted 21 July 2016 - 05:12 PM

By changing the names "Temperature" to "T" and "Humidity" to "H", you shortened your payload length. The original length was 116 characters. Now it is 99 characters. There is a good chance that you were exceeding the maxium length that the client.print() function or the CC3000 board could handle.By changing the names "Temperature" to "T" and "Humidity" to "H", you shortened your payload length. The original length was 116 characters. Now it is 99 characters. There is a good chance that you were exceeding the maxium length that the client.print() function or the CC3000 board could handle.







Also tagged with one or more of these keywords: Arduino, CC3000, wifi, temperature, humidity, DHT

1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users