Hi, I am facing issue with my project. I need to read and and send to the App, datas from a potentiometer linked to my Arduino UNO card. Datats are send using a HC06 bluetooth module.
Following picture are the App's bloks and Arduinos code I made. The problem is I can't get values, only 0s remains but I know the circuit and potentiometer works. It seems I can't send the values I obtain even if I can connect the App to the blutooth module....
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.
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.
Ok I changed the DelimiterByte to 10 as you recommanded it and also check the clock timer.
Still not working so I guess it is about something you say here :
The advice I gave is all boiler plate, common advice for all people new to this problem, so it is more broad in its advice than your current experience level.
A comma is the punctuation symbol ',' that on my PC keyboard appears between the "m" and '.' keys.
Yes, you already split your variables.
However, it would be wise to also display your incoming data stream in its unsplit form, in case of surprises.
For example, you are using two print streams,
Serial, and
HC06
I see in your sketch you are indeed doing a println() in your Serial data stream, but not in your HC06 print stream.
From your symptoms (no incoming data over BlueTooth), I'm going to guess that the HC06 print stream is the one that needed the Line Feed characters to trigger END OF MESSAGE processing in the AI2 BlueTooth Client component.
Here is some more boiler plate with a BlueTooth blocks set ...
Here is an updated blocks sample illustrating these ideas ...
Arthur, your Sketch has nothing to do with a potentiometer reading?
Take a look at this example set-up:
Things we need to know to get the App and Arduino singing the same song:
Arduino Model and Version
HC-06 Baud Rate
Android Device Make, Model, Bluetooth Version
Photo of your Arduino setup where we can clearly see pin numbers, module IDs etc.
Also, the Android device must have Location switched on (Google Security Measure) and Bluetooth Switched on, before the App is run (we can do those settings from within the App too, but best to keep it simple first).
For know, the source power come from the computer with the USB cable and the temperature reader device (black element with 3 pins) is changed with my potentiometer.
Can you verify the volts and amperage of the USB source. Most computers have some USB ports where amps are too low to power anything and your UNO needs about 300mA @ 9v (12v max)
Dear @Arthur,
the hints from @ChrisWard and @ABG are fundamental, but I would suggest you, in order to be sure that the Arduino side is OK, to load on your Android device an app like "Serial BT Monitor". By using such very basic app, you can verify whether the connection to your Arduino is working fine. In this way you avoid any uncertainty on Android side, since the Serial BT Monitor will work for sure.
Once confident that the Arduino board is sending correctly the data to the Android device, you can then investigate deeper in your app.
Best wishes !
The rs/tx Pins look correct, but in the Sketch, setup(), pinMode() is missing.
//06/06/2022 12:55:06
//ELEGOO UNO R3, Classic BT using HC-06 module
#include <SoftwareSerial.h>
#define rxPin 2
#define txPin 3
#define ANALOGPIN A0
SoftwareSerial HC06(rxPin, txPin); // Rx, Tx Bluetooth HC-05
unsigned long lgUpdateTime;
void setup()
{
pinMode(rxPin, INPUT);
pinMode(txPin, OUTPUT);
Serial.begin(9600);
HC06.begin(9600);
lgUpdateTime = millis();
}
void loop()
{
if(millis() - lgUpdateTime > 6000) //Loop approx every 6 seconds
{
lgUpdateTime = millis();
//Get values here
//To PC Arduino Serial Monitor via USB
Serial.print(Average);
Serial.print("|");
Serial.print(Range);
Serial.print("|");
Serial.print(SEM,2);
Serial.print("|");
Serial.print(MyTime);
Serial.println();
//To Android App via Bluetooth
HC06.print(Average);
HC06.print("|");
HC06.print(Range);
HC06.print("|");
HC06.print(SEM,2);
HC06.print("|");
HC06.print(MyTime);
HC06.println();
}
}
Note, you cannot send labels or other decoration, Bluetooth data packets are small. There is no need to convert to a string either, the App will simply read the data as a string if you use a string specific block. 'Time' is a Keyword, so give that var another name.
Ok yes indeed it is working fine with a Serial BT Monitor.
I guess the problem come from the command I wanted to input. In fact, without ON/OFF buttons, the app and arduino works fine together and I receive well datas on my phone.
But with RUN/Stop buttons, this doesn't work anymore. I checked on the serial monitor and well receive the comand "A" or "B" when I click on them but the Arduino don't seem to run (The LED which usually bright when the Arduino works remain turned OFF).
Here is my new Arduino's code (just the loop part) :
What is the var 'cmd' type? I'd use an integer, 1 run 0 stop.
I can't see how your loop has a time interval applied - this is required for the App to have time to receive and process data and should be as long as is practical for the task.
Are you displaying the values in the App? If so, 400ms is too fast for most eyes The App time interval needs to be approx 20% shorter than the Elegoo time interval to avoid data over-run. Ideally, we would not use a delay() as that stop-starts everything, whereas a time elapsed loop does not.
Can you post your Sketch File here? Change it's extension from .ino to .txt
The purpose of the modification to 'If' is to prevent send-receive at the same time, since you only have one channel between App and Elegoo.
For the delayI definitely miss something. Indeed 400ms is really hight for us but the App seem to be very slow if I put 500ms or more. On the clock in the App I input a delay of 100ms.