Jump to content









Photo

Connect to Carriots using Arduino Uno and CC3000

wifi cc3000 arduino

  • Please log in to reply
1 reply to this topic

#1 plantino

plantino

    Newbie

  • Members
  • Pip
  • 4 posts

Posted 26 February 2015 - 05:45 PM

Hey Folks (Hey Carriots-Team)

 

I am trying to connect to carriots with an Arduino Uno and a cc3000 Wifi-Breakout Shield.

Unfortunately i cannot connect to carriots (Connection failed!).

 

The CC3000-Shield is Working, i tested that multiple Times. Would be great if anyone can tell me what is wrong with my Sketch:

 


#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 A5  
#define DHTTYPE DHT22 
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       "Pretty Fly for a Wifi"
#define WLAN_PASS       "luk14pr3sid3nt"
// 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 "55f57d9d01529fca16cec37c80112af11613c5af9cb3e808815cf818ec9042bf"
#define DEVICE  "test@plantino.plantino"
uint32_t ip;


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 = 80;
  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();
  
  
  // Convert data to String
  String temperature = doubleToString(t,0);
  String humidity = doubleToString(h,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)+"}}";
  
  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.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(1000);
   
}

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

 

 

And this is what i see on my serial monitor:

 

Connected!
Request DHCP
api.carriots.com -> 0.0.0.80
Data length
85

POST /streams HTTP/1.1
Host: api.carriots.com
Accept: application/json
User-Agent: Arduino-Carriots
Content-Type: application/json
carriots.apikey: 55f57d9d01529fca16cec37c80112af11613c5af9cb3e808815cf818ec9042bf
Content-Length: 85
Connection: close
{"protocol":"v2","device":"test@plantino.plantino","at":"now","data":{"T":22,"H":57}}
Connection failed

Thank you in Advance!



#2 plantino

plantino

    Newbie

  • Members
  • Pip
  • 4 posts

Posted 27 February 2015 - 06:40 AM

I solved the problem. There was a misspelling in Line 63. ip = 80; should be ip = 0; (Because of the following while (ip == 0)) Works all fine now!





Also tagged with one or more of these keywords: wifi, cc3000, arduino

2 user(s) are reading this topic

0 members, 2 guests, 0 anonymous users