Hi everyone,
I am trying to send and receive data using HM-10 Bluetooth modules. The following is the code that I use from a tutorial (HM-10 Bluetooth 4 BLE Modules | Martyn Currey). The first is the code for the peripheral module and the other is the central module. I have also manually connected the two modules via AT commands (also setting the central module to role1 and the other to role0) and both modules are showing a solid red light when powered. However, in the peripheral code, using the Serial.println(Serial.read()) line, it shows that the peripheral only receives -1. Anyone knows what could have went wrong?
#include <AltSoftSerial.h>
AltSoftSerial BTSerial;
char c=' ';
byte LEDpin = 13;
void setup()
{
Serial.begin(9600);
BTSerial.begin(9600);
pinMode(LEDpin, OUTPUT);
digitalWrite(LEDpin,HIGH);
}
void loop()
{
Serial.println(Serial.read());
if (BTSerial.available())
{
Serial.println("HERE");
c = BTSerial.read();
Serial.println(c);
if (c==49) { digitalWrite(LEDpin,HIGH); }
if (c==48) { digitalWrite(LEDpin,LOW); }
Serial.println(c);
}
}
#include <AltSoftSerial.h>
AltSoftSerial BTserial;
byte switchPin = 2;
boolean switch_State = LOW;
boolean oldswitch_State = LOW;
void setup()
{
Serial.begin(9600);
BTserial.begin(9600);
pinMode(switchPin, INPUT);
// connect to the remote Bluetooth module
BTserial.print("AT+IMME1" );
delay(1000);
BTserial.print("AT+ROLE1" );
delay(1000);
BTserial.print("AT+CON4C3FD302B298" );
delay(1000);
}
void loop()
{
// Very simple debouce.
boolean state1 = digitalRead(switchPin); delay(1);
boolean state2 = digitalRead(switchPin); delay(1);
boolean state3 = digitalRead(switchPin); delay(1);
if ((state1 == state2) && (state1==state3))
{
switch_State = state1;
if (switch_State != oldswitch_State)
{
if ( switch_State == HIGH) { BTserial.print("1" ); Serial.println("1"); Serial.write("1");}
else { BTserial.print("0" ); Serial.println("0"); Serial.write("0");}
oldswitch_State = switch_State;
}
}
}