c++ - Arduino UNO, CC3000: while(client.connected) -



c++ - Arduino UNO, CC3000: while(client.connected) -

i'm playing around arduino uno , cc3000 shield connecting remote web service. though i'm having problem looping script. can see in code below, script should ping web service state of occupied every 5 seconds. though when using while(client.connected) makes arduino stop/hang forever. if while(client.connected) {} empty.

if don't include while(client.connected){} web service not pinged, why find myself in quite dilemma. please see arduino sketch file below , serial log below that.

#include <adafruit_cc3000.h> #include <ccspi.h> #include <spi.h> #include <string.h> #include "utility/debug.h" // these interrupt , command pins #define adafruit_cc3000_irq 3 // must interrupt pin! // these can 2 pins #define adafruit_cc3000_vbat 5 #define adafruit_cc3000_cs 10 // utilize hardware spi remaining pins // on uno, sck = 13, miso = 12, , mosi = 11 adafruit_cc3000 cc3000 = adafruit_cc3000(adafruit_cc3000_cs, adafruit_cc3000_irq, adafruit_cc3000_vbat, spi_clock_div2); // can alter clock speed di #define wlan_ssid "wifi" // cannot longer 32 characters! #define wlan_pass "wifi_pw" // security can wlan_sec_unsec, wlan_sec_wep, wlan_sec_wpa or wlan_sec_wpa2 #define wlan_security wlan_sec_wpa2 uint32_t ip; int ledpin = 8; // take pin led int ledpinsecond = 7; int inputpin = 2; // take input pin (for pir sensor) int pirstate = low; // start, assuming no motion detected int val = 0; // variable reading pin status string occupied; /**************************************************************************/ /*! @brief sets hw , cc3000 module (called automatically on startup) */ /**************************************************************************/ void setup(void) { serial.begin(115200); serial.println(f("hello, cc3000!\n")); displaydrivermode(); serial.print("free ram: "); serial.println(getfreeram(), dec); pinmode(ledpin, output); // declare led output pinmode(ledpinsecond, output); pinmode(inputpin, input); // declare sensor input /* initialise module */ serial.println(f("\ninitialising cc3000 ...")); if (!cc3000.begin()) { serial.println(f("unable initialise cc3000! check wiring?")); while(1); } /* optional: update mac address known value */ /* uint8_t macaddress[6] = { 0x08, 0x00, 0x28, 0x01, 0x79, 0xb7 }; if (!cc3000.setmacaddress(macaddress)) { serial.println(f("failed trying update mac address")); while(1); } */ uint16_t firmware = checkfirmwareversion(); if ((firmware != 0x113) && (firmware != 0x118)) { serial.println(f("wrong firmware version!")); for(;;); } displaymacaddress(); /* delete old connection info on module */ serial.println(f("\ndeleting old connection profiles")); if (!cc3000.deleteprofiles()) { serial.println(f("failed!")); while(1); } /* effort connect access point */ char *ssid = wlan_ssid; /* max 32 chars */ serial.print(f("\nattempting connect ")); serial.println(ssid); /* note: secure connections 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 dhcp finish */ serial.println(f("request dhcp")); while (!cc3000.checkdhcp()) { delay(1000); // todo: insert dhcp timeout! } /* display ip address dns, gateway, etc. */ while (! displayconnectiondetails()) { delay(1000); } #ifndef cc3000_tiny_driver /* seek looking webservice */ 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 /* need create sure clean after or cc3000 can freak out */ /* next time seek connect ... */ // serial.println(f("\n\nclosing connection")); // cc3000.disconnect(); } void loop(void) { val = digitalread(inputpin); // read input value occupied = "occupied"; serial.print("room state: "); serial.println(occupied); sendget(); delay(5000); } void sendget() //client function send/receive request data. { serial.print(f("initializing sendget request\n")); adafruit_cc3000_client client = cc3000.connecttcp(ip, 80); char servername[] = "www.webservice.com"; //webservice host if (client.connect(servername, 80)) { //starts client connection, checks connection serial.print(f("adding state db\n")); client.println("get /folder/sensor.php?occupied="+ occupied +" http/1.1"); //download text client.println("host: webservice.com"); client.println("connection: close"); //close 1.1 persistent connection client.println(); //end of request serial.print(f("ending connection db\n")); } else { serial.println("connection server failed"); //error message if no client connect serial.println(); } serial.print(f("checking connection bytes\n")); while (client.connected()) { while (client.available()) { //read reply char c = client.read(); } } serial.print(f("checked bytes\n")); serial.print("disconnecting."); serial.print("=================="); client.close(); //stop client } /**************************************************************************/ /*! @brief displays driver mode (tiny of normal), , buffer size if tiny mode not beingness used @note buffer size , driver mode defined in cc3000_common.h */ /**************************************************************************/ void displaydrivermode(void) { #ifdef cc3000_tiny_driver serial.println(f("cc3000 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 } /**************************************************************************/ /*! @brief tries read cc3000's internal firmware patch id */ /**************************************************************************/ uint16_t checkfirmwareversion(void) { uint8_t major, minor; uint16_t version; #ifndef cc3000_tiny_driver if(!cc3000.getfirmwareversion(&major, &minor)) { serial.println(f("unable retrieve 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 homecoming version; } /**************************************************************************/ /*! @brief tries read 6-byte mac address of cc3000 module */ /**************************************************************************/ void displaymacaddress(void) { uint8_t macaddress[6]; if(!cc3000.getmacaddress(macaddress)) { serial.println(f("unable retrieve mac address!\r\n")); } else { serial.print(f("mac address : ")); cc3000.printhex((byte*)&macaddress, 6); } } /**************************************************************************/ /*! @brief tries read ip address , other connection details */ /**************************************************************************/ bool displayconnectiondetails(void) { uint32_t ipaddress, netmask, gateway, dhcpserv, dnsserv; if(!cc3000.getipaddress(&ipaddress, &netmask, &gateway, &dhcpserv, &dnsserv)) { serial.println(f("unable retrieve ip address!\r\n")); homecoming 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(); homecoming true; } }

log

hello, cc3000! rx buffer : 131 bytes tx buffer : 131 bytes free ram: 1047 initialising cc3000 ... firmware v. : 1.24 mac address : mac_address deleting old connection profiles attempting connect 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 db ending connection db checking connection bytes

why not move code connected client if statement connect. not need poll client.connected() function.

it seems logic flawed. if client connected, remain connected until line have below loop: client.close(); cause loop never end.

alternatively add together code within loop prevent running ever.

uint32_t thetime = millis(); while(client.connected()){ if( ( millis() - thetime ) >1000 ) break; //exit loop after 1 second. }

c++ mysql c arduino

Comments

Popular posts from this blog

Delphi change the assembly code of a running process -

json - Hibernate and Jackson (java.lang.IllegalStateException: Cannot call sendError() after the response has been committed) -

C++ 11 "class" keyword -