Patryk, thank you very much. The improved when.connected block allows the listView to display as intended without bugging.
I think I now understand the error message: The when BluetoothLE1.DeviceFound block had already parsed the strings so that the elements of the listView were unbroken, single strings. Hence my attempt to parse them again in the wen BluetoothLE1.Connected block failed.
I wrote the snippet below so that, soon after connecting to the Arduino, the ToggleButton would have a display consistent with the state of the Arduino, i.e.
read "myPin is set to HIGH" with a green background when the string characteristic on the Arduino has value "9" (as per the already defined procedure TOGGLE_BUTTON_ON)
read "myPin is set to LOW" with a red background when the string characteristic on the Arduino has value "5" (as per the already defined procedure TOGGLE_BUTTON_OFF)
The snippet doesn't have any effect. Did I do anything obviously wrong?
It does - because the interface would be a ListView of Device names alongside a ListView of status data (we can make that a single two-column list using HTML or an extension now if required; Also next release of AI2 has an enhanced ListView). If devices are added in the future, that just requires a re-scan of devices to update the Lists. You would need an edit function to remove devices that should not be listed (code snippet on my website).
Hi Chris. I misunderstood your earlier post, I guess. I thought you were proposing a hard-coded list. Is there an example on your website that would be a reasonable template for me to look at? Also, which 'edit function to remove devices" are you referring to? I've looked on your website and don't know which one you mean. Separately, do you see anything wrong with my code snippet above to make the toggle button appearance reactive to the characteristic of the arduino peripheral?
Hi Patryk. I've attached the .aia file. The code is too spread out to capture in one screenshot. Thanks for your help.scratch_for_ai2_help.aia (195.0 KB)
Hi. Sorry about that - I misunderstood. I've attached the Arduino sketch code as a text file. Noteworthy features are:
localName of device is constructed from baseName and ON/OFF state of pin, as per all the discussion above.
the localName shows up correctly in the listView but neither the "myPin is set to HIGH(LOW)" button on the phone app nor the text in the Status label react to the change in localName on the Arduino. The "when BluetoothLE1.StringsReceived" code snippet above was intended to make the appearance of the button reactive to the change. I've done nothing to make the Status label reactive - that's still a to-do. sketch_for_ai2_help.txt (2.1 KB)
I don't know if the "ConnectedDeviceName" block always returns the current name or the name remembered while connecting. This needs to be checked and possibly corrected.
Thank you very much for your efforts. I hope to be able to follow your and Chris' good examples and give back to others on this forum in the future.
I looked over and installed your sketch. As the code is now, there are two functional changes from the previous version:
Firstly, it does correctly parse out the baseName piece of the advertised localName and display the label as "Status: Connected to Dev1" rather than the advertised "Status: Connected to Dev1_ON", for example.
But, secondly, the toggle button no longer works, i.e. it neither changes the state of the characteristic of the Arduino (which is printed out 1x per second in the serial monitor) nor does it change to reflect the state of that characteristic, i.e. it stays red and says "myPin is set to LOW" when the advertised localName is Dev1_ON.
Re. your question: No, the pin state will only ever be changed by the phone app. But, the phone app may be launched when the Arduino has already been running and is in some state that's advertised by the Arduino but not remembered or stored by the phone app.
I will try to figure out what's wrong with the sketch you sent, but more expert help is always welcome...
Debug your arduino code. Add lines that will be displayed in the terminal when arduino receives data from the app. Just see if the arduino is correctly receiving data 5 and 9.
Previously, you changed the labels hard in the app. Although arduino may not have received the value of 9, the label was set to ON. Now, when the button is clicked, the data is sent to arduino. The Arduino has to rename and only then the labels will be updated.
What color is the label immediately after connecting to the ON device.
Sorry, I haven't understood your comment but I can tell you the behavior. When I launch the app on the phone the initial state is that the button is red and reads "... LOW". When I connect to the ON device the button stays in that state and, furthermore, is unable to change the state of the Arduino (i.e. clicking the button doesn't cause the name printed in the serial monitor to change nor does the button on the phone change).
So the problem is in sending or receiving the values 5 and 9.
Note that I haven't changed anything in sending data to arduino. So it couldn't work before that as well? Check out this .aia. I changed the blocks from WriteStringWithResponse to WriteString. scratch_for_ai2_help (4).aia (195.8 KB)
The scratch (AI2) and sketch (arduino) code that i posted yesterday work fine together apart from the issue of making the button appearance and connected to: label on the phone app reactive to the arduino's state...
I've installed and tested your new aia file. The toggle button remains red and non-functional, i.e. unable to change the arduino. When I press the test button the toggle button changes it's color and text correctly based on the current arduino state and the correct device name, e.g. Dev2_OFF, is displayed below the toggle button.
the red toggle button has no effect on the arduino, either before or after clicking the test button. the test button changes the appearance of the toggle button correctly but does not make the toggle button functional.