Use Serial in Arduino as variable - arduino

I would like to choose which Serial should be used on my Mega (e.g. Serial for development & testing, and then Serial3 for communication with another board). I came with this code:
HardwareSerial HWSerial = Serial; //or Serial1, Serial2, Serial3
void setup() {
HWSerial.begin(115200);
}
void loop() {
HWSerial.println("Hello world!");
delay(1000);
}
However, this produces only first two bytes and a NUL ("He␀").
What am I doing wrong?
Note: I know I could use
HardwareSerial* HWSerial = &Serial; //or Serial1, Serial2, Serial3
void setup() {
HWSerial->begin(115200);
}
void loop() {
HWSerial->println("Hello world!");
delay(1000);
}
which works. I was, however, in fact planning to use ArduinoJSON and the serializeJson(JSONObj, serial) function which won't accept a pointer, so I'm stuck there - and my approach compiles, but again, produces only first two bytes...

Related

How to make a response in Bluetooth module (Arduino)

I connected my Bluetooth module to the mobile phone and made up a code to communicate between Arduino and mobile through Bluetooth (send messages from Bluetooth module to device and vice versa).
Now I want to make a response, which means that if I send from the mobile "hi" the arduino replies and says "Hello" or whatever.
I have tried tons of codes but none worked, so would anyone please help me?
#include <SoftwareSerial.h>
SoftwareSerial myserial (6,5);
void setup() {
myserial.begin(9600);
Serial.begin (9600);
}
void loop() {
if (myserial.available()) {
Serial.write(+ myserial.read());
}
if (Serial.available()) {
myserial.write(Serial.read());
}
}
Another code but making a loop without sending anything
#include <SoftwareSerial.h>
SoftwareSerial myserial(6,5); //Arduino: R:5,T:6; bluetooth: T:5, R:6;
void setup() {
myserial.begin(9600);
Serial.begin (9600);
}
void loop() {
if (myserial.available()) {
Serial.write(myserial.read());
}
if (Serial.available()) {
myserial.write(Serial.read());
}
for (int i = 0; i=2; i++) {
myserial.write("hello");
}
if (myserial.read() =="n") {
myserial.write("hello");
}
}
You need to build your serial string char by char using the SerialEvent() interrupt, then do a String comparison using the .equals method. Despite many people will tell you that String types are 'evil' (see this and this) it might be a good solution for making things clearer (and perhaps a bit easier if you don't want to mess with C char strcmp() functions and pointers. In the end, the String type is there for you and I see no reason for not using it in general projects.
Based on the SerialEvent() documentation [1], and the String reference [2], you could do something like:
String inputString = ""; // a String to hold incoming data
bool stringComplete = false; // whether the string is complete
void setup() {
// initialize serial:
Serial.begin(9600);
// reserve 200 bytes for the inputString:
inputString.reserve(200);
}
void loop() {
// print the string when a newline arrives:
if (stringComplete) {
//Then you compare the inputString with the word you want to detect using the .equals method from the String class
if(inputString.equals("Hi"){
Serial.println("Hello");
}
// clear the string for a new comparison:
inputString = "";
stringComplete = false;
}
}
/*
SerialEvent occurs whenever a new data comes in the hardware serial RX. This
routine is run between each time loop() runs, so using delay inside loop can
delay response. Multiple bytes of data may be available.
*/
void serialEvent() {
while (Serial.available()) {
// get the new byte:
char inChar = (char)Serial.read();
// add it to the inputString:
inputString += inChar;
// if the incoming character is a newline, set a flag so the main loop can
// do something about it:
if (inChar == '\n') {
stringComplete = true;
}
}
}
EDIT 1: Of course, you can replace the Serial object from this example code with your own myserial object from the BlueTooth communication.

Lora E45-TTL-100 Basic Arduino setup

I used this code to send data from the Arduino board to LoRa E45-TTL. The board seems to transmit data, but the receiving node doesn't seem to receive data. I am a real beginner to LoRa technology and any help is highly appreciated. The sending and receiving node codes I used are below:
Sender Node
void setup() {
Serial.begin(9600);
}
void loop() {
Serial.println("000106,supun");
delay(2000);
}
Receiver Node
void setup() {
Serial.begin(9600);
}
void loop() {
if(Serial.available()) {
char x=Serial.read();
Serial.println(x);
delay(200);
}
}
This post mentions a similar problem, with a hackish solution to close Serial and re-open it.
void loop() {
Serial.print("Test");
Serial.end();
delay(30);
Serial.begin(9600);
delay(70); //The rest of requested delay. So 100 - 30 = 70
}

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);
}
}

Arduino Nano Gnss Software Serial

I want to connect Arduino nano and GNSS (SIMCom’s SIM33ELA standalone GNSS module).
First I wrote a program for rx/tx, which is worked well, but now I want to use Software Serial and I got something wrong data.
#include <SoftwareSerial.h>
char incomingByte; // for incoming serial data
double tbs;
SoftwareSerial mySerial(8, 9); // RX, TX
void setup() {
Serial.begin(115200);
while (!Serial) {
}
mySerial.begin(115200);
while (!mySerial) {
}
}
void loop() {
if (mySerial.available()) {
tbs = mySerial.read();
incomingByte = (char)tbs;
Serial.print(incomingByte);
}
/*if (Serial.available() > 0) {
incomingByte = Serial.read();
Serial.print(incomingByte);
}*/
}
Any Idea?
Pictures about results:
Wrong data with Software serial
Good data with Serial
Mostly, don't read one character into a double floating-point variable. Just do this:
void loop()
{
if (mySerial.available()) {
char c = mySerial.read();
Serial.write( c );
}
}
You should also use AltSoftSerial on those two pins. SoftwareSerial is very inefficient, because it disables interrupts for long periods of time. It cannot transmit and receive at the same time. In fact, the Arduino can do nothing else while a character is transmitted or received.
For a GPS library, you could try NeoGPS. It's the only Arduino library that can parse the sentences from the newest devices. It's also smaller, faster, more reliable and more accurate than all other libraries.

I2C onReceive-handler called only once

I'm having trouble communicating between Arduino's over I2C. For some reason, the onReceive handler is only called once.
Master Code (sender):
#include <Wire.h>
#include "i2csettings.h" // defines address
void setup()
{
Wire.begin(I2C_MASTER_ADDRESS);
}
void loop()
{
Wire.beginTransmission(I2C_SLAVE_ADDRESS);
Wire.write(0x11);
Wire.endTransmission();
delay(1000);
}
Slave Code (receiver):
#include <Wire.h>
#include "i2csettings.h"
void takeAction(int);
void setup()
{
Serial.begin(9600);
Wire.begin(I2C_SLAVE_ADDRESS);
Wire.onReceive(takeAction);
}
void loop()
{}
void takeAction(int nBytes)
{
Serial.println("Action!");
}
The idea in this test-setup is to have the sender send a byte every second, and let the receiver act on this by printing a message. However, the message is only printed once. When I reset the Slave, it's printed again, but just once.
Any ideas where this may come from?
You have to make sure you read all the bytes from the stream.
Other wise it seems to block.
Make your event handler look like this. So you can call it multiple times.
void takeAction(int nBytes)
{
Serial.println("Action!");
while(Wire.available())
{
Wire.read();
}
return;
}

Resources