HC-05 Putty Photon

Any idea what the reason is I’m getting different numbers rather than the letters I type into putty window?

The object is to type into putty and it be received by HC-05 and then send to photon and read as a string.

code is:

  	while (Serial1.available()) {
		textToDisplay = String(Serial1.read());
	}

Serial1.read() returns an int - 0255 and -1 (or 0xFFFFFFFF) when there is nothing to read.
If you want your program to interpret that as character, you should explicitly put that into a char or char[] variable.
If textToDisplay is a String you could also use textToDisplay += Serial1.read();, but it’s usually better to not use String on these microcontrolers.

1 Like

0,128,255, 248 and -1

Has been the numbers. Maybe it’s not configured correctly somehow and the code is just fine?

Additionally when I use the += without String wrap it outputs strange hieroglyphics heh.

It’s difficult to tell what’s wrong without seeing your enitre code :wink:

There are some simple tests you could perform to narrow down on the underlying cause.

  • a “loopback” code that immediately mirrors back any incoming byte
  • a “passthrough” code that directly forwards any byte that comes in via Serial1.read() to USB Serial.write()

Once you have established that either of these work as expected you can move on to collecting the incoming data for further processing on the device.

Ok I send to bluetooth HC-05 from Photon hardcoded text and I receive it every time a loop is ran in the PuTTY window. I see the text just fine in the window over and over. In the properties on pc this window is called the outgoing port for the HC-05.

  	while (Serial1.available()) {
		
		Serial.write(Serial1.println("Receive This Text Every Loop"));

	}

I am not sure how to do the reverse! Which is what I am aiming for. Enter text in window and receive it on photon and do what I want with it.

This works to increment number send and receive increment number. So I believe I am getting closer. Not sure how to input into window and send to Photon.

#include "Particle.h"

// Constants
const unsigned long SEND_INTERVAL_MS = 2000;
const size_t READ_BUF_SIZE = 64;

// Forward declarations
void processBuffer();

// Global variables
int counter = 0;
unsigned long lastSend = 0;

char readBuf[READ_BUF_SIZE];
size_t readBufOffset = 0;

void setup() {
    Serial.begin(9600);

    // Serial1 RX is connected to Arduino TX (1)
    // Serial2 TX is connected to Arduino RX (0)
    // Photon GND is connected to Arduino GND
    Serial1.begin(9600);
}

void loop() {
    if (millis() - lastSend >= SEND_INTERVAL_MS) {
        lastSend = millis();

        Serial1.printlnf("%d", ++counter);
        Serial.printlnf("Sent to Arduiuno: %d", counter);
    }

    // Read data from serial
    while(Serial1.available()) {
        if (readBufOffset < READ_BUF_SIZE) {
            char c = Serial1.read();
            if (c != '\n') {
                // Add character to buffer
                readBuf[readBufOffset++] = c;
            }
            else {
                // End of line character found, process line
                readBuf[readBufOffset] = 0;
                processBuffer();
                readBufOffset = 0;
            }
        }
        else {
            Serial.println("readBuf overflow, emptying buffer");
            readBufOffset = 0;
        }
    }

}

void processBuffer() {
    Serial.printlnf("Received from Arduino: %s", readBuf);
}

That code is found at https://docs.particle.io/tutorials/learn-more/about-serial/

Still looking for Send from putty window.

Not sure what you intend with this.
This will write the length of the string back to USB Serial and will also never leave the loop as it never reads from Serial1 and hence keeps Serial1.available() “true”.

Not sure what you mean with that.
What window?

And how does Arduino come into play here?
And Why Serial2 TX?
Where is the HC-05 in that?
What code is running on the Arduino?

Can you clarify what is connected with what exactly?

Is your PuTTY connected to the Photon’s USB Serial port or the Arduino?

That at least lets me know I am getting information sent from photon through Bluetooth.

Yeah that page that I got the code from I guess didn’t uncomment or change the comments from arduino to photon. Funny thing is that, that is a particle doc tutorial.

So by window I mean a serial window using Putty on PC.

If you want to full run down. I bought the HC-05 to send phone text with blynk app to photon and then to an addressable LED matrix for text scrolling. Apparently the HC-05 isn’t compatible with the iphone. So while I am waiting on a HM-10 BLE board to do this I am learning more about how to use the HC-05 board with the PC using PuTTy. The project that I am doing with the LED matrix works great using wifi. I am trying to build it for Bluetooth so I don’t have to have wifi only as an option. I know a lot of that isn’t important though right now. I am just testing as far as putty/pc, and HC-05, and photon.

So the HC-05 Bluetooth 2.0 is connected wireless not a USB cable for serial data and is the transmitter and receiver between PC and Photon in this case.

I still haven’t got the full picture there.
But when I take your description and fill the gaps as they would make sense to me, I’d imagine something like this

  • Your PC has an inbuilt or connected BT transceiver.
  • This transceiver is paired to the HC-05’s SPP.
  • With PuTTY (running on that PC) you are talking via that transceiver to the HC-05.
  • The HC-05 is connected to the RX/TX pins (Serial1) on the Photon.
  • The Photon is connected via USB to your PC again.
  • You have another PuTTY session that hooks up to the Photon’s USB Serial.

If the latter two are not true, I’d suggest you make them true - makes testing much easier :wink:

With that setup I’d run this simple pass-through program

void setup() {
  Serial.begin(9600);
  Serial1.begin(9600);
}

void loop() {
  while(Serial.available())       // any data on USB Serial
    Serial1.write(Serial.read()); // read it from USB Serial and pass it to Serial1 (HC-05)

  while(Serial1.available())      // any data on Serial1 (HC-05)
    Serial.write(Serial1.read()); // read it from Serial1 and pass it to USB Serial
}

This way, whatever you enter in one PuTTY session should be passed through to the other and appear in that window.

Yeah that is exactly it but the latter two are not true. The HC-05 is paired with my PC so no need for the latter two to be connected with USB cables… The HC-05 is my incoming and outgoing connections. Both the PC and the HC-05 are transceivers. I would like you to imagine the HC-05 is my cable :slight_smile: By the test I’ve done, it’s working correctly it seems but …

I ran your passthrough program. Actually the PuTTY window doesn’t let me type anything into it. So either certain code enables this function or I may need to change the settings in the PuTTY. Keep in mind I’ve been able to send data from photon to pc using HC-05. I was also able to send a number from photon. Read that number and add to that number with a pass through type of method like the one you wrote…

I need to test sending to photon with HC-05. I’d like to use PuTTy. Later I will use a phone with a different bluetooth board. Which is another thing to tackle.

Earlier I was able to type into the Putty window back when it was just displaying numbers even though that wasn’t what I wanted. I will try to recreate that again… I am not sure why I was seeing it before and not now. The only thing that’s changed is the code…

Additionally Seeing readouts on the PuTTY window both ways when I did the passthough that incremented numbers and the print text every loop was showing it has good connections to HC-05…

Your passthrough code I tried with USB directly. I can type now. but when I hit enter nothing happens anywhere that I can see…

That might only be a misconception. PuTTY by default doesn’t mirror what you type but only sends it to the receiver.
You can activte local mirroring tho’

Since you want to test what’s wrong it would be foolish to rob yourself of that testing tool - even if your final use-case won’t need it as long you haven’t got to the bottom of your issue it will proof helpful.

I’m thinking about finding another serial interface program. But what I see may be the data received being displayed before typing each character as I go. May explain why some characters look like jibberish. Some post suggest a few resistors or transistors to clear up jibberish by putting it in line with tx and rx. Serial data is a new one for me.

How long are your leads between Photon and HC-05?

Have you found the setting to activate local echo in PuTTY?
If not, you find it in the Terminal settings

To test PuTTY you can use a simple loop-back to test that without the “unknown variable” of HC-05.

void setup() {
  Serial.begin(9600);
 }

void loop() {
  while(Serial.available())       // any data on USB Serial
    Serial.write(Serial.read());  // read it from USB Serial and loop back
}

With that and local echo enabled you should see each character you type twice (outgoing and incoming).

Yes, That returns the expected result of seeing twice. Type something in and hit enter and see it return back on next line.

And returning to your other code with serial1 and talking to HC-05 and this time also plugging in direct connection with USB…

void setup() {
  Serial.begin(9600);
  Serial1.begin(9600);
}

void loop() {
  while(Serial.available())       // any data on USB Serial
    Serial1.write(Serial.read()); // read it from USB Serial and pass it to Serial1 (HC-05)

  while(Serial1.available())      // any data on Serial1 (HC-05)
    Serial.write(Serial1.read()); // read it from Serial1 and pass it to USB Serial
}

I can see what I type after hitting enter on the USB serial port. I have three windows up COM4, COM5, and COM6…

No jibberish now. Lets see if this has the expected results you would be expecting…


COM4 (Incoming HC-05) “I can type and hit enter but does not show up anything on COM4, COM5, and COM6”


COM5 (Outgoing HC-05) “Reads what I type from COM6 after hitting enter but does not show return back up or as an echo on COM5 or COM4”


COM6 (USB PC to USB Photon) “Reads what I type from COM5 after hitting enter but does not show return back up or as an echo on COM4 or COM6”

COM4 is not required COM5 is bidirectional.

In Windows BT terms the outgoing and incoming port are not the denoting the direction of communication but how the communication is initiated.
Outgoing means the communication is established by the PC while incoming means that the PC is waiting for a device to actively connect to it.
So you can forget about COM4 (there is obviously no device connecting to) and only work with your COM5 on the HC-05.

Thanks for all of your help!

How do I properly translate serial data to be a string or char whichever is better. I am just trying to compare. Also in my script I know using delay isn’t best, I’d rather it wait to reply when I send it after hitting enter. But for now if I don’t put delay in there it will give me a huge amount of “is not a command” statements.

This returns numbers rather than what I type. So I figured I may need to use char.

#include "Particle.h"

void setup() {
  Serial.begin(9600);
  Serial1.begin(9600);
}


void loop() {
  while(Serial.available())       // any data in PuTTY
   Serial1.write(Serial.read()); // read it from PuTTY and pass it to Serial1  (HC-05) 


 while(Serial1.available()) {
    String thisCommand = String(Serial1.read()); // read it from Serial1 (HC-05) and pass it to a string variable on Photon
   
     // Make commands to decide on to turn on the on board led ON/OFF based on variable

    if(thisCommand  == "PIND7H")//If the command is HIGH
 
    {
        digitalWrite(D7, HIGH);//Write HIGH
        Serial1.write("Turned LED ON");//Report back to Serial
        
    } else if(thisCommand == "PIND7L")//If the command is LOW
        
    {
        digitalWrite(D7, LOW);
        Serial1.write("Turned LED OFF");//Report back to Serial
    } else {
        Serial1.write("'" + thisCommand + "'" + " is not a command ");//Report back to Serial
        delay(10000);//Wait 10 seconds to new or old command and report back to Serial
    }
}
    
}

You are currently only ever reading one single byte into your string and the overwrite it the next time. You’d at least need to add new characters to the end of your string till you are not getting any more. There is also little point in constantly checking the string on each iteration of while().
After having dealt with it reset thisCommand.

However, if your commands do feature a “stop character” then I’d use Serial1.readBytesUntil(.) (or Serial1.readStringUntil()).
If it doesn’t you can use Serial1.readBytes() (or Serial1.readString()) and just wait for a timeout to occure.