Hint
Sometimes to program ESP32 via serial you must keep GPIO0 LOW during the programming process
Go to the Thunkable site and set up an account or log in with a Google account. If you're new to Thunkable you won't see any existing projects, but that's about to change! Click "Apps" at the top left and click "Upload app project (.aia) from my computer".
The "native" file type for Thunkable is ".aia" files and these files will allow you to view and edit code blocks within Thunkable. First download the attached file called "ESP32_BLE_Demo.aia" and then load this file in Thunkable.
This should now bring you to the app's home screen where you can edit the user interface. To view and edit the code blocks, click "Blocks" sort of at the top left, next to "Designer". This tutorial isn't meant to teach you all the ins and outs of Thunkable, but I definitely recommend you explore it yourself and have fun with it!
Thunkable Companion App
You can also download the Thunkable companion app on your mobile device and do live testing with it, which is really darn cool because you can test the app without having to first compile and download it every time! Simply install it on your mobile device and under the "Test" tab at the top click "Thunkable Live" and it will bring up a QR code on the screen. Open the Thunkable app on your mobile device and scan the QR code to live test!
Now to actually get the app on your phone all you have to do is click "Export" and "App (provide QR code for .apk)" and scan the QR code with your phone using the Thunkable app. You can then install the app and open it! Alternatively, you can download the .apk file I've attached above and email it to yourself to get it on your phone.
Export the app with the first option and it will show a QR code for you to scan on your phone
When you first open the app it will ask you to turn on Bluetooth if you haven't already, and click "Yes". When the app is connected to your ESP32 it will print out arbitrary values that are sent to it from the ESP32 and the "LED" button allows you to toggle the LED on or off by sending "A" or "B" to the ESP32. But for now let's not jump the gun just yet!
Step 4: Code Explanation
First load the example sketch I attached a few steps ago and I'll try to give a brief explanation of what's happening. If you're using a different ESP32 dev board you should make sure that the LED pin is initialized correctly. Note that in Arduino IDE you should write the GPIO number, not necessarily the pin number shown on the board's pinout diagram.
BLE Intro
Bluetooth Low Energy (BLE) is a slightly different protocol than the traditional Bluetooth we might find in things like Bluetooth audio, for example. Instead of constantly streaming data, BLE "servers" (like the ESP32 reading sensor data) can "notify" clients (like your smartphone) periodically to send them bits of data. Therefore, BLE is more suitable for low-power IoT applications where large amounts of data aren't required.
Now in order to know which server and client to connect to, both the server and clients use a "service UUID" which describes the overarching service (kind of like a grocery store, Walmart for example). Inside this service there can be several "characteristics" which are defined by characteristic UUID's. This can be thought of kind of like the snack section in the Walmart, or the canned food section. Then we have "descriptors", which are attributes of the characteristics describing what it's being used for, and can be thought of like the brand of potato chips in the snack aisle of Walmart. This allows interoperability and standardization between various BLE devices so that you can, for example, connect your ESP32 with a heart rate monitor like what Andreas Spiess does in this YouTube video. You can view some example descriptors here.
So to summarize, when you (the client) check out Walmart (the service) you might be looking for potato chips (the characteristic) and pick up some Pringles (the descriptor). Because the product is labeled "Pringles" and not "Great Value" you know which product to choose from and what to expect. This is sort of how BLE devices operate. In our example, we use two different characteristics, TX and RX under the overarching "service" to send data to and receive data from a client (Android device) via these two channels. The ESP32 (acting as the server) "notifies" the client via the TX characteristic UUID and data is sent to the ESP32 and received via the RX characteristic UUID. However, since there is sending and receiving, TX on the ESP32 is actually RX on the Android app, so inside Thunkable you will notice that the UUID's are swapped from those in the Arduino sketch.
You could also think about this like AT&T customer service:
Server --> Waiting for client to connect
Client --> Connects to service
Server --> Via Customer Support characteristic: "Hi, nice to meet you! Would you like to consider our special family Internet bundle?"
Client --> Via Raised Voice characteristic: "No thanks, I would just like to know why my bill went up this time"
Server --> Via Customer Support characteristic: "OK, no problem. Would you also like to upgrade your Internet speed for only $5 more per month?"
Client --> Disconnects
Now in the example Arduino sketch we have some callback functions. These functions handle things like connecting/disconnecting as well as receiving data from the client. In the sketch I added an "if" statement that checks for an "A" or "B" being sent by the client and toggles the LED on or off accordingly. You will also notice that in the loop() function I use the "dtostrf" function to convert a number of type "float" into a char array so that the app can receive it properly.
Upload the Sketch!
Now upload the sketch to your ESP32 board, making sure that you have the right board and COM port selected. When it's done, open the serial monitor under Tools / Serial Monitor and you should see "Waiting a client connection to notify..." Now open the Android app, click the "Connect" button at the top left, and you should see a list of available nearby devices. Select the ESP32 and you should see the button text change to "Connected!" and start seeing values on the screen. To toggle the LED on or off press the "LED" button and check the serial monitor to see how it sends "A" or "B" to the ESP32. Pretty neat stuff huh?
1 / 5 • This is the list of nearby available Bluetooth devices
Step 5: Conclusions
The ESP32 is literally exploding with features! In this tutorial we've just learned the basics of how to create a simple Android app for two-way communication between your mobile device and the ESP32 using Bluetooth Low Energy. With this knowledge combined with WiFi and sensors we can now make some really cool projects with this! Also feel free to experiment with the app and throw in extra features for things like voice recognition, color pickers for LED control, slide bars for motor speed, or use your phone's accelerometer for controlling a robot via Bluetooth!
If you liked this tutorial, please give it a thumbs up and share!
If you're interested in building your own circuit boards at home, check out this tutorial on how to build your own reflow oven using an ESP32 via Bluetooth and Arduino IDE as well as an open-source ecosystem I invented called "Reflowduino".
Feel free to check out my website here and my humble YouTube channel here for more cool projects like this!
TESTE ARIDIO 4 ESP32 TTGO OLED LORA BATERIA 18650 DEMONSTRACAO DE DESENHO NO DISPLAY OLED ESP32 TTGO OLED LORA BATERIA 18650 USANDO INTEFACE IDE DO ARDUINO OBSERVACOES IMPORTANTES Define o endereço do i2c do Oled(0x3c) e os pinos SDA(5) e SCL(4) PARA ESTE ESP32 PRESSIONAR O BOTAO BOOT DURANTE A TRANSFERENCIA DO PROGRAMA SSD1306DEMODESENHOARIDIO.ino // EXEMPLO DEMO DISPLAY OLED; // // // ATENCAO ENDERECO DO DISPLAY E PINOS CLOCK E DADO //RESET SSD1306 PINO // SSD1306 display(0x3c, 5, 4); // // // For more informations, please vist www.heltec.cn or mail to support@heltec.cn #include <Wire.h> // Only needed for Arduino 1.6.5 and earlier #include "SSD1306.h" // alias for `#include "SSD1306Wire.h"` //OLED pins to ESP32 GPIOs via this connecthin: //OLED_SDA -- GPIO5 //OLED_SCL -- GPIO4 //OLED_RST -- GPIO0 #define DISPLAY_HEIGHT 64 #define DISPLAY_WIDTH 128 SSD1306 display(0x3c, 5, 4); // Adapted fro...
Comentários
Postar um comentário