Switch Button Blocks Help

I have my MIT App controlling my esp32 via bluetooth. I want a switch button to turn on and off an LED. Can anyone help me with the blocks so that when accessing the app the toggle is off (at startup) and when the toggle is used it turns the led on and vice versa?

Dear @Bob1651,
it's not pretty clear to me what is your request.
You say : "I have my MIT App controlling my esp32 via bluetooth": this means that all the stuff related to the BT communicationm is already working ?
You only need to implement a Toggle switch in your AI2 app ?
If yes, the following is a draft flow of code.
At AI2 design level, create a Button (whose name is, for example, Toggle_Button) whose initial text is "OFF" and initial background color is RED.In the "ScreenInitialize" block send via BT the OFF message to the ESP32.
Whenever the event Toggle_Button_Click is raised, set the Toggle_Button_Text to "ON", set also its background color to GREEN and send via BT the ON message to the ESP32.
All the above supposes that the BT comm's is already working. If not, you'd better have a look to my examples here below:
BT_to_ESP32.aia (25.5 KB)
ESP32_BT_Monitor.ino (1.4 KB)

Best wishes.

Thanks uskiara,

Yes, I have the BT communicating with esp32 successfully. I want my esp32 circuits controlled by Switches in the app. I have taken a screenshot of the current blocks I'm using including BT.

Thanks for you help

Ok,
nice to read that your BT is working fine (typically this is the main issue).
So, to drive your appliances by means of app's buttons is now quite easy.
If you take a look to my examples you'll find how to do it.
Cheers.

EDIT: to make it easy:
here below the blocks to handle the switch ON/OFF
image

The bulbglow.jpg is an image stored in the asset directory:
image
and the bulboff.jpg as well:
image

1 Like

Thanks again, your examples are code for esp32? I already have a fully working code on the esp32 already. I know it works as I can control with serial bluetooth terminal. I just cant figure out the blocks to get the app to send commands to the esp32

While you were writing your last post, I was editing mine :smile:
Please look at it.
Obviously your ESP32 code shall implement a receiving function that will switch On or OFF the appliance when it receives a "1" (i.e. ON) or a "0" (i.e. OFF)

Thanks, that makes more sense. I'm trying to use the swich sliders background to go green when on rather than use .jpg. Will this reset my slider(s) to be off automatically when the app is restarted? I will test your blocks later to make sure they are functioning.

Sure, the background images are just to have a "pictorial" effect. By using the standard colours of the switches is pretty enough.
The initial status of the switch is the one set by default in the design page (i'm not using the PC in this moment, so I cannot tell you 100% but I'm 99% sure of that :grin:)
To be 100% sure, you can force the status of the switch (or any other type of component that needs to have an initially configured status) in the Screen1initialise block.

Brilliant! All working perfect!! I feel I'm starting to get a very basic understanding of the blocks with your help. Would you be able to help me try and collect serial data (from Esp32) which is sending out battery voltage data every second. I want to try and collect this info, so that a slider shows the current battery state (12vdc) and when the slider is moved it lowers or raises the cut-off point, all loads will disconnect when it falls below the user defined point. In a perfect world there would be a slider with a threshold that can have a sweet operating spot that reconnects when voltage reaches a user level.

In order to:
"collect serial data (from Esp32) which is sending out battery voltage data every second".
you should activate a clock in your app that "polls" the BT incoming buffer every 100 milliseconds. If the buffer is not empty, the clock event shall read all the characters contained in the buffer and shall use those characters to move the slider. If no data are present, the clock event will exit immediately without wasting time. The reason why I suggest you 100 ms while your ESP32 sends data every 1000 ms (i.e. 1 second) is because to avoid missing data it is necessary to have the polling rate at least the double of the sending rate. In my applications typically I use a by 10 ratio instead of a by 2 ratio, so to have a greater chance to catch all the data without missing any of them.

Then i suggest you to send and receive text (though representing numerical data) because it is the easiest way to handle data exchange between an App and the Arduino boards.
To this purpose please refer to the posts that @ABG has published many times on the matter (i.e. how to use the string terminator in the Arduino code, and its relevant management in the AI2 app).

You can also find a huge amount of examples related to the use of AI2 apps to act as user interface towards Arduino on the web sites of @juan_antonio (KIO4.com) and of @ChrisWard (professorcad.co.uk)
Another chest of treasures is also @Taifun web site (puravidaapps.com).

The examples I've posted are effectively showing how you can send commands and feedbacks from/to AI2 and Arduino (ESP32).
Once you are done with the basic Rx and Tx data exchange, then you can add any additional feature you want.
Always remember to do one-step-at-a-time, and to save meaningful .aia's in order to avoid to lose working code.
I reckon it seems to be a boring life, but before losing your hair in looking for a lost piece of "working" code, it's better to have working back-ups from where you can restart in case of loss of code.

2 Likes

Thanks uskiara,

Now that I have a working MIT App I can see what good advice it is to save the incremental changes along the way. I'll check out the resources you have recommended and hope to try and get the battery monitoring element working.

2 Likes