I have an issue while transferring the values of string from UART to buffer. I am using ESP8266 to receive strings on serial from STM32 device. I have total 600 string and i have CSV file that is being transmitted from the STM32 device. I have used proper filtering for every row on the NodeMCU side... I am having no clue why after 300 string the value transmitted to the buffer gets changed as well as the string read on UART also changes exactly after 300/305 string being transmitted. please let me know if there's any mistake in the code.
int i,j,k,l=0;
int httpCode=0;
String fields[24] = {"","month","year","hours","minutes","seconds","rimin","riavg","rimax","yimin","yiavg","yimax","bimin","biavg","bimax","nimin","niavg","nimax","eimin","eiavg","eimax","simin","siavg","simax"};
String element = "API_key=123&mac=0fa&day=";
String postApi1[300];
String postApi2[300];
String str= "";
String str_tx = "";
char char_array[128];
char* token;
char* rest = char_array;
WiFiClient client;
HTTPClient http;
void setup()
{
Serial.begin(230400);
s.begin(9600);
pinMode(13, INPUT);
pinMode(15, OUTPUT);
}
void loop()
{
if (s.available() >0)
{
for(j=0;j<300;j++)
{
i=0;
str = s.readStringUntil('\n');
Serial.println(str);
Serial.println("j");
postApi1[j]= str;
Serial.println(postApi1[j]);
str = "";
}
for(k=0;k<300;k++)
{
i=0;
str = s.readStringUntil('\n');
Serial.println(str);
Serial.println("k");
postApi2[k]= str;
Serial.println(postApi2[k]);
str = "";
}
}
}
Here is the code i am using and following is the Serial output of the code.
I think that the readStringUntil function waits for the terminator char. Is the '\n' sent also after 300th character? Also, try to avoid Serial.println in the read function, since the print function is quite slow, you might miss some characters. I would also avoid two for loops, instead, declare a variable, count number of received bytes and according to this save the "str" into "postApi1" or "postApi2".
Also, what is the purpose of "i" variable?
Hey #Andrej I tried your suggestion but it worked as follows
#include <Arduino.h>
#include <ESP8266WiFi.h>
#include <ESP8266WiFiMulti.h>
#include <SoftwareSerial.h>
#include <ESP8266HTTPClient.h>
#include <WiFiClient.h>
#include <stdio.h>
#include <string.h>
using namespace std;
ESP8266WiFiMulti WiFiMulti;
SoftwareSerial s(13,15); //RX TX
double buff[24]; //Initialized variable to store recieved data
double val;
char c;
int i,j,k,l=0;
int httpCode=0;
String fields[24] = {"","month","year","hours","minutes","seconds","rimin","riavg","rimax","yimin","yiavg","yimax","bimin","biavg","bimax","nimin","niavg","nimax","eimin","eiavg","eimax","simin","siavg","simax"};
String element = "API_key=123&mac=0fa&day=";
String postApi[616];
String str= "";
String str_tx = "";
char char_array[128];
char* token;
char* rest = char_array;
WiFiClient client;
HTTPClient http;
void setup()
{
Serial.begin(230400);
s.begin(9600);
pinMode(13, INPUT);
pinMode(15, OUTPUT);
//Serial.setDebugOutput(true);
//ESP.wdtDisable();
//Serial.begin(115200);
// Serial.setDebugOutput(true);
Serial.println();
Serial.println();
Serial.println();
for (uint8_t t = 4; t > 0; t--) {
Serial.printf("[SETUP] WAIT %d...\n", t);
Serial.flush();
delay(1000);
}
WiFi.mode(WIFI_STA);
WiFiMulti.addAP("ECLP", "p##$w0rD");
if ((WiFiMulti.run() == WL_CONNECTED))
{
Serial.print("WIFI connected");
}
}
void loop()
{
if (s.available() >0)
{
for(j=0;j<616;j++)
{
i++;
str = s.readStringUntil('\n');
//Serial.println(str);
//Serial.println(j);
postApi[j]= str;
//Serial.println(postApi[j]);
str = "";
}
}
if(i == 616)
{
i=0;
for(k=0;k<616;k++)
{
Serial.println(k);
Serial.print(" : ");
Serial.print(postApi[k]);
}
}
}
And receive on the output the following thing
Just for the ref i printed "k" value with output to know at which string the data loss occurs in buffer
I have C++/CLI code function update to richTextbox.
public:void WriteToRichTextBox(char * outputText)
{
String^ result = gcnew String(outputText);
richTextBox1->Text += result;
}
And in C Code how can i do:
void updateString()
{
char * abc = "example text";
WriteToRichTextBox(abc);
}
Update answer:
https://msdn.microsoft.com/en-us/library/ektebyzx.aspx
If you call a function, it will do what it has to do, for example, you wanna do a divider formed by "="
and to not waste time you can create a function:
void divider()
{
int i;
for(i=1; i<=80; i++){
printf("=");
}
printf("\n");
}
and in the main function you call it:
int main()
{
some code;
divider();
}
and you will get onto you terminal a divider long 80.
I hope that's will help you, at least this is what I understood you wanna get.
I've been trying to achieve serial communication between my arduino-based project and my pc ,i need to send commands to arduino over serial and use "if and else" to call desired function (in parseMessage() function).
I can't use delay() since I'm using interrupts for multiplexing and bit-angle modulation so i had to do serial communication another way around, this is closest I've got to succeess but still I'm getting character skips and unstability in general. and as you know coding is all great except for when you don't know what's wrong with your code, so help me please gods of the internet! ;)
The reason I'm using '#' as end of the string declearation is that I can't be sure that all characters of my command sent to arduino is there when Serial.read() asks for it, there might be more on the way and since atmega328 is faster than serial port Serial.available() might actually return -1 in middle of transmission.
ps : oh, and I can't use String class, It's very expensive, this atmega328 is already sweating under 8x8 RGBLED multiplexing and 4bit-angle modulation and he is gonna have to do even more in future.
ps : and I'm still learning English so pardon me if there is something wrong with the grammer I'm using.
void setup() {
Serial.begin(9600);
}
bool dataRTP = false; // data ready to parse
void loop() {
readSerial();
}
char message[64];
int index = 0;
void readSerial() {
if (Serial.available() > 0)
while (Serial.available() > 0)
if (Serial.peek() != '#') // i'm using '#' as end of string declearation.
message[index++] = Serial.read();
else {
message[index++] = '\n';
dataRTP = true;
break;
}
while (Serial.read() != -1) {} // flushing any possible characters off of
if (dataRTP) // UARTS buffer.
parseMessage();
}
void parseMessage() { // just testing here, actual code would be like :
Serial.print(message); // if (!strcmp(message, "this expression"))
index = 0; // callthisfunction();
dataRTP = false; // else ... etc
}
Just managed to fix this code, seems flushing data off of serial UART wasn't a good idea after all. It's all solved. Here's how code looks now:
void setup() {
Serial.begin(9600);
}
bool dataRTP = false;
void loop() {
readSerial();
}
char message[64];
int index = 0;
void readSerial() {
if (Serial.available() > 0)
while (Serial.available() > 0)
if (Serial.peek() == '#') {
message[index++] = Serial.read();
message[index++] = '\0';
dataRTP = true;
break;
}
else
message[index++] = Serial.read();
if (dataRTP)
parseMessage();
}
void parseMessage() {
Serial.println(message);
index = 0;
dataRTP = false;
}
i have a piece of code thats makes a image search on flickr and returns the URL of the first image with that name. certain words i search on flickr doesn't have any matching images so because there are no images to get i get an ArrayOutOfBoundsException [0]. is there a way that i can make the program skip that particular words and keep on searching with next words?
this is my code so far:
PImage[] images;
int imageIndex;
XML xml;
String tag = "rutte";
String tag_mode = "all";
String words[];
void setup() {
size(50, 50);
String lines[] = loadStrings("text.txt");
words = split(lines[0], " ");
for (int k = 0; k<words.length; k++) {
String query = "https://api.flickr.com/services/rest/?method=flickr.photos.search&api_key=MY API KEY&tags="+ words[k] + "&sort=relevance&tag_mode="+ tag_mode +"format=rest";
xml = loadXML(query);
XML[] children = xml.getChildren("photos");
if(children.length > 0){
XML[] childPhoto = children[0].getChildren("photo");
for (int i = 0; i < 1; i++) {
String id = childPhoto[i].getString("id"); // this line generates the error :(
String title = childPhoto[i].getString("title");
String user = childPhoto[i].getString("owner");
String url = "https://www.flickr.com/photos/"+user+"/"+id;
println(url);
println("=====================");
}
}
}
textAlign(CENTER, CENTER);
smooth();
}
void draw() {
}
Just use the length field of your array. Something like this:
XML[] children = xml.getChildren("photos");
if(children.length > 0){
XML[] childPhoto = children[0].getChildren("photo");
if(childPhoto.length > 0){
String id = childPhoto[0].getString("id");
//rest of your code
}
You can find more info in the Processing reference.
In fact, you're already doing this with your words array!
I've collected some string information in processing and I am trying to send it to Arduino. I am getting processing to send the information, however, my output in arduino is weird. I get numbers like "77789...". I am not sure What I am doing wrong. All I need to do is, basically get a string from processing and send it to arduino to display it on a LCD screen.
Any help on this would be appreciated.
Here is my processing code:
import processing.serial.*;
Serial myPort;
XML MSFTxml; // loading Query
XML MSFT; // results of the query
XML row; // first row in the query
XML symbol; // Name of the stock
String symbolTxt;
String val;
void setup() {
size(200,200);
myPort = new Serial(this,"COM3", 9600);
}
void draw() {
updateXMLCall();
delay(10000);
}
void updateXMLCall () {
MSFTxml = loadXML("https://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20csv%20where%20url%3D%27http%3A%2F%2Fdownload.finance.yahoo.com%2Fd%2Fquotes.csv%3Fs%3DMSFT%26f%3Dsl1d1t1c1ohgv%26e%3D.csv%27%20and%20columns%3D%27symbol%2Cprice%2Cdate%2Ctime%2Cchange%2Ccol1%2Chigh%2Clow%2Ccol2%27&format=xml&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys");
MSFT = MSFTxml.getChild("results"); //Getting the first tag "results" in the query MSFT
row= MSFT.getChild("row"); //first child tag "row"
symbol = row.getChild("symbol"); //name of the stock
symbolTxt = symbol.getContent().toString(); //converting the name of the stock into a string
myPort.write(symbolTxt);
println(symbolTxt);
}
Here is my arduino Code:
#include <Wire.h>
#include "rgb_lcd.h"
rgb_lcd lcd;
const int colorR = 50;
const int colorG = 0;
const int colorB = 0;
void setup()
{
Serial.begin(9600);
// set up the LCD's number of columns and rows:
lcd.begin(16, 2);
lcd.setRGB(colorR, colorG, colorB);
// Print a message to the LCD.
lcd.setCursor(0, 1);
}
void loop()
{
String content = "";
if (Serial.available()) {
while (Serial.available()) {
content += Serial.read();
lcd.print(content);
Serial.print(content);
lcd.setCursor(0, 1);
}
}
delay(100);
}
The problem is the use of += in C += doesn't concat strings.
You need to concatenate the char get from Serial.read() like here for example:
Convert serial.read() into a useable string using Arduino?