Can not recieve a string from bluetooth module due to bad code

hey, I am complelty confuzzled, I am trying to recieve a bluetooth string from my iphone with hm10 module, connected to oled screen and nano arduino. I have the following code:


#include <SoftwareSerial.h>
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels

// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
// The pins for I2C are defined by the Wire-library. 
// On an arduino UNO:       A4(SDA), A5(SCL)
// On an arduino MEGA 2560: 20(SDA), 21(SCL)
// On an arduino LEONARDO:   2(SDA),  3(SCL), ...
#define OLED_RESET     -1 // Reset pin # (or -1 if sharing Arduino reset pin)
#define SCREEN_ADDRESS 0x3C ///< See datasheet for Address; 0x3D for 128x64, 0x3C for 128x32
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);

SoftwareSerial mySerial(2,3);
String readString = "";
char c;
void setup()
{
  if(!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) {
    Serial.println(F("SSD1306 allocation failed"));
    for(;;); // Don't proceed, loop forever
  }
mySerial.begin(9600);
Serial.begin(9600);
  display.clearDisplay();
  display.setTextSize(2);             // Normal 1:1 pixel scale
  display.setTextColor(SSD1306_WHITE);        // Draw white text
  display.setCursor(0,10);             // Start at top-left corner
  display.println(F("Power on"));
  display.display();
}
void loop()
{ 
  while (Serial.available() > 0) {
    Serial.println("serial on");
    delay(10);  //small delay to allow input buffer to fill
    c = Serial.read();  //gets one byte from serial buffer
    readString += c; 
  } //makes the string readString  
  
  if (readString.length() > 0) {
      display.clearDisplay();
      display.setTextColor(WHITE);
      display.setTextSize(2);
      display.setCursor(0, 10);
      // Display static text
      display.println(readString);
      display.display();
    readString=""; //clears variable for new input
  }
}

it is supposed to make the bluetooth characters into a string then display it on the oled screen, the display is not the problem only the gathering of the characters into a string which does not work, I do not know what is wrong with the code + the wiring is good(bluetooth to d2 and d3)

whats wrong with my code? why is it not recieiving the string properly??

whats wrong with my code? why is it not recieiving the string properly??

It's not clear how this is all wired together, but I think you want to read the output from the HM10 on software serial.

SoftwareSerial mySerial(2,3);

while (Serial.available() > 0) {
    Serial.println("serial on");
    delay(10);  //small delay to allow input buffer to fill
    c = Serial.read();  //gets one byte from serial buffer
    readString += c; 
  } //makes the string readString  

You serial commands look like those used for "classic" Bluetooth. This might not work, and using it with IOS might further reduce your chances. You might need a BLE serial library, of which I believe there are several.

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