Arduino Adafruit project: Uno + Xively + CC3000 + digital lu
Moderators: adafruit_support_bill, adafruit

Arduino Adafruit project: Uno + Xively + CC3000 + digital lu

by cretin on Wed Oct 30, 2013 12:47 am

Hey all here is my situation

When i run the following code i get up to initializing the connection with xively, but afterwards i get an error message that reads:

Code: Select all | TOGGLE FULL SIZE
<html>
<head><title>400 Bad Request</title></head>
<body bgcolor="white">
<center><h1>400 Bad Request</h1></center>
<hr><center>nginx/1.1.19</center>
</body>
</html>



I have tried many different things, but do keep in mind i have to modify the original project so that i could integrate my lux sensor instead. I am not sure what nginx means, and I keep getting that error over and over. Any help? Please find my code below

Please let me know if you have any ideas, i have just started with arduino today, and it's been a pain in the rear!!


Code: Select all | TOGGLE FULL SIZE
/***************************************************
  This is a sketch to use the CC3000 WiFi chip & Xively
 
  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 <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_TSL2561.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       "myssid" //i edited out the real ones
#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

// Xively parameters
#define WEBSITE  "api.xively.com"
#define API_key  "12345" //i edited out the real one
#define feedID  "123456" //i edited out the real one

uint32_t ip;


Adafruit_TSL2561 tsl = Adafruit_TSL2561(TSL2561_ADDR_FLOAT, 12345);

/**************************************************************************/
/*
    Displays some basic information on this sensor from the unified
    sensor API sensor_t type (see Adafruit_Sensor for more information)
*/
/**************************************************************************/
void displaySensorDetails(void)
{
  sensor_t sensor;
  tsl.getSensor(&sensor);
  Serial.println("------------------------------------");
  Serial.print  ("Sensor:       "); Serial.println(sensor.name);
  Serial.print  ("Driver Ver:   "); Serial.println(sensor.version);
  Serial.print  ("Unique ID:    "); Serial.println(sensor.sensor_id);
  Serial.print  ("Max Value:    "); Serial.print(sensor.max_value); Serial.println(" lux");
  Serial.print  ("Min Value:    "); Serial.print(sensor.min_value); Serial.println(" lux");
  Serial.print  ("Resolution:   "); Serial.print(sensor.resolution); Serial.println(" lux"); 
  Serial.println("------------------------------------");
  Serial.println("");
  delay(500);
}

/**************************************************************************/
/*
    Configures the gain and integration time for the TSL2561
*/
/**************************************************************************/
void configureSensor(void)
{
  /* You can also manually set the gain or enable auto-gain support */
  // tsl.setGain(TSL2561_GAIN_1X);      /* No gain ... use in bright light to avoid sensor saturation */
  // tsl.setGain(TSL2561_GAIN_16X);     /* 16x gain ... use in low light to boost sensitivity */
  tsl.enableAutoGain(true);          /* Auto-gain ... switches automatically between 1x and 16x */
 
  /* Changing the integration time gives you better sensor resolution (402ms = 16-bit data) */
  tsl.setIntegrationTime(TSL2561_INTEGRATIONTIME_13MS);      /* fast but low resolution */
  // tsl.setIntegrationTime(TSL2561_INTEGRATIONTIME_101MS);  /* medium resolution and speed   */
  // tsl.setIntegrationTime(TSL2561_INTEGRATIONTIME_402MS);  /* 16-bit data but slowest conversions */

  /* Update these values depending on what you've set above! */ 
  Serial.println("------------------------------------");
  Serial.print  ("Gain:         "); Serial.println("Auto");
  Serial.print  ("Timing:       "); Serial.println("13 ms");
  Serial.println("------------------------------------");
}

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);
  }
 
     /* Initialise the sensor */
  if(!tsl.begin())
  {
    /* There was a problem detecting the ADXL345 ... check your connections */
    Serial.print("Ooops, no TSL2561 detected ... Check your wiring or I2C ADDR!");
    while(1);
  }
 
  /* Display some basic information on this sensor */
  displaySensorDetails();
 
  /* Setup the sensor gain and integration time */
  configureSensor();
 
  /* We're ready to go! */
  Serial.println("");
}

void loop(void)
{
    int light = 0;
   sensors_event_t event;
   
  // 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);
  tsl.getEvent(&event);
  // Get data & transform to integers
 
 //uint16_t broadband = 0;
// uint16_t infrared = 0;
 
  light = event.light;
// getLuminosity (&broadband, &infrared);
 
 
  // Prepare JSON for Xively & get length
  int length = 0;

  String data = "";
  data = data + "\n" + "{\"version\":\"1.0.0\",\"datastreams\" : [ {\"id\" : \"Temperature\",\"current_value\" : \"" + String(light) + "\"},"
  + "{\"id\" : \"Humidity\",\"current_value\" : \"" + String(light) + "\"}]}";
  //data = data + "\n" + "{\"version\":\"1.0.0\",\"datastreams\" : [ {\"id\" : \"Temperature\",\"current_value\" : \"" + String(light) + "\"}]}""\"]},";
 
 
  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: http://www.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: http://www.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);
 
}
cretin
 
Posts: 6
Joined: Wed Oct 30, 2013 12:42 am

Re: Arduino Adafruit project: Uno + Xively + CC3000 + digita

by adafruit_support_mike on Wed Oct 30, 2013 1:32 am

ngix is a webserver. It's standard practice for servers to announce their names when something goes wrong.. it helps techs find lurking bugs.

In this case, the bug appears to be in this line:

Code: Select all | TOGGLE FULL SIZE
    client.println("Host: http://www.api.xively.com");

Punching that URL into the location bar of my browser says there's no such machine as 'www.api.xively.com'.
When you void a product warranty, you give up your right to sue the manufacturer if something goes wrong and accept full responsibility for whatever happens next. And then you truly own the product.
User avatar
adafruit_support_mike
 
Posts: 9192
Joined: Thu Feb 11, 2010 1:51 pm

Re: Arduino Adafruit project: Uno + Xively + CC3000 + digita

by cretin on Wed Oct 30, 2013 10:39 am

Thanks Mike!

I knew about that already...but i guess i looked over it, thank you very much for pointing that out.

Now, i am on another problem. Here are the last two lines of my serial monitor

Connect to 64.94.18.120:80
Connected!
-------------------------------------

So it seems like i connected successfully. However, I get no readings through xively, my feed page does not show anything for my sensor, and I am kind of unsure as to how to call the local libraries for the light sensor to get lux measurements. Do you have any help/suggestions?

Btw, the only instance of light measurement is with the call event.light, located in the loop function. I assign this call to a variable and then send that variable through the data stream. I am not sure if that is even correct, but it does compile and run. However as mentioned, i still see nothing on xively.
cretin
 
Posts: 6
Joined: Wed Oct 30, 2013 12:42 am

Re: Arduino Adafruit project: Uno + Xively + CC3000 + digita

by adafruit_support_mike on Wed Oct 30, 2013 1:28 pm

You should get a field of diagnostic output just before the lines you posted.. the PUT request, the data, etc. Please post that and we'll see if something is wonky in the data format.
When you void a product warranty, you give up your right to sue the manufacturer if something goes wrong and accept full responsibility for whatever happens next. And then you truly own the product.
User avatar
adafruit_support_mike
 
Posts: 9192
Joined: Thu Feb 11, 2010 1:51 pm

Re: Arduino Adafruit project: Uno + Xively + CC3000 + digita

by cretin on Wed Oct 30, 2013 5:44 pm

This highlights the area of the code that processes the serial output and communication. I don't know where to find the definition of " Adafruit_CC3000_Client client " which i am sure is a function call for the cc3000. Aside from that, as you can see the system works up to the point where i see "connected" out of the serial port, but nothing else from there.

To be honest, i need help understanding the digital lux sensors function calls. I want to call the lux sensor (adafruit product TSL2561) to take in lux readings in real time and then output it to xively as a sensing device. That is why at the beginning of the code i have "event.light" assigned to light, so i can transmit the value of light to xively, but i am not sure if that is even the right function call, because it's not working at the moment anyway.

Code: Select all | TOGGLE FULL SIZE
int light;

light = event.light 
  // Prepare JSON for Xively & get length
  int length = 0;

  String data = "";
  data = data + "\n" + "{\"version\":\"1.0.0\",\"datastreams\" : [ {\"id\" : \"Temperature\",\"current_value\" : \"" + String(light) + "\"},"
  + "{\"id\" : \"Humidity\",\"current_value\" : \"" + String(light) + "\"}]}";
 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: http://www.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: http://www.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);
    }
  }
cretin
 
Posts: 6
Joined: Wed Oct 30, 2013 12:42 am

Re: Arduino Adafruit project: Uno + Xively + CC3000 + digita

by adafruit_support_mike on Thu Oct 31, 2013 1:26 am

I'm familiar with that chunk of code, but was asking to see the output printed to the serial monitor while the code is running. It's possible that something is mis-formatted in your request, and it's easier to diagnose issues like that if you can see the actual output.
When you void a product warranty, you give up your right to sue the manufacturer if something goes wrong and accept full responsibility for whatever happens next. And then you truly own the product.
User avatar
adafruit_support_mike
 
Posts: 9192
Joined: Thu Feb 11, 2010 1:51 pm

Re: Arduino Adafruit project: Uno + Xively + CC3000 + digita

by cretin on Thu Oct 31, 2013 5:56 am

Hey Mike here is a snapshot of my serial monitor


Initializing...
------------------------------------
Sensor: TSL2561
Driver Ver: 1
Unique ID: 12345
Max Value: 17000.00 lux
Min Value: 0.00 lux
Resolution: 1.00 lux
------------------------------------

------------------------------------
Gain: Auto
Timing: 13 ms
------------------------------------

Started AP/SSID scan




Connecting to Filfill...Waiting to connect...Connected!
Request DHCP
api.xively.com -> 64.94.18.120Data length4

PUT /v2/feeds/123456(replaced with dummy id).json HTTP/1.0
Host: http://www.api.xively.com
X-ApiKey: 55555(replaced with dummy api key)
Content-Length: 4
Connection: close
"}]}


Connect to 64.94.18.120:80
Connected!
cretin
 
Posts: 6
Joined: Wed Oct 30, 2013 12:42 am

Re: Arduino Adafruit project: Uno + Xively + CC3000 + digita

by adafruit_support_mike on Thu Oct 31, 2013 6:07 pm

Aha! It took me a few minutes to spot the detail on that one..

The HTTP/1.0 protocol only defines HEAD, GET, and POST request methods. The PUT method is defined for HTTP/1.1

Try replacing 'HTTP/1.0' with 'HTTP/1.1' and see if that works better.
When you void a product warranty, you give up your right to sue the manufacturer if something goes wrong and accept full responsibility for whatever happens next. And then you truly own the product.
User avatar
adafruit_support_mike
 
Posts: 9192
Joined: Thu Feb 11, 2010 1:51 pm

Re: Arduino Adafruit project: Uno + Xively + CC3000 + digita

by cretin on Sun Nov 03, 2013 10:51 pm

Hey Mike sorry for the delay,

I made the change you mentioned above and now i get the error below, any suggestions?

HTTP/1.1 400 Bad Request
Date: Mon, 04 Nov 2013 03:51:23 GMT
Content-Type: text/html
Content-Length: 173
Connection: close

<html>
<head><title>400 Bad Request</title></head>
<body bgcolor="white">
<center><h1>400 Bad Request</h1></center>
<hr><center>nginx/1.1.19</center>
</body>
</html>
cretin
 
Posts: 6
Joined: Wed Oct 30, 2013 12:42 am

Re: Arduino Adafruit project: Uno + Xively + CC3000 + digita

by adafruit_support_mike on Mon Nov 04, 2013 5:17 pm

Okay, let's see what else we have..

Ah.. the Content-length header says you're sending 4 bytes of data, and the previous debug information shows that the four bytes in question are '"}]}'. Something is wrong with the code that's assembling the data string. That's being done with the Arduino environment's String object concatenation operator, so heaven only knows what the problem is.

All the quotes seem to balance, but it's a long sequence of concatenations with a few redundancies and typecasts.

Try this and see what happens:

Code: Select all | TOGGLE FULL SIZE
String data = "\n{\"version\":\"1.0.0\",\"datastreams\" : [ {\"id\" : \"Temperature\",\"current_value\" : \"";
Serial.println( data );
data += String(light);
Serial.println( data );
data += "\"},{\"id\" : \"Humidity\",\"current_value\" : \"";
Serial.println( data );
data += String(light);
Serial.println( data );
data += "\"}]}";
Serial.println( data );
When you void a product warranty, you give up your right to sue the manufacturer if something goes wrong and accept full responsibility for whatever happens next. And then you truly own the product.
User avatar
adafruit_support_mike
 
Posts: 9192
Joined: Thu Feb 11, 2010 1:51 pm

Re: Arduino Adafruit project: Uno + Xively + CC3000 + digita

by cretin on Tue Nov 05, 2013 11:34 am

Hey Mike a couple of things.

So i added in your code by ccommenting out the following in my code

Code: Select all | TOGGLE FULL SIZE
  String data = "";
  data = data + "\n" + "{\"version\":\"1.0.0\",\"datastreams\" : [ {\"id\" : \"Temperature\",\"current_value\" : \"" + String(light) + "\"},"
  + "{\"id\" : \"Humidity\",\"current_value\" : \"" + String(light) + "\"}]}";


and i copied and pasted the code you posted in your previous message. Then I opened up my serial monitor, and this was the entire output.: Still not working it seems....any insight?



Code: Select all | TOGGLE FULL SIZE
api.xively.com -> 64.94.18.120
{"version":"1.0.0","datastreams" : [ {"id" : "Temperature","current_value" : "

{"version":"1.0.0","datastreams" : [ {"id" : "Temperature","current_value" : "

{"version":"1.0.0","datastreams" : [ {"id" : "Temperature","current_value" : "

{"version":"1.0.0","datastreams" : [ {"id" : "Temperature","current_value" : "

{"version":"1.0.0","datastreams" : [ {"id" : "Temperature","current_value" : ""}]}
Data length83

PUT /v2/feeds/.json HTTP/1.0.json HTTP/1.0
Host: api.xively.com
X-ApiKey: dw8lmrHzFOC2oelaJYnWpTSqZ1VYnrL8KsHPjz7VHZAzxlUK
83
Connection: close

{"version":"1.0.0","datastreams" : [ {"id" : "Temperature","current_value" : ""}]}


Connect to 64.94.18.120:80
Connected!
-------------------------------------
<html>
<head><title>400 Bad Request</title></head>
<body bgcolor="white">
<center><h1>400 Bad Request</h1></center>
<hr><center>nginx/1.1.19</center>
</body>
</html>
cretin
 
Posts: 6
Joined: Wed Oct 30, 2013 12:42 am

Re: Arduino Adafruit project: Uno + Xively + CC3000 + digita

by adafruit_support_mike on Tue Nov 05, 2013 7:34 pm

There's no feed name in the PUT request:

Code: Select all | TOGGLE FULL SIZE
PUT /v2/feeds/.json HTTP/1.0.json HTTP/1.0
              ^^^^^

The output is also a bit wonky.. none of the additions after `String(light)` are showing up until the final set of closing brackets. Try removing the call to `String()` and just appending the raw variable.. let's see if the type system is smart enough to produce the right results.
When you void a product warranty, you give up your right to sue the manufacturer if something goes wrong and accept full responsibility for whatever happens next. And then you truly own the product.
User avatar
adafruit_support_mike
 
Posts: 9192
Joined: Thu Feb 11, 2010 1:51 pm