Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Python calls to Library #29

Closed
fischerjade opened this issue Dec 5, 2014 · 20 comments
Closed

Python calls to Library #29

fischerjade opened this issue Dec 5, 2014 · 20 comments

Comments

@fischerjade
Copy link

First of all thank you for the libraries and the documentation, they have been very helpful. I have an issue though. I am trying to make rest calls to the CC3000 and it doesn't seem to work even though I can ping it. I have also created my own variables and were able to retrieve them through the URL. EX. http://192.168.0.100/temperature. I want to access it through Python so I tried:

import urllib2
response = urllib2.urlopen('http://python.org/')
html = response.read()
print html

We even tried to output this to a file and retrieve it with a browser and we can't retrieve anything from the CC3000 it just hangs. This python code will work though. We were able to get html pages from the Raspberry Pi web server in this manner.

I know java script might work but I really want to use Kivy with python and retrieve my variables from the CC3000 so I can display them with python. Any help would be appreciated.

@marcoschwartz
Copy link
Owner

Hello, thanks for using my library! Actually I had some success with calls from Python, but using the pycurl module (http://pycurl.sourceforge.net/). I used it for automated tests, but now I moved to Node.js for that part.

Here is a function I used to make cURL calls using pycurl:

def curl_call(target, command):

  buf = StringIO.StringIO()

  c = pycurl.Curl()
  c.setopt(c.URL, target + command)
  c.setopt(c.WRITEFUNCTION, buf.write)
  c.setopt(pycurl.CONNECTTIMEOUT, 5)
  c.perform()

  return buf.getvalue()

Then you can do calls with: curl_call('ip_address',"/digital/6/1")

or to get data back using the json module:

answer = json.loads(curl_call('ip_address',"/temperature"))

Let me know if it works!

@fischerjade
Copy link
Author

I believe there is a problem with the arduino script.  We get a message of 
{"temperature": 28, "id": "008", "n
when I enter this in the browser.
http://192.168.0.100/temperature
Then the browser just hangs.  We tried to access the CC3000 with 2 different scripts incorporating 2 different URL libraries and it just hangs. 
Any help would be great!
 Jade Fischer,
fischerjade@yahoo.com

 On Sunday, December 7, 2014 8:10 AM, marcoschwartz <notifications@github.com> wrote:

Hello, thanks for using my library! Actually I had some success with calls from Python, but using the pycurl module (http://pycurl.sourceforge.net/). I used it for automated tests, but now I moved to Node.js for that part.Here is a function I used to make cURL calls using pycurl:def curl_call(target, command):buf = StringIO.StringIO()c = pycurl.Curl()
c.setopt(c.URL, target + command)
c.setopt(c.WRITEFUNCTION, buf.write)
c.setopt(pycurl.CONNECTTIMEOUT, 5)
c.perform()return buf.getvalue()Then you can do calls with: curl_call('ip_address',"/digital/6/1")or to get data back using the json module: answer = json.loads(curl_call('ip_address',"/temperature"))Let me know if it works!—
Reply to this email directly or view it on GitHub.

@makecademy
Copy link

In that case it looks more like an Arduino than a Python problem indeed. Could you give me:

  • the Arduino board you are using
  • which Arduino IDE you are using

Also make sure you have the latest versions of the CC3000 library & of the aREST library. Thanks!

@fischerjade
Copy link
Author

The board is the uno but I do have a Mega2560The IDE is the Arduino 1.5.8 because you had stated that I needed the upgraded version.
Thank you again for such a quick reply. Jade Fischer,
fischerjade@yahoo.com

 On Thursday, December 11, 2014 11:50 AM, openhomeautomation <notifications@github.com> wrote:

In that case it looks more like an Arduino than a Python problem indeed. Could you give me:

  • the Arduino board you are using
  • which Arduino IDE you are using
    Also make sure you have the latest versions of the CC3000 library & of the aREST library. Thanks!—
    Reply to this email directly or view it on GitHub.

@makecademy
Copy link

I guess the problem is the IDE indeed. There are many known problems with the CC3000 library + the 1.5.8 IDE. Try with the 1.5.7 and let me know if it works. You can get it from: http://arduino.cc/en/Main/OldSoftwareReleases

@cpmath
Copy link

cpmath commented Dec 12, 2014

I had the same problem, also using arduino 1.5.8, I followed
https://forums.adafruit.com/viewtopic.php?f=22&t=49722 and set
#define SEND_NON_BLOCKING 1
in socket.cpp in the CC3000 library, and now the CC3000 and aREST libraries are working together perfectly for me.

@fischerjade
Copy link
Author

Thanks Guys I have made the changes and I will try it later. I will let you know what happened.

@fischerjade
Copy link
Author

I uninstalled the Arduino 1.5.8 IDE. I then installed the 1.5.7 version from the link above. I then changed the socket.cpp file by adding #define SEND_NON_BLOCKING 1 above #define SOCKET_OPEN_PARAMS_LEN (12) .
I get the same result.

{"temperature": 28, "id": "008", "n

when "192.168.0.100/temperature" is used in the URL. I get the result:

{"humidity": 40, "id": "008", "name

when i use humidity instead of temperature. This looks like only 35 characters can be sent is there a variable that would be set to only allow a certain amount of characters to be sent in json format? Here is the debug info I am getting because I set debug to 1.

GET /
temperature HTTP/
Sending command
Command: v
State: x
State of buffer at the start:
Added to buffer: HTTP/1.1 200 OK
Access-Control-Allow-Origin: *
Access-Control-Allow-Methods: POST, GET, PUT, OPTIONS
Content-Type: application/json
Connection: close

Added to buffer: {"
Added to buffer: temperature
Added to buffer: ":
Added to buffer: 28
Added to buffer: ,
Added to buffer: "id": "
Added to buffer: 008
Added to buffer: ", "name": "
Added to buffer: mighty_cat
Added to buffer: ", "connected": true}

State of buffer at the end: HTTP/1.1 200 OK
Access-Control-Allow-Origin: *
Access-Control-Allow-Methods: POST, GET, PUT, OPTIONS
Content-Type: application/json
Connection: close

{"temperature": 28, "id": "008", "name": "mighty_cat", "connected": true}

Sending buffer: HTTP/1.1 200 OK
Access-Control-Allow-Origin: *
Access-Control-Allow-Methods: POST, GET, PUT, OPTIONS
Content-Type: application/json
Connection: close

@marcoschwartz
Copy link
Owner

Strange, according to the debug part everything works fine, but not in your browser. What browser are you using for the test? Can you try with another one? I have success with Chrome & Safari for Mac.

@fischerjade
Copy link
Author

I have used the chromium browser with the raspberry pi and I also hit it with my windows machine with IE and Chrome.  It all shows the same message with only 35 characters.
 Jade Fischer,
fischerjade@yahoo.com

 On Monday, December 15, 2014 7:49 AM, marcoschwartz <notifications@github.com> wrote:

Strange, according to the debug part everything works fine, but not in your browser. What browser are you using for the test? Can you try with another one? I have success with Chrome & Safari for Mac.—
Reply to this email directly or view it on GitHub.

@marcoschwartz
Copy link
Owner

Thanks for the info, I will try it again with the browser you mentioned and keep you updated!

@fischerjade
Copy link
Author

Thank you for all the help.
 Jade Fischer,
fischerjade@yahoo.com

 On Tuesday, December 16, 2014 12:22 PM, marcoschwartz <notifications@github.com> wrote:

Thanks for the info, I will try it again with the browser you mentioned and keep you updated!—
Reply to this email directly or view it on GitHub.

@fischerjade
Copy link
Author

Thank you everyone that helped. We figured it out. If you change the sendBuffer(client,64,1000); call you will get the whole json message. We first worked with the 3rd parameter and hoped that the delay would let the whole message get sent but it looks like the 2nd and 3rd parameter work together. The best result was with this call sendBuffer(client,64,1000); in the "void handle(Adafruit_CC3000_ClientRef& client) " method.

@marcoschwartz
Copy link
Owner

Interesting, I will test that on the Mega I have available. Can you submit a pull request with your solution? Thanks a lot!

@fischerjade
Copy link
Author

Can I send you the file because I don't know how to submit a pull request and I don't want to create a problem with your code.  If you have steps to submit it I am all ears. Jade Fischer,
fischerjade@yahoo.com

 On Friday, December 19, 2014 4:49 AM, marcoschwartz <notifications@github.com> wrote:

Interesting, I will test that on the Mega I have available. Can you submit a pull request with your solution? Thanks a lot!—
Reply to this email directly or view it on GitHub.

@marcoschwartz
Copy link
Owner

Sure, just send it over to: marcolivier.schwartz@gmail.com. I will then test it & push the changes. I need to make sure it doesn't interfere with the behavior of the library with other boards. Thanks!

@fischerjade
Copy link
Author

I sent the file but here is the only change and it is the 2 parameters going into the sendBuffer() call.
// Handle request with the Adafruit CC3000 WiFi library//Fix.  Changed the sendBuffer values from (client,32,100)#ifdef ADAFRUIT_CC3000_Hvoid handle(Adafruit_CC3000_ClientRef& client) {    if (client.available()) {
    // Handle request    handle_proto(client,true,0);            // Answer*********************************************************_    sendBuffer(client,64,1000); //Here is the change**_***********************************************************    client.stop();  
    // Reset variables for the next command    reset_status();  } }
 Jade Fischer,
fischerjade@yahoo.com

 On Monday, December 22, 2014 3:47 AM, marcoschwartz <notifications@github.com> wrote:

Sure, just send it over to: marcolivier.schwartz@gmail.com. I will then test it & push the changes. I need to make sure it doesn't interfere with the behavior of the library with other boards. Thanks!—
Reply to this email directly or view it on GitHub.
/*
aREST Library for Arduino
See the README file for more details.

Written in 2014 by Marco Schwartz under a GPL license.

Version 1.9.4

Changelog:

Version 1.9.4: Bug fixes & added support for configuring analog pints as digital outputs
Version 1.9.3: Added description of available variables for the /id and / routes
Version 1.9.2: Added compatibility with the Arduino WiFi library
Version 1.9.1: Added compatibility with CORS
Version 1.9: New speedup of the library (answers 2x faster in HTTP compared to version 1.8)

Version 1.8: Speedup of the library (answers 2.5x faster with the CC3000 WiFi chip)

Version 1.7.5: Reduced memory footprint of the library
Version 1.7.4: Added a function to read all analog & digital inputs at once
Version 1.7.3: Added LIGHTWEIGHT mode to only send limited data back
Version 1.7.2: Added possibility to assign a status pin connected to a LED
Version 1.7.1: Added possibility to change number of exposed variables & functions
Version 1.7: Added compatibility with the Arduino Due & Teensy 3.x

Version 1.6: Added compatibility with the Arduino Yun

Version 1.5: Size reduction, and added compatibility with Adafruit BLE

Version 1.4: Added authentification with API key

Version 1.3: Added support for the Ethernet shield

Version 1.2: Added support of Serial communications

Version 1.1: Added variables & functions support

Version 1.0: First working version of the library

*/

// Include Arduino header
#include "Arduino.h"

// Which board?
#if defined(AVR_ATmega1280) || defined(AVR_ATmega2560)
#define NUMBER_ANALOG_PINS 16
#define NUMBER_DIGITAL_PINS 54
#define OUTPUT_BUFFER_SIZE 600
#elif defined(AVR_ATmega328P)
#define NUMBER_ANALOG_PINS 6
#define NUMBER_DIGITAL_PINS 14
#define OUTPUT_BUFFER_SIZE 275
#else
#define NUMBER_ANALOG_PINS 6
#define NUMBER_DIGITAL_PINS 14
#define OUTPUT_BUFFER_SIZE 275
#endif

// Size of name & ID
#define NAME_SIZE 20
#define ID_SIZE 10

// Debug mode
#ifndef DEBUG_MODE
#define DEBUG_MODE 0
#endif

// Use light answer mode
#ifndef LIGHTWEIGHT
#define LIGHTWEIGHT 0
#endif

// Default number of max. exposed variables
#ifndef NUMBER_VARIABLES
#define NUMBER_VARIABLES 5
#endif

// Default number of max. exposed functions
#ifndef NUMBER_FUNCTIONS
#define NUMBER_FUNCTIONS 5
#endif

class aREST {

public:
aREST() {
command = 'u';
pin_selected = false;

status_led_pin = 255;
state = 'u';
}

// Set status LED
void set_status_led(uint8_t pin){

// Set variables
status_led_pin = pin;

// Set pin as output
pinMode(status_led_pin,OUTPUT);
}

// Glow status LED
void glow_led() {

if(status_led_pin != 255){
unsigned long i = millis();
int j = i % 4096;
if (j > 2048) { j = 4096 - j;}
analogWrite(status_led_pin,j/8);
}
}

// Send HTTP headers for Ethernet & WiFi
void send_http_headers(){

addToBuffer(F("HTTP/1.1 200 OK\r\nAccess-Control-Allow-Origin: *\r\nAccess-Control-Allow-Methods: POST, GET, PUT, OPTIONS\r\nContent-Type: application/json\r\nConnection: close\r\n\r\n"));
}

// Reset variables after a request
void reset_status() {
answer = "";
command = 'u';
pin_selected = false;
state = 'u';
arguments = "";

index = 0;
//memset(&buffer[0], 0, sizeof(buffer));

}

// Handle request with the Adafruit CC3000 WiFi library
//Fix. Changed the sendBuffer values from (client,32,100)
#ifdef ADAFRUIT_CC3000_H
void handle(Adafruit_CC3000_ClientRef& client) {

if (client.available()) {

// Handle request
handle_proto(client,true,0);

// Answer
sendBuffer(client,64,1000);
client.stop();  

// Reset variables for the next command
reset_status();

}
}

// Handle request with the Arduino Yun
#elif defined(YUN_CLIENT_H)
void handle(YunClient& client) {

if (client.available()) {

// Handle request
handle_proto(client,false,0);

// Answer
sendBuffer(client,25,50);
client.stop();

// Reset variables for the next command
reset_status();

}
}

// Handle request with the Adafruit BLE board
#elif defined(ADAFRUIT_BLE_UART_H)
void handle(Adafruit_BLE_UART& serial) {

if (serial.available()) {

// Handle request
handle_proto(serial,false,0);

// Answer
sendBuffer(serial,100,5);

// Reset variables for the next command
reset_status();

}
}

// Handle request for the Arduino Ethernet shield
#elif defined(ethernet_h)
void handle(EthernetClient& client){

if (client.available()) {

// Handle request
handle_proto(client,true,0);

// Answer
sendBuffer(client,50,5);
client.stop();  

// Reset variables for the next command
reset_status();   

}
}

// Handle request for the Arduino Ethernet shield
#elif defined(WiFi_h)
void handle(WiFiClient& client){

if (client.available()) {

// Handle request
handle_proto(client,true,0);

// Answer
sendBuffer(client,50,5);
client.stop();  

// Reset variables for the next command
reset_status();   

}
}

#elif defined(CORE_TEENSY)
// Handle request on the Serial port
void handle(usb_serial_class& serial){

if (serial.available()) {

// Handle request
handle_proto(serial,false,1);

// Answer
sendBuffer(serial,25,10);

// Reset variables for the next command
reset_status();     

}
}

#else
// Handle request on the Serial port
void handle(HardwareSerial& serial){

if (serial.available()) {

// Handle request
handle_proto(serial,false,1);

// Answer
sendBuffer(serial,25,10);

// Reset variables for the next command
reset_status();     

}
}
#endif

void handle(char * string) {

// Process String
handle_proto(string);

// Reset variables for the next command
reset_status();
}

void handle_proto(char * string) {
// Check if there is data available to read
for (int i = 0; i < strlen(string); i++){

char c = string[i];
answer = answer + c;

// Process data
process(c);

}

// Send command
send_command(false);
}

template
void handle_proto(T& serial, bool headers, uint8_t read_delay)
{

// Check if there is data available to read
while (serial.available()) {

// Get the server answer
char c = serial.read();
delay(read_delay);
answer = answer + c;
//if (DEBUG_MODE) {Serial.print(c);}

// Process data
process(c);

}

// Send command
send_command(headers);
}

void process(char c){

// Check if we are receveing useful data and process it
if ((c == '/' || c == '\r') && state == 'u') {

  if (DEBUG_MODE) {Serial.println(answer);}

  // If the command is mode, and the pin is already selected    
  if (command == 'm' && pin_selected && state == 'u') {

    // Get state
    state = answer[0];

 }

 // If a digital command has been received, process the data accordingly     
 if (command == 'd' && pin_selected && state == 'u') {

   // If it's a read command, read from the pin and send data back
   if (answer[0] == 'r') {state = 'r';}

   // If not, get value we want to apply to the pin        
   else {value = answer.toInt(); state = 'w';}
 }

 // If analog command has been selected, process the data accordingly     
 if (command == 'a' && pin_selected && state == 'u') {

   // If it's a read, read from the correct pin
   if (answer[0] == 'r') {state = 'r';}

   // Else, write analog value        
   else {value = answer.toInt(); state = 'w';}
 }

 // If the command is already selected, get the pin     
 if (command != 'u' && pin_selected == false) {

   // Get pin
   if (answer[0] == 'A') {
     pin = 14 + answer[1] - '0';  
   }
   else {
     pin = answer.toInt();
   }
   if (DEBUG_MODE) {
    Serial.print("Selected pin: ");
    Serial.println(pin);
   }
   pin_selected = true;

   // Nothing more ?
   if ((answer[1] != '/' && answer[2] != '/') 
    || (answer[1] == ' ' && answer[2] == '/')
    || (answer[2] == ' ' && answer[3] == '/')) {

    // Nothing more & digital ?
    if (command == 'd') {

      // Read all digital ?
      if (answer[0] == 'a') {state = 'a';}

      // Save state & end there
      else {state = 'r';}
    }

   // Nothing more & analog ?
   if (command == 'a') {

     // Read all analog ?
     if (answer[0] == 'a') {state = 'a';}

     // Save state & end there
     else {state = 'r';}
   }
 }  

}

 // Digital command received ?    
 if (answer.startsWith("digital")) {command = 'd';}

 // Mode command received ?
 if (answer.startsWith("mode")) {command = 'm';}

 // Analog command received ?
 if (answer.startsWith("analog")) {command = 'a';}

 // Variable or function request received ?
 if (command == 'u') {

   // Check if variable name is in array
   for (uint8_t i = 0; i < variables_index; i++){
     if(answer.startsWith(int_variables_names[i])) {

       // End here
       pin_selected = true;
       state = 'x';

       // Set state
       command = 'v';
       value = i;
     }
   }

   // Check if function name is in array
   for (uint8_t i = 0; i < functions_index; i++){
     if(answer.startsWith(functions_names[i])) {

       // End here
       pin_selected = true;
       state = 'x';

       // Set state
       command = 'f';
       value = i;

       // Get command
       uint8_t header_length = strlen(functions_names[i]) + 8;
       //strcpy(arguments, answer.substring(header_length).c_str());
       arguments = answer.substring(header_length);
     }
   }

   // If the command is "id", return device id, name and status
   if ( (answer[0] == 'i' && answer[1] == 'd') ){

       // Set state
       command = 'i';

       // End here
       pin_selected = true;
       state = 'x';
   }

   if (answer[0] == ' '){

       // Set state
       command = 'r';

       // End here
       pin_selected = true;
       state = 'x';
   }

 }

 answer = "";
 }

}

bool send_command(bool headers) {

if (DEBUG_MODE) {
Serial.println(F("Sending command"));
Serial.print(F("Command: "));
Serial.println(command);
Serial.print(F("State: "));
Serial.println(state);
Serial.print(F("State of buffer at the start: "));
Serial.println(buffer);
}

// Start of message
if (headers) {send_http_headers();}

// Mode selected
if (command == 'm'){

 // Send feedback to client 
 if (!LIGHTWEIGHT){
   addToBuffer(F("{\"message\": \"Pin D"));
   addToBuffer(pin); 
 } 

 // Input
 if (state == 'i'){

  // Set pin to Input     
  pinMode(pin,INPUT);

  // Send feedback to client
  if (!LIGHTWEIGHT){addToBuffer(F(" set to input\", "));}
 }

 // Output
 if (state == 'o'){

   // Set to Output  
   pinMode(pin,OUTPUT);

   // Send feedback to client
   if (!LIGHTWEIGHT){addToBuffer(F(" set to output\", "));}
 }

}

// Digital selected
if (command == 'd') {
if (state == 'r'){

   // Read from pin
   value = digitalRead(pin);

   // Send answer
   if (LIGHTWEIGHT){addToBuffer(value);}
   else {
    addToBuffer(F("{\"return_value\": "));
    addToBuffer(value);
    addToBuffer(F(", "));
  }
 }

 #if !defined(__AVR_ATmega32U4__)
 if (state == 'a') {
   if (!LIGHTWEIGHT) {addToBuffer(F("{"));}

   for (uint8_t i = 0; i < NUMBER_DIGITAL_PINS; i++) {       

     // Read analog value
     value = digitalRead(i);

     // Send feedback to client
     if (LIGHTWEIGHT){
       addToBuffer(value);
       addToBuffer(F(","));
     }
     else {
       addToBuffer(F("\"D"));
       addToBuffer(i);
       addToBuffer(F("\": "));
       addToBuffer(value);
       addToBuffer(F(", "));
     } 
 }
}
#endif

 if (state == 'w') {

   // Apply on the pin      
   digitalWrite(pin,value);

   // Send feedback to client
   if (!LIGHTWEIGHT){
    addToBuffer(F("{\"message\": \"Pin D"));
    addToBuffer(pin);
    addToBuffer(F(" set to "));
    addToBuffer(value);
    addToBuffer(F("\", "));
   }
 }

}

// Analog selected
if (command == 'a') {
if (state == 'r'){

   // Read analog value
   value = analogRead(pin);

   // Send feedback to client
   if (LIGHTWEIGHT){addToBuffer(value);}
   else {
    addToBuffer(F("{\"return_value\": "));
    addToBuffer(value);
    addToBuffer(F(", "));
   }
 }
 #if !defined(__AVR_ATmega32U4__)
 if (state == 'a') {
   if (!LIGHTWEIGHT) {addToBuffer(F("{"));}

   for (uint8_t i = 0; i < NUMBER_ANALOG_PINS; i++) {       

     // Read analog value
     value = analogRead(i);

     // Send feedback to client
     if (LIGHTWEIGHT){
       addToBuffer(value);
       addToBuffer(F(","));
     }
     else {
       addToBuffer(F("\"A"));
       addToBuffer(i);
       addToBuffer(F("\": "));
       addToBuffer(value);
       addToBuffer(F(", "));
     } 
 }

}
#endif
if (state == 'w') {

 // Write output value
 analogWrite(pin,value);

 // Send feedback to client
 addToBuffer(F("{\"message\": \"Pin D"));
 addToBuffer(pin);
 addToBuffer(F(" set to "));
 addToBuffer(value);
 addToBuffer(F("\", "));

}
}

// Variable selected
if (command == 'v') {

   // Send feedback to client
   if (LIGHTWEIGHT){addToBuffer(*int_variables[value]);}
   else {
    addToBuffer(F("{\""));
    addToBuffer(int_variables_names[value]);
    addToBuffer(F("\": "));
    addToBuffer(*int_variables[value]);
    addToBuffer(F(", ")); 
   }

}

// Function selected
if (command == 'f') {

// Execute function
uint8_t result = functions[value](arguments);

// Send feedback to client
if (!LIGHTWEIGHT) {
 addToBuffer(F("{\"return_value\": "));
 addToBuffer(result);
 addToBuffer(F(", "));
 //addToBuffer(F(", \"message\": \""));
 //addToBuffer(functions_names[value]);
 //addToBuffer(F(" executed\", "));
}

}

if (command == 'r') {
if (LIGHTWEIGHT) {addToBuffer(id);}
else {
addToBuffer(F("{"variables": {"));
if (variables_index > 0){

    for (uint8_t i = 0; i < variables_index-1; i++){
      addToBuffer(F("\""));
      addToBuffer(int_variables_names[i]);
      addToBuffer(F("\": \"int32\", "));
    }
    addToBuffer(F("\""));
    addToBuffer(int_variables_names[variables_index-1]);
    addToBuffer(F("\": \"int32\"}, "));
  }
  else {
    addToBuffer(F(" }, "));
  }

}

}

if (command == 'i') {
if (LIGHTWEIGHT) {addToBuffer(id);}
else {
addToBuffer(F("{"));
}
}

// End of message
if (LIGHTWEIGHT){
addToBuffer(F("\r\n"));
}

else {

 addToBuffer(F("\"id\": \""));
 addToBuffer(id);
 addToBuffer(F("\", \"name\": \""));
 addToBuffer(name);
 addToBuffer(F("\", \"connected\": true}\r\n"));

}

if (DEBUG_MODE) {
Serial.print(F("State of buffer at the end: "));
Serial.println(buffer);
}

// End here
return true;
}

void variable(char * variable_name, int *variable){

int_variables[variables_index] = variable;
int_variables_names[variables_index] = variable_name;
variables_index++;

}

void function(char * function_name, int (*f)(String)){

functions_names[functions_index] = function_name;
functions[functions_index] = f;
functions_index++;
}

// Set device ID
void set_id(char *device_id){

strcpy(id,device_id);
}

// Set device name
void set_name(char *device_name){

strcpy(name, device_name);
}

// Set device name
void set_name(String device_name){

device_name.toCharArray(name, NAME_SIZE);
}

// Set device ID
void set_id(String device_id){

device_id.toCharArray(id, NAME_SIZE);
}

// Add to output buffer
void addToBuffer(char * toAdd){

if (DEBUG_MODE) {
Serial.print(F("Added to buffer: "));
Serial.println(toAdd);
}

for (int i = 0; i < strlen(toAdd); i++){
buffer[index+i] = toAdd[i];
}
index = index + strlen(toAdd);
}

// Add to output buffer
void addToBuffer(int toAdd){

char number[10];
itoa(toAdd,number,10);

addToBuffer(number);
}

// Add to output buffer
void addToBuffer(const __FlashStringHelper *toAdd){

if (DEBUG_MODE) {
Serial.print(F("Added to buffer: "));
Serial.println(toAdd);
}

uint8_t idx = 0;

const char PROGMEM *p = (const char PROGMEM *)toAdd;
while (1) {
unsigned char c = pgm_read_byte(p++);
if (c == 0) break;
buffer[index + idx] = c;
idx++;
}
index = index + idx;
}

template
void sendBuffer(T& client, uint8_t chunkSize, uint8_t wait_time) {

if (DEBUG_MODE) {
Serial.print(F("Sending buffer: "));
Serial.println(buffer);
}

// Max iteration
uint8_t max_iteration = (int)(index/chunkSize) + 1;

// Send data
for (uint8_t i = 0; i < max_iteration; i++) {
char intermediate_buffer[chunkSize+1];
memcpy(intermediate_buffer, buffer + i*chunkSize, chunkSize);
intermediate_buffer[chunkSize] = '\0';

// Send intermediate buffer
#ifdef ADAFRUIT_CC3000_H
client.fastrprint(intermediate_buffer);
#else
client.print(intermediate_buffer);
#endif

}

// Wait for the client to get data
delay(wait_time);
resetBuffer();

}

char * getBuffer() {
return buffer;
}

void resetBuffer(){
memset(&buffer[0], 0, sizeof(buffer));
}

private:
String answer;
char command;
uint8_t pin;
char state;
uint16_t value;
boolean pin_selected;

char name[NAME_SIZE];
char id[ID_SIZE];
String arguments;

// Output buffer
char buffer[OUTPUT_BUFFER_SIZE];
uint16_t index;

// Status LED
uint8_t status_led_pin;

// Variables arrays
uint8_t variables_index;
int * int_variables[NUMBER_VARIABLES];
char * int_variables_names[NUMBER_VARIABLES];

// Functions array
uint8_t functions_index;
int (*functions[NUMBER_FUNCTIONS])(String);
char * functions_names[NUMBER_FUNCTIONS];
};

@marcoschwartz
Copy link
Owner

Hi, thanks for the details on the changes. I finally got myself a working Arduino Mega to make some tests. Unfortunately I couldn't reproduce the issue you are having, when I call the API from a browser or an app it works just fine with the current state of the library:

{"temperature": 22, "id": "001", "name": "wifi", "connected": true}

The issue I have with the change you proposed is that it nearly doubles the response time of the board (tested with Postman for Chrome), so I can't push this changes. I would really check again that you are using the latest versions of all libraries, the right Arduino IDE (1.5.7). I would also check that you are using the latest firmware for the CC3000 chip. To update it, just use the driverpatch_xxx sketch included in the Adafruit library. Let me know if that helps!

@fischerjade
Copy link
Author

I will check to see if everything is the correct versions. Jade Fischer,
fischerjade@yahoo.com

 On Tuesday, December 16, 2014 1:41 PM, Jade Fischer <fischerjade@yahoo.com> wrote:

Thank you for all the help.
 Jade Fischer,
fischerjade@yahoo.com

 On Tuesday, December 16, 2014 12:22 PM, marcoschwartz <notifications@github.com> wrote:

Thanks for the info, I will try it again with the browser you mentioned and keep you updated!—
Reply to this email directly or view it on GitHub.

@marcoschwartz
Copy link
Owner

Closing this now :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants