ArduinoGeneral

Data streams to Carriots using WiDo - additional stream values?

userHead Rozz 2014-12-20 18:30:49 3428 Views1 Replies
Using the Wido2Carriots example sketch given in the WiDo library I have been able to upload a data stream from a soil moisture sensor over my wifi network. The below code reads a value from the A0 pin gives an output in my Carriots dara stream in the format {"SoilMoisture1":700}.

[code]
#include <Adafruit_CC3000.h>
#include <ccspi.h>
#include <SPI.h>
#define Wido_IRQ 7
#define Wido_VBAT 5
#define Wido_CS 10
#include "utility/debug.h"

Adafruit_CC3000 Wido = Adafruit_CC3000(Wido_CS, Wido_IRQ, Wido_VBAT,SPI_CLOCK_DIVIDER); // Create CC3000 instances

// WLAN parameters
#define WLAN_SECURITY WLAN_SEC_WPA2
#define WLAN_SSID "Wifi Name" // cannot be longer than 32 characters!
#define WLAN_PASS "WidiPassword" // For connecting router or AP, don't forget to set the SSID and password here!!


#define TCP_TIMEOUT 3000

#define API_key "API key goes here"
#define DEVICE "device name goes here"


void setup(){

Serial.begin(115200);

/* Initialise the module */
Serial.println(F("\nInitialising the CC3000 ..."));
if (!Wido.begin())
{
Serial.println(F("Unable to initialise the CC3000! Check your wiring?"));
while(1);
}

/* Attempt to connect to an access point */
char *ssid = WLAN_SSID; /* Max 32 chars */
Serial.print(F("\nAttempting to connect to "));
Serial.println(ssid);

/*
By default connectToAP will retry indefinitely, however you can pass an
optional maximum number of retries (greater than zero) as the fourth parameter.
*/
if (!Wido.connectToAP(WLAN_SSID, WLAN_PASS, WLAN_SECURITY)) {
Serial.println(F("Failed!"));
while(1);
}

Serial.println(F("Connected!"));

/* Wait for DHCP to complete */
Serial.println(F("Request DHCP"));
while (!Wido.checkDHCP())
{
delay(100); // ToDo: Insert a DHCP timeout! - ??
}

}

uint32_t ip = 0; // Store Carriots ip address
float soilM1 = 0; // Store temporary sensor data for post
Adafruit_CC3000_Client WidoClient;

void loop(){

static unsigned long RetryMillis = 0;
static unsigned long uploadtStamp = 0;
static unsigned long sensortStamp = 0;

if(!WidoClient.connected() && millis() - RetryMillis > TCP_TIMEOUT){
// Update the time stamp for reconnecting the ip
RetryMillis = millis();

Serial.println(F("Try to connect the cloud server"));

// Connect to the Carriots Server
ip = Wido.IP2U32(82,223,244,60);
WidoClient = Wido.connectTCP(ip, 80); // Try to connect cloud server
Serial.println(F("Successed to connect Carriots server."));

}

if(WidoClient.connected() && millis() - uploadtStamp > 2000){
// If the device is connected to the cloud server, upload the data every 2000ms.
uploadtStamp = millis();

sendstream2Carriots("SoilMoisture1",int(soilM1)); // send the sensor reading from sensor to server

/********** Get the http page feedback and print the response ***********/ //do we need this?
unsigned long rTimer = millis();
Serial.println(F("Reading Cloud Response!!!\r\n"));
while (millis() - rTimer < 2000) {
while (WidoClient.connected() && WidoClient.available()) {
char c = WidoClient.read();
Serial.print(c);
}
}
delay(1000); // Wait for 1s to finish posting the data stream

WidoClient.close(); // Close the service connection
RetryMillis = millis(); // Reset the timer stamp for applying the connection with the service
}

if(millis() - sensortStamp > 1000){
sensortStamp = millis();
// read the LM35 sensor value and convert to the degrees every 100ms.

int reading = analogRead(0);
soilM1 = reading;
Serial.print(F("Real Time SoilMoisture: "));
Serial.println(soilM1);
}
//delay(1800000); // 30 min delay between readings
}



void sendstream2Carriots(String dataType, int value){

// Serial.print("Free RAM: "); Serial.println(getFreeRam(), DEC);

// Variables for storing the length of http package body
int length = 0;
char lengthstr[5];

String httpBodyPackage = "{\"protocol\":\"v2\",\"device\":\""+String(DEVICE)+"\",\"at\":\"now\",\"data\":{\"" + dataType + "\":"+String(value)+"}}";
Serial.println(httpBodyPackage); // Debug the http body stream

//Make an HTTP request to the Carriots server
Serial.print(F("Sending Http Headers..."));
WidoClient.fastrprintln(F("POST /streams HTTP/1.1"));
WidoClient.fastrprintln(F("Host: api.carriots.com"));
WidoClient.fastrprintln(F("Accept: application/json"));
WidoClient.fastrprintln(F("User-Agent: Arduino-Carriots"));
WidoClient.fastrprintln(F("Content-Type: application/json"));
WidoClient.fastrprint(F("carriots.apikey: "));
WidoClient.fastrprintln(API_key);
WidoClient.fastrprint(F("Content-Length: "));
WidoClient.println(String(httpBodyPackage.length()));
WidoClient.fastrprintln(F("Connection: close"));
WidoClient.fastrprintln("");

Serial.println(F("Sending Http Data Body..."));
WidoClient.println(httpBodyPackage);

Serial.println(F("Done....."));
}[/code]

I would like to add additional sensors that also upload to the data stream but I have been unable to figure out how to adjust the sketch for this. Can someone please help me do this?

The sketch given in the Carriots tutorial for Arduino [url=https://www.carriots.com/tutorials/arduino_carriots/arduino_library](found here)[/url] is much more intuitive and easy to follow than the WiDo example sketch but unfortunately the Carriots library does not seem to be compatible with the WiDo.

2014-12-23 11:41:19 I adapted the sketch in [url=http://forum.carriots.com/index.php/topic/61-wireless-gardening-with-arduino-cc3000-wifi-modules/]this thread[/url] on the Carriots forum and my Wido is now outputting two streams of data.

The code below transmits two soil moisture readings from analog pins 0 and 1 to a Carriots data stream.

[code]/***************************************************
*
* Adapted from Marco Schwartz original work
* Adapted by Paul Quade
* Further adapted by Rozz to be compatible with WiDoIoT Node

****************************************************/

// Libraries
#include <Adafruit_CC3000.h>
#include <ccspi.h>
#include <SPI.h>
#include "utility/debug.h"


// Define CC3000 chip pins
#define Wido_IRQ  7 
#define Wido_VBAT  5 
#define Wido_CS    10 

// I2C pins
//const uint8_t dataPin  =  6; // what is this? - Rozz
//const uint8_t clockPin =  7; // what is this? - Rozz

// Sensor variables
float sensor00;
float sensor01;

// Sensors
int sensorPin0 = 0;
int sensorPin1 = 1;

// Create CC3000 instances
Adafruit_CC3000 Wido = Adafruit_CC3000(Wido_CS, Wido_IRQ, Wido_VBAT,SPI_CLOCK_DIVIDER); // Create WiDo CC3000 instances

// WLAN parameters
#define WLAN_SECURITY  WLAN_SEC_WPA2
#define WLAN_SSID      "yourSSID"        // cannot be longer than 32 characters!
#define WLAN_PASS      "yourPassword"        // For connecting router or AP, don't forget to set the SSID and password here!!


// Carriots parameters
#define WEBSITE  "api.carriots.com"
#define API_key  "yourApiKey" 
#define DEVICE  "[email protected]"     

uint32_t ip;  // what is this? - Rozz

Adafruit_CC3000_Client WidoClient;

void setup()
{
  // Initialize
  Serial.begin(115200);


  Serial.println(F("\nInitializing."));
  if (!Wido.begin())
  {
    Serial.println(F("Couldn't begin CC3000.  Check wiring?"));
    while(1);
  }


  /* Attempt to connect to an access point */
  char *ssid = WLAN_SSID;            /* Max 32 chars */
  Serial.print(F("\nAttempting to connect to "));
  Serial.println(ssid);

  // Connect to WiFi network
  Wido.connectToAP(WLAN_SSID, WLAN_PASS, WLAN_SECURITY);
  Serial.println(F("Connected!"));

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

}

void loop()
{


/*
  // Get the website IP & print it // Don't think we need this with WiDo Code - Rozz
  ip = 0;
  Serial.print(WEBSITE);
  Serial.print(F(" -> "));
  while (ip == 0) {
    if (! Wido.getHostByName(WEBSITE, &ip)) { // what is this? - Rozz
      Serial.println(F("Couldn't resolve."));
    }
    delay(500);
  }
  Wido.printIPdotsRev(ip); // what is this? - Rozz
*/

ip = Wido.IP2U32(82,223,244,60);  //Assuming this is the IP address of Carriots - Rozz

  // Get data & transform to integers
  sensor00 = analogRead(sensorPin0);
  sensor01 = analogRead(sensorPin1);
 

  // Convert Floats to Strings.
  char TempString[32];  //  A temporary character array to hold data.
  // dtostrf( [Float variable] , [Minimum SizeBeforePoint] , [sizeAfterPoint] , [WhereToStoreIt] )
  dtostrf(sensor00,2,1,TempString);
  String SoilMoisture0 =  String(TempString);
  dtostrf(sensor01,2,2,TempString);
  String SoilMoisture1 =  String(TempString);

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

  String data = "{\"protocol\":\"v2\",\"device\":\""+String(DEVICE)+"\",\"at\":\"now\",\"data\":{\"SoilMoisture0\":"+String(SoilMoisture0)+",\"SoilMoisture1\":"+String(SoilMoisture1)+"}}";

  length = data.length();
  Serial.print(F("\nData length"));
  Serial.println(length);

  // Print request for debug purposes
  // 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.print(F("carriots.apikey: "));
  Serial.println(String(API_key));
  Serial.print(F("Content-Length: "));
  Serial.println(String(length));
  Serial.println(F("Connection: close"));
  //Serial.println();
  Serial.println(data);

  // Send request
  WidoClient = Wido.connectTCP(ip, 80); 
  if (WidoClient.connected()) {
    Serial.println(F("Connected!"));
    WidoClient.println(F("POST /streams HTTP/1.1"));
    WidoClient.println(F("Host: api.carriots.com"));
    WidoClient.println(F("Accept: application/json"));
    WidoClient.println(F("User-Agent: Arduino-Carriots"));
    WidoClient.println(F("Content-Type: application/json"));
    WidoClient.print(F("carriots.apikey: "));
    WidoClient.println(String(API_key));
    WidoClient.print(F("Content-Length: "));
    WidoClient.println(String(length));
    WidoClient.println(F("Connection: close"));
    WidoClient.println();
    WidoClient.println(data);
  }
  else {
    Serial.println(F("Connection failed"));   
    return;
  }

  Serial.println(F("-------------------------------------"));
  while (WidoClient.connected()) {
    while (WidoClient.available()) {
      char c = WidoClient.read();
      Serial.print(c);
    }
  }
  WidoClient.close();
  Serial.println(F("\n-------------------------------------"));
  Serial.println(F("Disconnecting\n"));
  //cc3000.disconnect(); // Doesn't seem to work with WiDo - Rozz
 
  // Wait 30 minutes until next update
  delay(1800000);
}[/code]

There are a couple of comments I have signed with my username where I am not sure of the functionality of the code but it seems to work. 
userHeadPic Rozz