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.