Reading Data from Bluetooth using HC-05 Serial - arduino

I am having trouble reading data from the bluetooth serial interface on the Arduino.
Here my code:
#include <SoftwareSerial.h>
SoftwareSerial mySerial(8, 7); // RX, TX
void setup() {
mySerial.begin(38400);
Serial.begin(38400);
}
String Data = "";
void loop(){
//mySerial.println("Bluetooth Out");
while(mySerial.available()){
char character = mySerial.read();
Data.concat(character);
if (character == '\n'){
Serial.print("Received: ");
Serial.println(Data);
Data = "";
}
}
}
This should take whatever I enter into the bluetooth serial and print it on the regular serial port. Here is what I've tried:
If I uncomment mySerial.println("Bluetooth Out"); then I see the message bring printed out to the bluetooth terminal.
I tried similar to above to print to normal serial out and I see it being printed.
I've tried multiple ways (from some tutorials online) to decode the string and the data coming from mySerial, but nothing is happening.
I am using arduino Serial Monitor to check the ports, however I also tried to use the bluetooth terminal on my laptop and same behavior.
So I guess, what is the proper way for me to read data from the bluetooth serial port?

Related

How can I connect an ESP32 to an A6 GSM module?

I'm really in need of help with this problem and as most commonly advised, I have researched and researched and researched for days and I can't figure out what's wrong...
With that being said, I'm working on a project that uses an ESP-32S (pinout) and an A6 GSM module. (pinout) I'm attempting to get them connected to have the A6 send data to ThingSpeak... My problem is that I can't get it to work... I have the exact same configuration between the two using an ESP8266 and it connects and works but with the ESP32S it just doesn't seem to work...
Below is the code I have used to connect the 32 to the A6 and everything says success and pass but it won't change any ThingSpeak values using any methods...
#define TINY_GSM_MODEM_A6
#include "TinyGsmClient.h"
#include "ThingSpeak.h"
#define SerialMon Serial
HardwareSerial SerialAT(1);
bool modemConeted;
// Your GPRS credentials
// Leave empty, if missing user or pass
const char apn[] = "wireless.twilio.com";
const char user[] = "";
const char pass[] = "";
bool dataSent = false;
TinyGsm modem(SerialAT);
TinyGsmClient client(modem);
void setup(){
SerialMon.begin(9600);
delay(1000);
gsmModSetup();
}
void gsmModSetup() {
// Set GSM module baud rate
SerialAT.begin(9600, SERIAL_8N1, 27, 26, false); //27 and 26 are the pins on the ESP32S connected to the U_Rxd/U_Txd pins of A6
delay(3000);
SerialMon.println("Initializing modem...");
modem.init();
delay(3000);
String modemInfo = modem.getModemInfo();
SerialMon.print("Modem: ");
SerialMon.println(modemInfo);
SerialMon.print("Waiting for network...");
if (!modem.waitForNetwork()) {
SerialMon.println(" fail");
delay(10000);
return;
}
SerialMon.println(" OK");
SerialMon.print("Connecting to ");
SerialMon.print(apn);
if (!modem.gprsConnect(apn, user, pass)) {
SerialMon.println(" fail");
delay(5000);
return;
}
SerialMon.println(" OK");
}
void loop(){
}
I'm using a Twilo Sim card that's loaded with data (as said above it works with esp8266 so not the SIM and not the board) The only part of this code that doesn't pass is the part that says GPRS connect. it never says pass or fail. It just sits for like five minutes then continues.
After this, I try and post to ThingSpeak and it gives ThingSpeak ERROR-307 which means failed to connect to ThingSpeak...
I have tried 2 different ways
The what I find the easy and normal way
ThingSpeak.setField(1, temp);
ThingSpeak.setField(2, h);
ThingSpeak.setField(3, p);
ThingSpeak.setField(4, pt);
ThingSpeak.writeFields(myChannelNumber, myWriteAPIKey);
and the more complicated way of connecting to the server then sends a GET command with all the values put together with strings.
if (client.connect(thingSpeakAddress, 80))
{
client.print("POST /update HTTP/1.1\n");
client.print("Host: api.thingspeak.com\n");
client.print("Connection: close\n");
client.print("X-THINGSPEAKAPIKEY: " + WriteAPIKey + "\n");
client.print("Content-Type: application/x-www-form-urlencoded\n");
client.print("Content-Length: ");
client.print(thingSpeakData.length());
client.print("\n\n");
client.print(thingSpeakData);
}
client.stop();
Yet neither work and I don't know what to do... My A6 is connected to the 32 using GPIO26 & 27 they're connected to the A6 U_Rxd/U_Txd pins. All boards share common ground as they should.
Any more guides to read or code to change or libraries to try would be extremely helpful as my days of digging has yielded nothing but frustration as one attempt after another fails. the readers of this really are my last hope in solving this problem that's hindering my farther progression.
(Any questions about anything on my end, please ask. -Thank you)
The enclose documentation shows ESP32S pins for HW Serial 0 RX/TX on pin GIPO01/GIPO03, HW Serial 1 RX/TX on pin GIPO09/GIPO10 Given you have attached no oter HW at the moment, why not try with the basic config with serial1Read about this: https://github.com/G6EJD/ESP32-Using-Hardware-Serial-Ports and use the latest core ESP32 package (as of today 23.3.2020 is 1.04, previous releases had issues with serial. Do notforget to setup the serial a given in the article its different to esp8266 (!) so much about code compability. If you use the code on both platforms, you have to work with
#ifdef ESP32
//configure serial for esp32
#elif ESP8266
//configure serial for esp8266
#else
#error "Hardware not supported"
#endif
Here a small test routine for just the serial communication. If you type something in the input line of serial monitor it should be mirrored in the output window
#include <HardwareSerial.h>
void setup() {
Serial.begin(9600);
// set the data rate for the HardwareSerial port
Serial2.begin(9600);
}
void loop() {
if (Serial2.available()) {
Serial.write(Serial2.read());
}
if (Serial.available()) {
Serial2.write(Serial.read());
}
}
You can then expand the serial2 to your settings. If it fails it may be dueto: U1UXD is unused and can be used for your projects. Some boards use this port for SPI Flash access though - so try Serial3.

use another serial to send data from arduino to processing

i need to use another serial to send data from arduino teensy to processing because default serial (Serial.begin(9600)) already used for big program
i try to read some reference about how maybe i can change from which serial i want to receive (https://processing.org/reference/libraries/serial/Serial.html), but i dont think it can be change
void setup() {
Serial.begin(115200); // already used
Serial2.begin(9600); // processing
}
void loop() {
Serial.println("...") //big code that i am not allow to change
Serial2.println("hello world");
delay(1000);
}
i expected to get "hello world" in my processing repeatly, but i really dont have any idea how to write the code so i can get value from Serial2 instead from Serial
It depends on what Teensy module you are using and how you're doing the wiring.
Please see the Teensy Using the Hardware Serial Ports article for more details.
If possible I'd try their UART/USB example:
// set this to the hardware serial port you wish to use
#define HWSERIAL Serial1
void setup() {
Serial.begin(9600);
HWSERIAL.begin(9600);
}
void loop() {
int incomingByte;
if (Serial.available() > 0) {
incomingByte = Serial.read();
Serial.print("USB received: ");
Serial.println(incomingByte, DEC);
HWSERIAL.print("USB received:");
HWSERIAL.println(incomingByte, DEC);
}
if (HWSERIAL.available() > 0) {
incomingByte = HWSERIAL.read();
Serial.print("UART received: ");
Serial.println(incomingByte, DEC);
HWSERIAL.print("UART received:");
HWSERIAL.println(incomingByte, DEC);
}
}
If that hows with the same USB connection at the same time, negotiate with your colleague so you get to use the easier one that simply shows up as another Serial port in Processing.
If that is not an option:
double check the pinout the Serial Ports article above but also the logic level voltage (e.g. might be 3.3V, not 5V)
get a USB Serial converter (for the right logic level) - this will show up as a different Serial port using Processing's Serial.list()
connect Serial2's TX pin to the convertor's RX pin and read the data in Processing (similar to process to how you'd read Serial, just a different port name)

Unable to do Arduino Mega to Arduino Mega serial communication

Based on the circuit below, I tried hooking up two Arduino Mega for serial communication.
The code for sender:
char mystr[3] = "Hello"; //String data
void setup() {
// Begin the Serial at 9600 Baud
Serial.begin(9600);
}
void loop() {
Serial.write(mystr, 5); //Write the serial data
delay(1000);
}
The code for receiver:
char mystr[5]; //Initialized variable to store received data
void setup() {
// Begin the Serial at 9600 Baud
Serial.begin(9600);
}
void loop() {
Serial.readBytes(mystr, 5); //Read the serial data and store in var
delay(1000);
}
There is no output in the Serial console of Arduino. Could someone please inform me of the possible cause and solution for the same. If I've missed out anything, over- or under-emphasized a specific point let me know in the comments.
If I understood this right you have one Arduino connected to your pc and to another Arduino?
The problem is that you need to specify which Serial port to use:
That is rather easy, just type Serial1 or Serial2 instead of just Serial. That allows you to open 2 Serial ports: One to your other Arduino and one to your Computer for Displaying the results !
LINK: https://www.arduino.cc/en/Tutorial/MultiSerialMega
You need to check available data from serial:
void loop() {
if (Serial.available() > 0) {
// read the incoming byte:
Serial.readBytes(mystr, 5);
Serial.print("I received: ");
Serial.println(mystr, DEC);
}
}

PC to Arduino RS-485 connection via converters

I'm trying to make modbus RS-485 connection between PC (Windows) and Arduino.
On the PC side I use USB-to-RS485 converter like this one http://ru.aliexpress.com/item/USB-to-485-RS485-converter-Adapter-Support-Windows-7-8-for-Arduino/1577970568.html
On the Arduino side I use TTL-to-RS-485 like this one http://ru.aliexpress.com/item/5pcs-lot-MAX485-Module-RS-485-TTL-to-RS485-Module-for-Arduino/32262338107.html
Problem1:
When I send byte from PC to Arduino. Nothing happens. Arduino does not receive anything. In this case i upload to Arduino this code:
#include <SoftwareSerial.h>
#include "RS485_protocol.h"
SoftwareSerial rs485 (10, 11); // receive pin, transmit pin
const byte ENABLE_PIN = 3;
void setup()
{
Serial.begin(9600);
rs485.begin (28800);
pinMode (ENABLE_PIN, OUTPUT); // driver output enable
}
void loop()
{
byte buf [1];
byte received = recvMsg (fAvailable, fRead, buf, sizeof (buf));
if (received)
{
Serial.println(buf[0]);
} else {
Serial.println("--");
}
} // end of loop
int fAvailable ()
{
return rs485.available ();
}
int fRead ()
{
return rs485.read ();
}
And open Serial Monitor in Arduino IDE to show received data.
Then I open new instance of Arduino IDE, chose proper USB-to-RS485 COM port and opens it Serial Monitor to send some data.
So in Arduino side Serial Monitor I see only "--" lines. Even when I'm trying to send something in PC side Serial Monitor.
The other problem is:
Vice versa. When i sending some byte from Arduino to PC I receive some odd symbols instead sent byte.
The Arduino code in this case is:
#include <SoftwareSerial.h>
#include "RS485_protocol.h"
SoftwareSerial rs485 (10, 11); // receive pin, transmit pin
const byte ENABLE_PIN = 3;
void setup()
{
rs485.begin (28800);
pinMode (ENABLE_PIN, OUTPUT); // driver output enable
}
void loop()
{
byte msg[1] = {3};
sendMsg(msg);
} // end of loop
void sendMsg(byte msg[])
{
delay (1); // give the master a moment to prepare to receive
digitalWrite (ENABLE_PIN, HIGH); // enable sending
sendMsg (fWrite, msg, 1);
digitalWrite (ENABLE_PIN, LOW); // disable sending
}
void fWrite (const byte what)
{
rs485.write (what);
}
int fAvailable ()
{
return rs485.available ();
}
On the PC side (in the Arduino IDEs Serial Monitor) I receive odd symbols instead "3" symbol.
=======
RS485 converters wired with twisted pare A to A and B to B.
RS485 to TTL converter connected to Arduino properly. (Communication between two arduinos works fine).
Please help.
Example of RS485 :
Using a SN75176 IC.
RE (pin 2) connect to ground (for always reading )
Arduino control only DE PIN for writing data
But PC side problems:
Where bridged DE pins ? (CTS,DTR,RTD if supported)
What is your flow control ? (related #1)
Did you connect any resistor to A, B pin ?<+5V>----[560R]-----(A)----[120R]-----(B)------[560R]------<-5V> So mean line end
Did you filter DE Pin 2 signal (for noise)
A tricks : Use SN75176 on arduino side because this IC a RS232(UART) to (RS485) converter.
Edit : Modbus got 2 type on serial (RTU, ASCII). Dont care RS232/485 differences because different signal, same protocol.
Example packet type:
ASCII : :010300200004+CRC+FE(crc =packet check code, fe=end delimeter)
RTU : \x01\x03\x00\x20\x00\x04\x +CRC
On rtu : every byte need 1.5 char space and without start and end delimeter.
And Modbus node type meaning master and slave.
I hope helpfull.

Communicating serially through arduino xbeeshield

I have a ladyada xbee adapter on the computer side and an arduino xbeeshield which I am trying to communicate with over wireless. Both xbees are configured correctly in that I can receive data from the xbeeshield to the computer. However it doesn't work the other way i.e. xbeeshield does not echo a byte sent from the computer serially. Any idea what I might be doing wrong? (Note: When I connect the arduino board to the computer using USB cable, the echo program works just fine. It seems to be a problem in wireless mode only)
processing code
void setup() {
Serial.begin(9600);
}
void loop() {
if (Serial.available()) {
Serial.print((char) Serial.read());
delay(10);
}
}
I am just sending keystrokes from computer and waiting for a reply. I am not getting any.
I use the code I answered to the following question in regards to sending serial bytes from PC to Xbee/Arduino. It's been working fine for months. Ensure you've configured both your Xbee modules on the PC and Arduino side. Ensure your PAN ID's are the same as well.
Arduino making decision according to a packet received from serial port
What version of the Xbee modules are you using? My code works with Series 1 but should work with newer versions as well.
Try using softwareSerial library and connecting Tx and Rx to pin 4 and 2. Run the following sketch and tell me what happens. Change the Baudrate value to match your own
#include <SoftwareSerial.h>
uint8_t pinRx = 2 , pinTx = 4; // the pin on Arduino
long BaudRate = 57600; // Please set your Baudrate. It should match the one in XC-TU
char GotChar, getData;
// Xbee SoftwareSerial initialization
SoftwareSerial xbee(pinRx, pinTx); // RX, TX
void setup()
{
Serial.begin(9600);
Serial.println( "Welcome to the XBee Communication Test" );
Serial.print("BaudRate:");
Serial.println(BaudRate);
Serial.print(" Rx Pin#");
Serial.println(pinRx,DEC);
Serial.print(" Tx Pin#");
Serial.println(pinTx,DEC);
// set the data rate for the SoftwareSerial port
xbee.begin( BaudRate );
xbee.println("Setup Completed!");
}
void loop()
{
if (Serial.available())
{
GotChar = Serial.read();
xbee.print(GotChar);
Serial.print(GotChar);
}
while (xbee.available()>0)
{
Serial.println("Ohohoh");
getData = xbee.read();
Serial.print(" Received: );
Serial.print(getData);
Serial.println();
if(getData == 'a')
{
Serial.println(" sbam");
}
else if(getData == 'b')
{
Serial.println(" sbo");
}
}
}
Upload the program and open the serial monitor. Do you get the 'Setup completed' message on the computer? What happens if you send 'a' or 'b' from the Pc to the Arduino?

Resources