Send data from phone to arduino to regulate a led brightness

How can I do it though?

Send the maximum temperature together with temperature and humidity. Then a three-element list will be created, where the third element will be max temperature.

The problem is:

if (Serial.available() > 0) {
        

      led_color_str = Serial.readStringUntil(';');
      int led_color = led_color_str.toInt();
      Serial.println(led_color);
      switch(led_color){

       case 1: 
               int led_value = Serial.read();
               led_value = map(led_value,0,100,0,255);
               Serial.println(led_value);
               analogWrite(9,led_value);
              
               break;
              }

The app doesn't know what data you're sending, whether it's temperature with humidity, LED color, or some other value. Therefore, the easiest way is to add the two values together for temperature and humidity and send them all together in one session.

Serial.print( temperature );
Serial.print("|");
Serial.print( humidity );
Serial.print("|");
Serial.print(led_value);
Serial.print("|");
Serial.print(led_color);
Serial.println();

I get what you're saying, but the only data I need to send is temperature and humidity. All the other values are read by arduino, not sent. I printed them at the end because I wanted to know if the data was being sent correctly. So if I remove that serial.println for those values at the end will it work fine?

No.

Why you need println()

Please see the Delimiter article in FAQ

Be sure to use println() at the end of each message to send from the sending device, to signal end of message. Do not rely on timing for this, which is unreliable.

In the AI2 Designer, set the Delimiter attribute of the BlueTooth Client component to 10 to recognize the End of Line character.
BlueToothClient1_Properties
Also, return data is not immediately available after sending a request,
you have to start a Clock Timer repeating and watch for its arrival in the Clock Timer event. The repeat rate of the Clock Timer should be faster than the transmission rate in the sending device, to not flood the AI2 buffers.

In your Clock Timer, you should check

  Is the BlueTooth Client still Connected?
  Is Bytes Available > 0?
     IF Bytes Available > 0 THEN
       set message var  to BT.ReceiveText(-1) 

This takes advantage of a special case in the ReceiveText block:

ReceiveText(numberOfBytes)
Receive text from the connected Bluetooth device. If numberOfBytes is less than 0, read until a delimiter byte value is received.

If you are sending multiple data values per message separated by | or comma, have your message split into a local or global variable for inspection before trying to select list items from it. Test if (length of list(split list result) >= expected list length) before doing any select list item operations, to avoid taking a long walk on a short pier. This bulletproofing is necessary in case your sending device sneaks in some commentary messages with the data values.

I had already set the Delimiter Byte to 10. And, so far, I had done what you requested for the ClockTimer(see clock sensors, it's the one you circled). Plus, I wanted to point out that so far that clock timer has worked properly, because I only had the app display the temp and hum coming from arduino, so if anything is wrong I think that it's the buttons blocks part, the ones that say imp_tempmax, imp_tempmin, imp hum_max and imp_hum_min, because that's when that error has started popping up. Might there be a problem in the button blocks? Or do I have to set the clock block again? I'm rather new to this programming language so sorry if I do sound naive sometimes. I'm just trying to understand.

Yes, if the last two data is not sent to the app then it will work ok. Your blocks are correct, you correctly compose the list with "|". Only sending these individual data is a problem because it does not create a two-item list.
If you want to view debugging code for arduino, add the Software Serial library and create a serial port for BT on other pins, Hardware Serial will then remain for debugging the code in the terminal.

As @ABG mentioned, you can also protect all button blocks where you use blue "selection list item" blocks. because the arduino garbage received makes a list with one item. But as you delete unnecessary sent data to the app, the problem will also disappear.

1 Like

Okay so, besides the SoftwareSerial library option, what changes do I have to make in order not to have that error pop up and make everything work?

I think I've specified this before but I will say it again, maybe I explained it poorly. The only data I have to send are the temperature and humidity. I don't need to serial.print() led_color, led_value etc. as for those are values that arduino has to Serial.read() from the phone digits. That part works just fine. It's the part where I wanna trigger an alarm whether the temp value is greater than the temp max value I set from the phone or not (and same goes for temp_min,hum_max,hum_min) that doesn't work, and I get that error about the list length that equals 0. I already fixed the correct proportion between arduino clock and the app clock, so I don't think that's the issue. This is the error I get, and for any other thing, I sent both the sketches above.

If you don't need it, don't send it. Remove it from the code ...

This part of the arduino code sends unnecessary data to the phone, the app receives it and creates a wrong list. By clicking on the button, you are trying to download the second item from the list that does not exist, because the app took garbage from arduino and overwritten the list with garbage.

How can I remove it though? That's the part I need to make the led section work. Plus, I didn't think it sent arduino data as it only reads data incoming from the phone, through Serial.readStringUntil(). Either way, I need this part for the program. I will send the updated code once again since I've also sent older sketches and you might have got confused.

bluetooth_automation.txt (8.6 KB)

What it is? And why do you send it via bt to the app, if you don't receive this data in the app?

}

 if (Serial.available() > 0) {
    

  led_color_str = Serial.readStringUntil(';');
  int led_color = led_color_str.toInt();
  Serial.println(led_color);
  switch(led_color){

   case 1: 
           int led_value = Serial.read();
           led_value = map(led_value,0,100,0,255);
           Serial.println(led_value);
           analogWrite(9,led_value);
          
           break;
          }

Exactly that:

Serial.println(led_color);

and that:

Serial.println(led_value);

The fact that you set the brightness of the led I understand. But why are you sending it back to the App?

Export your .aia file and upload it here.

1 Like

Wait, I think I got something wrong by copying the sketch, I'll send it again shortly.

Here's the sketch. I apologize for the inconvenience.

bluetooth_automation.txt (7.9 KB)

home_automation_ch.aia (47.9 KB)

Or just like this:
bluetooth_automation (2).txt (8.6 KB)

Try it and see if you get an error.

Nevermind that file, I mistakenly copied it twice and some parts of the program appeared where they shouldn't have. Now look at the one I just sent

This is the correct one.