I learned about StringStream today at work.
#include <iostream>
#include <sstream>
#include <stdio.h>
#include <string.h>
using namespace std;
char* getSql() {
float outdoorTempInC = 15;
int outoorHumidity = 23;
float indorTempinC = 20;
int humidity = 22;
int val = 400; // soil sensor reading
int avgVal = 38; // avaerage of 10 values of soils sensor
char statusMsg[50] = {"Hello World"} ;
std::stringstream sqlQuery;
sqlQuery << "INSERT INTO arduinoSensorData.sensorLog (out_temperature, "
<< "out_humidity, drwngRoom_temperature, drwngRoom_humidity, "
<< "pot1_soilMoisture, pot1_avg_SoilMoisture, wateringPot1) VALUES ('"
<< outdoorTempInC << "', '" << outoorHumidity << "', '" << indorTempinC
<< "', '" << humidity << "', '" << val << "', '" << avgVal << "', '"
<< statusMsg << "');";
char *mychar = new char[sqlQuery.str().length() + 1];
strcpy(mychar, sqlQuery.str().c_str());
return mychar;
}
string getSqlStream() {
float outdoorTempInC = 15;
int outoorHumidity = 23;
float indorTempinC = 20;
int humidity = 22;
int val = 400; // soil sensor reading
int avgVal = 38; // avaerage of 10 values of soils sensor
char statusMsg[50] = {"Hello World"} ;
std::stringstream sqlQuery;
sqlQuery << "INSERT INTO arduinoSensorData.sensorLog (out_temperature, "
<< "out_humidity, drwngRoom_temperature, drwngRoom_humidity, "
<< "pot1_soilMoisture, pot1_avg_SoilMoisture, wateringPot1) VALUES ('"
<< outdoorTempInC << "', '" << outoorHumidity << "', '" << indorTempinC
<< "', '" << humidity << "', '" << val << "', '" << avgVal << "', '"
<< statusMsg << "');";
return sqlQuery.str();
}
int main() {
string sql = getSqlStream();
cout << getSql() << "\n";
cout << sql.c_str() << "\n";
return 0;
}
I was very happy until I discovered I cannot use StringStream in Arduino, so I dug in the internet and came across with this.
So if I try to use the above linked gist StringSteam like I have used in my example code, I am getting this error:
error: no match for 'operator<<' (operand types are 'StringStream' and 'const char [60]')
sqlQuery << "INSERT INTO arduinoSensorData.sensorLog (out_temperature, "
^
webServer_Displaying_everything_v01:24: error: 'outdoorTempInC' was not declared in this scope
<< outdoorTempInC << "', '" << outoorHumidity << "', '" << indorTempinC
^
webServer_Displaying_everything_v01:24: error: 'outoorHumidity' was not declared in this scope
<< outdoorTempInC << "', '" << outoorHumidity << "', '" << indorTempinC
^
webServer_Displaying_everything_v01:24: error: 'indorTempinC' was not declared in this scope
<< outdoorTempInC << "', '" << outoorHumidity << "', '" << indorTempinC
^
webServer_Displaying_everything_v01:25: error: 'humidity' was not declared in this scope
<< "', '" << humidity << "', '" << val << "', '" << avgVal << "', '"
^
webServer_Displaying_everything_v01:25: error: 'val' was not declared in this scope
<< "', '" << humidity << "', '" << val << "', '" << avgVal << "', '"
^
webServer_Displaying_everything_v01:25: error: 'avgVal' was not declared in this scope
<< "', '" << humidity << "', '" << val << "', '" << avgVal << "', '"
^
webServer_Displaying_everything_v01:28: error: 'class StringStream' has no member named 'str'
char *mychar = new char[sqlQuery.str().length() + 1];
^
webServer_Displaying_everything_v01:29: error: 'class StringStream' has no member named 'str'
strcpy(mychar, sqlQuery.str().c_str());
Or I am doing it completely the wrong way? It feels I still cannot use it the way I have done in the example. Please help.
Stream
in the Arduino sense, i.e. it provides an interface similar toSerial
. You can for instanceprint()
andprintln()
to it.sqlQuery
method of storing to a variable in arduino without having to useprint()
,println
orsprintf
?StringStream
is to let youprint()
andprintln()
into it.