ESP8266WebServer setting a value inside a class - c++

I'm having a bit of trouble with the ESP8266WebServer. My WebServer{} class is wrapped around the ESP8266WebServer object and looks like this:
Header file:
#include <WiFiClient.h>
#ifndef WebServer_h
#define WebServer_h
#include "Arduino.h"
class WebServer {
public:
WebServer();
void begin();
void handleClient();
void finishedProcessingData(String clientReply);
String queryString;
private:
// page/url handlers
friend void handleSomeData();
};
#endif
Cpp file:
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#include "Arduino.h"
#include "WebServer.h"
ESP8266WebServer server(80);
int aNumberHere = 0;
String queryString = "";
WebServer::WebServer(){
}
void handleSomeData(){
aNumberHere++;
queryString = "";
// this loop appends all the queries fro the query string back into a query string
// (not sure if there is an easier way to grab this directly from the server api)
int totalArgs = server.args();
for (int counter = 0; counter < totalArgs; counter++){
queryString += server.argName(counter) +"="+ server.arg(counter);
if(counter < (totalArgs - 1)){
queryString += "&";
}
}
Serial.println(queryString);
Serial.println(aNumberHere);
}
void WebServer::handleClient(){
server.handleClient();
}
void WebServer::begin(){
server.on("/data.html", handleSomeData);
server.begin();
}
void WebServer::finishedProcessingData(String clientReply){
// empty the string so it isn't cached (just in case)
Serial.print("Sending reply back to client: ");
Serial.println(clientReply);
queryString = "";
server.send(200, "text/plain", clientReply);
}
The idea is to grab a query string from an http request, do some processing, then return the response.
How it is called from outside is:
WebServer webServer;
String processingResult;
void setup(){
webServer.begin();
}
void loop(){
delay(10);
webServer.handleClient();
// check if the query string has stuff in it, if it doesn't then WebServer.handleSomeData() never fired, thus no request yet
if(webServer.queryString != ""){
// do stuff that results in a string being returned
processingResult = handWavyMagic();
// then respond back to client
webServer.finishedProcessingData(processingResult);
}
}
My issue is that from my outside loop (which is in my main sketch), "webServer.queryString" is always equal to an empty string. If I print out "queryString" from within handleSomeData(), I can see the printed result from within "handleSomeData()" (which is correct), but it somehow can't seem to set it 'outside' that method. I did check to see that it is able to access and modify a variable (which is why "aNumberHere" is printed) and there seem to be no issues; "aNumberHere" gets incremented as expected every time I go to the corresponding url.
My only suspect is that "queryString" is a String object which for some reason can't be set, while "aNumberHere" is a primitive int which is fine. Is this correct or is something else going on here?
I have also tried making "queryString" a static variable, but with no luck -either I did it wrong or it just doesn't work that way either.
I am new to friend functions (well, new to c/c++ in general actually -any advice is welcome), though I read that it should be able to access the classes private properties just fine, so I'm not sure how to fix this.
Any ideas?

I know, this is a bit late for the OP to help with his problem, but maybe other readers will find this useful.
To avoid splitting parts of the logic/data inside and outside the class it would be more elegant to have everything inside. Using callbacks to non-static methods of a class instance is a bit tricky (I learned it the hard way for my current project), but here is the alternative:
void WebServer::begin()
{
// instead of server.on("/data.html", handleSomeData);
server.on("/data.html", std::bind(&WebServer::handleSomeData, this));
server.begin();
}
void WebServer::handleSomeData()
{
// do whatever you need
}
This uses std:bind() to bind the instance method and its this pointer to the callback. As a result, everything is contained inside the web server instance, which is a cleaner approach.

There is 2 different variables called queryString :
the global one declared in WebServer.cpp
the member of WebServer declared in WebServer.h
In the callback handleSomeData you set the global one, but in the loop and finishedProcessingData you access to the member of WebServer.
To make the code works, you could remove the member of WebServer and use the global one (as you did for aNumberHere) like this :
extern String queryString;
void loop(){
delay(10);
webServer.handleClient();
// check if the query string has stuff in it, if it doesn't then WebServer.handleSomeData() never fired, thus no request yet
if(queryString != ""){
// do stuff that results in a string being returned
processingResult = handWavyMagic();
// then respond back to client
webServer.finishedProcessingData(processingResult);
queryString = "";
}
}

Related

Buffer gets overwritten

I'm facing this issue on an ESP8266 (Arduino like board), but this problem is regarding c/c++, so I'm asking this here.
I have not that much experience with native languages like c/c++ and I'm facing a strange issue, which drives me crazy. So I'm using an Wemos D1 mini (ESP8266) which uses a class calles ConfigManager to read a configuration file from eeprom. The config file is formatted as json, so I'm using ArduinoJson to parse the content. I have declared a StaticJsonBuffer and pointer to a JsonObject in the header, like you can see in the code example:
//FILE: ConfigManager.h
#ifndef ConfigManager_H
#define ConfigManager_H
#include <Arduino.h>
#include <ArduinoJson.h>
#include <FS.h>
#include "Logger.h"
class ConfigManager {
public:
Settings *settings;
ConfigManager();
void read_from_eeprom();
private:
File configFile;
JsonObject *json;
StaticJsonBuffer<200> jsonBuffer;
void open_file(const char *permission);
void read_json();
void recreate_file();
void create_json();
void check_success();
void populate_settings();
void clean_up();
};
#endif
When the function read_from_eeprom is invoked, it opens the file and invokes the functionread_json:
void ConfigManager::read_json() {
size_t size = configFile.size();
Log.verbose("[ConfigManager] Config file size: %d", size);
std::unique_ptr<char[]> buf(new char[size]);
configFile.readBytes(buf.get(), size);
Log.verbose("[ConfigManager] File content: %s", buf.get());
Log.verbose("[ConfigManager] Parsing json");
json = &jsonBuffer.parseObject(buf.get());
Log.notice("[ConfigManager] Json is:");
json->printTo(Serial);
Serial.println();
}
Which is followed by a call to check_success()
void ConfigManager::check_success() {
Log.notice("[ConfigManager] Json is:");
json->printTo(Serial);
Serial.println();
bool should_recreate = true;
if (json->success()) {
Log.notice("[ConfigManager] Parsed json successfully");
auto version = json->get<const char*>("version");
if (version) {
if (strcmp(version, Settings::current_version) == 0) {
Log.notice("[ConfigManager] Config version is up2date");
should_recreate = false;
} else {
Log.warning("[ConfigManager] Config version outdated");
}
} else {
Log.warning("[ConfigManager] Invalid config file");
}
} else {
Log.warning("[ConfigManager] Config file is not valid json");
}
if (should_recreate) {
Log.notice("[ConfigManager] Recreating config file");
recreate_file();
create_json();
}
Log.notice("JSON IS: ");
json->prettyPrintTo(Serial);
Log.notice("[ConfigManager] Sucessfully read json");
}
So what I noticed is that the file content is fine. E.g. {"version":"0.2","led_count":"64"}.
Then the json is parsed, which succeeds and logs the json object, which is again {"version":"0.2","led_count":"64"}.
Afterwards the function returns, and calls check_success, which again prints the content of the json object to the log, but this time it seems that something has overwritten the JsonBuffer, which causes the json object to be corrupted. This time the logged content is {"v␂":"0.2","led_count":"64"} (with some strange unicorn characters that change as the source code changes). I'm trying to figure out whats going on for many hours now, but I'm stuck. Can someone please point me in the right direction to solve this problem? Thank you!
The full Log can be found HERE, as well as ConfigManager.h and ConfigManager.cpp
*I'd prefer write that in comments, because I don't have arduino and can't verify that my advice 100% helpful. But I can't use comments with "my reputation" :). So please don't press "minus button" if my answer didn't help... *
According to that it seems you need to keep original json string while you using json buffer.
Keep the JSON string in memory long enough
The library never make memory duplication. This has an important implication on string
values, it means that the library will return pointer to chunks of the
string.
For instance, let’s imagine that you parse ["hello","world"], like
this:
char[] json = "[\"hello\",\"world\"]";
StaticJsonBuffer<32> buffer;
JsonArray& array = buffer.parseArray(json);
const char* first = array[0];
const char* second = array[1];
In that
case, both first and second are pointers to the content of the
original string json. So this will only work if json is still in
memory.
So, it make sense try make std::unique_ptr buf a class member (same as StaticJsonBuffer) and check how it works.
BTW, IMO std::vector will be more suitable there... And I'm not sure that unique_ptr deletes arrays properly.

Arduino variable usage

I'm a newbie when it comes to Arduino and C++.
I'm trying to write a program that reads the input data from analog pin zero (a POT).
after the value is read I want it to print to the serial monitor, but only once. if the value of from analog pin zero changes I want it to print the new value to the serial monitor. I'm trying to use global variables, but to no avail. any help would be greatly appreciated!
int entered=0;
int flag;
void setup()
{
Serial.begin(9600);
}
void loop() {
int potValue=analogRead(A0);
if (!entered){
entered=1;
Serial.println(potValue);
}
int flag=potValue;
if (flag!=flag){
entered=0;
}
}
That is really close. This line is your mistake
int flag=potValue;
As written, that creates a new local variable flag. The local variable hides the global variable. So the comparison is always to itself and never fails. Change the line to :
flag=potValue;
and your program will function as desired.
You can save some memory and code space like this:
int g_lastValue = 0;
void loop() {
int nowValue = analogRead(A0);
if (nowValue != g_lastValue) {
Serial.println(nowValue);
g_lastValue = nowValue;
}
...
}
The use of g_ as name prefix is a cue that a variable is global. I use this naming convention as it helps when reading a function to know variables that are not local. Without a name cue, you need to scan the entire function body to see if there is a variable declaration present, and only by looking through the function and not finding a declaration can you know the variable must be global. On small functions, not really an issue, but as your code grows, you may want some self documentation a naming convention provides.
You're on your way but you are getting tangled a bit in variables.
It can be simpler: just one global variable and one conditional check.
int lastRead = -1; // init to value outside of possible range
void setup()
{
Serial.begin(9600);
}
void loop() {
// get current value
int currentRead = analogRead(0);
//compare and only print if !=
if (currentRead != lastRead){
lastRead = currentRead; // store
Serial.println(lastRead);
}
}

Calling a function from another file into another function C++

I have some header files which host functions and in other enums.
So in file Lexer.h, I have a function called getNextToken() which returns a token and in this function I need to call a function called reservedLookup(string tokenString) found in token.h
reservedWords is another header file called reservedWords.h which has enum declarations of the reserved Words
This function is found in token.h
reservedWords reservedLookup (string tokenString)
{
for(rIt = reservedMap.begin(); rIt!= reservedMap.end(); rIt++)
{
if(tokenString == (rIt->first))
{
return rIt->second;
}
}
}
in lexer.h I tried using this in private (and even in public:) :
reservedWords reservedLookup(string tokenString);
it compiles, but when in function Token* getNextToken() I use
int tokenType = reservedLookup(strBuffer);
it gives me an error stating:
obj\Release\main.o:main.cpp:(.text$_ZN5Lexer12getNextTokenEv[__ZN5Lexer12getNextTokenEv]+0x371)||undefined reference to `Lexer::reservedLookup(std::string)'|
I don't want my compiler to read reservedLookup as part of Lexer::reservedLookup(string str) but as Token::reservedLookup(string str)
Is there any way I can do it?
EDIT:
Token.h
class Token
{
.....
.....
public:
void reservedDeclare ()
{
// iterator used for looping through reservedWords
//Taking care of the Reserved Words first
reservedMap["function"] = reservedWords::tkfunction;
//if - then - else - while - halt
reservedMap["if"] = reservedWords::tkif;
reservedMap["else"] = reservedWords::tkelse;
reservedMap["while"] = reservedWords::tkwhile;
reservedMap["halt"] = reservedWords::tkhalt;
//and, or, not, true, else
reservedMap["and"] = reservedWords::tkand;
reservedMap["or"] = reservedWords::tkor;
reservedMap["not"] = reservedWords::tknot;
reservedMap["true"] = reservedWords::tktrue;
reservedMap["false"] = reservedWords::tkfalse;
//sets and read/write
reservedMap["set"] = reservedWords::tkset;
reservedMap["let"] = reservedWords::tklet;
reservedMap["read"] = reservedWords::tkread;
reservedMap["write"] = reservedWords::tkwrite;
//variable type
reservedMap["int"] = reservedWords::tkint;
reservedMap["char"] = reservedWords::tkchar;
reservedMap["bool"] = reservedWords::tkbool;
reservedMap["real"] = reservedWords::tkreal;
reservedMap["string"] = reservedWords::tkstring;
reservedMap["unit"] = reservedWords::tkunit;
}
reservedWords reservedLookup (string tokenString)
{
for(rIt = reservedMap.begin(); rIt!= reservedMap.end(); rIt++)
{
if(tokenString == (rIt->first))
{
return rIt->second;
}
}
}
reservedWords.h
#ifndef RESERVEDWORDS_H_INCLUDED
#define RESERVEDWORDS_H_INCLUDED
#include <string>
#include <vector> //std::vector
#include <algorithm> // std::find
using namespace std;
/**
All the reserved words used by the compiler
*/
/**
This file contains all the keywords or reserved words or reserved Symbols used by the compiler
In the lexer, we will check whether the string we are passing is either a Reserved word
or it is actually and identifier
*/
enum reservedWords
{
tkfunction,
tkif,
tkelse,
tkwhile,
tkhalt,
tkand,
tkor,
tknot,
tktrue,
tkfalse,
tkset,
tklet,
tkread,
tkwrite,
tkint,
tkchar,
tkbool,
tkreal,
tkstring,
tkunit,
tkreservedWord,
tkidentifier
};
#endif // RESERVEDWORDS_H_INCLUDED
PARTIAL Code of Lexer.h
class Lexer
{
private:
string strBuffer ="";//holds the current characters that have been read and waiting to be matched
int tokenType = 0;
reservedWords reservedLookup(string tokenString); // THIS DOES NOT WORK. SEES IT AS Lexer::reservedLookup
....
....
...
...
tokenType = reservedLookup(strBuffer); // GIVES ME ERROR BECAUSE OF ABOVE
Let's look at this part of your code first:
class Token
{
...
public:
...
reservedWords reservedLookup (string tokenString)
{ // start of function body
for(rIt = reservedMap.begin(); rIt!= reservedMap.end(); rIt++)
{
if(tokenString == (rIt->first))
{
return rIt->second;
}
}
} // end of function body
...
};
In this file you have declared a function whose fully-scoped name is Token::reservedLookup. (If you're unfamiliar with the term "fully scoped", OK; for our purposes here, the important thing is that this is a specific way of naming this function.) We can write Token:: at the front of this name because this function is a member of the class Token. You have also defined the function by providing a function body (the code enclosed in braces { and }). I have added comments on the lines where the function body begins and ends.
So far, so good. Now, this function is an ordinary member of the class Token, not a "static" function, so in order to call it, you must first have an object of type Token, and then you can call this function on that object, for example by writing code like this inside some other function:
Token token;
token.reservedDeclare();
reservedWords word = token.reservedLookup("read");
Now comes the part where things get confused. You wrote this code:
class Lexer
{
private:
...
reservedWords reservedLookup(string tokenString);
...
};
What this does is to declare a different function from the first one. This function's fully-scoped name is Lexer::reservedLookup. You have not provided any definition of the function, however (neither here, nor apparently anywhere else). So when you are within the code of the Lexer class and you write a line such as this,
tokenType = reservedLookup(strBuffer);
the compiler interprets this in the way the C++ language specification says it should, which is that this should be a call to the function reservedLookup that belongs to the class of the same function where this call appeared. In short, it is a call to Lexer::reservedLookup. Since you never defined that function, it is impossible to produce the code that calls the function correctly, and therefore you have an error.
So you probably do not want to define Lexer::reservedLookup at all. If you are not calling Token::reservedLookup from within one of the functions of the class Token itself, you may provide an object of type Token so that you may call the function, as I did in my example. Alternatively, you might want to make your definition of the function static so that you can call it this way, without requiring an object of type Token:
reservedWords word = Token::reservedLookup("read");
In order to make this work as desired, however, you would also have to make reservedDeclare a static function and make reservedMap a static variable of Token, or make some other changes so that the data used by Token::reservedLookup exists independently of a specific object of type Token.
You may also want to look at the answers to this question: The Definitive C++ Book Guide and List and read a recommended book to help understand the terminology of the language better so that you can get better advice about your programs.

Visual Studio Compiler is highlighting Static variables differently?

I'm programming in C++ and have a method which uses a static variable. The method isn't working as I think it should; upon investigation, I found that my static variable is being highlighted in red in two places and blue in other places. Below is the code:
int GameModeState::changeJob(int number)
{
static int job = 1; //red
if (number == 1)
{
job = (job+1); //First one is red, second one is blue
return job; //blue
} else {
return job; //blue
}
}
I'm calling this method with other methods, one shown for example:
int GameModeState::getJob()
{
int currentJob = (changeJob(2));
return currentJob;
}
I want a method like getJob() to simply return the current value of job, while another method, when calling changeJob(number) is changeJob(1), to increment job's value by one. (Hence the if/else statement in changeJob(number)).
Since the job variables are highlighted differently, I'm thinking the compiler is saying that it views the two separately somehow? I'm getting stuck with job being some even value.
EDIT I also have Awesomium... I believe that is the only addition to the compiler, but I'm not completely sure.
MOAR EDIT In another class, I have a method which should determine the current job's number and do something based on if the number is even or odd (since right now there are only two jobs)
void ZoneMovementState::_changeZone(const String& message, const Awesomium::JSValue& input, Awesomium::JSValue& output)
{
//Awesomium::JSValue::Object object = input.getObject();
//String zoneFilename = Convert::toString(object[L"zoneFilename"].toString());
// If the number from getJob is even, the player is currently a geologist
if (GameModeState::getJob()%2 == 0)
{
ZoneParser::getSingleton().load("../media/zones/geology_zone.xml", false);
} else {
ZoneParser::getSingleton().load("../media/zones/farm_zone.xml", false);
}
transitionHandler->go();
}
Ignore the two commented out lines; they deal with JS, which I'm not working on for now.
In the program, I can access the farm_zone until I increment job's value using the below method in GameModeState:
void GameModeState::_openNotebook(const String& message, const Awesomium::JSValue& input, Awesomium::JSValue& output)
{
mNotebookTransition->go();
static int currentJob = changeJob(1);
}
.... So I figured out my problem. While going through the code to show you guys, I realized that the static for currentJob was probably unneeded... once I removed it, my code works as it should now.
Thanks for the help guys!
Part of the problem here is you're using a static local for what very likely should just be a member variable. A static local maintains it's value across all calls to a function in all threads in a process. It's much more likely that you want it to persist for all calls to changeJob in a particular GameModeState instance (else why make it a member functon to begin with?).
To do this you'll need to define a member variable on GameModeState initialize it in the constructor and then access it in the method. For example
class GameModeState {
int job;
GameModeState() : job(1) {}
int changeJob(int number);
};
int GameModeState::changeJob(int number) {
if (number == 1) {
job = (job+1);
return job;
} else {
return job;
}
}
Note: I'm not entirely sure why you're seeing the color's your are seeing. Visual Studio by default won't color member variables a particular color in C++ so it's very likely another add-in you are using.
Nah, highlighting doesn't mean anything. That is, the editor doesn't call the compiler before deciding how/what/when to highlight. So that is not your problem. Sorry 'bout that :-)
You can prove this to yourself by going to Tools->Options->TextEditor and noticing that you can change the highlighting by choosing a different text-editing model.

C++ MySQL++ Delete query statement brain killer question

I'm relatively new to the MySQL++ connector in C++, and have an really annoying issue with it already!
I've managed to get stored procedures working, however i'm having issues with the delete statements. I've looked high and low and have found no documentation with examples.
First I thought maybe the code needs to free the query/connection results after calling the stored procedure, but of course MySQL++ doesn't have a free_result method... or does it?
Anyways, here's what I've got:
#include <iostream>
#include <stdio.h>
#include <queue>
#include <deque>
#include <sys/stat.h>
#include <mysql++/mysql++.h>
#include <boost/thread/thread.hpp>
#include "RepositoryQueue.h"
using namespace boost;
using namespace mysqlpp;
class RepositoryChecker
{
private:
bool _isRunning;
Connection _con;
public:
RepositoryChecker()
{
try
{
this->_con = Connection(false);
this->_con.set_option(new MultiStatementsOption(true));
this->_con.set_option(new ReconnectOption(true));
this->_con.connect("**", "***", "***", "***");
this->ChangeRunningState(true);
}
catch(const Exception& e)
{
this->ChangeRunningState(false);
}
}
/**
* Thread method which runs and creates the repositories
*/
void CheckRepositoryQueues()
{
//while(this->IsRunning())
//{
std::queue<RepositoryQueue> queues = this->GetQueue();
if(queues.size() > 0)
{
while(!queues.empty())
{
RepositoryQueue &q = queues.front();
char cmd[256];
sprintf(cmd, "svnadmin create /home/svn/%s/%s/%s", q.GetPublicStatus().c_str(),
q.GetUsername().c_str(), q.GetRepositoryName().c_str());
if(this->DeleteQueuedRepository(q.GetQueueId()))
{
printf("query deleted?\n");
}
printf("Repository created!\n");
queues.pop();
}
}
boost::this_thread::sleep(boost::posix_time::milliseconds(500));
//}
}
protected:
/**
* Gets the latest queue of repositories from the database
* and returns them inside a cool queue defined with the
* RepositoryQueue class.
*/
std::queue<RepositoryQueue> GetQueue()
{
std::queue<RepositoryQueue> queues;
Query query = this->_con.query("CALL sp_GetRepositoryQueue();");
StoreQueryResult result = query.store();
RepositoryQueue rQ;
if(result.num_rows() > 0)
{
for(unsigned int i = 0;i < result.num_rows(); ++i)
{
rQ = RepositoryQueue((unsigned int)result[i][0],
(unsigned int)result[i][1],
(String)result[i][2],
(String)result[i][3],
(String)result[i][4],
(bool)result[i][5]);
queues.push(rQ);
}
}
return queues;
}
/**
* Allows the thread to be shut off.
*/
void ChangeRunningState(bool isRunning)
{
this->_isRunning = isRunning;
}
/**
* Returns the running value of the active thread.
*/
bool IsRunning()
{
return this->_isRunning;
}
/**
* Deletes the repository from the mysql queue table. This is
* only called once it has been created.
*/
bool DeleteQueuedRepository(unsigned int id)
{
char cmd[256];
sprintf(cmd, "DELETE FROM RepositoryQueue WHERE Id = %d LIMIT 1;", id);
Query query = this->_con.query(cmd);
return (query.exec());
}
};
I've removed all the other methods as they're not needed...
Basically it's the DeleteQueuedRepository method which isn't working, the GetQueue works fine.
PS: This is on a Linux OS (Ubuntu server)
Many thanks,
Shaun
MySQL++ doesn't have a free_result method... or does it?
It doesn't need one. When the result object goes out of scope at the end of GetQueue(), all memory associated with it is automatically freed.
this->_con = Connection(false);
Three problems here:
When you create the RepositoryChecker object, you already have created a Connection object. If you need to pass different parameters to its constructor, you'd do that in the initialization list of the RepositoryChecker constructor, not in its body. Read your C++ book.
What you've done here instead is a) create a default Connection object, then b) create a different Connection object with exceptions turned off, then c) overwrite the first with the second. If that works, it's highly inefficient. MySQL++ Connection objects have had problems with their copy ctors in the past, so if you're using an old version of the library, it could explain your problems.
You're telling the Connection object (and every object it creates, even indirectly, which means pretty much everything in MySQL++) you don't want it to throw exceptions, but then you wrap it in a big try block. Pick one.
I'd suggest using exceptions — the default in MySQL++ — given the way your code is currently structured. If there is a query error way down in DeleteQueuedRepository(), there's no way to see what happened because you'd just pass false up to the caller, which is ignored because there is no else clause on the call. If you do this, log the e.what() message in your catch block. You're just throwing that information away right now.
There are several places where you're using constructs that look more like Python (or perhaps JavaScript) than C++. This makes me wonder if your problem isn't damage caused by some other misuse of C++.
On this line in particular, you're using the this pointer explicitly, for which there is no need in C++. This code does exactly the same thing:
_con = Connection(false);
Though again, the line should be replaced entirely, using the RepositoryChecker ctor initializer list instead.
Moving on...
sprintf(cmd, "DELETE FROM RepositoryQueue WHERE Id = %d LIMIT 1;", id);
As others have commented, you'd be better off using the Query stream interface:
Query q = _con.query();
q << "DELETE FROM RepositoryQueue WHERE Id = " << id << " LIMIT 1";
This has several advantages:
Fixes the type safety problem implied by the one who suggested changing your %d to %u. C++ IOStreams take care of that for you.
Automatic quoting of data inserted into the stream, if needed. (Which it isn't, in this case.)
Prevents any possibility of running off the end of the buffer. You could use the nonportable snprintf() instead here, but why?
If you're really happy with printf(), there's the template query interface instead.
boost::this_thread::sleep(boost::posix_time::milliseconds(500));
Have you read the threads chapter in the user manual? You don't get thread safety for free in MySQL++. Your problem could be due to memory corruption.
Warren Young, MySQL++ Maintainer
Try changing "%d" to "%u" in sprintf.