String gets corrupted - stack overflow? - c++

I'm despairing! I want to have my ESP8266 receive a string from a TCP client, execute the corresponding function and give a TCP response. But unfortunately the response string gets corrupted in some strange way:
Assuming that I enter an 'unknown command', the first 11 bytes are printed correctly via the serial interface (the rest is dump) and the first 11 bytes received by the client are dump, but the rest is correct (see comments in script below). But when I enter the 'dim' command the result is correct (but the return string is also shorter than "error: unknown command").
For the moment I have absolutely no idea how to fix this, even though I've tried a lot.
#include <WiFiClient.h>
#include <ESP8266WiFi.h>
#include <Wire.h>
#include <string.h>
struct parsed_query{
String command;
String arguments;
};
struct parsed_query parser(void){
// this function receives and parses a query
struct parsed_query result;
result.command="entered command";
result.arguments="entered arguments"
return result
}
char* str2char(String as_string){
int i_0=0;
while(as_string[i_0]!='\0'){i_0++;}
char as_char[i_0+1];
as_char[i_0]='\0';
for(int i=0;i<i_0;i++){
as_char[i]=as_string[i];
}
return as_char;
}
String executor(String command,String arguments){
String response;
if(command=="dim"){
response="dimming";
}
else if(command=="on"){
response="switching ON";
}
else{
response="error: unknown command";
}
return response;
}
void setup(){
// initialize serial interface, wifi & tcp-server
Serial.begin(115200);
WiFi.begin("<SSID>","<PASSWORD>");
while (WiFi.status() != WL_CONNECTED){delay(500);}
TCPserver.begin();
}
void loop() {
if(!client.connected()){
client=TCPserver.available();
}else{
struct parsed_query query=parser();
// This prints "error: unkno??*/???*??"
Serial.println(str2char(executor(query.command,query.arguments)));
// here, the client receives "????**?*??*?wn command"
client.write(str2char(executor(query.command,query.arguments))));
}
}
I have two ideas what could be causing this result (even though I don't know where to fix it in my code):
Case 1:
Maybe, I meshed up call-by-reference and call-by-value at some point (if yes, where??)
Case 2:
My programm is causing a stack overflow (if yes, where??)
Any help highly appreciated as I don't want to spend one more night.

In str2char you're returning a pointer to a local array, but like every local variable, it doesn't exist anymore after the function has returned. So reading from the returned pointer causes undefined behavior.
Compiling with warnings enabled (which is highly recommended) should output something like:
warning: address of local variable 'as_char' returned

(one) correct code would be
#include <WiFiClient.h>
#include <ESP8266WiFi.h>
#include <Wire.h>
#include <string.h>
#define TCP_RESPONSE_L 1024
struct parsed_query{
String command;
String arguments;
};
struct parsed_query parser(void){
// this function receives and parses a query
struct parsed_query result;
result.command="entered command";
result.arguments="entered arguments"
return result
}
int str2char(char *as_char, String as_string, int max_length){
int i_0=0;
while(as_string[i_0]!='\0'){
if(i_0>=max_length){as_string="error: caught an overflow! increase TCP_BUFFER_L";break;}
i_0++;
}
as_char[i_0]='\0';
for(int i=0;i<i_0;i++){
as_char[i]=as_string[i];
}
return 1;
}
String executor(String command,String arguments){
String response;
if(command=="dim"){
response="dimming";
}
else if(command=="on"){
response="switching ON";
}
else{
response="error: unknown command";
}
return response;
}
void setup(){
// initialize serial interface, wifi & tcp-server
Serial.begin(115200);
WiFi.begin("<SSID>","<PASSWORD>");
while (WiFi.status() != WL_CONNECTED){delay(500);}
TCPserver.begin();
}
void loop() {
if(!client.connected()){
client=TCPserver.available();
}else{
struct parsed_query query=parser();
char response[TCP_RESPONSE_L];
str2char(response,executor(query.command,query.arguments),TCP_RESPONSE_L);
//prints fine
Serial.println(str2char(executor(query.command,query.arguments)));
//correctly sending to client
client.write(str2char(executor(query.command,query.arguments))));
}
}

Related

Strange characters after JSON

I have an application that gets the Arduino Nano information and sends it to ESP-01 via UART. The ESP-01 send this to MQTT.
NANO CODE:
#include "DHTesp.h"
#include <ArduinoJson.h>
#include <SoftwareSerial.h>
#define gasSensor A1
#define dhtPin 5
#define rain A2
#define soil A3
#define ldr A4
DHTesp dht;
void setup() {
Serial.begin(115200);
pinMode(gasSensor, INPUT);
pinMode(rain, INPUT);
pinMode(soil, INPUT);
pinMode(ldr, INPUT);
digitalWrite(dhtPin, LOW);
dht.setup(dhtPin, DHTesp::DHT11);
}
void loop() {
delay(dht.getMinimumSamplingPeriod());
float humidity = dht.getHumidity();
float temperature = dht.getTemperature();
DynamicJsonBuffer jBuffer;
JsonObject& measure = jBuffer.createObject();
JsonObject& data = jBuffer.createObject();
measure["gas"] = analogRead(gasSensor);
measure["humidity"] = humidity;
measure["temperature"] = temperature;
measure["heatindex"] = dht.computeHeatIndex(temperature, humidity, false);
measure["rain"] = analogRead(rain);
measure["soil"] = analogRead(soil);
measure["ldr"] = analogRead(ldr);
data["measure"] = measure;
data.printTo(Serial);
}
ESP-01 CODE:
#include <Arduino.h>
#include <ESP8266WiFi.h>
#include <PubSubClient.h>
const char* mqtt_server = "0.0.0.0";
WiFiClient espClient;
PubSubClient client(espClient);
char mystr[100];
void setup() {
Serial.begin(115200);
WiFi.begin("", "");
Serial.print("Connecting");
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.print(".");
}
Serial.println();
Serial.print("Connected to IP: ");
Serial.println(WiFi.localIP());
client.setServer(mqtt_server, 1883);
}
void loop() {
if (client.connect("ESP")) {
Serial.println("STATUS MQTT-ESP: OK");
while (true) {
Serial.readBytes(mystr, 108);
client.publish("esp", mystr);
delay(1000);
}
} else {
Serial.println("STATUS MQTT-ESP: OFF");
}
}
But, I'm getting strange characters into MQTT:
And, at the connection between NANO <-uart-> Computer, the JSON is normal:
Someone could help me?
Thanks.
I am basing this answer on a bit of assumptions as I don't have an Arduino at hand.
You send your data like this:
data.printTo(Serial);
This sends the string holding the formatted JSON data. This does not include the terminating 0 byte.
Then you receive it like this:
char mystr[100];
...
while(true){
Serial.readBytes(mystr, 108);
client.publish("esp", mystr);
delay(1000);
}
This has multiple errors:
You do not care if you got any bytes at all. The method returns the number of bytes but you do not handle the return value at all. If you got 0 bytes within the timeout value, you will just send the previous message again instead of waiting for valid data.
You cannot put 108 bytes into memory location of 100 bytes.
You put an array of char into the publish method that does not contain any termination. How should that method know how many characters are really part of the string?
Try this:
while(true){
size_t num = Serial.readBytes(mystr, sizeof(mystr)-1);
mystr[num] = 0;
client.publish("esp", mystr);
delay(1000);
}
You do not have any protocol that allows you to detect when a message starts or ends. UART communiation is a byte stream without any boundaries for datagrams. You must ensure that you know what belongs to a message and what does not. This also means that you can never know if you have a complete message in your receive bufffer or an incomplete or even more than one message. It's up to you to detect and split messages. Fixing this is a bit more complex. I can only give some hints what you need to do.
a) Detect message boundaries:
You might send some terminator like \n, 0 or similar after each JSON string and scan for this extra byte on receiver side.
Or you can send the length of the string before you send the string.
You could also just check on receiver side when you have a matching pair of {}. This would not require any change on sender side.
b) Collect messages:
Call your read function as long as it takes until you detect the end of a string.
You might need to use intermediate buffer to collect multiple read buffers.
c) Forward messages:
As soon as you detected the end of a message, forward it via publish function.
Then move the remaining content of your collection buffer to the start of that buffer.
If you immediately find the end of another message, repeat that step.
If you don't find any complete message, continue to collect more data until the next message is complete.

How to store JSON Array in arduino program?

I was working on IOT project for which I needed JSON parsing in NodeMCU.
I saw this sample code for arduino for parsing JSON with the help of ArduinoJson library which worked well and I was able to get and parse the data from url(say url_1) successfully. Now I want to store this data in an array so that when I get data from the other ur2(say url_2) I can compare them with each other and trigger an event repective to the result.
Data in url_1 and url_2 is of form,
["1","1","1","1","0","0","0","0"]
and assume that url_1 had same values as specified.
What I did was I declared an array Data[] in which I stored the parsed JSON values so that I could use them later on in if else statements in the code.
As you can see that the data I am retrieving in url have 1's and 0's only, so what I want to do is that "If get 1 do this", "else do that", which you can see in the code. But the problem is that once I end the connection to the url the Data[] array gives only garbage values which I checked by printing them on serial monitor as shown in the code.
What I believe is that "const char* Data[20];" stores the position of JSON data and when I end the connection, the data at those position is also lost that's why I'm getting the garbage values. Now, I could be wrong since I'm new to this stuff. That's why I wanted to know how to solve this problem which is that if what I said was right, then how could I store the parsed json data in an array so that it is not lost even if the connection to the url has been ended.
(I'm new to this platform so If I did something wrong and wish that you guys can guide me for the future and I also apologize for my broken english).
#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
#include <ArduinoJson.h>
const char* Data[20];
const char* ssid = "SSID";
const char* password = "Password";
//Connecting to WiFi
void setup() {
WiFi.mode(WIFI_OFF);
delay(1000);
WiFi.mode(WIFI_STA);// Hides the viewing of ESP as wifi Hotspot
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting...");
}
Serial.println("Connected to WiFi Successfully");
}
void loop()
{
if(WiFi.status()== WL_CONNECTED)
{
HTTPClient http;
//Starting connection to url_1
http.begin("url_1");
int httpCode = http.GET();
if(httpCode > 0)
{
String data = http.getString();
const size_t bufferSize = JSON_ARRAY_SIZE(8) + 20;
DynamicJsonBuffer jsonBuffer(bufferSize);
JsonArray& root = jsonBuffer.parseArray(data);
for(int i=0;i<8;i++){
Data[i] = root[i];
Serial.println("Printing whole Data");
Serial.println(Data[i]);
}
}
http.end();//ending the connetion
for(int i=0;i<8;i++)
{
if(strcmp(Data[i],"1")==0){
Serial.print("if satement, Data = ");
Serial.println(Data[i]);
}
else
{
Serial.print("else satement, Data = ");
Serial.println(Data[i]);
}
}
}
}
Data[] contains pointers to dynamically allocated strings. When you leave the code block with the JSON parser, its destructor is called and therefore the allocated memory can and has been overwritten by something else.
I would suggest to use instead
bool Data[...];
...
// true for "1", false for "0"
Data[i] = strcmp(root[i], "1") == 0;
...
EDIT if you need to store more "complicated" data, e.g. actual strings, you will need to make a copy of the string pointed to by root[i].

XBee, external libraries and passing structures as arguments

I have very weird problem with a library I am creating. The library will be used to communicate between Arduino modules using XBee Series 1 modules. Library is very simple wrapper library around Arduino XBee library.
I have one function that reads received packet and sends it back. At the moment it is implemented as a simple "echo" service - the function just displays the data received and sends it back to per-defined address.
At the moment I have three versions of this function, out of which one is not working.
A function taking no arguments: void processPacket()
A function taking structure as a value as an argument: void processPacket(valuesStruct valuesStructData) - THIS VERSION OF THE FUNCTION IS NOT WORKING!
A function taking pointer to the structure as an argument: void processPacket(valuesStruct* valuesStructData)
At this moment I noticed strange behavior in the 2nd version of the function. I do nothing with the passed argument - the content of all three functions is the same. In 2nd case the function reads wrong values from received XBee packet. In the 1st and 3rd case the function performs correctly.
Code:
ExampleLib.h
#ifndef ExampleLib_h
#define ExampleLib_h
#include "Arduino.h"
#include <XBee.h>
#define ADDRESS_BROADCAST 0xffff
#define ADDRESS_PC 0x3333
typedef struct
{
int valA;
int valB;
int valC;
} valuesStruct;
class ExampleLib
{
public:
ExampleLib();
void setSerial(Stream &serial);
boolean tryReceivePacket();
void processPacket();
// THIS FUNCTION IS NOT WORKING!
void processPacket(valuesStruct valuesStructData);
void processPacket(valuesStruct* valuesStructData);
private:
XBee xbee;
Rx16Response rx16;
};
#endif
ExampleLib.cpp
The value read in line byte* packetData = rx16.getData(); is wrong when we trigger processPacket(valuesStruct valuesStructData) function. In other cases the behavior is correct.
#include "Arduino.h"
#include <XBee.h>
#include "ExampleLib.h"
ExampleLib::ExampleLib()
{
xbee = XBee();
rx16 = Rx16Response();
}
void ExampleLib::setSerial(Stream &serial)
{
xbee.setSerial(serial);
}
boolean ExampleLib::tryReceivePacket()
{
xbee.readPacket();
if (xbee.getResponse().isAvailable()) {
// got something
if (xbee.getResponse().getApiId() == RX_16_RESPONSE) {
// got a rx packet
xbee.getResponse().getRx16Response(rx16);
return true;
}
else {
return false;
}
}
else if (xbee.getResponse().isError()) {
//nss.print("Error reading packet. Error code: ");
//nss.println(xbee.getResponse().getErrorCode());
// or flash error led
return false;
}
return false;
}
void ExampleLib::processPacket()
{
byte* packetData = rx16.getData();
byte dataLength = rx16.getDataLength();
Serial.print("START L:");
Serial.println(dataLength);
for (int i = 0; i < dataLength; i++) {
Serial.print(packetData[i]);
Serial.print(" - ");
}
Serial.println("END");
//16-bit addressing: Enter address of remote XBee, typically the coordinator
Tx16Request tx = Tx16Request(ADDRESS_PC, packetData, sizeof(packetData));
xbee.send(tx);
}
void ExampleLib::processPacket(valuesStruct valuesStructData)
{
processPacket();
}
void ExampleLib::processPacket(valuesStruct* valuesStructData)
{
processPacket();
}
Arduino sketch
#include <XBee.h>
#include <ExampleLib.h>
ExampleLib exampleLibObj = ExampleLib();
void setup()
{
Serial.begin(9600);
exampleLibObj.setSerial(Serial);
}
void loop()
{
boolean isPacketReceived = exampleLibObj.tryReceivePacket();
if (isPacketReceived) {
// leave only one section, the rest should be commented
//Section 1: working
exampleLibObj.processPacket();
//Section 2: not working
// valuesStruct test;
// test.valA = 0;
// test.valB = 0;
// test.valC = 0;
// exampleLibObj.processPacket(test);
//Section 3: working
// valuesStruct* test;
// test->valA = 0;
// test->valB = 0;
// test->valC = 0;
// exampleLibObj.processPacket(test);
}
}
I am really puzzled why in this one case function is performing differently. Looking forward to any suggestions to that issue.
Thanks,
Michal
Are you sure it isn't your section 3 that's causing problems? Because you're declaring a pointer to a structure, but not allocating memory for that structure.
You'd typically write your code like this:
valuesStruct test;
test.valA = 0;
test.valB = 0;
test.valC = 0;
//Section 2: not working
exampleLibObj.processPacket(test);
//Section 3: working
exampleLibObj.processPacket(&test);
But you also wouldn't typically pass a structure to a function -- you'd pass a pointer to that structure. There really isn't a need for your second sample.

'SDL_main' : must return a value

I am trying to retreive content of websice in c++ usind SDL but it is giving me this error:
'SDL_main' : must return a value
my code is:
#include <iostream>
#include "SDL.h"
#include "SDL_net.h"
#include <cstring>
int main(int argc,char** argv)
{
SDL_Init(SDL_INIT_EVERYTHING);
SDLNet_Init();
IPaddress ip;
SDLNet_ResolveHost(&ip,"www.linux.org",80);
const char* http="GET / HTTP/1.1\nHost: www.linux.org\n\n";
TCPsocket client=SDLNet_TCP_Open(&ip);
SDLNet_TCP_Send(client,http,strlen(http)+1);
char text[10000];
while(SDLNet_TCP_Recv(client,text,10000))
std::cout << text;
SDLNet_TCP_Close(client);
SDLNet_Quit();
SDL_Quit();
}
When I put return 0; at the end, it built project but it finished immediately after that
(I am using vs2012)
UPDATE
cout<<"Some message";
doesn't print anything, is it possible that I have configured my imports wrong? are those additional dependencies right?
SDL.lib;SDL_net.lib;SDLmain.lib
I don't know what else could be wrong ...
It's because SDL defines a macro like this:
#define main SDL_main
So the function you've written is actually called SDL_main and like any other function that is not the actual main function, if it doesn't return void, you have to give it a return statement.
because your code doesn't loop forever it just returns 0 after first pass, you need to make a loop like:
while(1){
sdl_events event;
switch(event){
//handle events, drawings and so on
...
...
...
case SDL_QUIT:
exit (0);
break;
}
}
http://sdl.beuc.net/sdl.wiki/OpenGL_Full_Example
UPDATE
you may also have some problem connecting to host so you could check if connection succeed like this:
#define MAXLEN 1024
int result;
char msg[MAXLEN];
result = SDLNet_TCP_Recv(sock,msg,MAXLEN-1);
if(result <= 0) {
// TCP Connection is broken. (because of error or closure)
SDLNet_TCP_Close(sock);
exit(1);
}
else {
msg[result] = 0;
printf("Received: \"%s\"\n",msg);
}
UPDATE 2
change this:
while(SDLNet_TCP_Recv(client,text,10000))
std::cout << text;
to this:
while(SDLNet_TCP_Recv(client,text,9999))
std::cout << text;
UPDATE 3
try this, put your receive part in this if statement
if(SDLNet_SocketReady(client) == 1)
{
while(SDLNet_TCP_Recv(client,text,9999))
std::cout << text;
}
if this still doesn't work I suggest to use QT sockets or Boost asio, both async and more intuitive

Using SD.remove() and SD library in Arduino

I'm building a program which takes 10 measurements of an analog voltage on pin0 and is printing it to a log file. The issue I'm running into comes when I try to ensure that the file is blank. I am using SD.remove() in order to remove a previous logfile. When I do this, the new log file is never actually written to. When I remove the call to SD.remove(), the program works correctly. Is this some known bug in the SD library or is there some sneaky way around this?
The code is below.
#include <SD.h>
#define OUTPUT_PIN 9 //Using SparkFun MP3 shield
#define DEFAULT_OUTPUT 10
#define VOLTAGE_REF (5)
//Reads a voltage on pin0. by default, the reference voltage is set to 5 V, but
//it can be changed by changing VOLTAGE_REF.
void setup() {
Serial.begin(9600);
Serial.println("Program Initialized");
pinMode(DEFAULT_OUTPUT ,OUTPUT); //Needs to be on to use the library
pinMode(0, INPUT);
if (!SD.begin(OUTPUT_PIN)) {
//init error
Serial.println("Error initializing SD card. Reset the Arduino and try again");
return;
}
Serial.println("Card sucessfully initialized");
if (SD.exists("LOGFILE.LOG") {
SD.remove("LOGFILE.LOG"); //We don't want to use the same file <<THIS IS THE BUG?
}
delay(10); //Make sure changes are applied
File logFile = SD.open("ANALOG.LOG", FILE_WRITE); //Create a new one every time
if (!SD.exists("LOGFILE.LOG")) {
Serial.println("There was some error making a new instance of the logfile?");
delay(1000);
SD.open("ANALOG.LOG", FILE_WRITE);
}
int i;
if (logFile) {
for(i=0;i<10;i++) {
int j = 0;
char str[64];
Serial.print("Reading analog sensor value");
for(j=0;j<=i;j++) {
Serial.print(".");
}
Serial.println();
logFile.print("Read #");
logFile.print(i+1);
logFile.print(" : ");
logFile.print(doVoltageRead(0));
unsigned char l = logFile.println(" V");
if (!l)
Serial.println("No data written");
delay(500);
}
Serial.println("Done.");
logFile.close(); //Close the logfile
Serial.println("Data sucessfully written");
}
else {
//Couldn't create file
Serial.println("There was an error creating the logfile");
}
}
void loop() {
//We don't really need to do anything here
}
float doVoltageRead(int pin) {
int voltageRead = analogRead(pin);
double divisor = (voltageRead * 0.00097752);
float finalVoltage =(VOLTAGE_REF * divisor);
Serial.println(finalVoltage);
return finalVoltage;
}
So.. First you see if LOGFILE.LOG exists, if it does you delete ANALOG.LOG. Then you create ANALOG.LOG. After that you check if LOGFILE.LOG exists. If it doesn't, you print an error and open ANALOG.LOG.
What exactly is the purpose of LOGFILE.LOG? Shouldn't you just change LOGFILE to ANALOG?
I have found the source of the error. When I called if(SD.exists()) after opening a new logfile, the library didn't like that. I am assuming SD.exists() just checks if the address or object returned by open is NULL. However, calling it when the file is already open would cause some strange behaviors. After removing that call inside open, all is well. Thanks for all the suggestions!