Leonardo serial comunication - serial-port

I wrote this sample sketch to test serial communication with Arduino Leonardo (using Arduino IDE 1.0.5 on Windows 7):
int delayTime = 10000;
long lastExec = 0;
void setup()
{
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only
}
}
void loop()
{
long t = millis();
if (t - lastExec >= delayTime) {
if(Serial.available() > 0){
Serial.println('Hello world');
}
lastExec = t;
}
}
The serial port selected seems working because the sketch uploads correctly.
However I didn't get anything in the serial monitor window. Why?

You first need to send a character to the Arduino.
Because you have if(Serial.available() > 0), the Arduino won't Serial.println("Hello World"); unless there is something in the Serial buffer.
You may also want to reduce the delayTime which is very long.
In conclusion, you can try this by typing something in your Serial Monitor:
void setup()
{
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only
}
}
void loop()
{
if(Serial.available() > 0){
Serial.println('Hello world');
}
}
Hope it helps! :)

Does help to have "Hello world", not 'Hello world'.

Related

Could not find a valid MPU6050 sensor

I am using the below code with arduino-uno, but often getting "Could not find a valid MPU6050 sensor
#include <Wire.h>
#include <MPU6050.h>
MPU6050 mpu;
void setup() {
Serial.begin(115200);
Serial.println("Initialize MPU6050");
while (!mpu.begin()) {
Serial.println("Could not find a valid MPU6050 sensor, check wiring!");
delay(500);
}
}
void loop() {
}
My Arduino is working fine,
So, I checked MPU6050 using below code,
#include <Wire.h>
void setup()
{
Wire.begin();
Serial.begin(115200);
while (!Serial); // Leonardo: wait for serial monitor
Serial.println("\nI2C Scanner");
}
void loop()
{
byte error, address;
int nDevices;
Serial.println("Scanning...");
nDevices = 0;
for(address = 1; address < 127; address++ )
{
// The i2c_scanner uses the return value of
// the Write.endTransmisstion to see if
// a device did acknowledge to the address.
Wire.beginTransmission(address);
error = Wire.endTransmission();
if (error == 0)
{
Serial.print("I2C device found at address 0x");
if (address<16)
Serial.print("0");
Serial.print(address,HEX);
Serial.println(" !");
nDevices++;
}
else if (error==4)
{
Serial.print("Unknown error at address 0x");
if (address<16)
Serial.print("0");
Serial.println(address,HEX);
}
}
if (nDevices == 0)
Serial.println("No I2C devices found\n");
else
Serial.println("done\n");
delay(5000); // wait 5 seconds for next scan
}
Got Output as expected
Scanning...
I2C device found at address 0x68 !
done
From the above output I hope GPU6050 is working
How can I get values from GPU6050?
Your code is working as expected, it tells you in setup() when it can't connect to the device, until it can.
So when it stops printing the message, it is connected.
Now in loop() you should write your code to negotiate with the device.
Here's an excellent place to start with.

Xbee Serial.read is not clearing buffer

I wrote a example program to test Serial read from xbee. I was expecting a message passed from transmitter to receiver every 5 sec's but in serial monitor of receiver I am observing a continuous stream of repeat messages. Can anyone what I am missing. FYI: Also attached link to serial monitor screenshot.
[1]: https://i.stack.imgur.com/Lgxx5.png
/* ~ Simple Arduino - xBee Transmitter sketch ~ Router
*/
int count = 0;
void setup() {
Serial.begin(9600);
}
void loop() {
//Send the message:
count ++;
Serial.println(String("Hello World : " + String(count)));
delay(5000);
}
/* ~ Simple Arduino - xBee Receiver sketch ~ Coordinator
*/
void setup() {
Serial.begin(9600);
}
void loop() {
if (Serial.available() > 0){
Serial.write(Serial.read());
}
}
For the receiver, don't you just want to pass Serial.read() to print() instead of Serial.write()? If you have two serial ports, one to the console and one to the XBee, they should have different names.
Could you provide some more details on your serial connections? What are COM3 and COM6 attached to? Are you sharing serial port pins with the XBee and your console? it seems like that could be part of your problem, if either the Arduino or XBee can drive the RX pin of your receiver's serial port, you'd end up echoing your characters back to yourself.
Figured out a work around to address the issue. Here are the details:
https://i.stack.imgur.com/3qZMi.png
Circuit connections from Arduino to XBEE Shield:
D0/RX to TX
D1/TX to RX
5V to 5V
GND to GND
/* ~ Simple Arduino - xBee Transmitter sketch ~ Router
*/
void setup() {
Serial.begin(9600);
}
void loop() {
//Send the message:
Serial.print('<');
Serial.print("Hello World");
Serial.println('>');
delay(1000);
}
/* ~ Simple Arduino - xBee Receiver sketch ~ Coordinator
*/
bool started = false; //True: Message is strated
bool ended = false; //True: Message is finished
byte index; //Index of array
char character; //Variable to store the incoming byte
char msg[13]; //Message - array
void setup()
{
Serial.begin(9600);
}
void loop()
{
while (Serial.available())
{
character = Serial.read();
if (character == '<')
{
started = true;
index = 0;
msg[index] = '\0'; // Throw away any incomplete packet
}
//End the message when the '>' symbol is received
else if (character == '>')
{
ended = true;
break; // Done reading - exit from while loop!
}
//Read the message!
else
{
if (index < 11)
{ // Make sure there is room
msg[index] = character; // Add char to array
index++;
msg[index] = '\0'; // Add NULL to end
}
}
}
if (started && ended)
{
Serial.print("Message: ");
Serial.println(msg);
index = 0;
msg[index] = '\0';
started = false;
ended = false;
}
}

SIM7600CE - How to know when SIM card is registered into network by software

I am trying to set up the SIM7600CE to have it connect to the internet whenever I turn it on with Arduino Mega. I know how to turn it on with software by set the pin D12 to HIGH but I don't know how to read the signal of the NETLIGHT pin to get to know when the NETLIGHT starts to blink, which means the SIM card is registered into network successfully. Is there any way I can read that signal? Or is there any other way I can acknowledge when SIM card is registered into network successfully by software?
edit: I am trying to get the information that my SIM7600 is connected by using AT command. Even though I can send the AT command, I cannot parse the response. The result of the code below turns out that the Serial keeps printing the string "at+csq" constantly. Can anybody help?
#define mySerial Serial1
#define PWRKEY 12
void setup()
{
digitalWrite(PWRKEY, HIGH); //Press the boot button
Serial.begin(115200);
delay(500);
mySerial.begin(115200);
delay(5000);
while (1)
{
Serial.println("AT+CSQ"); //AT command for Signal quality test
updateSerial();
delay(1500);
}
}
void loop()
{
updateSerial();
}
void updateSerial()
{
delay(500);
while (Serial.available())
{
mySerial.write(Serial.read());
}
while (mySerial.available())
{
Serial.write(mySerial.read());
if (Serial.find("+CSQ: ")) //Find the AT+CSQ response
{
char c = mySerial.read();
if (c != '9') //check the first digit after "+CSQ: ", +CSQ: 99,99 means not detectable,
{
Serial.println("connected");
break;
}
}
}
Checkout the AT manual for the MODEM. Enable all URCs related to network registration, once registered on the network - you will receive a URC that will have information if your SIM was able to register on the network or not.
I have solved the problem with the same logic. However, I tried to just write and read in the SIM7600 serial, with some printing on the monitor to guide/debug. Plus, I used a connection flag as a condition to break the while loop when the MODEM is connected.
#define mySerial Serial1
#define PWRKEY 12
bool connectionFlag = 0; //will be set when connected
void setup()
{
digitalWrite(PWRKEY, HIGH); //Press the boot button
Serial.begin(115200);
delay(500);
mySerial.begin(115200);
delay(5000); //Give it a little time to initialize
while (1)
{
mySerial.println("AT+CSQ"); //AT command for Signal quality test
connectionCheck();
if (connectionFlag ==1) break;
delay(1500);
}
Serial.println("done");
mySerial.println("AT+CSQ"); //Get the CSQ response to confirm.
updateSerial();
}
void loop()
{
updateSerial();
}
void connectionCheck()
{
delay(500);
while (mySerial.available())
{
// Serial.write(mySerial.read());
if (mySerial.find("+CSQ: ")) //Find the AT+CSQ response
{
Serial.print("initializing\t");
char c = mySerial.read();
if (c != '9') //check the first digit after "+CSQ: ", +CSQ: 99,99 means not detectable,
{
Serial.println("connected");
connectionFlag = 1;
break;
}
}
}
}
void updateSerial()
{
delay(500);
if (mySerial.available())
{
Serial.write(mySerial.read());
}
if (Serial.available())
{
mySerial.write(Serial.read());
}
}

Arduino SIM900 GSM how to Join Char on String

I am currently making an Arduino project with GSM900 GSM GPRS. In this project, I have to receive data sent from a phone. I could easily receive data with a single character, but I can`t join does character to obtain a full word (String). I have to use this full word inside an If statement if this word equals to that other word (string), make something...
#include <SoftwareSerial.h>
// Configure software serial port
SoftwareSerial SIM900(7, 8);
//Variable to save incoming SMS characters
char incoming_char=0;
String newchar = "";
void setup() {
// Arduino communicates with SIM900 GSM shield at a baud rate of 19200
SIM900.begin(19200);
Serial.begin(19200);
// Give time to your GSM shield log on to network
delay(20000);
// AT command to set SIM900 to SMS mode
SIM900.print("AT+CMGF=1\r");
delay(100);
// Set module to send SMS data to serial out upon receipt
SIM900.print("AT+CNMI=2,2,0,0,0\r");
delay(100);
}
void loop() {
if(SIM900.available() >0) {
incoming_char=SIM900.read();
Serial.print(incoming_char);
}
}
I tried putting this command on the the if statement inside the loop, but after i tried comparing the words, it wouldnt work.
void loop() {
if(SIM900.available() >0) {
incoming_char=SIM900.read();
newString = incoming_char + "";
Serial.print(incoming_char);
}
if (newString == "Test"){
Serial.println("It worked");
}
}
The output that i get from the Monitor Serial is this:
+CMT: "+myNumber","","19/09/20,16:31:05-12"
Test
void loop() {
if (SIM900.available() >0) {
incoming_char=SIM900.read();
newString += incoming_char;
Serial.print(incoming_char);
}
if (newString.endsWith("Test")) {
Serial.println("It worked");
}
}
For does who are wondering how it finished:
Thanks to phoenixstudio...
void loop() {
if(SIM900.available() >0) {
incoming_char=SIM900.read();
newString += incoming_char;
Serial.print(incoming_char);
}
if (newString.endsWith("Test1")){
Serial.println("Worked1");
}
if (newString.endsWith("Test2")){
Serial.println("Worked2");
}
if (newString.endsWith("Test3"){
Serial.println("Worked3");
}
}

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.

Resources