I am playing with Arduino UNO and a CC3000 screen connecting to a remote web service. Although I have a problem with the script loop. As you can see in the code below, the script should ping web services with a state occupiedevery 5 seconds. While using while(client.connected)something makes Arduino stop / hang forever. Even if while(client.connected) {}just empty.
If I don't turn it on while(client.connected){}, the web service does not ping, so I'm in a pretty dilemma. Please see the Arduino Sketch File below and the serial log below this.
#include <Adafruit_CC3000.h>
#include <ccspi.h>
#include <SPI.h>
#include <string.h>
#include "utility/debug.h"
#define ADAFRUIT_CC3000_IRQ 3
#define ADAFRUIT_CC3000_VBAT 5
#define ADAFRUIT_CC3000_CS 10
Adafruit_CC3000 cc3000 = Adafruit_CC3000(ADAFRUIT_CC3000_CS, ADAFRUIT_CC3000_IRQ, ADAFRUIT_CC3000_VBAT,
SPI_CLOCK_DIV2);
#define WLAN_SSID "WIFI"
#define WLAN_PASS "WIFI_PW"
#define WLAN_SECURITY WLAN_SEC_WPA2
uint32_t ip;
int ledPin = 8;
int ledPinSecond = 7;
int inputPin = 2;
int pirState = LOW;
int val = 0;
String occupied;
void setup(void)
{
Serial.begin(115200);
Serial.println(F("Hello, CC3000!\n"));
displayDriverMode();
Serial.print("Free RAM: ");
Serial.println(getFreeRam(), DEC);
pinMode(ledPin, OUTPUT);
pinMode(ledPinSecond, OUTPUT);
pinMode(inputPin, INPUT);
Serial.println(F("\nInitialising the CC3000 ..."));
if (!cc3000.begin())
{
Serial.println(F("Unable to initialise the CC3000! Check your wiring?"));
while(1);
}
uint16_t firmware = checkFirmwareVersion();
if ((firmware != 0x113) && (firmware != 0x118)) {
Serial.println(F("Wrong firmware version!"));
for(;;);
}
displayMACAddress();
Serial.println(F("\nDeleting old connection profiles"));
if (!cc3000.deleteProfiles()) {
Serial.println(F("Failed!"));
while(1);
}
char *ssid = WLAN_SSID;
Serial.print(F("\nAttempting to connect to "));
Serial.println(ssid);
if (!cc3000.connectToAP(WLAN_SSID, WLAN_PASS, WLAN_SECURITY)) {
Serial.println(F("Failed!"));
while(1);
}
Serial.println(F("Connected!"));
Serial.println(F("Request DHCP"));
while (!cc3000.checkDHCP())
{
delay(1000);
}
while (! displayConnectionDetails()) {
delay(1000);
}
#ifndef CC3000_TINY_DRIVER
Serial.print(F("www.webservice.com -> "));
if (! cc3000.getHostByName("www.webservice.com", &ip)) {
Serial.println(F("Could not resolve!"));
}
else {
cc3000.printIPdotsRev(ip);
}
Serial.print(F("\n\rPinging "));
cc3000.printIPdotsRev(ip);
Serial.print("...");
uint8_t replies = cc3000.ping(ip, 5);
Serial.print(replies);
Serial.println(F(" replies"));
if (replies)
Serial.println(F("Ping successful!"));
#endif
}
void loop(void)
{
val = digitalRead(inputPin);
occupied = "Occupied";
Serial.print("Room state: ");
Serial.println(occupied);
sendGET();
delay(5000);
}
void sendGET()
{
Serial.print(F("Initializing SendGET request\n"));
Adafruit_CC3000_Client client = cc3000.connectTCP(ip, 80);
char serverName[] = "www.webservice.com";
if (client.connect(serverName, 80)) {
Serial.print(F("Adding state to DB\n"));
client.println("GET /folder/sensor.php?occupied="+ occupied +" HTTP/1.1");
client.println("Host: webservice.com");
client.println("Connection: close");
client.println();
Serial.print(F("Ending connection to DB\n"));
} else {
Serial.println("Connection to server failed");
Serial.println();
}
Serial.print(F("Checking connection for bytes\n"));
while (client.connected()) {
while (client.available()) {
char c = client.read();
}
}
Serial.print(F("Checked for bytes\n"));
Serial.print("disconnecting.");
Serial.print("==================");
client.close();
}
void displayDriverMode(void)
{
#ifdef CC3000_TINY_DRIVER
Serial.println(F("CC3000 is configure in 'Tiny' mode"));
#else
Serial.print(F("RX Buffer : "));
Serial.print(CC3000_RX_BUFFER_SIZE);
Serial.println(F(" bytes"));
Serial.print(F("TX Buffer : "));
Serial.print(CC3000_TX_BUFFER_SIZE);
Serial.println(F(" bytes"));
#endif
}
uint16_t checkFirmwareVersion(void)
{
uint8_t major, minor;
uint16_t version;
#ifndef CC3000_TINY_DRIVER
if(!cc3000.getFirmwareVersion(&major, &minor))
{
Serial.println(F("Unable to retrieve the firmware version!\r\n"));
version = 0;
}
else
{
Serial.print(F("Firmware V. : "));
Serial.print(major);
Serial.print(F("."));
Serial.println(minor);
version = major;
version <<= 8;
version |= minor;
}
#endif
return version;
}
void displayMACAddress(void)
{
uint8_t macAddress[6];
if(!cc3000.getMacAddress(macAddress))
{
Serial.println(F("Unable to retrieve MAC Address!\r\n"));
}
else
{
Serial.print(F("MAC Address : "));
cc3000.printHex((byte*)&macAddress, 6);
}
}
bool displayConnectionDetails(void)
{
uint32_t ipAddress, netmask, gateway, dhcpserv, dnsserv;
if(!cc3000.getIPAddress(&ipAddress, &netmask, &gateway, &dhcpserv, &dnsserv))
{
Serial.println(F("Unable to retrieve the IP Address!\r\n"));
return false;
}
else
{
Serial.print(F("\nIP Addr: "));
cc3000.printIPdotsRev(ipAddress);
Serial.print(F("\nNetmask: "));
cc3000.printIPdotsRev(netmask);
Serial.print(F("\nGateway: "));
cc3000.printIPdotsRev(gateway);
Serial.print(F("\nDHCPsrv: "));
cc3000.printIPdotsRev(dhcpserv);
Serial.print(F("\nDNSserv: "));
cc3000.printIPdotsRev(dnsserv);
Serial.println();
return true;
}
}
LOG
Hello, CC3000!
RX Buffer : 131 bytes
TX Buffer : 131 bytes
Free RAM: 1047
Initialising the CC3000 ...
Firmware V. : 1.24
MAC Address : MAC_ADDRESS
Deleting old connection profiles
Attempting to connect to 64 Allen Street - East
Connected!
Request DHCP
IP Addr: XXX.XXX.XX.X
Netmask: 255.255.255.0
Gateway: 192.168.0.1
DHCPsrv: 192.168.0.1
DNSserv: XXX.XX.X.X
www.webservice.com -> XX.XX.XXX.XXX
Pinging XX.XX.XXX.XXX...5 replies
Ping successful!
Room state: Free
Initializing SendGET request
Adding state to DB
Ending connection to DB
Checking connection for bytes
source
share