ULTRASONIC + ARDUINO + CC3000

Post here about your Arduino projects, get help - for Adafruit customers!

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
alex_mcc
 
Posts: 6
Joined: Sun Jul 20, 2014 8:53 am

ULTRASONIC + ARDUINO + CC3000

Post by alex_mcc »

Total noob here, but again, nice knowing everyone here.

I'm trying to hook up an ultrasonic sensor (ping))) from parallax) to an arduino, and a cc3000 module was used so that the data i take (range in centimetre) could be transmitted through it to xively.

I have attached my connection here:
CONNECTIONS
CONNECTIONS
ULTRA_bb.jpg (774.58 KiB) Viewed 313 times
The code i'm using is as the following. What i did was simply to modify the tutorial code from adafruit. Please refer to the following:

Code: Select all

// Libraries
#include <Adafruit_CC3000.h>
#include <ccspi.h>
#include <SPI.h>
#include <string.h>
#include <stdlib.h>

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

// 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       "Mynetwork"
#define WLAN_PASS       "Mypassword"
#define WLAN_SECURITY   WLAN_SEC_WPA2

// Xively parameters
#define WEBSITE  "https://api.xively.com"
#define API_key  "myAPI"
#define feedID  "myFeedID"

uint32_t ip;
const int pingPin = 6;
unsigned int duration, centimeter;

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

void loop() {
  
  //Sonic Sensor
  
  pinMode(pingPin, OUTPUT);          // Set pin to OUTPUT
  digitalWrite(pingPin, LOW);        // Ensure pin is low
  delayMicroseconds(2);
  digitalWrite(pingPin, HIGH);       // Start ranging
  delayMicroseconds(5);              //   with 5 microsecond burst
  digitalWrite(pingPin, LOW);        // End ranging
  pinMode(pingPin, INPUT);           // Set pin to INPUT
  duration = pulseIn(pingPin, HIGH); // Read echo pulse
  centimeter = duration / 29 / 2;        // Convert to cm
  delay(200);		             // Short delay
  
  // 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);
  }  

  // Set the website IP
uint32_t ip = cc3000.IP2U32(216,52,233,120);
cc3000.printIPdotsRev(ip);
  
   // Prepare JSON for Xively & get length
int length = 0;
String data = "";
data = data + "\n" + "{\"version\":\"1.0.0\",\"datastreams\" : {\"id\" : \"Range\",\"current_value\" : \"" + String(centimeter) + "\"},}";

length = data.length();
  Serial.print("Data length");
  Serial.println(length);
  Serial.println();
  
  // Print request for debug purposes
  Serial.print("PUT /v2/feeds/");
  Serial.print(feedID);
  Serial.println(".json HTTP/1.0");
  Serial.println("Host: api.xively.com");
  Serial.print("X-ApiKey: ");
  Serial.println(API_key);
  Serial.print("Content-Length: ");
  Serial.println(length, DEC);
  Serial.print("Connection: close");
  Serial.println();
  Serial.print(data);
  Serial.println();
  
  // Send request
  Adafruit_CC3000_Client client = cc3000.connectTCP(ip, 80);
  if (client.connected()) {
    Serial.println("Connected!");
    client.println("PUT /v2/feeds/" + String(feedID) + ".json HTTP/1.0");
    client.println("Host: api.xively.com");
    client.println("X-ApiKey: " + String(API_key));
    client.println("Content-Length: " + String(length));
    client.print("Connection: close");
    client.println();
    client.print(data);
    client.println();
  } 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);
  
}
Connection wise seems to be ok, at least it could get connected to my wifi network, the pain lies in my data string (correct me if i'm wrong) because i get the following reply from my serial monitor.

Initializing...
Connected!
Request DHCP
216.52.233.120Data length75

PUT /v2/feeds/myFeedID.json HTTP/1.0
Host: api.xively.com
X-ApiKey: MyAPIKey
Content-Length: 75
Connection: close

{"version":"1.0.0","datastreams" : {"id" : "Range","current_value" : "4"}}
Connected!
-------------------------------------
HTTP/1.1 400 Bad Request
Date: Sun, 20 Jul 2014 12:59:50 GMT
Content-Type: application/json; charset=utf-8
Content-Length: 73
Connection: keep-alive
X-Request-Id: c5949f5ecca8cba28656db81b0b929466c99795b

{"title":"JSON Parser Error","errors":"\"datastreams\" must be an array"}-------------------------------------


Disconnecting


I would appreciate any help in this. Thanks

alex_mcc
 
Posts: 6
Joined: Sun Jul 20, 2014 8:53 am

Re: ULTRASONIC + ARDUINO + CC3000

Post by alex_mcc »

anyone here that could help me out?

User avatar
adafruit_support_bill
 
Posts: 88093
Joined: Sat Feb 07, 2009 10:11 am

Re: ULTRASONIC + ARDUINO + CC3000

Post by adafruit_support_bill »

Please don't double post. You have another identical thread here: http://forums.adafruit.com/viewtopic.php?f=8&t=57233

Locked
Please be positive and constructive with your questions and comments.

Return to “Arduino”