Arduino uno cc3000 wifi -- data stops sending after 3-4 times | Arduino | Forum

Avatar

Please consider registering
Guest

sp_LogInOut Log In sp_Registration Register

2   +   1   =  

Register | Lost password?
Advanced Search

— Forum Scope —






— Match —





— Forum Options —





Minimum search word length is 3 characters - maximum search word length is 84 characters

sp_Feed sp_TopicIcon
Arduino uno cc3000 wifi -- data stops sending after 3-4 times
No permission to create posts
December 15, 2015
5:56 pm
Avatar
jbrh530
New Member
Members
Forum Posts: 1
Member Since:
December 15, 2015
sp_UserOfflineSmall Offline

Hello,

I am an Arduino newbie, but have succeeded in assembling and programming an Arduino uno with DHT11 and BMP180 and have sent data to Thingspeak via a CC3000 wifi breakout board. Like others, a wifi connection is established, and data is sent/updated to my ThingSpeak channel about 3 to 4 times before stopping. I understand that others have mentioned this problem in this forum, but what I have read has been in the context of ethernet connection, not wifi. Being new to Arduino, it is not clear how to adapt their suggestions to the code for CC3000 wifi. I would greatly appreciate if someone could look at the code I am using (based primarily on http://obaej.blogspot.com/2015/01/ti-cc3000-wifi-and-thingspeak-online.html) and suggest what I might adjust to prevent the data transmission from ceasing. Some folks have mentioned the warm up time needed by the DHT I believe. However, I have tried a version that only transmits data from the bmp180 and that produces the same failed transmission after about 4 cycles. Thank you so much for your time and expertise 🙂

Here is the code:

// Libraries
#include
#include
//#include // don't need with earlier version of Adafruit_Sensor.h
#include //earlier version was used in Instructable link
#include
#include
#include
#include
#include "utility/debug.h"
#include "DHT.h"
#include

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

// temperature and humidity sensor
#define DHTPIN 2
#define DHTTYPE DHT11

DHT dht(DHTPIN, DHTTYPE);

Adafruit_BMP085 bmp;

// 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 "WGSKB"
#define WLAN_PASS "8RVQWYWKNWMB3X9K"

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

// ThingSpeak Settings
char thingSpeakAddress[] = "api.thingspeak.com";
String writeAPIKey = "WVE8CXSX2QK08RYR";

uint32_t ip;

// Variable Setup
long lastConnectionTime = 0;
boolean lastConnected = false;
int failedCounter = 0;

void setup(void)
{
// Initialize
Serial.begin(115200);
ConnectWifi(); //// Connect with Wifi

}

void loop(void)
{
//dht.begin(); //added this on Tuesday cause it was in the bmp180_and_dht11 sketch

bmp.begin();

/* Display atmospheric pressue in hPa */
float p = (bmp.readPressure()/100.0);
Serial.print("Absolute Pressure = ");
Serial.println(p); /*this is where I need to change the code to convert to QHN pressure */

// Get data & transform to integers
float h = dht.readHumidity();
float t = dht.readTemperature(true); //fahrenheit
float hif = dht.computeHeatIndex(t, h);
Serial.print("Temperature = ");
Serial.println(t);
Serial.print("Humidity = ");
Serial.println(h);
Serial.print("Heat Index = ");
Serial.println(hif);

int temperature = (int) t;
int humidity = (int) h;
int heatindex = (int) hif;
int pressure = (int) p;
String tem=String(temperature, DEC); //Thingspeak only data stream is string type
String hum=String(humidity, DEC); // Converting integer data to string
String heat=String(heatindex, DEC);
String pr =String(pressure, DEC);

Serial.print(F("api.thingspeak.com -> "));
while (ip == 0) {
if (! cc3000.getHostByName("api.thingspeak.com", &ip)) {
Serial.println(F("Couldn't resolve!, Restarting....."));
wdt_enable(WDTO_8S); // if host does not response wait 8 sec and restart
while(1){}
}

delay(500);
}
cc3000.printIPdotsRev(ip);
Serial.println(F(""));
Adafruit_CC3000_Client client = cc3000.connectTCP(ip, 80);
// Print Update Response to Serial Monitor
if (client.available())
{
char c = client.read();
Serial.print(c);
}

// Update ThingSpeak
if(client.connected())
{
// Send temperature and humidity data to Thingspeak site
updateThingSpeak("1="+tem+"&2="+hum+"&3="+pr+"&4="+heat);
delay(25000); // Thingspeak needs a 15s delay between 2 data stream.
failedCounter=0;

}
else
{
Serial.println(F("Connection failed"));
failedCounter++;
if (failedCounter==10)
{
Serial.println(F("Connection failed for 10 times, Restarting......"));
wdt_enable(WDTO_8S); // if 10 continuous data upload failed, restart host after 8s.
while(1){}
}
}

}

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

Serial.println("Connecting to ThingSpeak...");
Serial.println("Sending Data");
client.print("POST /update HTTP/1.1
");
client.print("Host: api.thingspeak.com
");
client.print("Connection: close
");
client.print("X-THINGSPEAKAPIKEY: "+writeAPIKey+"
");
client.print("Content-Type: application/x-www-form-urlencoded
");
client.print("Content-Length: ");
client.print(tsData.length());
client.print("

");

client.print(tsData);
Serial.println(F("Done"));
Serial.println(F("Waiting 30 sec for next sending"));
Serial.println("_________");
}

void ConnectWifi(){
Serial.println(F("
Initializing..."));
if (!cc3000.begin())
{
Serial.println(F("Couldn't begin()! Check your wiring?"));
while(1);
}
// 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);
}

}

Here is the printout to my serial monitor where it failed on the second transmission. What is wrong that the code does not allow reconnection to ThingSpeak?

Initializing...
Connected!
Request DHCP
Absolute Pressure = 992.42
Temperature = 68.00
Humidity = 46.00
Heat Index = 66.66
api.thingspeak.com -> 144.212.80.11
Connecting to ThingSpeak...
Sending Data
Done
Waiting 30 sec for next sending
_________
Absolute Pressure = 992.44
Temperature = 68.00
Humidity = 46.00
Heat Index = 66.66
api.thingspeak.com -> 144.212.80.11

December 16, 2015
9:57 am
Avatar
Rob Purser
Natick, MA

MathWorks
Members
Forum Posts: 47
Member Since:
August 22, 2015
sp_UserOfflineSmall Offline

Hi,

It looks like the Adafruit_CC3000_Client class is a standard Arduino TCP client, which means that you could pass it into the ThingSpeak Communication Library for Arduino (it looks similar to the wi-fi shield). This has been heavily tested and reliably sends data.

More info on the library:
http://community.thingspeak.com/2015/10/official-thingspeak-library-for-arduino-and-particle/

See if you can get the examples working with your connection, and let us know how it goes.

-Rob

Senior Development Manager for IoT and Hardware Interfacing for MATLAB at MathWorks.  Visit ThingSpeak.com to explore the IoT Analytic platform that speaks MATLAB made for engineers and scientists.  You can collect, analyze, and act in 5 minutes or less!

April 12, 2016
1:54 pm
Avatar
Hans
Natick, MA

Admin
Forum Posts: 411
Member Since:
January 18, 2011
sp_UserOfflineSmall Offline

I would also like to point out the actual ThingSpeak IP address incase you have an issue with this code in the future. The offical static IP for ThingSpeak is 184.106.153.149 found here (http://www.mathworks.com/help/thingspeak/channel-settings.html#endpoints).

June 23, 2020
1:18 am
Avatar
Luis Reyes
New Member
Members
Forum Posts: 1
Member Since:
June 10, 2020
sp_UserOfflineSmall Offline

Hello, I was able to make some modifications to the program and I think it already works without stopping.
Maybe you can make some improvements too.

// Libraries
#include
#include
#include
#include
#include
#include "utility/debug.h"
#include "DHT.h"
#include

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

// temperature and humidity sensor
#define DHTPIN 6
#define DHTTYPE DHT11

// Create CC3000 instances
Adafruit_CC3000 cc3000 = Adafruit_CC3000(ADAFRUIT_CC3000_CS, ADAFRUIT_CC3000_IRQ, ADAFRUIT_CC3000_VBAT,
SPI_CLOCK_DIVIDER); // you can change this clock speed SPI_CLOCK_DIVIDER

#define DEBUGPRINT(x) Serial.print(x); // Prueba debug
#define DEBUGPRINTLN(x) Serial.println(x);

// WLAN parameters
#define WLAN_SSID "Reyes_G"
#define WLAN_PASS "luisluis"

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

// ThingSpeak Settings
char thingSpeakAddress[] = "api.thingspeak.com";
String writeAPIKey = "6DYOPUERPIPV3C2L";

uint32_t ip = 0;
// Variable Setup

int failConnected = 0;
int failedCounter = 0;

// for themperature and humidity
DHT dht(DHTPIN, DHTTYPE);

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

void loop(void)
{
bool v= cc3000.checkConnected();
DEBUGPRINTLN("El Chequeso es: " +v)
// Get data & transform to integers
float h = dht.readHumidity();
float t = dht.readTemperature();

// Check if any reads failed and exit early (to try again).
if (isnan(h) || isnan(t)) {
DEBUGPRINTLN("Failed to read from DHT sensor!")
//Serial.println(F("Failed to read from DHT sensor!"));
return;
}

DEBUGPRINT("Temperature = ")
DEBUGPRINTLN(t)
DEBUGPRINT("Humidity = ")
DEBUGPRINTLN(h)
//Serial.print("Temperature = ");
//Serial.println(t);
//Serial.print("Humidity = ");
//Serial.println(h);

int temperature = (int) t;
int humidity = (int) h;
String tem = String(temperature, DEC); //Thingspeak only data stream is string type
String hum = String(humidity, DEC); // Converting integer data to string

Serial.print(F("api.thingspeak.com -> "));
ip = 0;
while (ip == 0) {
if (!cc3000.getHostByName("api.thingspeak.com", &ip)) {
Serial.println(F("Couldn't resolve!, Restarting....."));
while(1){}
}
delay(500);
}
cc3000.printIPdotsRev(ip);
Serial.println(F(""));

// Send temperature and humidity data to Thingspeak site

updateThingSpeak("1=" + tem + "&2=" + hum);
//delay(25000); // Thingspeak needs a 15s delay between 2 data stream.

}

void updateThingSpeak(String tsData)
{

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

// Update ThingSpeak
if (!client.connected()){
DEBUGPRINTLN("Cliente esperando conexion a SpeakThing...!")
while (1);
}
else
{
Serial.println("Connecting to ThingSpeak...");
Serial.println("Sending Data");
client.print("POST /update HTTP/1.1
");
client.print("Host: api.thingspeak.com
");
client.print("Connection: close
");
client.print("X-THINGSPEAKAPIKEY: " + writeAPIKey + "
");
client.print("Content-Type: application/x-www-form-urlencoded
");
client.print("Content-Length: ");
client.print(tsData.length());
client.print("

");

client.print(tsData);
DEBUGPRINTLN("Listo..!")
DEBUGPRINTLN("Cliente cerrando conexión...!")
//client.close();
client.stop();
DEBUGPRINTLN("Espera de 30 seg proximos datos...!")
delay(25000);

}

}

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

/* Delete any old connection data on the module */
Serial.println(F("
Deleting old connection profiles"));
if (!cc3000.deleteProfiles()) {
Serial.println(F("Failed!"));
while (1);
}

// Connect to WiFi network
if (!cc3000.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 (!cc3000.checkDHCP())
{
delay(100);
}

}

Forum Timezone: America/New_York

Most Users Ever Online: 166

Currently Online:
18 Guest(s)

Currently Browsing this Page:
1 Guest(s)

Top Posters:

rw950431: 272

Vinod: 240

piajola: 95

turgo: 70

vespapierre: 63

Adarsh_Murthy: 62

Member Stats:

Guest Posters: 1

Members: 8665

Moderators: 1

Admins: 2

Forum Stats:

Groups: 3

Forums: 14

Topics: 1600

Posts: 5760

Newest Members:

briskpanda, Dejw0089, Yassine Ben Salah, anis, 43994554AKS, suda

Moderators: cstapels: 460

Administrators: Hans: 405, lee: 457