String & char[] operator+ on ESP8266

Hi,

I recently moved a code from a nano to an ESP8266 and Im getting an error when I combine a String with a char[], which on the nano worked fine. Im thinking it must be due to a String object that is in a library that the nano loads automatically whereas the ESP8266 doesnt have it.

What is the best solution? Should I load an extra library onto the ESP8266 or should I parse this differently?

#include <ESP8266WiFi.h>
#include "DHT.h"
#define DHTPIN 2     // what pin we're connected to
#define DHTTYPE DHT22   // DHT 22  (AM2302)
DHT dht(DHTPIN, DHTTYPE);

const char* ssid     = "myssid";
const char* password = "mypwd";
const char* host = "myserver.com";
unsigned long previousMillis = 0;        // will store last time was updated
const long interval = 180000; 
char tempString[20];
char humString[20];

void setup() {
  Serial.begin(115200);
  delay(100);
 
  // We start by connecting to a WiFi network
 
  Serial.println();
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);
  
  WiFi.begin(ssid, password);
  
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
 
  Serial.println("");
  Serial.println("WiFi connected");  
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());
}
 
int value = 0;

void start_test () {
  //For DHT22 Grove Pro
  float h = dht.readHumidity();
  float t = dht.readTemperature();
  dtostrf(t, 4, 2, tempString);  //convert flat to char  
  dtostrf(h, 4, 2, humString);  //convert flat to char
}

void loop() {
  ///////////INSERTED millis-sample code
    unsigned long currentMillis = millis();
  if (currentMillis - previousMillis <= interval) { //when time elapsed is LESS than interval of 30 mins    
  } else { //if deltaTimeElapsed is GREATER than target interval, sample and post...
    previousMillis = currentMillis; //sets previous-0 to current-3600
    start_test(); //sample
  }
  delay(5000);
  ++value;
 
  Serial.print("connecting to ");
  Serial.println(host);
  
  // Use WiFiClient class to create TCP connections
  WiFiClient client;
  const int httpPort = 80;
  if (!client.connect(host, httpPort)) {
    Serial.println("connection failed");
    return;
  }
  
  // We now create a URI for the request
  String url = "/emoncms/input/post.json?apikey=mykey&node=fortnite&json={\"t\":" + tempString + ",\"h\":" + humString + "}";
  Serial.print("Requesting URL: ");
  Serial.println(url);
  
  client.print(String("GET ") + url + " HTTP/1.1\r\n" +
               "Host: " + host + "\r\n" + 
               "Connection: close\r\n\r\n");
  delay(500);
  
  while(client.available()){
    String line = client.readStringUntil('\r');
    Serial.print(line);
  }
  
  Serial.println();
  Serial.println("closing connection");
}

Error:

exit status 1
invalid operands of types 'const char [90]' and 'char [20]' to binary 'operator+'

There should be more info in the error message -- including calling out the exact offending line. Please post it in its entirety.

  String url = "/emoncms/input/post.json?apikey=mykey&node=fortnite&json={\"t\":" + tempString + ",\"h\":" + humString + "}";

Nothing on the right side of that assignment is a String. You can't add character arrays to each other, only to a String object. You can work around the problem with the same trick used several lines below, cast the leftmost string as a String object and append to that:

  // We now create a URI for the request
  String url = String("/emoncms/input/post.json?apikey=mykey&node=fortnite&json={\"t\":") + tempString + ",\"h\":" + humString + "}";
  Serial.print("Requesting URL: ");
  Serial.println(url);


  client.print(String("GET ") + url + " HTTP/1.1\r\n" +
               "Host: " + host + "\r\n" +
               "Connection: close\r\n\r\n");

The offending line is where I make the final String url object:

/Users/quique123/Documents/Arduino/ESP8266DirectHTTPGET/ESP8266DirectHTTPGET.ino: In function 'void loop()':
ESP8266DirectHTTPGET:76:112: error: invalid operands of types 'const char [90]' and 'char [20]' to binary 'operator+'
   String url = "/emoncms/input/post.json?apikey=mykey&node=fortnite&json={\"t\":" + tempString + ",\"h\":" + humString + "}";
                                                                                                                ^
Multiple libraries were found for "DHT.h"
 Used: /Users/quique123/Documents/Arduino/libraries/DHT
 Not used: /Users/quique123/Documents/Arduino/libraries/Grove_Temperature_And_Humidity_Sensor-master
exit status 1
invalid operands of types 'const char [90]' and 'char [20]' to binary 'operator+'

Since then I tried this mod:

  String url = "/emoncms/input/post.json?apikey=mykey&node=fortnite&json={\"t\":";
  url.concat(tempString);
  url = url + ",\"h\":";
  url.concat(humString);
  url = url + "}";

which compiled and tried to post to the server but came up empty like this:

186.32.135.25 - - [17/Oct/2018:18:55:16 -0400] "GET /emoncms/input/post.json?apikey=mykey&node=fortnite&json={\"t\":,\"h\":} HTTP/1.0" 200 742 "-" "-"

Thanks Johnwasser, I got it working with your code.