Jump to content









Photo

Reducing Power Consumption on Arduino UNO with CC3000 Breakout Board

DHT22 CC3000 VBAT UNO

  • Please log in to reply
2 replies to this topic

#1 whittaker9

whittaker9

    Newbie

  • Members
  • Pip
  • 8 posts

Posted 09 May 2015 - 02:24 AM

While experimenting with my Adafruit CC3000 Breakout board, I discovered that I could reduce the power consumption of the board by controlling the state of the VBAT signal.  VBAT is also called VBAT_SW_EN on the Texas Instruments datasheet for the CC3000.  When the VBAT signal is high, the CC3000 can draw up to 275 ma of current.  The CC3000 current draw is reduced to about 5 microamps when VBAT is low.  The board must reconnect to the network everytime that you send a payload.

 

 

This sketch has been sending data every minute for over three weeks to the Carriots server.  The sketch requires a guest open wifi network on my router because using a password took 9 seconds to login.  The watchdog timer library that I used can only support watchdog timer intervals up to 8 seconds. 

 

I hardcoded the Carriots server IP address because the getHostByName() function didn't work reliably.  I doubt that the IP address will be changing anytime soon and we also save time by not calling this function.

 

If you try this sketch, make sure that you are using the DHT library that supports the DHT22 sensor.

 

I may have more code than is necessary to place the CC3000 in the standby mode.  Some documents indicate that a wlan_stop() or cc3000.stop() may be all I need to enable the standby mode.  I'll test these functions over the next few days and revise the sketch if they work.

 

Glen

 

#include <Adafruit_CC3000.h>
#include <SPI.h>
#include <string.h>
#include <dht.h>      //dht22 functions
#include <avr/wdt.h>  //watchdog timer functions

/* 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 DHT22_PIN 2  
dht DHT;

/* 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       "yournetwork"
#define WLAN_PASS       "yourpassword"	//Not needed when WLAN_SEC_WPA2 is used

/* Security can be WLAN_SEC_UNSEC, WLAN_SEC_WEP, WLAN_SEC_WPA or WLAN_SEC_WPA2 */
#define WLAN_SECURITY   WLAN_SEC_UNSEC

/* Carriots parameters */
#define WEBSITE  "api.carriots.com"
#define API_KEY "yourAPIkey"
#define DEVICE  "yourDevice"

/*Initialize variables */
   uint32_t ip = 60+ (244*256) + (224 * 65536L) + (82 * 16777216L);
  
void setup(void)
{
  /* Initialize */
  Serial.begin(115200);
 // Serial.println(F("\nInitializing CC3000..."));
  if (!cc3000.begin())
  {
    Serial.println(F("Couldn't start CC3000! Check wiring?"));
    while(1);
  }
}

void loop(void)
{

  // Start watchdog //reset when watchdog not reset within 8 seconds
  // wdt_enable(WDTO_8S); 

  /* Connect to WiFi network in about 13 seconds */
  cc3000.connectToAP(WLAN_SSID, WLAN_PASS, WLAN_SECURITY);
  Serial.println(F("Connected to WiFi!"));
  
  // Reset watchdog
  // wdt_reset();

  /* Wait for DHCP to complete in about 9 seconds */
  //Serial.println(F("Request DHCP"));
  while (!cc3000.checkDHCP())
  {
     delay(100);
  } 
    // Reset watchdog
  // wdt_reset();

  /* Get the website IP & print it */
//    Serial.print(WEBSITE); Serial.print(F(" -> "));
//    while (ip == 0) {
//      if (! cc3000.getHostByName(WEBSITE, &ip)) {
//        Serial.println(F("Couldn't resolve!"));
//     }
//     delay(500);
//   }
//   
  
  /* Read DHT22 Sensor */
   int chk = DHT.read22(DHT22_PIN);
 
/* TODO: Add a DHT sensor health value to payload  */
  switch (chk)
  {
  case DHTLIB_OK:
    Serial.println("\nDHT22 OK");
    break;
  case DHTLIB_ERROR_CHECKSUM:
    Serial.println("Checksum error");
    break;
  case DHTLIB_ERROR_TIMEOUT:
    Serial.println("Time out error");
    break;
  case DHTLIB_ERROR_CONNECT:
    Serial.println("Connect error");
    break;
  case DHTLIB_ERROR_ACK_L:
    Serial.println("Ack Low error");
    break;
  case DHTLIB_ERROR_ACK_H:
    Serial.print("Ack High error,\t");
    break;
  default:
    Serial.print("Unknown error,\t");
    break;
  }

/* Read analog input pins */
  float ADC0 = analogRead(A0) * 0.00488;
  float ADC1 = analogRead(A1)* 0.00488;         
  float ADC2 = analogRead(A2)* 0.00488;
  float ADC3 = analogRead(A3)* 0.00488;
  float ADC4 = analogRead(A4)* 0.00488;
  float ADC5 = analogRead(A5)* 0.00488;

  /* Convert analog data to String */
  String Monitor_0 = doubleToString(ADC0,2);
  String Monitor_1 = doubleToString(ADC1,2);
  String Monitor_2 = doubleToString(ADC2,2);
  String Monitor_3 = doubleToString(ADC3,2);
  String Monitor_4 = doubleToString(ADC4,2);
  String Monitor_5 = doubleToString(ADC5,2);

  /* Prepare JSON for Carriots & get length */
  int length = 0;

  String data = "{\"protocol\":\"v2\",\"device\":\""+String(DEVICE)+"\",\"at\":\"now\",\"data\":{\"T\":"+String(DHT.temperature)+",\"H\":"+String(DHT.humidity)+",\"A0\":"+String(Monitor_0)+",\"A1\":"+String(Monitor_1)+",\"A2\":"+String(Monitor_2)+",\"A3\":"+String(Monitor_3)+",\"A4\":"+String(Monitor_4)+"}}";
 
  length = data.length();

  // Reset watchdog
  //  wdt_reset();

     Adafruit_CC3000_Client client = cc3000.connectTCP(ip, 80);

  // Reset watchdog
  //  wdt_reset();

  if (client.connected()) {
    Serial.println(F("\nConnected to Carriots!"));
    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(DHT.temperature));
    client.fastrprint(F(",\"H\":"));
    client.print(String(DHT.humidity));
    client.fastrprint(F(",\"A0\":"));
    client.print(String(Monitor_0));
    client.fastrprint(F(",\"A1\":"));
    client.print(String(Monitor_1));
    client.fastrprint(F(",\"A2\":"));
    client.print(String(Monitor_2));
    client.fastrprint(F(",\"A3\":"));
    client.print(String(Monitor_3));
    client.fastrprint(F(",\"A4\":"));
    client.print(String(Monitor_4));
    client.fastrprintln(F("}}"));
    // Reset watchdog
    //  wdt_reset();
  } 
  else {
    Serial.println(F("Connection to Carriots 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 from Carriots Service"));
  cc3000.disconnect();
  
  /* Place CC3000 in Standby Mode to reduce power consumption */
  cc3000.stop();
  
  digitalWrite(ADAFRUIT_CC3000_VBAT,LOW);

  /* Reset watchdog & disable */
  //  wdt_reset();
  // wdt_disable();

  /* Wait 1 minute until next update. The maximum value for delay() is unclear. */
  delay(30000); // 30seconds
  delay(30000); // 30 seconds
  
  /* Restore power to cc3000 */
  cc3000 = Adafruit_CC3000(ADAFRUIT_CC3000_CS, ADAFRUIT_CC3000_IRQ, ADAFRUIT_CC3000_VBAT,
SPI_CLOCK_DIV2); // you can change this clock speed

  digitalWrite(ADAFRUIT_CC3000_VBAT,HIGH);
  
 cc3000.reboot();
 if (!cc3000.begin())
  {
    Serial.println(F("Couldn't start CC3000! Check wiring?"));
    while(1);
  }

}

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




#2 sdeancos

sdeancos

    Master of Puppets

  • Members
  • PipPipPip
  • 102 posts
  • LocationMadrid, Spain.

Posted 11 May 2015 - 07:20 AM

Great job Glen!

 

It is very interesting!

 

Thank you for sharing your work with the community.

 

BR.



#3 billwatson

billwatson

    Member

  • Members
  • PipPip
  • 14 posts

Posted 10 April 2016 - 09:41 PM

Cheers Glen, 

 

It's surprising that the CC3000 draws so much current.

 

What did you find with your testing in regards to putting the board in standby mode?







Also tagged with one or more of these keywords: DHT22, CC3000, VBAT, UNO

1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users