Help me for this simple app

Hello everyone, I'm working on a project with ESP32 and App Inventor and I need some help with creating the application. I have written a program that allows me to turn on and off an LED and vary its intensity using a Bluetooth module. I would like to create a graphical user interface for the application using App Inventor, and I would like to know how I can put the correct blocks to handle the Bluetooth connection, the turning on and off of the LED, and the variation of intensity. I have already tried to use some App Inventor blocks, but I can't get them to work properly. Does anyone have any suggestions or examples to show me? Thank you in advance
This is the code :

#include <BluetoothSerial.h>

BluetoothSerial SerialBT;

int ledPin = 13; // Pin del LED
int intensity = 0; // Luminosità del LED

void setup() {
  Serial.begin(9600);
  SerialBT.begin("ESP32_LED"); // Nome del dispositivo Bluetooth
  pinMode(ledPin, OUTPUT);
}

void loop() {
  if (SerialBT.connected()) {
    if (SerialBT.available()) {
      int command = SerialBT.read(); // Legge il comando inviato dall'app
      if (command == '0') { // Accendi il LED
        digitalWrite(ledPin, HIGH);
      } else if (command == '1') { // Spegni il LED
        digitalWrite(ledPin, LOW);
      } else if (command >= 2 && command <= 255) { // Imposta la luminosità del LED
        intensity = command;
        analogWrite(ledPin, intensity);
      }
      Serial.println(command);
    }
  } else {
    Serial.println("Bluetooth disconnesso!");
    // Aggiungi qui il codice per gestire la disconnessione del Bluetooth
  }
}

Dear @David1,
I see from the comments into your .ino file that you are Italian, so I'll tell you a few things in Italian, then I'll revert to English for the Community :slight_smile:

Per prima cosa diamo per scontato che, adoperando la libreria BluetoothSerial, tu stia usando il BT classico, il che è una buonissima cosa, per iniziare, essendo molto più semplice da usare del BLE.
Detto questo, però, vedo un problema con la tua ricezione : definisci command come int, ma poi lo confronti con '0' o '1' che sono caratteri, che nella codifica ASCII sono i numeri 48 e 49 (0x30 e 0x31).
Credo che possa "fare casino" (questo in Italiano lo posso dire :slight_smile: ) con il resto del tuo algoritmo, in cui per numeri > 2 imposti la luminosità.
A parte questo, però se non posti almeno qualche tuo blocco di AI2 non possiamo sapere cos'è che non funziona. Di solito suggerisco di andare sul sito web di @Juan_Antonio che ha praticamente sviscerato tutto lo scibile universale della comunicazione BT tra Arduino, ed i suoi derivati, e AI2.
Ad esempio qui trovi già molto :
http://kio4.com/appinventor/index.htm
Sono convinto che usando i suoi blocchi e adattando il suo codice Arduino otterrai il risultato in poco tempo (vedrai, in un'oretta scarsa, compresa la pausa :coffee:)
Ciao !

In a nutshell, for the Community: I believe that thanks to the huge availability of BT / Arduino tutorials of @Juan_Antonio 's web site (but also that of @ChrisWard and the FAQ maintained by @ABG ) most of the questions related to the same subject can be answered just linking their web sites.
:muscle: :muscle: :muscle:

in realtà volevo fare un programma per accendere e spegnere un led e variare intensità del led

Ciao @David1,
è esattamente quello che sul sito di @Juan_Antonio puoi trovare.
Se vai su www.KIO4 trovi veramente una quantità esagerata (ma molto ben ordinata e facile da trovare) di app in AI2 con i corrispettivi codici Arduino già bell'e fatti :slight_smile:
Tra l'altro, quello per accendere e spegnere il LED è proprio il primo ed il più classico.
Sempre sul suo sito trovi anche :

http://kio4.com/arduino/index.htm
http://kio4.com/arduino/curso.htm
http://kio4.com/arduino/cursoiot.htm

Una volta data un'occhiata, o usato, un suo esempio, per la prima volta, poi potrai andare avanti "con le tue gambe" e aggiungere e modificare quello che ti serve: "l'appetito vien mangiando". E anche per questo motto approfitto dell'Italiano :slight_smile: :innocent:
Ciao e buon weekend.

PS (ça va sans dire, mais c'est mieux le dire...) Ovviamente, se ti "pianti" da qualche parte di specifico, non esitare a scrivere ancora... :nerd_face:

2 Likes

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.