8:33 pm


Silver
November 6, 2015

hey guys, when I run my sketch, it updates quite a few times (arbitrary) and then it stops. Not sure why. Can you look through my code? It's largely a copy of the code from here: http://community.thingspeak.com/tutorials/arduino/using-an-arduino-ethernet-shield-to-update-a-thingspeak-channel/
#include
#include
#include
#include
#include
// These are the interrupt and control pins
#define ADAFRUIT_CC3000_IRQ 2 // MUST be an interrupt pin!
// These can be any two pins
#define ADAFRUIT_CC3000_VBAT 7
#define ADAFRUIT_CC3000_CS 10
// Use hardware SPI for the remaining pins
// On an UNO, SCK = 13, MISO = 12, and MOSI = 11
Adafruit_CC3000 cc3000 = Adafruit_CC3000(ADAFRUIT_CC3000_CS, ADAFRUIT_CC3000_IRQ, ADAFRUIT_CC3000_VBAT,
SPI_CLOCK_DIVIDER); // you can change this clock speed but DI
#define WLAN_SSID "wlan_ssid" // cannot be longer than 32 characters!
#define WLAN_PASS "wlan_pass"
// Security can be WLAN_SEC_UNSEC, WLAN_SEC_WEP, WLAN_SEC_WPA or WLAN_SEC_WPA2
#define WLAN_SECURITY WLAN_SEC_WPA2
char server[]= "api.thingspeak.com";
String writeAPIKey = "4JRIMA3GJF3PIQKE"; // API key for Thingspeak
long lastConnectionTime = 0;
boolean lastConnected = false;
int failedCounter = 0;
int test = 0; //THIS IS THE VALUE BEING LOGGED.
Adafruit_CC3000_Client client;
const unsigned long
connectTimeout = 15L * 1000L, // Max time to wait for server connection
responseTimeout = 15L * 1000L; // Max time to wait for data from server
int
countdown = 0; // loop() iterations until next time server query
unsigned long
lastPolledTime = 0L, // Last value retrieved from time server
sketchTime = 0L; // CPU milliseconds since last server query
void setup(void)
{
Serial.begin(9600);
Serial.println(F("Hello, CC3000!
"));
Serial.println(F("
Initialising the CC3000 ..."));
if (!cc3000.begin()) {
Serial.println(F("Unable to initialise the CC3000! Check your wiring?"));
for(;;);
}
Serial.println(F("
Deleting old connection profiles"));
if (!cc3000.deleteProfiles()) {
Serial.println(F("Failed!"));
while(1);
}
/* Attempt to connect to an access point */
char *ssid = WLAN_SSID; /* Max 32 chars */
Serial.print(F("
Attempting to connect to ")); Serial.println(ssid);
/* NOTE: Secure connections are not available in 'Tiny' mode! */
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); // ToDo: Insert a DHCP timeout!
}
}
void loop(void) {
if(countdown == 0) { // Time's up?
unsigned long t = getTime(); // Query time server
if(t) { // Success?
lastPolledTime = t; // Save time
sketchTime = millis(); // Save sketch time of last valid time query
countdown = 24*60*4-1; // Reset counter: 24 hours * 15-second intervals
}
}
else {
countdown--; // Don't poll; use math to figure current time
}
unsigned long currentTime = lastPolledTime + (millis() - sketchTime) / 1000;
setTime(currentTime-18000);
Serial.print(hour());
Serial.print(":");
Serial.println(minute());
test = 7;
if (client.available()){
char c = client.read();
//Serial.print(c);
}
if (!client.connected() && lastConnected){
Serial.println("...disconnected");
Serial.println();
client.stop();
}
updateThingSpeak("field1="+ String(test,DEC));
// if(!client.connected() && (millis() - lastConnectionTime > updateThingSpeakInterval)){
// updateThingSpeak("field1="+ String(test,DEC));
//}
if (failedCounter > 3){
cc3000.connectToAP(WLAN_SSID, WLAN_PASS, WLAN_SECURITY);
}
lastConnected = client.connected();
delay(15000);
}
// Minimalist time server query; adapted from Adafruit Gutenbird sketch,
// which in turn has roots in Arduino UdpNTPClient tutorial.
unsigned long getTime(void) {
uint8_t buf[48];
unsigned long ip, startTime, t = 0L;
Serial.print(F("Locating time server..."));
// Hostname to IP lookup; use NTP pool (rotates through servers)
if(cc3000.getHostByName("pool.ntp.org", &ip)) {
static const char PROGMEM
timeReqA[] = { 227, 0, 6, 236 },
timeReqB[] = { 49, 78, 49, 52 };
Serial.println(F("
Attempting connection..."));
startTime = millis();
do {
client = cc3000.connectUDP(ip, 123);
} while((!client.connected()) &&
((millis() - startTime) < connectTimeout));
if(client.connected()) {
Serial.print(F("connected!
Issuing request..."));
// Assemble and issue request packet
memset(buf, 0, sizeof(buf));
memcpy_P( buf , timeReqA, sizeof(timeReqA));
memcpy_P(&buf[12], timeReqB, sizeof(timeReqB));
client.write(buf, sizeof(buf));
Serial.print(F("
Awaiting response..."));
memset(buf, 0, sizeof(buf));
startTime = millis();
while((!client.available()) &&
((millis() - startTime) < responseTimeout));
if(client.available()) {
client.read(buf, sizeof(buf));
t = (((unsigned long)buf[40] << 24) |
((unsigned long)buf[41] << 16) |
((unsigned long)buf[42] << 8) |
(unsigned long)buf[43]) - 2208988800UL;
Serial.print(F("OK
"));
}
client.close();
}
}
if(!t) Serial.println(F("error"));
return t;
}
void updateThingSpeak(String tsData){ //this block of lines basically accesses the Thingspeak channel in the required format
if (client.connect(server, 80)){
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);
lastConnectionTime = millis();
if (client.connected()){
Serial.println("Connected 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();
}
}
Most Users Ever Online: 166
Currently Online:
28 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:
kusmumichael, petersmith99, Rambant, blakeharriss09, optisol, Niyonzima FilsModerators: cstapels: 460
Administrators: Hans: 405, lee: 457