Go Down

Topic: replace function after a http request (Read 104 times) previous topic - next topic

sebastiaan_maes

hello, I"m currently working on a wheather station project. What I try to do is the following: first of all make connection to the wheather API, read all of it and place it in a string.Finally I want to set all the diffrent values that I get in to diffrent variables. So far I can read my api, and print the text from the api to the serial monitor. Now the problem: This string contains double quotes, so I want to use operator functions to remove them. But when I try to replace the quotes (because these are causing trouble) and print it out to the monitor it does nothing. Can someone help? Thanks in advandce!

what it looks like:
  {"location":{"name":"Paris","region":"Ile-de-France","country":"France","lat":48.87,"lon":2.33,"tz_id":"Europe/Paris","localtime_epoch":1531602120,"localtime":"2018-07-14 23:02"},"current":{"last_updated_epoch":1531601105,"last_updated":"2018-07-14 22:45","temp_c":24.0,"temp_f":75.2,"is_day":0,"condition":{"text":"Clear","icon":"//cdn.apixu.com/weather/64x64/night/113.png","code":1000},"wind_mph":8.1,"wind_kph":13.0,"wind_degree":30,"wind_dir":"NNE","pressure_mb":1016.0,"pressure_in":30.5,"precip_mm":0.0,"precip_in":0.0,"humidity":50,"cloud":0,"feelslike_c":25.2,"feelslike_f":77.4,"vis_km":10.0,"vis_miles":6.0}

I want to replace all the double quotes to a hashtag or an another character that doenst really matter, only not single or double quotes. my code so far:


//zoomkat 11-04-13
//simple client test
//for use with IDE 1.0.1
//with DNS, DHCP, and Host
//open serial monitor and send an e to test client GET
//for use with W5100 based ethernet shields
//remove SD card if inserted
//data from myIP server captured in readString

#include <SPI.h>
#include <Ethernet.h>
String readString;

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; //physical mac address

char serverName[] = "api.apixu.com"; // myIP server test web page server
EthernetClient client;

//////////////////////

void setup(){

  if (Ethernet.begin(mac) == 0) {
    Serial.println("Failed to configure Ethernet using DHCP");
    // no point in carrying on, so do nothing forevermore:
    while(true);
  }

  Serial.begin(9600);
}

void loop(){
   sendGET(); // call sendGET function below when byte is an e
}
//////////////////////////

void sendGET() //client function to send/receive GET request data.
{
  if (client.connect(serverName, 80)) {  //starts client connection, checks for connection
    Serial.println("connected");
    client.println("GET /v1/current.json?key=***************&q=Paris HTTP/1.1"); //download text
    client.println("Host: api.apixu.com");
    client.println("Connection: close");  //close 1.1 persistent connection 
    client.println(); //end of get request
  }
  else {
    Serial.println("connection failed"); //error message if no client connect
    Serial.println();
  }

  while(client.connected() && !client.available()) delay(1); //waits for data
  while (client.connected() || client.available()) { //connected or data available
    char c = client.read(); //gets byte from ethernet buffer
    readString += c; //places captured byte in readString
  }

  //Serial.println();
  client.stop(); //stop client
  Serial.print(readString); //prints readString to serial monitor
  readString.replace("\"", "\#");
  Serial.print(readString);
  readString=""; //clear readString variable

}



6v6gt

Your sample from the API is json encoded.
There are arduino json parser libraries which can extract the values from the json encoded string so you don't have to do it manually:

Eg https://github.com/bblanchon/ArduinoJson


If you do want to do something like this:

Code: [Select]
readString.replace("\"", "\#");

I guess it should be something like this because I don't think the # has to be escaped:

Code: [Select]
String myNewString = readString.replace("\"", "#");

or what result did you get with your attempt ?

Go Up