CC3000 and Thingspeak

HI everyone I'm from Mexico and I want to use the CC3000 wifi module from adafruit to updtae a channel/channels into Thingspeak, so I modify a code from xively and from a Thingspeak project, so I obtain something like this, what do you tink? .

#include <SPI.h>

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


// Definimos los pines del shield
#define ADAFRUIT_CC3000_IRQ   3
#define ADAFRUIT_CC3000_VBAT  5
#define ADAFRUIT_CC3000_CS    10

// Definimos el sensor DHT
#define DHTPIN 35
#define DHTTYPE DHT11

// Se crean las instancias del shield
Adafruit_CC3000 cc3000 = Adafruit_CC3000(ADAFRUIT_CC3000_CS, ADAFRUIT_CC3000_IRQ, ADAFRUIT_CC3000_VBAT,
                                         SPI_CLOCK_DIV2); // you can change this clock speed
                                         
// Instancias del sensor
DHT dht(DHTPIN, DHTTYPE);

// Parametros de la red a conectarse
#define WLAN_SSID       "XXXX"
#define WLAN_PASS       "XXXX"
//La seguridad puede ser WLAN_SEC_UNSEC, WLAN_SEC_WEP, WLAN_SEC_WPA or WLAN_SEC_WPA2
#define WLAN_SECURITY   WLAN_SEC_WPA2

// Configuracion de Thingspeak
char site[]= "api.thingspeak.com";
String APIKey = "XXXX";
const int updateThingSpeakInterval = 15000;      // Time interval in milliseconds to update ThingSpeak (number of seconds * 1000 = interval)

// Variable para alojar la ip de thingspeak
uint32_t ip;

long lastConnectionTime = 0; 
boolean lastConnected = false;
int failedCounter = 0;

void setup(void)
{
  // Iniciamos el sensor
  dht.begin();
  
  // Iniciamos el monitor serial
  Serial.begin(115200);
  //Inciamos el shield
  if (!cc3000.begin())
  {
    Serial.println(F("El shield no puede inciar, ¿Esta bien conectado?"));
    while(1);
  }
   
  // Conectamos a la red
  cc3000.connectToAP(WLAN_SSID, WLAN_PASS, WLAN_SECURITY);
  Serial.println("Conexion establecida");
    
  // Solicitamos un IP
  Serial.println(F("Solicitando IP"));
  while (!cc3000.checkDHCP())
  {
    delay(100);
  }  
      // Obteniendo la IP del sitio
    ip = 0;
    Serial.print(site); Serial.print(F(" -> "));
    while (ip == 0) {
      if (! cc3000.getHostByName(site, &ip)) {
        Serial.println(F("No se puede obtener la ip"));
      }
      delay(500);
    }
    cc3000.printIPdotsRev(ip);
}

 void loop(void)
 {
    //Obteniendo la humedad y la temperatura
    float h = dht.readHumidity();
    float t = dht.readTemperature();
     
    // Transformando los datos a un String
    String temperature = String((int) t);
    String humidity = String((int) h);
    Adafruit_CC3000_Client client = cc3000.connectTCP(ip, 80);
    //Actualizamod el feed
    if(client.connected() && (millis() - lastConnectionTime > updateThingSpeakInterval))
    {
      updateThingSpeak("field1="+temperature);
    }
     while (client.connected()) {
      while (client.available()) {

      // Read answer
      char c = client.read();
      }
    }
    Serial.println("Closing connection");
    Serial.println("");
    client.close();
    
    lastConnected = client.connected();
 }
 
 void updateThingSpeak(String Data)
{
  Adafruit_CC3000_Client client = cc3000.connectTCP(ip, 80);
  if (client.connected())
  {         
    client.println("POST /update HTTP/1.1\n");
    client.println("Host: api.thingspeak.com\n");
    client.println("Connection: close\n");
    client.println("X-THINGSPEAKAPIKEY: "+APIKey+"\n");
    client.println("Content-Type: application/x-www-form-urlencoded\n");
    client.println("Content-Length: ");
    client.println(Data.length());
    client.println("\n\n");

    client.println(Data);
    
    lastConnectionTime = millis();
    
    if (client.connected())
    {
      Serial.println("Connecting to ThingSpeak...");
      Serial.println();
      
      failedCounter = 0;
    }
    else
    {
      failedCounter++;
  
      Serial.println("Connection to ThingSpeak failed ("+String(failedCounter, DEC)+")");   
      Serial.println();
    }
    
  }
  else
  {
    failedCounter++;
    
    Serial.println("Connection to ThingSpeak Failed ("+String(failedCounter, DEC)+")");   
    Serial.println();
    
    lastConnectionTime = millis(); 
  }
}

If you have some idea of what i can add, you are welcome

Muy buenas.... con esto que has publicado.... se podría manejar el modulo WIFI de Microduino? está basado en el CC3000

Hola Magnus512

Estuve probando tu código, que me vino muy bien (gracias!). Encontré, al menos en mi cc3000 que al abrir 2 instancias dejaba de funcionar en algún momento, por lo que puse 1 sola instancia y llamo únicamente al procedimiento.

Me falta actualizar el delay entre una llamada y otra y arreglar un poco el código.

Saludos.-

void updateThingSpeak(String tsData)
{
Adafruit_CC3000_Client client = cc3000.connectTCP(ip, 80);

if (client.connected())
{
Serial.println(F("connected"));

client.print("POST /update HTTP/1.1\n");
client.print("Host: api.thingspeak.com\n");
client.print("Connection: close\n");
client.print("X-THINGSPEAKAPIKEY: "+writeAPIKey+"\n");
client.print("Content-Type: application/x-www-form-urlencoded\n");
client.print("Content-Length: ");
client.print(tsData.length());
client.print("\n\n");

client.print(tsData);

lastConnectionTime = millis();
Serial.print("millis:");
Serial.print(lastConnectionTime);

if (client.connected())
{
Serial.println("Connecting to ThingSpeak...");
Serial.println();

failedCounter = 0;
}
else
{
failedCounter++;

Serial.println("Connection to ThingSpeak failed ("+String(failedCounter, DEC)+")");
Serial.println();
}

}
else
{
failedCounter++;

Serial.println("Connection to ThingSpeak Failed ("+String(failedCounter, DEC)+")");
Serial.println();

lastConnectionTime = millis();
}

while (client.connected())
{
while (client.available())
{
// Read answer
char c = client.read();
Serial.print(c);

}
}

Serial.println("Closing connection");
Serial.println("");
client.close();

lastConnected = client.connected();
Serial.print("Last Connected");
Serial.print(lastConnected);

}

mattsven471:
Muy buenas.... con esto que has publicado.... se podría manejar el modulo WIFI de Microduino? está basado en el CC3000

Matt la verdad no lo se no lo probé, debe tener integradas sus propias librerías.