Arduino not taking multiple commands - c++

I'm trying to get the basics done with my Arduino, and thus I'm starting off small.
That said, I want the Arduino to listen for simple, multiple commands being sent from my Raspberry Pi (I'm emulating this through the serial monitor now, however)
This is the code I'm working with:
#include "SoftwareSerial.h"
void setup()
{
Serial.begin(9600);
delay(100);
}
void loop() {
if (Serial.find("test1")) {
delay(100);
Serial.println("TEST1 command received");
}
if (Serial.find("test2")) {
delay(100);
Serial.println("TEST2 command received");
}
}
}
Sadly, only the test1 command triggers a serial print response, test2 no. Can anyone here help point me in the right direction?
Thank you!

From reading the documentation, I don't think you can use the find() function like that.
Consider what happens if test2 is input, when the find("test1") call is running. It will probably consume all characters up to and including the 2, and then return false, at which point those characters are lost.
I think you should design an actual protocol, with some delimiter between commands, and read/parse those.

Related

C++ Boost::asio serial communcation with Arduino can't write

I tried to communicate with my Arduino Uno using the Boost Asio library. Somehow I can't send data to my Arduino, and I have no idea what i'm doing wrong. Reading works fine, but writing only works when i open a terminal and say:
cat /dev/ttyACM0
When this terminal window is open, and I run my C++ application it works otherwise it doesn't work.
Code of the test application (C++):
#include <iostream>
#include <boost/asio.hpp>
char* message;
int main()
{
boost::asio::io_service ioservice;
boost::asio::serial_port serial(ioservice, "/dev/ttyACM0");
serial.set_option(boost::asio::serial_port_base::baud_rate(115200));
serial.set_option(boost::asio::serial_port::flow_control(boost::asio::serial_port::flow_control::none));
serial.set_option(boost::asio::serial_port::parity(boost::asio::serial_port::parity::none));
serial.set_option(boost::asio::serial_port::stop_bits(boost::asio::serial_port::stop_bits::one));
serial.set_option(boost::asio::serial_port::character_size(boost::asio::serial_port::character_size(8)));
std::string s = "u";
boost::asio::streambuf b;
std::ostream os(&b);
os << s;
boost::asio::write(serial, b.data());
if (serial.is_open()) {
serial.close();
}
return 0;
}
Code of my Arduino application:
#include "Servo.h"
Servo servo;
void setup() {
Serial.begin(115200);
servo.attach(9);
servo.write(0);
}
void loop() {
if(Serial.available()) {
char c = Serial.read();
if(c == 'u') {
servo.write(180);
} else if (c == 'v') {
servo.write(0);
}
}
}
I tried this both on my Ubuntu 18.04 and Debian 10 installation to rule out a permission issue, so I think there is something wrong with my code.
Update:
I found the issue, the Arduino is restarting when making a serial connection. When I add a thread sleep for for example 5 seconds and after that resent the data it works (because then it keeps the serial connection alive). I'm still looking for a permanent solution, so that I don't have to do a write before I really want to write something.
Update 2:
Apparently I don't even have to do a write, but where must be a small delay before I can start writing, because when after opening the port the Arduino is still restarting.
I fixed it with adding a small delay before writing to the serial port. As I also wrote in my comment above, the Arduino is restarting when you start a serial communication.
This can be disabled on several ways: https://playground.arduino.cc/Main/DisablingAutoResetOnSerialConnection/
Another option is to send a "ready" signal from the Arduino to know in your application that the Arduino is rebooted. So then start in your application with reading, and when you received that message, you can start writing.

My esp32 print weird characters but I do anything

I only want to print simple text but it print unilimited weird character and never stop, (when I remove Serial.print() it continues to print weird characters).
This is the weird text that loop
-*⸮ql⸮7⸮$⸮
*!8P⸮⸮⸮V⸮)3 ⸮;⸮ 1⸮zY⸮b⸮ڔ!⸮⸮$q⸮,*⸮ı⸮N
⸮a!u⸮ 1⸮zY⸮b⸮⸮⸮⸮⸮⸮!⸮ 1⸮zY⸮W(⸮⸮⸮xI⸮
,*-⸮l⸮N
⸮a!u⸮,*⸮⸮⸮nbb⸮H⸮⸮⸮⸮,*⸮⸮⸮nbb⸮H$⸮
1⸮zY⸮b⸮ڔ!X⸮⸮⸮⸮,*⸮⸮⸮nbb⸮H$⸮
*!8Pt⸮
My code :
void setup() {
}
void loop() {
}
I hope you can help me, thanks :)
UPDATE
I completely removed Serial.begin(9600) and Serial.print() and I still have the problem O_o
If you are using arduino IDE ( you probably are ) you can try changing the baud rates. That's what happened to me in arduino some time ago
I just changed the Flash memory speed to 40MHz instead of 80MHz
You need to make sure that the baud rate in the code and baud rate of the serial monitor are the same.
Example:
if your code has : Serial.begin(115200);
then change the baud rate of serial monitor to 115200, this can be done by accessing the baud as shown in the pictureimage below

How to communicate with sim800c using Raspberry pi 3 in c++

My end goal is to try and send some simple data that's stored on my Raspberry pi 3 to an external server/website using a sim800c from a c++ program. I believe the easiest way to do this is to issue "AT commands" to the sim/modem however I'm struggling to do this. I can't figure out how I'm actually suppose to issue AT commands or how I can check if they're working. For testing purposes I've written some code which should send a text message to a phone number once I've gotten this working it should be fairly straight forward to figure out the AT commands to communicate with a server.
Below is an image of how I've wired everything up which I'm fairly confident is correct.
https://i.imgur.com/zysmNXE.jpg
Below is the basic c++ code that I've written using various guides, it compiles and runs in terminal however I don't get any response from the AT commands and more importantly as far as I can tell their not actually being executed. I've tried changing the fake number to my personal one but it didn't make a difference.
#include <stdio.h>
#include <string.h>
#include <wiringPi.h>
#include <wiringSerial.h>
int main ()
{
int connection;
printf("Opening connection \n");
connection = serialOpen("/dev/ttyAMA0", 9600);
delay(1000);
printf("Connection: %d\n", connection);
printf("\n");
//Set gsm to text mode
serialPuts(connection,"AT+CMGF \r\n");
delay(1000);
//Number that the message should be sent to
serialPuts(connection,"AT+CMGS=\"12345678900\"\r\n");
delay(1000);
//The message
serialPuts(connection,"Hello World");
delay(1000);
//Print ctrl+x
serialPuts(connection,"\x1A");
delay(1000);
printf("Done \n");
return 0 ;
}
As far as I can tell there are 3 likely reasons why it's not working;
A possible config/settings issue with the pi
I've wired it up incorrectly
The sim/module isn't working
I'm just not sure how to go about testing/finding what is causing the issue. If I could get my code to output the response of the AT commands after doing "serialPuts" that might help me figure out whats going on but I haven't made much luck doing that. Alternatively If anyone has an idea why the commands don't appear to be working or can provide me some ways to debug/test them that would be great.
The issue turned out just to be a simple wiring mistake.
I had wired the txd pin of the sim module to the txd pin on the raspberry pie as I was under the impression you just needed to match the pins however I didn't think/realize that the txd pin needed to go to the rxd pin instead. Changing these around allowed me to see the commands & responses being returned from the module.
To actually get the gprs working I also needed to temporarily set the pwr pin to low then high.

python arduino usb communication

I'm trying to move data from Python to Arduino nano, but I can't see anything on the console on the Arduino side, but when
I tried to move data from Arduino to python, it worked very well.
Follow my codes below:
arduino code
void setup() {
Serial.begin(9600);
}
void loop() {
if(Serial.available() > 0) {
char data = Serial.read();
char str[2];
str[0] = data;
str[1] = '\0'
Serial.print(str);
}
}
python code
import serial, time
arduino = serial.Serial('COM3', 9600, timeout=.1)
time.sleep(1) # give the connection a second to settle
arduino.write("Hello from Python!")
while True:
data = arduino.readline()
if data:
print data.rstrip('\n') # strip out the new lines
The Arduino is using print, the Python is using readLINE.. Readline reads until it finds a '\n' the arduino is never sending a '\n'.'
Replace \0 in the sketch with \n and it should work.
It's always useful to test using the serial monitor and seeing what happens, if this doesn't work then please try sending some data using the serial monitor and let us know the results.
The problem may be that you have set too little time after connecting to the COM port.
try this
time.sleep(5) # give the connection 5 seconds to establish

C++ Serial Communication Issue

I am trying to make a Console C++ program that will be able to communicate through the serial port with my Arduino microcontroller, however I am having a problem with the ReadFile() function:
This is the ReadFile() function code from my C++ Console Program:
if(ReadFile(myPortHandle, &szBuf, 1, &dwIncommingReadSize, NULL) != 0)
{
cout<<"FOUND IT!"<<endl;
Sleep(100);
}
else
{
cout<<".";
Sleep(100);
}
The ReadFile function is consistently returning the "False" value, meaning it is not finding anything in the serial port. On the other side of the serial port, I have my Arduino Hooked up with the following code:
int switchPin = 4; // Switch connected to pin 4
void setup() {
pinMode(switchPin, INPUT); // Set pin 0 as an input
Serial.begin(9600); // Start serial communication at 9600 bps
}
void loop() {
if (digitalRead(switchPin) == HIGH) { // If switch is ON,
Serial.write(1); // send 1 to Processing
} else { // If the switch is not ON,
Serial.write(0); // send 0 to Processing
}
delay(100); // Wait 100 milliseconds
}
And every time I press a push button, I would send a "1" value to the serial port, and a "0" every time I don't press a push button. Basically, I got the Arduino code from a tutorial I watched on how to do serial communication with the program Processing (which worked perfectly), though I am unable to do the same with a simple Console Application I made with C++ because for some reason the ReadFile() function isn't finding any information in the serial port.
Anyone happen to know why?
P.S.: The complete code in the C++ Console Program can be found here:
https://stackoverflow.com/questions/27844956/c-console-program-serial-communication-arduino
The ReadFile function is consistently returning the "False" value, meaning it is not finding anything
No, that is not what it means. A FALSE return value indicates that it failed. That is never normal, you must implement error reporting code so you can diagnose the reason. And end the program since there is little reason to continue running. Unless you setup the serial port to intentionally fail by setting a read timeout.
Use GetLastError() to obtain the underlying Windows error code.
You look to use MS Windows so try to catch the arduino output using portmon first, then you can debug your C++ code.