Pull int out of HTTP Response

Thanks for the help in advance. I am currently writing a program that receives an http response and I need to pull one specific integer out of it. The http response currently comes through as a char. I have used sscanf for something like this before, but I can't figure out how to use it here. I am trying to pull the value after Current Value.

This is an example of the response:

HTTP/1.1 200 OK
Date: Wed, 18 Dec 2013 04:28:20 GMT
Content-Type: application/json; charset=utf-8
Content-Length: 177
Connection: close
X-Request-Id: XXXXXXXXXX
Cache-Control: max-age=86400
X-Licence: http://creativecommons.org/publicdomain/zero/1.0/
Vary: Accept-Encoding

{"id":"RequestedTemp","current_value":"150","at":"2013-12-15T03:23:09.472765Z","max_value":"225.0","min_value":"150.0","unit":{"symbol":"F","label":"degrees"},"version":"1.0.0"}

The http response currently comes through as a char.

No, it does not. A collection of characters, perhaps. In which case, collecting them in an array is a worthwhile thing to do.

but I can't figure out how to use it here. I am trying to pull the value after Current Value.

You could use strtok() and atoi() after you have collected the data in a NULL terminated char array. When the token is "current_value", the next token of interest (not necessarily the next token) is the temperature token, which you can pass to atoi().

I am trying to pull the value after Current Value.

There is an application called textfinder (below) that captures text out of a text stream. there have been previous post of "weather" code that captures data from returned data from weather sites. You can do simple things like count the " in the returned string and capture the characters after the appropriate number have been counted.

http://playground.arduino.cc/Code/TextFinder

I attempted to use textFinder in the code below, but get the error "no matching function for call to "Textfinder::TextFinder(Adafruit_CC3000_Client&)

/*************************************************** 
  This is a sketch to use the CC3000 WiFi chip & Xively
  
  Written by Marco Schwartz for Open Home Automation
 ****************************************************/

// Libraries
#include <Adafruit_CC3000.h>
#include <ccspi.h>
#include <SPI.h>
#include <string.h>
#include "utility/debug.h"
#include<stdlib.h>
#include <PID_V1.h>
#include <TextFinder.h>

// Define CC3000 chip pins
#define ADAFRUIT_CC3000_IRQ   3
#define ADAFRUIT_CC3000_VBAT  5
#define ADAFRUIT_CC3000_CS    10

// Define Thermo Pins
#define Thermistor1PIN 1                  // Cabinet Thermistor
#define Thermistor2PIN 2                  // Meat Thermistor 1
#define Thermistor3PIN 3                  // Meat Thermistor 2
#define Thermistor4PIN 4                  // Meat Thermistor 3

//Define Pin for the Relay
#define RelayPin 6

// Create CC3000 instances
Adafruit_CC3000 cc3000 = Adafruit_CC3000(ADAFRUIT_CC3000_CS, ADAFRUIT_CC3000_IRQ, ADAFRUIT_CC3000_VBAT,
                                         SPI_CLOCK_DIV2); // you can change this clock speed
                                         


// WLAN parameters
#define WLAN_SSID       "XXXXXXX"
#define WLAN_PASS       "XXXXXXX"
// Security can be WLAN_SEC_UNSEC, WLAN_SEC_WEP, WLAN_SEC_WPA or WLAN_SEC_WPA2
#define WLAN_SECURITY   WLAN_SEC_WPA2

// Xively parameters
#define WEBSITE  "XXXXXXX"
#define API_key  "XXXXXXX"
#define feedID  "XXXXXXX"

//////Thermometer Function

////////

uint32_t ip;


void setup(void)
{
  // Initialize
  Serial.begin(115200);
  
  Serial.println(F("\nInitializing..."));
  if (!cc3000.begin())
  {
    Serial.println(F("Couldn't begin()! Check your wiring?"));
    while(1);
  }
 
}

void loop(void)
{
  // Connect to WiFi network
  cc3000.connectToAP(WLAN_SSID, WLAN_PASS, WLAN_SECURITY);
  Serial.println(F("Connected!"));
  
  /* Wait for DHCP to complete */
  Serial.println(F("Request DHCP"));
  while (!cc3000.checkDHCP())
  {
    delay(100);
  }  

  // Get the website IP & print it
  ip = 0;
  Serial.print(WEBSITE); Serial.print(F(" -> "));
  while (ip == 0) {
    if (! cc3000.getHostByName(WEBSITE, &ip)) {
      Serial.println(F("Couldn't resolve!"));
    }
    delay(500);
  }
  cc3000.printIPdotsRev(ip);
  
  senddata();
  
  delay (10000);
  
  getdata();
    
  cc3000.disconnect();
  
  ////////////////
  
  
  
  // Wait 10 seconds until next update
  delay(50000);
}
/////sending additional request

  char getdata(){
  Adafruit_CC3000_Client clientb = cc3000.connectTCP(ip, 80);
    clientb.println("GET /v2/feeds/" + String(feedID) + "/datastreams/RequestedTemp.json HTTP/1.0");
    clientb.println("Host: api.xively.com");
    clientb.println("X-ApiKey: " + String(API_key));
    clientb.print("Connection: close");
    clientb.println();
    clientb.println();  
    
  Serial.println(F("-------------------------------------"));
  while (clientb.connected()) {
    while (clientb.available()) {
      char d = clientb.read();
      TextFinder finder(clientb);
      finder.find("current_value");
      long value = finder.getValue();
      Serial.print(value);
      
      
        
      
    }
  }
  clientb.close();
  }
  
  
  void senddata(){  
  // Get data & transform to integers
  int temp1=Thermistor(analogRead(Thermistor1PIN));
  int temp2=Thermistor(analogRead(Thermistor2PIN));
  int temp3=Thermistor(analogRead(Thermistor3PIN));
  int temp4=Thermistor(analogRead(Thermistor4PIN));
  
  // Prepare JSON for Xively & get length
  
  int messagelength = 0;
  String message2;
  
  message2 = messages(temp1, temp2, temp3);
  
  Serial.print("priting message");
  Serial.print (message2);
  
  messagelength = message2.length();
  Serial.print("Data length");
  Serial.println(messagelength);
  Serial.println();
  
  // Send request
  Adafruit_CC3000_Client clientb = cc3000.connectTCP(ip, 80);
    Serial.println("Connected!");
    clientb.println("PUT /v2/feeds/" + String(feedID) + ".json HTTP/1.0");
    clientb.println("Host: api.xively.com");
    clientb.println("X-ApiKey: " + String(API_key));
    clientb.println("Content-Length: " + String(messagelength));
    clientb.print("Connection: close");
    clientb.println();
    clientb.println(message2);
    clientb.println();  

  
  while (clientb.connected()) {
    while (clientb.available()) {
      char c = clientb.read();
      Serial.print(c);
    }
  }
  
  

  clientb.close();
  
 }
    
  
  
  

  




//////Functions//////


/////Thermometer Function
float vcc = 4.91;                       // only used for display purposes, if used
                                        // set to the measured Vcc.
float pad = 109800;                     // balance/pad resistor value, set this to
                                        // the measured resistance of your pad resistor
float thermr = 222000;                   // thermistor nominal resistance

float Thermistor(int RawADC) {
  long Resistance;  
  float Temp;  // Dual-Purpose variable to save space.

  Resistance=((1024 * pad / RawADC) - pad);
  Temp = log(Resistance); // Saving the Log(resistance) so not to calculate  it 4 times later
  Temp = 1 / (-0.000631535825 + (0.0003824312118 * Temp) + (-0.0000003752313105 * Temp * Temp * Temp));
  Temp = Temp - 273.15;  // Convert Kelvin to Celsius                     
  

  // Uncomment this line for the function to return Fahrenheit instead.
  Temp = (Temp * 9.0)/ 5.0 + 32.0;                  // Convert to Fahrenheit
  return Temp;                                      // Return the Temperature
}




/////////Data Function

String messages(int temp1, int temp2, int temp3){
  String message2;
  String message = "";
  message2 = message + "\n" + "{\"version\":\"1.0.0\",\"datastreams\" : [ {\"id\" : \"CabinetTemp\",\"current_value\" : \"" + String(temp1) + "\"}," + "{\"id\" : \"Thermo1\",\"current_value\" : \"" + String(temp2) + "\"}," + "{\"id\" : \"Thermo2\",\"current_value\" : \"" + String(temp3) + "\"}]}";
  return message2;
}

Did you add the textfinder library to your arduino libraries folder and restart the arduino IDE?

Yes I did, it appears the stream is different for the CC3000 client.

I'm attempting PaulS suggestions now.

I attempted, but all this will return is count = infinite zeros

  Serial.println(F("-------------------------------------"));
  while (clientb.connected()) {
    while (clientb.available()) {
      char d = clientb.read();
      char* getstring;
      getstring +=d;
      char *p;
      p = strchr(getstring, 'current value');
      if(p){
        p++;
        int count;
        count = atoi(p);
        Serial.print(count);
      }

You can try counting the " like below if you capture the data string as a String. Try the below with the serial monitor, copy your data string and ctrl-v paste in the serial monitor text box, then send it to the arduino.

// zoomkat 7-30-11 serial I/O string test
// type a string in serial monitor. then send or enter
// for IDE 0019 and later

int x=0;
String readString;

void setup() {
  Serial.begin(9600);
  Serial.println("serial test 0021"); // so I can keep track of what is loaded
}

void loop() {
  while (Serial.available()) {
    delay(2);  //delay to allow byte to arrive in input buffer
    char c = Serial.read();
    if (c=='"') x=(x+1);
    if (x==7) readString += c;
  }
  if (readString.length() >0) {
    readString = readString.substring(1);
    Serial.print("Your value is: ");
    Serial.println(readString);
    readString="";
    x=0;
  } 
}
      char* getstring;
      getstring +=d;

The pointer, getstring, doesn't point to any memory. You can't add to a string using pointer value manipulation.

      char *p;
      p = strchr(getstring, 'current value');

Even if that had worked, the second argument to this function call is a single character. Which ONE key did you press to make that single character appear?

The first argument is a garbage pointer that you think points to one character. It is NOT a string (a NULL terminated array of chars), which is what strchr() expects.

You REALLY need to get a C book, and understand pointers, arrays, strings, etc. BEFORE you resort to the String crutch that zoomkat advocates like everyone was a cripple.

Paul S, I completely understand your point and am trying to learn the information as much as possible. As an engineer I prefer to figure out the way things work instead of using band aids to solve problems. I've already invested in a couple books and have started to work through them.

zoomkat, that section of code worked perfectly. Thank you for your help.

PaulS:
…like everyone was a cripple.

That appears to be your thoughts, not mine. Merry Christmas!

zoomkat:

PaulS:
...like everyone was a cripple.

That appears to be your thoughts, not mine. Merry Christmas!

Thank you, and a Merry Christmas to you, too.