invalid operands of types 'const char [35]' and 'float' to binary 'operator+'

Hi,

First of all, i only had my Arduino Uno for a week so im still trying to get the hang of things.

I bought i CC3000 adafruit wifi sheild, and im trying to post my temperature data to my REST web server. I broken down to sketch so that it would be easier for me to understand, since i get the same error with the code here as with the wifi sheild hooked up.

#define WEBPAGE  "/datasources/29/addvalue?value='"
int yellowLed = 6;
int blueLed = 7;
int TempPin = A0;

void setup(void)
{
  Serial.begin(115200);
  pinMode(yellowLed, OUTPUT);
  pinMode(blueLed, OUTPUT);
}

void loop(void)
{
  //Serial.println ("------------------");
  digitalWrite(yellowLed, LOW);
  digitalWrite(blueLed, HIGH);
  float Temp = (CalcTemp(analogRead(TempPin)));
  Serial.println (Temp);
  Serial.println (WEBPAGE + Temp); // This is where it goes wrong.
  
  digitalWrite(yellowLed, HIGH);
  digitalWrite(blueLed, LOW);
  delay(10000);
}


float CalcTemp (float SensorData)
// Denna funktion berärknar tempraturen på en analog sensor på 5 volt.
{
  SensorData = (SensorData / 1024.0) * 5.0;
  SensorData = (SensorData - 0.5) * 100;
  return SensorData;
}
float CalcVolt (float SensorData)
// Denna funktion beräknar ut spänningen på en analog input på 5v.
{
  SensorData = (SensorData / 1024.0) * 5.0;
  return SensorData;
}

test.ino: In function 'void loop()': test:23: error: invalid operands of types 'const char [35]' and 'float' to binary 'operator+'

I have googled, searched and found people with the same problem, but i couldn't find a solution to my problem. Help is welcomed :)

  Serial.println (WEBPAGE + Temp); // This is where it goes wrong.

When the preprocessor is done, this looks like:

  Serial.println ("/datasources/29/addvalue?value='" + Temp); // This is where it goes wrong.

Now, really, what do you expect when you try to ADD a string literal and a float?

You can't just make up expected functionality for operators.

PaulS: Now, really, what do you expect when you try to ADD a string literal and a float?

You can't just make up expected functionality for operators.

Well thanks for confirming my problem, now some help to solve this would be appreciated :) As i said, i only had my Arduino one week. :~

now some help to solve this would be appreciated

I don't know how to solve your problem because I don't know what you expect "elephant" + 3.1.4i59 to add up to.

Now, if you were trying to CONCATENATE strings, then first you'd need to make a string out of the float. Before I explain how to do that, you need to explain when, given that serial output happens one byte/char at a time, why you perceive a benefit in using just one statement, when using two is more obvious and no slower.

PaulS:

now some help to solve this would be appreciated

I don’t know how to solve your problem because I don’t know what you expect “elephant” + 3.1.4i59 to add up to.

Oh sorry, perhaps i didn’t explain as good as i thought.
Lets say Temp = 23.7 degrees Celsius.
Then i would like WEBPAGE to be /datasources/29/addvalue?value=23.7
There is also WEBSITE that is set to 192.168.10.100
So that becomes: http://192.168.10.100/datasources/29/addvalue?value=23.7
As this is data that my REST webserver now what to do with.

This is my hole code if that also helps.

/*************************************************** 
  This is an example for the Adafruit CC3000 Wifi Breakout & Shield

  Designed specifically to work with the Adafruit WiFi products:
  ----> https://www.adafruit.com/products/1469

  Adafruit invests time and resources providing this open source code, 
  please support Adafruit and open-source hardware by purchasing 
  products from Adafruit!

  Written by Limor Fried & Kevin Townsend for Adafruit Industries.  
  BSD license, all text above must be included in any redistribution
 ****************************************************/
 
 /*
This example does a test of the TCP client capability:
  * Initialization
  * Optional: SSID scan
  * AP connection
  * DHCP printout
  * DNS lookup
  * Optional: Ping
  * Connect to website and print out webpage contents
  * Disconnect
SmartConfig is still beta and kind of works but is not fully vetted!
It might not work on all networks!
*/
#include <Adafruit_CC3000.h>
#include <ccspi.h>
#include <SPI.h>
#include <string.h>
#include "utility/debug.h"

// These are the interrupt and control pins
#define ADAFRUIT_CC3000_IRQ   3  // MUST be an interrupt pin!
// These can be any two pins
#define ADAFRUIT_CC3000_VBAT  5
#define ADAFRUIT_CC3000_CS    10
// Use hardware SPI for the remaining pins
// On an UNO, SCK = 13, MISO = 12, and MOSI = 11
Adafruit_CC3000 cc3000 = Adafruit_CC3000(ADAFRUIT_CC3000_CS, ADAFRUIT_CC3000_IRQ, ADAFRUIT_CC3000_VBAT,
                                         SPI_CLOCK_DIVIDER); // you can change this clock speed

#define WLAN_SSID       "Router"        // cannot be longer than 32 characters!
#define WLAN_PASS       "Password"
// Security can be WLAN_SEC_UNSEC, WLAN_SEC_WEP, WLAN_SEC_WPA or WLAN_SEC_WPA2
#define WLAN_SECURITY   WLAN_SEC_WPA2

#define IDLE_TIMEOUT_MS  3000      // Amount of time to wait (in milliseconds) with no data 
                                   // received before closing the connection.  If you know the server
                                   // you're accessing is quick to respond, you can reduce this value.

// What page to grab!
#define WEBSITE      "192.168.10.100"
#define WEBPAGE      "/datasources/29/addvalue?value="

int yellowLed = 6;
int blueLed = 7;
int TempPin = A0;

/**************************************************************************/
/*!
    @brief  Sets up the HW and the CC3000 module (called automatically
            on startup)
*/
/**************************************************************************/

uint32_t ip;

void setup(void)
{
  Serial.begin(115200);
  pinMode(yellowLed, OUTPUT);
  pinMode(blueLed, OUTPUT);

  
  
}

void loop(void)
{
  digitalWrite(yellowLed, LOW);
  digitalWrite(blueLed, HIGH);
  Serial.println(F("Hello, CC3000!\n")); 
  Serial.print("Free RAM: "); Serial.println(getFreeRam(), DEC);
  float Temp = (CalcTemp(analogRead(TempPin)));
  Serial.println (Temp);
  
  /* Initialise the module */
  Serial.println(F("\nInitializing..."));
  if (!cc3000.begin())
  {
    Serial.println(F("Couldn't begin()! Check your wiring?"));
    while(1);
  }
  
  // Optional SSID scan
  // listSSIDResults();
  
  if (!cc3000.connectToAP(WLAN_SSID, WLAN_PASS, WLAN_SECURITY)) {
    Serial.println(F("Failed!"));
    while(1);
  }
   
  Serial.println(F("Connected!"));
  
  /* Wait for DHCP to complete */
  Serial.println(F("Request DHCP"));
  while (!cc3000.checkDHCP())
  {
    delay(100); // ToDo: Insert a DHCP timeout!
  }  

  /* Display the IP address DNS, Gateway, etc. */  
  while (! displayConnectionDetails()) {
    delay(1000);
  }

  ip = 0;
  // Try looking up the website's IP address
  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);
  
  // Optional: Do a ping test on the website
  /*
  Serial.print(F("\n\rPinging ")); cc3000.printIPdotsRev(ip); Serial.print("...");  
  replies = cc3000.ping(ip, 5);
  Serial.print(replies); Serial.println(F(" replies"));
  */  

  /* Try connecting to the website.
     Note: HTTP/1.1 protocol is used to keep the server from closing the connection before all data is read.
  */
  Adafruit_CC3000_Client www = cc3000.connectTCP(ip, 10000);
  if (www.connected()) {
    www.fastrprint(F("GET "));
    www.fastrprint(WEBPAGE + Temp);
    www.fastrprint(F(" HTTP/1.1\r\n"));
    www.fastrprint(F("Host: ")); www.fastrprint(WEBSITE); www.fastrprint(F("\r\n"));
    www.fastrprint(F("\r\n"));
    www.println();
  } else {
    Serial.println(F("Connection failed"));    
    return;
  }

  Serial.println(F("-------------------------------------"));
  
  /* Read data until either the connection is closed, or the idle timeout is reached. */ 
  unsigned long lastRead = millis();
  while (www.connected() && (millis() - lastRead < IDLE_TIMEOUT_MS)) {
    while (www.available()) {
      char c = www.read();
      Serial.print(c);
      lastRead = millis();
    }
  }
  www.close();
  Serial.println(F("-------------------------------------"));
  
  /* You need to make sure to clean up after yourself or the CC3000 can freak out */
  /* the next time your try to connect ... */
  Serial.println(F("\n\nDisconnecting"));
  cc3000.disconnect();

  digitalWrite(yellowLed, HIGH);
  digitalWrite(blueLed, LOW);
  delay(10000);



}

/**************************************************************************/
/*!
    @brief  Begins an SSID scan and prints out all the visible networks
*/
/**************************************************************************/

void listSSIDResults(void)
{
  uint8_t valid, rssi, sec, index;
  char ssidname[33]; 

  index = cc3000.startSSIDscan();

  Serial.print(F("Networks found: ")); Serial.println(index);
  Serial.println(F("================================================"));

  while (index) {
    index--;

    valid = cc3000.getNextSSID(&rssi, &sec, ssidname);
    
    Serial.print(F("SSID Name    : ")); Serial.print(ssidname);
    Serial.println();
    Serial.print(F("RSSI         : "));
    Serial.println(rssi);
    Serial.print(F("Security Mode: "));
    Serial.println(sec);
    Serial.println();
  }
  Serial.println(F("================================================"));

  cc3000.stopSSIDscan();
}

/**************************************************************************/
/*!
    @brief  Tries to read the IP address and other connection details
*/
/**************************************************************************/
bool displayConnectionDetails(void)
{
  uint32_t ipAddress, netmask, gateway, dhcpserv, dnsserv;
  
  if(!cc3000.getIPAddress(&ipAddress, &netmask, &gateway, &dhcpserv, &dnsserv))
  {
    Serial.println(F("Unable to retrieve the IP Address!\r\n"));
    return false;
  }
  else
  {
    Serial.print(F("\nIP Addr: ")); cc3000.printIPdotsRev(ipAddress);
    Serial.print(F("\nNetmask: ")); cc3000.printIPdotsRev(netmask);
    Serial.print(F("\nGateway: ")); cc3000.printIPdotsRev(gateway);
    Serial.print(F("\nDHCPsrv: ")); cc3000.printIPdotsRev(dhcpserv);
    Serial.print(F("\nDNSserv: ")); cc3000.printIPdotsRev(dnsserv);
    Serial.println();
    return true;
  }
}

float CalcTemp (float SensorData)
// Denna funktion berärknar tempraturen på en analog sensor på 5 volt.
{
  SensorData = (SensorData / 1024.0) * 5.0;
  SensorData = (SensorData - 0.5) * 100;
  return SensorData;
}
float CalcVolt (float SensorData)
// Denna funktion beräknar ut spänningen på en analog input på 5v.
{
  SensorData = (SensorData / 1024.0) * 5.0;
  return SensorData;
}

Its the line : www.fastrprint(WEBPAGE + Temp);
That is my problem on that code. Same error.

And thank you for trying to help me :slight_smile:

Lets say Temp = 23.7 degrees Celsius. Then i would like WEBPAGE to be /datasources/29/addvalue?value=23.7

First, WEBPAGE needs to be an array, not a #define. If you make WEBPAGE an array that has room to concatenate another array, then you could define an initial value for WEBPAGE and concatenate another array onto it.

I don't recommend this, though.

You could create an array containing "/datasources/29/addvalue?value=", and another array, not initialized, that can hold 60 characters. Then, you could use strcpy() to copy the first array into the second array, and then use strcat() to concatenate the third array onto the end of the second arrray.

I don't recommend that, either, though.

    www.fastrprint(F("GET "));
    www.fastrprint(WEBPAGE);
    www.fastprint(Temp);
    www.fastrprint(F(" HTTP/1.1\r\n"));

will send the GET statement (and space), the script name and value identifier, and the Temp variable's contents,m and the HTTP part in 4 packets, which is not ideal, but uses the least amount of memory. It is hardly less efficient than sending just 3 packets.

This is what I recommend that you do.

PaulS:

    www.fastrprint(F("GET "));

www.fastrprint(WEBPAGE);
    www.fastprint(Temp);
    www.fastrprint(F(" HTTP/1.1\r\n"));



will send the GET statement (and space), the script name and value identifier, and the Temp variable's contents,m and the HTTP part in 4 packets, which is not ideal, but uses the least amount of memory. It is hardly less efficient than sending just 3 packets.

This is what I recommend that you do.

Thank you. I tried this:

    www.fastrprint(F("GET "));
    www.fastrprint(WEBPAGE);
    www.fastrprint(Temp);
    www.fastrprint(F(" HTTP/1.1\r\n"));
    www.fastrprint(F("Host: ")); www.fastrprint(WEBSITE); www.fastrprint(F("\r\n"));
    www.fastrprint(F("\r\n"));
    www.println();

But then i get this error below and it points out this row: www.fastrprint(Temp);
WebClient.ino: In function ‘void loop()’:
WebClient:145: error: no matching function for call to 'Adafruit_CC3000_Client::fastrprint(float&)'
C:\Users\HTPC\Documents\Arduino\libraries\Adafruit_CC3000_Library/Adafruit_CC3000.h:82: note: candidates are: size_t Adafruit_CC3000_Client::fastrprint(const char*)
C:\Users\HTPC\Documents\Arduino\libraries\Adafruit_CC3000_Library/Adafruit_CC3000.h:84: note: size_t Adafruit_CC3000_Client::fastrprint(const __FlashStringHelper*)

This is the Adafruit_CC3000.h file:

/**************************************************************************/
/*! 
  @file     Adafruit_CC3000.h
  @author   KTOWN (Kevin Townsend for Adafruit Industries)
	@license  BSD (see license.txt)	

	This is a library for the Adafruit CC3000 WiFi breakout board
	This library works with the Adafruit CC3000 breakout
	----> https://www.adafruit.com/products/1469
	
	Check out the links above for our tutorials and wiring diagrams 
	These chips use SPI to communicate.
  
	Adafruit invests time and resources providing this open source code, 
	please support Adafruit and open-source hardware by purchasing 
	products from Adafruit!	
*/
/**************************************************************************/

#ifndef ADAFRUIT_CC3000_H
#define ADAFRUIT_CC3000_H

#if ARDUINO >= 100
 #include "Arduino.h"
#else
 #include "WProgram.h"
#endif
#include "utility/cc3000_common.h"
#include "utility/wlan.h"
#include "utility/netapp.h"
#include "ccspi.h"

#if defined(__arm__) && defined(__SAM3X8E__) // Arduino Due
  #define SPI_CLOCK_DIVIDER 6 // used to set the speed for the SPI bus; 6 == 14 Mhz on the Arduino Due
#else
  #define SPI_CLOCK_DIVIDER SPI_CLOCK_DIV128
#endif

#define WLAN_CONNECT_TIMEOUT 10000  // how long to wait, in milliseconds
#define RXBUFFERSIZE  64 // how much to buffer on the incoming side
#define TXBUFFERSIZE  32 // how much to buffer on the outgoing side

#define WIFI_ENABLE 1
#define WIFI_DISABLE 0
#define WIFI_STATUS_CONNECTED 1

typedef struct Result_Struct
{
	uint32_t	num_networks;
	uint32_t 	scan_status;
	uint8_t 	rssiByte;
	uint8_t 	Sec_ssidLen;
	uint16_t 	time;
	uint8_t 	ssid_name[32];
	uint8_t 	bssid[6];
} ResultStruct_t;  	/**!ResultStruct_t : data struct to store SSID scan results */

/* Enum for wlan_ioctl_statusget results */
typedef enum 
{
  STATUS_DISCONNECTED = 0,
  STATUS_SCANNING     = 1,
  STATUS_CONNECTING   = 2,
  STATUS_CONNECTED    = 3
} status_t;

class Adafruit_CC3000;

class Adafruit_CC3000_Client : public Print {
 public:
  Adafruit_CC3000_Client(uint16_t s);
  Adafruit_CC3000_Client(void);
  Adafruit_CC3000_Client(const Adafruit_CC3000_Client& copy);
  void operator=(const Adafruit_CC3000_Client& other);
  
  // NOTE: If public functions below are added/modified/removed please make sure to update the 
  // Adafruit_CC3000_ClientRef class to match!

  bool connected(void);
  size_t write(uint8_t c);

  size_t fastrprint(const char *str);
  size_t fastrprintln(const char *str);
  size_t fastrprint(const __FlashStringHelper *ifsh);
  size_t fastrprintln(const __FlashStringHelper *ifsh);

  int16_t write(const void *buf, uint16_t len, uint32_t flags = 0);
  int16_t read(void *buf, uint16_t len, uint32_t flags = 0);
  uint8_t read(void);
  int32_t close(void);
  uint8_t available(void);

  uint8_t _rx_buf[RXBUFFERSIZE], _rx_buf_idx;
  int16_t bufsiz;

 private:
  int16_t _socket;

};

// Ugly but necessary to include the server header after the client is fully defined.
// A forward reference in the server header won't cut it because the server needs to contain
// instances of the client.  The client definition above can be pulled into a separate
// header in a later change to make this cleaner.
#include "Adafruit_CC3000_Server.h"

class Adafruit_CC3000 {
  public:
  Adafruit_CC3000(uint8_t csPin, uint8_t irqPin, uint8_t vbatPin, uint8_t spispeed = SPI_CLOCK_DIVIDER);
    bool     begin(uint8_t patchReq = 0, bool useSmartConfigData = false);
    void     reboot(uint8_t patchReq = 0);
    void     stop(void);
    bool     disconnect(void);
    bool     deleteProfiles(void);
    void     printHex(const byte * data, const uint32_t numBytes);
    void     printHexChar(const byte * data, const uint32_t numBytes);
    void     printIPdots(uint32_t ip);
    void     printIPdotsRev(uint32_t ip);
    uint32_t IP2U32(uint8_t a, uint8_t b, uint8_t c, uint8_t d);
    bool     getMacAddress(uint8_t address[6]);
    bool     setMacAddress(uint8_t address[6]);

    bool     connectToAP(const char *ssid, const char *key, uint8_t secmode);
    bool     connectSecure(const char *ssid, const char *key, int32_t secMode);
    bool     connectOpen(const char *ssid); 
    bool     checkConnected(void);
    bool     checkDHCP(void);
    bool     getIPAddress(uint32_t *retip, uint32_t *netmask, uint32_t *gateway, uint32_t *dhcpserv, uint32_t *dnsserv);

    bool     checkSmartConfigFinished(void);

    Adafruit_CC3000_Client connectTCP(uint32_t destIP, uint16_t destPort);
    Adafruit_CC3000_Client connectUDP(uint32_t destIP, uint16_t destPort);
     
    #ifndef CC3000_TINY_DRIVER
    bool     getFirmwareVersion(uint8_t *major, uint8_t *minor);
    status_t getStatus(void);
    uint16_t startSSIDscan(void);
    void     stopSSIDscan();
    uint8_t  getNextSSID(uint8_t *rssi, uint8_t *secMode, char *ssidname);

    bool     listSSIDResults(void);
    bool     startSmartConfig(bool enableAES);

    bool     getIPConfig(tNetappIpconfigRetArgs *ipConfig);


    uint16_t ping(uint32_t ip, uint8_t attempts=3,  uint16_t timeout=500, uint8_t size=32);
    uint16_t getHostByName(char *hostname, uint32_t *ip);
    #endif

    /* Functions that aren't available with the tiny driver */
    #ifndef CC3000_TINY_DRIVER
    bool     scanSSIDs(uint32_t time);
    #endif

    void setPrinter(Print*);

  private:
    bool _initialised;

};

extern Print* CC3KPrinter;

#endif

82 = size_t fastrprint(const char *str);
83 = size_t fastrprintln(const char *str);
84 = size_t fastrprint(const __FlashStringHelper *ifsh);
85 = size_t fastrprintln(const __FlashStringHelper *ifsh);

Do i need to convert Temp to something else perhaps?

But then i get this error below and it points out this row: www.fastrprint(Temp); WebClient.ino: In function 'void loop()': WebClient:145: error: no matching function for call to 'Adafruit_CC3000_Client::fastrprint(float&)' C:\Users\HTPC\Documents\Arduino\libraries\Adafruit_CC3000_Library/Adafruit_CC3000.h:82: note: candidates are: size_t Adafruit_CC3000_Client::fastrprint(const char*) C:\Users\HTPC\Documents\Arduino\libraries\Adafruit_CC3000_Library/Adafruit_CC3000.h:84: note: size_t Adafruit_CC3000_Client::fastrprint(const __FlashStringHelper*)

Then complain to Adafruit that the library is crap. It REALLY should be able to handle a float.

As a crutch, look at dtostrf() to create a string from a float, and then fastprint() that.

I finally got it to work :)

I changed:

    www.fastrprint(F("GET "));
    www.fastrprint(WEBPAGE);
    www.fastrprint(Tempd);
    www.fastrprint(F(" HTTP/1.1\r\n"));
    www.fastrprint(F("Host: ")); www.fastrprint(WEBSITE); www.fastrprint(F("\r\n"));

To:

    www.print(F("\r\n"));
    www.print(F("GET "));
    www.print(WEBPAGE);
    www.print(Temp);
    www.print(F(" HTTP/1.1\r\n"));
    www.print(F("Host: ")); www.print(WEBSITE); www.print(F("\r\n"));

And now it accepts flots :) But it hangs very often, it can do about 10 loops, then it gets stuck on connecting to 192.168.10.100 (WEBSITE)... Back to the drawing board...