ChipChop Support Forum
Log in
Log out
Join the forum
My Details
REPLIES: 3
VIEWS: 283
BACK
REPLY TO THIS POST
Edgardo
19 Mar 2024
What are the arguments of triggerEvent()? ----- Serial Comunication Project
Hi everybody. This is my first post, thanks for your help.
I´m realizing a project with a device (we called "lock-in") that take as input simple commands of string like "Q1 \n" and return string like "2.01e-3". For that purpose I´m using a DOIT ESP32 DEVKIT V1 with a RS232 to TTL module (and a push button for control).

Basically, i took the smartbutton example and included the next function when i push the button:


void Lectura(char comando[]){ //Read a command with Lectura("G")
char data[]=" ";// Reset the char for new data
int i=1;
SerialPort.println(comando); //Send the string to lock-in

while(SerialPort.available()){
     data[i]= SerialPort.read(); //Read each character that the lock-in send
     i++;
}

ChipChop.triggerEvent("Resultado",data);//Send the complete data to ChipChop (Yes, my component on the live control is
//named "Resultado")

Serial.println(data); //Print on the terminal of the PC.
}

When I put directly:(ChipChop.triggerEvent("Resultado","Some");), on the live control works fine but with the complete reading of the lock-in is not. On the next image I expect a " 20" on the Resultado component but it give noting. I think that is something to do with the char-String conversion but I don't know a lot about it. Thanks again I will keep doing tests.



Attached images
Gizmo
20 Mar 2024
ACCEPTED

Hi Edgardo,

Yes, it looks like you need a conversion from char[] to String.

char[] is not something we can work with as it's meaningless to the ChipChop engine, if let's say your lock-in serial data is "Edgardo", viewed as char[] would be {"E","d","g","a","r","d","o"}

I am actually surprised you can even compile the program as updateStatus() and triggerEvent() can only accept: String, int & float so there is no option for "char" !? Maybe Arduino IDE is less strict, PlatformIO would not allow you to compile it :-)

Anyway, you have few easy solutions, without seeing the rest of the code I will list you some options that you can try:

Option 1

If you have to use char data[] then you can try




        ChipChop.triggerEvent("Resultado",String(data));

        //or maybe

        ChipChop.triggerEvent("Resultado",String(data.c_str()));



Option 2

If you just need to pass the info to triggerEvent you can use the Arduino readString(). Technically this function may not be as fast as option 1 as it relies on some internal timeout but I've used it many times in my projects and didn't notice any significant slowdown



    
        //there are few different ways of doing this
        
        while(Serial.available() > 0) {

            String data = Serial.readString(); // read the incoming data as string

            ChipChop.triggerEvent("Resultado",data);

        }


        //or maybe if you can terminate your string with something like "\n"

        if (Serial.available() > 0) {

             String data = Serial.readStringUntil('\n');

            ChipChop.triggerEvent("Resultado", data);

         }




One of these options should work, let me know if you still have the problem and I'll see if I can think of another way.




Edgardo
20 Mar 2024

Thanks Gizmo. I tried to used a float variable, but since the lock-in data is "small" (0.0002), always return 0.00.
But since accept String, I tried a double and a String auxiliary variables:



double j=atof(data);// Convert the data to an double variable
sprintf(bufferr, "%e ", j); //Convert that double to a String (even i can change the format)
ChipChop.triggerEvent("Resultado",bufferr); //Send to ChipChop the data with format n.n




Just for asking, there is a way to make the heartbeat of the log more frequent? Like a minute or 10 second, even with the max storage of 100 kb it is fine, if I can keep more data per minute.
Thanks again, and sorry for the troubles. Have a nice day.


Attached images
Gizmo
21 Mar 2024

Hi Edgardo,

Ah, the double and float, always a pain to work with. I didn't realise you were reading double values.

I don't know if this helps but If you know the number of decimal places, there are couple of methods of the Arduino String class



        String(val) // simple cast/convert to string

        String(val, base) // optional base can be DEC, HEX or BIN

        String(val, decimalPlaces) // <<< this is the one you can try like: String(0.00002, 5) should return "0.00002"



Regarding the log frequency, I have to limit it on the Community engine so everyone has a fair share of the resources as reading/writing data intensively can affect the performance for other users.

But...I know that this is for a college/university work so I have made an exception and the best I could do at the moment is to give you 60 sec log frequency.

You will have to use at the moment the testing Dev Console to set the frequency, I've given you access and it's at: https://dev26.chipchop.io (you can also have a play with the Code Builder there if you want)

If you want to log more frequently and you are ok with 100kb, you can use a simple hack, I've allowed that on purpose (here is a bit of info: https://forum.chipchop.io/post/219/)

Basically, you create an additional component, in your Arduino code you keep "buffering" the readings into it and then send it's status let's say every 60 sec (to match roughly the log frequency) with the combined readings.
It won't be perfectly organised on the log preview but it will work.
You can still keep sending in parallel the triggerEvent for the actual component as the reading happens.

Here's a simplified example to give you an idea, it doesn't matter how quickly you record, you can do it every millisecond if you want just make sure you don't exceed around 1kb as that would make the status too big to send



String log_buffer = "";
unsigned long combined_log_timer = 0;

void setup(){

        ..... your code

        ChipChop.useClock(true); // use this if you want to track the time


}

void record_buffer(String val){

    String current_time = ChipChop.Date.hour + ":" + ChipChop.Date.minute + ":" + ChipChop.Date.second; // you need to have in the setup ChipChop.useClock(true)
    
    buffer += val + " - " + current_time + ", ";

}

void loop(){
    if(millis() - combined_log_timer >= 60000){
        
        //send everything we have collected in the last minute assuming that the component name is "logger"
        ChipChop.updateStatus("logger", log_buffer);
        
        log_buffer = ""; // empty the string

        combined_log_timer = millis(); //reset the 60 sec timer


    }

}





When you view the log in the Dev Console it should look something like:

0.0023 - 10:15:24, 0.0031 - 10:15:25, 0.0021 - 10:15:26 .....

or you can omit the "hour" or even the "minute" as the log entry will anyway contain it so really you just need seconds

Let me know if this works