Bt voltage meter

Hello, I am trying to adapt an excellent example that I found on the web, originally for arduino, but I want to adapt it to the photon, it is a Bluetooth voltage meter, which in turn controls 3 LEDs is a good example.

https://www.gadgetas.com/blog/tutorial-arduino-con-android-app-inventor-2/

For now I have tested with arduino and it works well, but I have not managed to make it work with Photon because I do not understand the use of the serial port with Photon, I adapted and added some codes commands like the AT commands, I leave the link of the original program and, in adition, i send my code; someone can help me solve it?

/*Arduino's Bluetooth with Android
 
 This program was created to control three leds connected to Arduino's output terminal
 and read the analog's input A0 to send the value to Android by HC-05 Bluetooth module.
 
 Created 8 Jun 2015
 by Augusto Valdez
 
 This example code is in the public domain.
 
 http://www.gadgetas.com/blog/tutorial-arduino-con-android-app-inventor-2/
 
 */
 
const int ledRed = D0;      // Terminal donde conectamos nuestro led Rojo
const int ledGreen = D1;    // Terminal donde conectamos nuestro led Verde
const int ledBlue = D2;       // Terminal donde conectamos nuestro led Azul
 
const char separadorDatos = '#'; // Carácter que indica que es un Dato lo que enviamos 
 
boolean statusLedRed = true;
boolean statusLedGreen = true;
boolean statusLedBlue = true;
 
void setup() {
  // Configuramos las terminales donde están conectados los leds como salidas
  pinMode(ledRed, OUTPUT);
  pinMode(ledGreen, OUTPUT);
  pinMode(ledBlue, OUTPUT);
 
  // Inicializamos los leds y deben de prenderse
  digitalWrite(ledRed, statusLedRed);
  digitalWrite(ledGreen, statusLedGreen);
  digitalWrite(ledBlue, statusLedBlue);
 
  // Inicializamos el puerto serial con una velocidad de 9600
Serial.begin(9600);
Serial1.begin(9600, SERIAL_8N1); // via TX/RX pins, 9600, 8bits, sin paridad, 1 bit de Stop
//Comandos AT
Serial1.print("AT+NAMELT-01"); //Modifique LT-01 segun como quiera llamarle
delay(1000);
Serial1.print("AT+PIN1234"); //Código de vinculación
delay(1000);
Serial1.print("AT+BAUD4"); //Velocidad de comunicación serial
delay(1000);

}
 
 
 
 
 
// Este metodo se ejecuta infinitas veces
void loop() {

  // Agregamos el caracter # para indicar que es un dato
  Serial1.print("#");
  
  // Leemos la entrada analógica A0 y enviamos el resultado por el puerto serial
  Serial1.println(analogRead(A0));
  Serial.write(analogRead(A0));
  // Esperamos un tiempo de 20 milisegundos
  delay(200);
}

/*La función SerialEvent se ejecuta cuando un nuevo dato
  llega al hardware por la terminal Rx*/
void serialEvent() {
  // Si esta disponible el puerto serial leemos los datos
  while (Serial.available()) {
    
    // Obtiene el siguiente byte que se recibió, este es un carácter
    char comando = (char)Serial.read();
    Serial1.write(comando);
    // Dependiendo del carácter recibido ejecutamos una acción
    switch (comando) {
      case 'r':
        cambiarLedRojo();
        break;
      case 'a':
        cambiarLedAzul();
        break;
      case 'v':
        cambiarLedVerde();
        break;
      default:
        // Si no es ningún carácter de comando regresamos el siguiente mensaje
         Serial.print(comando);
         Serial1.print(comando);
        Serial.println(": Comando no reconocido");
        break;
    }
  }
}
 
void cambiarLedRojo() {
  // Cambiamos el estado del LED
  statusLedRed = !statusLedRed;
  digitalWrite(ledRed, statusLedRed);
   Serial.println("Led Rojo cambio");
  Serial.println("Led Rojo cambio");
 }
 
void cambiarLedAzul() {
  // Cambiamos el estado del LED
  statusLedBlue = !statusLedBlue;
  digitalWrite(ledBlue, statusLedBlue);
  Serial1.println("Led Azul cambio");
  Serial.println("Led Azul cambio");
}
 
void cambiarLedVerde() {
  // Cambiamos el estado del LED
  statusLedGreen = !statusLedGreen;
  digitalWrite(ledGreen, statusLedGreen);
  Serial1.println("Led Verde cambio");
  Serial.println("Led Verde cambio");
}

The LEDs do not turn off, apparently, the Photon is not receiving the commands to turn them off, I do not know how to solve it, since I do not know how to use the ports, Serial and Serial1.

The RX/TX serial port on the Photon is accessed via Serial1 - Serial refers to the USB serial port.
Consequently the function to catch the data also needs to be serving that port
https://docs.particle.io/reference/device-os/firmware/photon/#serialevent-

To read the data from that port you use serialEvent1() instead of serialEvent() - and of course, in that function you also need to use Serial1.

ok I will make the necessary changes, and I will see if I can make it work, thank you so much

Thanks ScruffR, I already solved it

If you have found a solution, would you mind actually posting it for the benefit of others too?

2 Likes

Ok, ScruffR; it is simple, in arduino there is no problem if you do not call the routine of command checking received by the Serial port, but with the Photon what you should do is call that routine as you can see in the loop, later, you must print all in the serial port1.

void loop() {

// Agregamos el caracter # para indicar que es un dato

    Serial1.print("#");

// Leemos la entrada analógica A0 y enviamos el resultado por el puerto serial
enviar= map(analogRead(A0),0,4095,0,1023);
Serial1.println(enviar);
delay(200);
serialEvent();
}

besides that, I added a variable to be able to do a MAP, considering values ​​of 0-1023

/*Arduino's Bluetooth with Android
 
 This program was created to control three leds connected to Arduino's output terminal
 and read the analog's input A0 to send the value to Android by HC-05 Bluetooth module.
 
 Created 8 Jun 2015
 by Augusto Valdez
 
 This example code is in the public domain.
 
 http://www.gadgetas.com/blog/tutorial-arduino-con-android-app-inventor-2/
 
 */
 
const int ledRed = D0;      // Terminal donde conectamos nuestro led Rojo
const int ledGreen = D1;    // Terminal donde conectamos nuestro led Verde
const int ledBlue = D2;       // Terminal donde conectamos nuestro led Azul
int enviar= 0;
const char separadorDatos = '#'; // Carácter que indica que es un Dato lo que enviamos 
 
boolean statusLedRed = true;
boolean statusLedGreen = true;
boolean statusLedBlue = true;
 
void setup() {
  // Configuramos las terminales donde están conectados los leds como salidas
  pinMode(ledRed, OUTPUT);
  pinMode(ledGreen, OUTPUT);
  pinMode(ledBlue, OUTPUT);
 
  // Inicializamos los leds y deben de prenderse
  digitalWrite(ledRed, statusLedRed);
  digitalWrite(ledGreen, statusLedGreen);
  digitalWrite(ledBlue, statusLedBlue);
 
  // Inicializamos el puerto serial con una velocidad de 9600
Serial.begin(9600);
Serial1.begin(9600, SERIAL_8N1); // via TX/RX pins, 9600, 8bits, sin paridad, 1 bit de Stop
//Comandos AT
Serial1.print("AT+NAMELT-01"); //Modifique LT-01 segun dispositivo
delay(1000);
Serial1.print("AT+PIN1234"); //Código de vinculación
delay(1000);

}
 
 
 
// Este metodo se ejecuta infinitas veces
void loop() {

// Agregamos el caracter # para indicar que es un dato

        Serial1.print("#");
  
// Leemos la entrada analógica A0 y enviamos el resultado por el puerto serial
     enviar= map(analogRead(A0),0,4095,0,1023);
     Serial1.println(enviar);
     delay(200);
     serialEvent();
}

/*La función SerialEvent se ejecuta cuando un nuevo dato
  llega al hardware por la terminal Rx*/
void serialEvent() {
  // Si esta disponible el puerto serial leemos los datos
  while (Serial1.available()) {
    
    // Obtiene el siguiente byte que se recibió, este es un carácter
unsigned char comando = (char) Serial1.read();
             Serial.write(comando);
    // Dependiendo del carácter recibido ejecutamos una acción
    switch (comando) {
         case 'r':
             cambiarLedRojo();
         break;
         case 'a':
             cambiarLedAzul();
         break;
         case 'v':
             cambiarLedVerde();
         break;
         default:
// Si no es ningún carácter de comando regresamos el siguiente mensaje
             Serial1.println(": Comando no reconocido");
             
         break;
    }
  }
}
 
void cambiarLedRojo() {
  // Cambiamos el estado del LED
  statusLedRed = !statusLedRed;
  digitalWrite(ledRed, statusLedRed);
  Serial1.println("Led Rojo cambio");
 
 }
 
void cambiarLedAzul() {
  // Cambiamos el estado del LED
  statusLedBlue = !statusLedBlue;
  digitalWrite(ledBlue, statusLedBlue);
  Serial1.println("Led Azul cambio");
  
}
 
void cambiarLedVerde() {
  // Cambiamos el estado del LED
  statusLedGreen = !statusLedGreen;
  digitalWrite(ledGreen, statusLedGreen);
  Serial1.println("Led Verde cambio");
  
}

Of course now it allows me to generate actions on the LEDs

There is no need to call serialEvent() or serialEvent1() from loop(). That’s automatically called by the device OS between iterations of loop().

1 Like

I just removed that call, and it does not work, it’s weird but really if I add it works otherwise it does not work,

How is it when you name it void serialEvent1()?
Since you are working with Serial1 in that function that function needs to be called serialEvent1().
If there is no data received via Serial then the function serialEvent() will not be called but when there is data on Serial1 then serialEvent1() will be called.

That's why I said this

1 Like

Well, I'm just discovering some things and beforehand, I appreciate very much your explanation, I'm not a programmer and I'm very interested in learning and understanding, thank you newly

1 Like

i share the code

/*Arduino's Bluetooth with Android
 
 This program was created to control three leds connected to Arduino's output terminal
 and read the analog's input A0 to send the value to Android by HC-05 Bluetooth module.
 
 Created 8 Jun 2015
 by Augusto Valdez
 
 This example code is in the public domain.
 
 http://www.gadgetas.com/blog/tutorial-arduino-con-android-app-inventor-2/
 
 */
 
const int ledRed = D0;      // Terminal donde conectamos nuestro led Rojo
const int ledGreen = D1;    // Terminal donde conectamos nuestro led Verde
const int ledBlue = D2;       // Terminal donde conectamos nuestro led Azul
int enviar= 0;
const char separadorDatos = '#'; // Carácter que indica que es un Dato lo que enviamos 
 
boolean statusLedRed = true;
boolean statusLedGreen = true;
boolean statusLedBlue = true;
 
void setup() {
  // Configuramos las terminales donde están conectados los leds como salidas
  pinMode(ledRed, OUTPUT);
  pinMode(ledGreen, OUTPUT);
  pinMode(ledBlue, OUTPUT);
 
  // Inicializamos los leds y deben de prenderse
  digitalWrite(ledRed, statusLedRed);
  digitalWrite(ledGreen, statusLedGreen);
  digitalWrite(ledBlue, statusLedBlue);
 
  // Inicializamos el puerto serial con una velocidad de 9600
Serial.begin(9600);
Serial1.begin(9600, SERIAL_8N1); // via TX/RX pins, 9600, 8bits, sin paridad, 1 bit de Stop
//Comandos AT
Serial1.print("AT+NAMELT-01"); //Modifique LT-01 segun dispositivo
delay(1000);
Serial1.print("AT+PIN1234"); //Código de vinculación
delay(1000);

}
 
 
 
// Este metodo se ejecuta infinitas veces
void loop() {

// Agregamos el caracter # para indicar que es un dato

        Serial1.print("#");
  
// Leemos la entrada analógica A0 y enviamos el resultado por el puerto serial
     enviar= map(analogRead(A0),0,4095,0,1023);
     Serial1.println(enviar);
     delay(200);
     serial1Event();
}

/*La función SerialEvent se ejecuta cuando un nuevo dato
  llega al hardware por la terminal Rx*/
void serial1Event() {
  // Si esta disponible el puerto serial leemos los datos
  while (Serial1.available()) {
    
    // Obtiene el siguiente byte que se recibió, este es un carácter
unsigned char comando = (char) Serial1.read();
             Serial.write(comando);
             Serial1.write(comando);
    // Dependiendo del carácter recibido ejecutamos una acción
    switch (comando) {
         case 'r':
             cambiarLedRojo();
         break;
         case 'a':
             cambiarLedAzul();
         break;
         case 'v':
             cambiarLedVerde();
         break;
         default:
// Si no es ningún carácter de comando regresamos el siguiente mensaje
             Serial1.println(": Comando no reconocido");
             
         break;
    }
  }
}
 
void cambiarLedRojo() {
  // Cambiamos el estado del LED
  statusLedRed = !statusLedRed;
  digitalWrite(ledRed, statusLedRed);
  Serial1.println("Led Rojo cambio");
 
 }
 
void cambiarLedAzul() {
  // Cambiamos el estado del LED
  statusLedBlue = !statusLedBlue;
  digitalWrite(ledBlue, statusLedBlue);
  Serial1.println("Led Azul cambio");
  
}
 
void cambiarLedVerde() {
  // Cambiamos el estado del LED
  statusLedGreen = !statusLedGreen;
  digitalWrite(ledGreen, statusLedGreen);
  Serial1.println("Led Verde cambio");
  
}
´´´

I'm terribly sorry, but you got it wrong again.

Can you spot the difference

and

Or the official docs I already linked above
https://docs.particle.io/reference/device-os/firmware/photon/#serialevent-

You need to pay attention to the details.

Once you have the function name correct you should not need to call that function from loop()

1 Like

:rofl::joy::sweat_smile::joy:

/*Arduino's Bluetooth with Android
 
 This program was created to control three leds connected to Arduino's output terminal
 and read the analog's input A0 to send the value to Android by HC-05 Bluetooth module.
 
 Created 8 Jun 2015
 by Augusto Valdez, 
 now modified by Luis Torres for use in Photon

 This example code is in the public domain.
 
 http://www.gadgetas.com/blog/tutorial-arduino-con-android-app-inventor-2/
 
 */
 
const int ledRed = D0;      // Terminal donde conectamos nuestro led Rojo
const int ledGreen = D1;    // Terminal donde conectamos nuestro led Verde
const int ledBlue = D2;       // Terminal donde conectamos nuestro led Azul
int enviar= 0;
const char separadorDatos = '#'; // Carácter que indica que es un Dato lo que enviamos 
 
boolean statusLedRed = true;
boolean statusLedGreen = true;
boolean statusLedBlue = true;
 
void setup() {
  // Configuramos las terminales donde están conectados los leds como salidas
  pinMode(ledRed, OUTPUT);
  pinMode(ledGreen, OUTPUT);
  pinMode(ledBlue, OUTPUT);
 
  // Inicializamos los leds y deben de prenderse
  digitalWrite(ledRed, statusLedRed);
  digitalWrite(ledGreen, statusLedGreen);
  digitalWrite(ledBlue, statusLedBlue);
 
  // Inicializamos el puerto serial con una velocidad de 9600
Serial.begin(9600);
Serial1.begin(9600, SERIAL_8N1); // via TX/RX pins, 9600, 8bits, sin paridad, 1 bit de Stop
//Comandos AT
Serial1.print("AT+NAMELT-01"); //Modifique LT-01 segun dispositivo
delay(1000);
Serial1.print("AT+PIN1234"); //Código de vinculación
delay(1000);

}
 
 
 
// Este metodo se ejecuta infinitas veces
void loop() {

// Agregamos el caracter # para indicar que es un dato

        Serial1.print("#");
  
// Leemos la entrada analógica A0 y enviamos el resultado por el puerto serial
     enviar= map(analogRead(A0),0,4095,0,1023);
     Serial1.println(enviar);
     delay(200);
   
}

/*La función SerialEvent se ejecuta cuando un nuevo dato
  llega al hardware por la terminal Rx*/
void serialEvent1() {
  // Si esta disponible el puerto serial leemos los datos
  while (Serial1.available()) {
    
    // Obtiene el siguiente byte que se recibió, este es un carácter
unsigned char comando = (char) Serial1.read();
             Serial1.println(comando);
             
    // Dependiendo del carácter recibido ejecutamos una acción
    switch (comando) {
         case 'r':
             cambiarLedRojo();
         break;
         case 'a':
             cambiarLedAzul();
         break;
         case 'v':
             cambiarLedVerde();
         break;
         default:
// Si no es ningún carácter de comando regresamos el siguiente mensaje
             Serial1.println(": Comando no reconocido");
             
         break;
    }
  }
}
 
void cambiarLedRojo() {
  // Cambiamos el estado del LED
  statusLedRed = !statusLedRed;
  digitalWrite(ledRed, statusLedRed);
  Serial1.println("Led Rojo cambio");
 
 }
 
void cambiarLedAzul() {
  // Cambiamos el estado del LED
  statusLedBlue = !statusLedBlue;
  digitalWrite(ledBlue, statusLedBlue);
  Serial1.println("Led Azul cambio");
  
}
 
void cambiarLedVerde() {
  // Cambiamos el estado del LED
  statusLedGreen = !statusLedGreen;
  digitalWrite(ledGreen, statusLedGreen);
  Serial1.println("Led Verde cambio");
  
}

who does not make mistakes does not learn

4 Likes

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