Calculations with float & strings

Hi All,

I have been working on a script on my WEMOS D1 mini that gets data via JSON and then performs a calculation on it. For purposes of this example, I have supplied some code which works fine until I try and do a calculation when the String returned from JSON fails as its a string and not a float(It does not have to be a float its just what I thought it was supposed to be).
I have done a search and tried to find some good example scripts to see how it's done but I've not been very successful.

The addition of lines 6, 79-81 causes the script to fail.

79: error: no match for 'operator-' (operand types are 'String' and 'int')

    value = result - 100;

                   ^

exit status 1
no match for 'operator-' (operand types are 'String' and 'int')

I also tried changing the lines from String to float or unsigned long without much success.

//Libraries
#include <ESP8266WiFi.h> //Provides WiFI
#include <ArduinoJson.h> //Handles the returned JSON data

//Setup Calc Var
  String value;

//WiFi settings
  const char* ssid     = "VMxxxx";
  const char* password = "xxxxx";

//URL Domain
  const char* host = "coinmarketcap.northpole.ro"; //Test JSON data
//We now create the full URL for the request
  String url = String("/api/btc.json");
  
void setup() {
  Serial.begin(115200);   // Serial Setup
  WiFi.begin(ssid, password); // Do wifi stuff
   while (WiFi.status() != WL_CONNECTED) {
   delay(500);
   }
  
  Serial.println(WiFi.localIP()); //Show WiFi connection
  delay(100);
  //Display URL on serial
  Serial.print("URL: ");
  Serial.print(host);
  Serial.println(url);
  }

void jsonfunc(){
    WiFiClient client;
  const int httpPort = 80;
  if (!client.connect(host, httpPort)) {
     Serial.println("connection failed");
    return;
  }

// This will send the request to the server
  client.print(String("GET ") + url + " HTTP/1.1\r\n" +
               "Host: " + host + "\r\n" + 
               "Connection: close\r\n\r\n");
//Serial.println("Got Data...");
  delay(1000);//Delay added as without data request fails.
  
// Read all the lines of the reply from server.
  String answer;
  while(client.available()){
    String line = client.readStringUntil('\r');
    answer += line;
  }
   client.stop();

// Convert to JSON
  String jsonAnswer;
  int jsonIndex;

  for (int i = 0; i < answer.length(); i++) {
   if (answer[i] == '{') {
     jsonIndex = i;
     break;
    }
  }

// Get JSON data
  jsonAnswer = answer.substring(jsonIndex);
  jsonAnswer.trim();

//Get current currency price data
  int rateIndex = jsonAnswer.indexOf("price");
  String result = jsonAnswer.substring(rateIndex + 8, rateIndex + 18);

//Print Results to the serial
   Serial.print("Returns = ");
   Serial.println(result);

//Calulations on Result
   value = result - 100;
   Serial.print("Calc Returns = ");
   Serial.println(value);
}

void loop() {
   jsonfunc(); //Call the jsonfunc Function :)
   delay(10000);//Wait to limit number or web requests
}

If anyone can show me how to get around this or point me at an example that does something similar it would be a great help.

Many thanks

Calculations with float & strings

Can you think of a calculation involving, let's say π, and " the quick brown fox jumps over the lazy dog" ?

Thanks for your constructive comment.

pobrika:
Thanks for your constructive comment.

You're very welcome

pobrika:
Thanks for your constructive comment.

I hope you're not being sarcastic. If you don't understand the relevance of that comment then think about what you are trying to do, add a String and a number, and then read that comment again until you understand what he is trying to tell you.

Next, go to the page in the reference for the String data type and look at the bottom of the page at the functions it provides. One of them should stand out to you.

If you declare value as an int, you could use:

value = result.toInt() - 100;

There also exists toFloat()

Thankyou lesept :slight_smile:

I spent most of yesterday afternoon stuck trying to work this out. I knew I could not use a string and that it had to be a float as I wrote in my post, but I could not find a resource that showed how this was achieved. It's sometimes as someone new to Arduino hard to know what you're looking for when you don't know all the available options and terminology. I found the following example which is exactly the kind of example and result I was looking for toFloat() - Arduino Reference I'm slightly embarrassed that I have now seen the example String toInt example in the ide which I missed.

Many thanks.
P

You're welcome, have fun!