2014년 12월 30일 화요일

receiving data from the arduino uno


Hay, I'm still pretty new to the app inventor, but I'm trying to use this for my senior years project.

At the moment I have a pretty big problem, it is about reiciving the data my mate gets from our sensor at the arduino ( it is about controlling the temperature of water ) , he should send this data via bluetooth ( hc 05 ) to our android device where my app is installed, but I have no idea how I should programm the recieve part, does any1 of you have an idea about that? 
Ah what might be intresting to know, my bluetooth programm already works, also the part where I send data to him, my only problem is recieving it.

-- 

Possibly:

Have you paired the the bluetooth devices first (regarding the AI2 Bluetooth documentation   http://ai2.appinventor.mit.edu/reference/components/connectivity.html#BluetoothClient    )?



AddressesAndNames
The addresses and names of paired Bluetooth devices

--
I did a project using the Arduino and App inventor sending and receiving data, and I also used the HC-05 Bluetooth. Make sure that your Bluetooth is set to the clock speed of your phone's Bluetooth. I have an S3 phone so the clock speed I set was 57600.

In the file "bluetooth.png", the first two things you must do is have a way to select the Bluetooth device. When the device is on, make sure your phone is able to find it and connect to it. Next is being able to connect to the Bluetooth in the application which is completely separate from connecting it the phone. Now that we understand this, you can make your blocks based on the image.

To explain the code, the button that is being used is a list picker component. Before the button is pressed, the application will retrieve a list of Bluetooth devices available in your range. After the button is pressed, you will get a list of the Bluetooth devices available, once you click on your device, the HC-05 will begin blinking slowly verifying that you are connected.

The next part is the in the file "bluetooth2.png". Here we have a clock controlling when data is being received from the Bluetooth. We put ".BytesAvailableToReceieve > 0" to see if data is available. Afterwards, we use the ".ReceiveText" with a "BytesAvalableToReceive" attached to it and set it to a global or local variable, and that's it.

*Couple of important notes while doing this project:
- I implemented a clock to control the time it takes to retrieve data. The reason I did this is to prevent possible overflow, from what I could remember, the Bluetooth can store up to 64 Bytes before it begins to stop accepting data. Timing is very important!

- From the Arduino side, you can use "btserial.println" to send a string of data to the phone, and this is assuming you are using softwareserial for communication. If you send values, you will be limited to sending only a byte at a time and then you will have to manipulate the blocks to collectively get the data after every byte which can get messy. In other words, since this is new, convert your data value to a string on the Arduino and then send it.

Best of luck! If you have any further questions, I will be happy to answer them.



--
Question: What if we want to receive data for more than two devices?

This becomes a bit trickier, and not because of its difficulty, but because of how you setup your code. You must understand is that you can only send or receive data through one line, which means you need some kind of control flow so that the data is properly distributed. For that reason, I used multiple clocks for the data I was retrieving, but this was because of the code from the Arduino.

You must also ask yourself:

How is the data being retrieved from your temperature sensor through the Arduino? 

What are you going to do with that data afterwards? Are you going to send it immediately or are you going to wait for a signal from the Android phone to then retrieve data from the Arduino?

How is your information going to display on the application?

From there you can setup your protocol on how data is flowing. Consider the following:

-You can combine all your data from the Arduino into a string and in between the data from each temperature sensor, you can put a comma or space. After, under one clock you can receive the data, and process the string by splitting the data by the comma that you put before, then send your two temperature data into wherever you need to send it in the application. App inventor has blocks that are great for handling strings.

-The way I did it was much more complex. I setup a menu for the different devices, and I had a clock for each device. When I wanted information for a device, I pressed on that part of the menu which activated a clock with a 20 ms interval. This clock would send an ID number to the Arduino only once to let it know that I only wanted data from a specific device. The Arduino would read this ID number and will start sending data to the application every second (1 second delay). The application will then constantly check if there is anything to read from the Bluetooth.

These again are suggestions, but you should play around with the blocks and see what you can come up with. I would recommend starting with just one temperature sensor, once you understand how receiving information on the application works, then move onto the second temperature sensor. As far as clock speed, again it all depends on your code.

Best of luck! Please post any further questions here so that people may benefit from it.

--

댓글 없음:

댓글 쓰기