I'm using arduino uno with cc3000 shield and I'm trying to get some sort of data from my server but I only get some of it and the remaining part is delivered after so long, when I send the request I get this:
HTTP/1.1 200 OK.
Date: Sun, 14 Feb 2016 14:00:46 GMT.
Server: Apache.
Last-Modified: Sun, 14 Feb 2016 11:58:31 GMT.
Accept-Ranges: bytes.
Content-Length: 16.
Keep-Alive: timeout=2, max=100.
Connection: Keep-Alive.
Content-Type: application/json.
and the full data should look like this:
HTTP/1.1 200 OK.
Date: Sun, 14 Feb 2016 13:59:26 GMT.
Server: Apache.
Last-Modified: Sun, 14 Feb 2016 11:58:31 GMT.
Accept-Ranges: bytes.
Content-Length: 16.
Keep-Alive: timeout=2, max=100.
Connection: Keep-Alive.
Content-Type: application/json.
{"light": "off"}
the {"light": "off"}
part takes too long to get parsed to my code.
I'm using this code:
#include <SPI.h>
#include <SFE_CC3000.h>
#include <SFE_CC3000_Client.h>
#include <ArduinoJson.h>
// Pins
#define CC3000_INT 3 // Needs to be an interrupt pin (D2/D3)
#define CC3000_EN 5 // Can be any digital pin
#define CC3000_CS 10 // Preferred is pin 10 on Uno
// Connection info data lengths
#define IP_ADDR_LEN 4 // Length of IP address in bytes
// Constants
char ap_ssid[] = "xxxxx"; // SSID of network
char ap_password[] = {0xXX, 0xXX, 0xXX, 0xXX, 0xXX, 0xXX}; // Password of network
unsigned int ap_security = WLAN_SEC_WEP; // Security of network
unsigned int timeout = 30000; // Milliseconds
#define SERVER "XXXXX"
#define DIRECTORY "/esp8266pir/light.json"
const int pin = 2;
#define IDLE_TIMEOUT_MS 5000
// Global Variables
SFE_CC3000 wifi = SFE_CC3000(CC3000_INT, CC3000_EN, CC3000_CS);
SFE_CC3000_Client client = SFE_CC3000_Client(wifi);
void setup() {
pinMode(pin, OUTPUT);
pinMode(pin, HIGH);
ConnectionInfo connection_info;
int i;
// Initialize Serial port
Serial.begin(115200);
Serial.println();
Serial.println("---------------------------");
Serial.println("SparkFun CC3000 - WebClient");
Serial.println("---------------------------");
// Initialize CC3000 (configure SPI communications)
if ( wifi.init() ) {
Serial.println("CC3000 initialization complete");
} else {
Serial.println("Something went wrong during CC3000 init!");
}
// Connect using DHCP
Serial.print("Connecting to SSID: ");
Serial.println(ap_ssid);
if(!wifi.connect(ap_ssid, ap_security, ap_password, timeout)) {
Serial.println("Error: Could not connect to AP");
}
// Gather connection details and print IP address
if ( !wifi.getConnectionInfo(connection_info) ) {
Serial.println("Error: Could not obtain connection details");
} else {
Serial.print("IP Address: ");
for (i = 0; i < IP_ADDR_LEN; i++) {
Serial.print(connection_info.ip_address[i]);
if ( i < IP_ADDR_LEN - 1 ) {
Serial.print(".");
}
}
Serial.println();
}
}
void loop() {
send_request();
}
void send_request () {
client.connect(SERVER, 80);
if (client.connected())
{
client.print(F("GET "));
client.print(DIRECTORY);
client.print(F(" HTTP/1.1\r\n"));
client.print(F("Host: ")); client.print(SERVER); client.print(F("\r\n"));
client.print(F("Connection: keep-alive\r\n\r\n"));
client.println();
}
else
{
Serial.print(F("failed to connect"));
return;
}
// read response
String section="header";
unsigned long lastRead = millis();
while (client.connected()&& (millis() - lastRead < IDLE_TIMEOUT_MS)) {
while (client.available()) {
String line = client.readStringUntil('\r');
Serial.print(line);
// we’ll parse the HTML body here
if (section=="header") { // headers..
Serial.print(".");
if (line=="\n") { // skips the empty space at the beginning
section="json";
}
}
else if (section=="json") { // print the good stuff
section="ignore";
String result = line.substring(1);
// Parse JSON
int size = result.length() + 1;
char json[size];
result.toCharArray(json, size);
StaticJsonBuffer<200> jsonBuffer;
JsonObject& json_parsed = jsonBuffer.parseObject(json);
if (!json_parsed.success())
{
Serial.println("parseObject() failed");
return;
}
// Make the decision to turn off or on the LED
if (strcmp(json_parsed["light"], "on") == 0) {
digitalWrite(pin, HIGH);
Serial.println("LED ON");
}
else {
digitalWrite(pin, LOW);
Serial.println("led off");
}
}
lastRead = millis();
}
}
}
and here is my light.json
code:
<?php
$light = $_GET['light'];
if($light == "on") {
$file = fopen("light.json", "w") or die("can't open file");
fwrite($file, '{"light": "on"}');
fclose($file);
}
else if ($light == "off") {
$file = fopen("light.json", "w") or die("can't open file");
fwrite($file, '{"light": "off"}');
fclose($file);
}
?>
<a href="?light=on">Turn On</a>
<a href="?light=off">Turn Off</a>
<div>
<?php
if($light=="on") {
echo("Turn LED on.");
}
else if ($light=="off") {
echo("Turn LED off.");
}
else {
echo ("Do something.");
}
?>
</div>
thanks in advance.
Edit
memory statistics:
Global variables use 1,187 bytes (57%) of dynamic memory, leaving 861 bytes for local variables. Maximum is 2,048 bytes.
Edit
I did some enhancement to my arduino code and used adafruit library but still the server response takes too long to get it (up to 10 seconds).
this is my new code:
/***************************************************
This is an example for the Adafruit CC3000 Wifi Breakout & Shield
Designed specifically to work with the Adafruit WiFi products:
----> https://www.adafruit.com/products/1469
Adafruit invests time and resources providing this open source code,
please support Adafruit and open-source hardware by purchasing
products from Adafruit!
Written by Kevin Townsend & Limor Fried & Rick Lesniak for Adafruit Industries.
BSD license, all text above must be included in any redistribution
****************************************************/
/*
This example does a test of the SNTP (Simple Network Time Protocol) client:
* Initialization
* SSID Scan
* AP connection
* DHCP printout
* SNTP time synchronization
* Extract and print current time and date information
*/
#include <Adafruit_CC3000.h>
#include <ccspi.h>
#include <SPI.h>
//#include "utility/NetTime.h"
#include <string.h>
#include "utility/debug.h"
#include <ArduinoJson.h>
#include <TextFinder.h>
// These are the interrupt and control pins
#define ADAFRUIT_CC3000_IRQ 3 // MUST be an interrupt pin!
// These can be any two pins
#define ADAFRUIT_CC3000_VBAT 5
#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_DIV2); // you can change this clock speed but DI
Adafruit_CC3000_Client client;
TextFinder finder(client);
#define WLAN_SSID "XXXXX" // cannot be longer than 32 characters!
const char WLAN_PASS[] = {0xXX, 0xXX, 0xXX, 0xXX, 0xXX, 0x00};
// Security can be WLAN_SEC_UNSEC, WLAN_SEC_WEP, WLAN_SEC_WPA or WLAN_SEC_WPA2
#define WLAN_SECURITY WLAN_SEC_WEP
#define SERVER "XXXXX"
String DIRECTORY = "XXXXX";
uint32_t ip;
const int pin = 2;
/**************************************************************************/
/*!
@brief Sets up the HW and the CC3000 module (called automatically
on startup)
*/
/**************************************************************************/
void setup()
{
pinMode(pin, OUTPUT);
pinMode(pin, HIGH);
Serial.begin(115200);
Serial.println(F("Hello, CC3000!\n"));
Serial.print("Free RAM: "); Serial.println(getFreeRam(), DEC);
/* Initialise the module */
Serial.println(F("\nInitialising the CC3000 ..."));
if (!cc3000.begin())
{
Serial.println(F("Unable to initialise the CC3000! Check your wiring?"));
while(1);
}
/* Optional: Update the Mac Address to a known value */
/*
uint8_t macAddress[6] = { 0x08, 0x00, 0x28, 0x01, 0x79, 0xB7 };
if (!cc3000.setMacAddress(macAddress))
{
Serial.println(F("Failed trying to update the MAC address"));
while(1);
}
*/
uint16_t firmware = checkFirmwareVersion();
if (firmware < 0x113) {
Serial.println(F("Wrong firmware version!"));
for(;;);
}
/* Delete any old connection data on the module */
Serial.println(F("\nDeleting 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("\nAttempting 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!
}
ip = 0;
// Try looking up the website's IP address
Serial.print(SERVER); Serial.print(F(" -> "));
while (ip == 0) {
if (! cc3000.getHostByName(SERVER, &ip)) {
Serial.println(F("Couldn't resolve!"));
}
delay(500);
}
cc3000.printIPdotsRev(ip);
client = cc3000.connectTCP(ip, 80);
}
void loop()
{
if(!cc3000.checkConnected())
{
cc3000.connectToAP(WLAN_SSID, WLAN_PASS, WLAN_SECURITY);
Serial.println("connection was lost but succefully retrieved");
}
if(!client.connected())
{
client = cc3000.connectTCP(ip, 80);
Serial.println("client lost connection but succefully retrieved it");
}
String request = "GET " + DIRECTORY + " HTTP/1.1\r\n" +
"Host: " + SERVER + "\r\n" +
"Connection: keep-alive\r\n\r\n";
send_request(request);
}
/**************************************************************************/
/*!
@brief Tries to read the 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 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 send_request (String request) {
String req = request;
if (client.connected())
{
client.print(req);
delay(5);
client.println();
delay(5);
}
else
{
Serial.print(F("failed to connect from loop"));
return;
}
// read response
while (client.connected()) {
while (client.available()) {
if(finder.find("\n\r")){
int line = finder.getValue();
Serial.print(line);
if (line == 1) {
digitalWrite(pin, HIGH);
Serial.println("LED ON");
}
else {
digitalWrite(pin, LOW);
Serial.println("led off");
}
}
}
}
client.close();
}
can somene tell me how to reduce this delay time?