How can I receive data from an HM10 on another Arduino?

1.3k views Asked by At

Link to set up of arduino's I am a beginner in things like the Arduino. I tried to set up a BLE HM10 connection between 2 Arduinos. I connected the BLE modules to my FTDI cable and set them as master and slave. They connect instantly when I power them up. I set the baud rate to 9600 (Default) and tried to send a "sensor value" from the slave to the master board where a servo should be moved.

I wrote my code down and tested it in many ways. The HM10 Rx/Tx pins in Arduino pin 0/1 and 7/8... nothing works for me. I wrote a statement that if the data of the slave reaches the master the serial terminal outputs nothing instead of "A".

I used an Arduino Uno as slave with this code:

#include <SoftwareSerial.h>

SoftwareSerial mySerial(7, 8); // RX, TX  
// Connect HM10      Arduino Uno
//     TXD          Pin 7
//     RXD          Pin 8

int reading = A0; // FSR attached to A0
int fsrreading;
int val;   

void setup() {
  Serial.begin(9600);
  mySerial.begin(9600);   //bluetooth serial begin
}

void loop() {
 int reading = analogRead(A0); //read fsr value
    Serial.print("Analog reading = ");
    Serial.println(reading);
    int val = map(reading, 0, 1023, 0, 180); // convert to servo value
    Serial.print("servo value = ");
    Serial.println(val);
    mySerial.write(val);//send fsr value to bt serial to the master
    delay(500); //Tweak this to lower value if communication is working
}

And this is the code for the master on an Arduino Leonardo:

#include <SoftwareSerial.h>
#include <Servo.h>

SoftwareSerial mySerial(7, 8); // RX, TX
// Connect HM10      Arduino Uno
//     TXD          Pin 7
//     RXD          Pin 8
int servoPin = 9; //attached to pin 9 (PWM)
Servo myservo;

int val;
int data;

void setup() {
  Serial.begin(9600);
  mySerial.begin(9600);//bluetooth serial begin
  myservo.attach(servoPin);//servo obj is attached to pin9
}

void loop()
{
    int avail = mySerial.available();//check is serial is available
        if (avail > 0)
        {
            for (int i = 0; i < avail; i++)
            {
                int data = mySerial.read();
                //no need to map or constrain
                // data is already between 0 an 180
                myservo.write(data);//move servo to pos
                Serial.println(data,DEC);
                delay(50);
            }
        }
}

If I open up the serial terminal I just get the A and don't get any action if I press on the "sensor" (FSR)... I get the FSR value in the serial but not in the serial of the master... I don't know what the hell I did wrong. Please guys, can you see through my spaghetti code and tell me my fault?

1

There are 1 answers

9
GrooverFromHolland On

There are some problems with your code.and maybe with the FSR analog input.

Connect one end of FSR to 5V, the other end to Analog 0. Then connect one end of a 10K resistor from Analog 0 to ground.

This way you will get a voltage between 0 volt(no pressure) and 4.9 volt(full pressure ~22lb) on the analog input.

This will result in a analog value between 0 and 1023.

mySerial.write(val) is sending an integer as byte, but a int value higher than 255 does not fit in byte, so garbage is send. map your analog value before sending. if you map it to value for your servo than no conversion is needed in the receiver. so:

 int reading = analogRead(A0); //read fsr value
    Serial.print("Analog reading = ");
    Serial.println(reading);
    int val = map(reading, 0, 1023, 0, 180); // convert to servo value
    Serial.print("servo value = ");
    Serial.println(val);
    mySerial.write(val);//send fsr value to bt serial to the master
    delay(500); //Tweak this to lower value if communication is working

this line:

int val = mySerial.println(fsrreading); //convert fsr value in clean int

is not doing what you think it is doing. It maybe returns fsrreading length, but probable garbage and is not needed, so remove it.

In my example lines I set the delay to 500. The buffer of the HM10 can handle 20 bytes, so set the delay as low as possible without overflowing the buffer.

In your receiving code you read only one byte. To have a smooth servo motion you must read all available and handle all bytes to set the servo. As you read all available there is no need to clean up Serial and checking available after reading or cleaning will always print A in your sketch.

instead check for data received and print that, so you know what you receive. Code:

void loop()
{
    int avail = mySerial.available();//check is serial is available
        if (avail > 0)
        {
            for (int i = 0; i < avail; i++)
            {
                int data = mySerial.read();
                //no need to map or constrain
                // data is already between 0 an 180
                myservo.write(data);//move servo to pos
                Serial.println(data,DEC);
                delay(50);
            }
        }
}