Arduino accepting only alternate characters - arduino

I am writing an Arduino code to receive SMS from a SIM800 module and then send the same message back to the sender. My code is as follows:
#include<SoftwareSerial.h>
SoftwareSerial mySerial(9, 10);//Connect to pin 9&10 on GSM module
const byte numChars = 128; //character size
char receivedChars[numChars]; // Store data read from SMS
boolean newData = false; //flag to check if new data has arrived
void setup() {
Serial.begin(9600);
mySerial.begin(9600);
Serial.println("<Arduino is ready>");
mySerial.println("AT");
mySerial.println("AT+CMGF=1"); //text mode
mySerial.println("AT+CNMI=1,2,0,0,0");
mySerial.println("AT+CSQ"); //Check signal strength
while (Serial.available())
{
mySerial.write(Serial.read());//Forward what Serial received to Software Serial Port
}
while(mySerial.available())
{
Serial.write(mySerial.read());//Forward what Software Serial received to Serial Port
}
}
void loop() {
//code for checking sms
scanSMS();
//Print if a new data arrived
showNewData();
delay(25000);
}
void scanSMS() {
static boolean recvInProgress = false;
static byte ndx = 0;
char startMarker = '#'; //Start and end markers. Eg. SMS body will be sent as #status#
char endMarker = '#';
char rc;
while (Serial.available())
{
mySerial.write(Serial.read());//Forward what Serial received to Software Serial Port
}
while (mySerial.available() > 0 && newData == false) {
Serial.write(mySerial.read());//Forward what Software Serial received to Serial Port
rc = mySerial.read();
if (recvInProgress == true) {
if (rc != endMarker) {
receivedChars[ndx] = rc;
ndx++;
if (ndx >= numChars) {
ndx = numChars - 1;
}
}
else {
receivedChars[ndx] = '\0'; // terminate the string
recvInProgress = false;
ndx = 0;
newData = true;
}
}
else if (rc == startMarker) {
recvInProgress = true;
}
}
}
/*PRINT IF THERE IS A NEW DATA*/
void showNewData() {
if (newData == true) {
SendMessage(receivedChars);
newData = false;
}
}
/*SEND BACK WHATEVER SMS IS RECEIVED*/
void SendMessage(String receivedChars)
{
Serial.print("here");
mySerial.println("AT+CMGS=\"+xxxxxxxxxx\"\r"); // Replace x with mobile number
mySerial.println("Humidity Alert!");// The SMS text you want to send
mySerial.print(receivedChars);
mySerial.println((char)26);// ASCII code of CTRL+Z
}
The issue is that the module seems to receive only alternate characters instead of the whole message. For example, if I send the text #program#, the Arduino will only print porm or rga. Am I missing something here? Any help is highly appreciated.

You are calling mySerial.read() twice in the while loop.
while (mySerial.available() > 0 && newData == false) {
Serial.write(mySerial.read());//Forward what Software Serial received to Serial Port
rc = mySerial.read();
I think that is why you are not seeing the whole string. read reads the first byte in the buffer and advances.
You want to store a returned value and pass it to the write call like this.
while (mySerial.available() > 0 && newData == false) {
rc = mySerial.read();
Serial.write(rc);//Forward what Software Serial received to Serial Port
....
}

Related

Sending data over ESP_NOW

I'm a total noob and just starting out with PlatformIO and Arduino/ESP32. I also want to say thanks in advance to any help I can get.
Plan:
I have 2 ESP32's talking over ESP_NOW, I just can't verify the data being sent in order to progress with my project. Basically, I have a Nextion display that sends specific info to an ESP32 (tested and working) and that ESP32 is then to send that information via ESP_NOW to the other ESP32 which will translate it into serial data to send to an Arduino Due and perform some tasks.
Problem:
The issue I have is that when I test, I see the data I think I am transmitting, but when I try to Serial.print said info, I get "0 0 0 0". I'm not sure that I am sending OR receiving the data properly. All I know is that when I press the button on the Nextion, I get a response on the ESP32 that is not connected.
Code:
#include <Arduino.h>
#include <esp_now.h>
#include <WiFi.h>
// Example to receive data with a start marker and length byte
// For ease of testing this uses upper-case characters A and B
// for the request and response types
// and Z for the start marker
// this version saves all the bytes after the start marker
// if the TYPE byte is wrong it aborts
const byte numBytes = 32;
byte receivedBytes[numBytes];
const byte typeBytePosition = 6; // Position after the start byte
const byte requestType = 0x65;
const byte requestLength = 7;
boolean newData = false;
int LED = 21;
// REPLACE WITH THE MAC Address of your receiver
uint8_t broadcastAddress[] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
// Define variables to store Nextion readings to be sent
byte NexEventID;
byte NexPageID;
byte NexObjectID;
byte NexStatusID;
// Define variables to store incoming readings// Define variables to store incoming readings
byte incomingEventID;
byte incomingPageID;
byte incomingObjectID;
byte incomingStatusID;
// Variable to store if sending data was successful
String success;
typedef struct struct_message
{
byte EventID;
byte PageID;
byte ObjectID;
byte StatusID;
} struct_message;
// Create a struct_message called NextionInfo to hold Nextion settings
struct_message NextionInfo;
// Create a struct_message called IncomingInfo to hold incoming info
struct_message IncomingInfo;
// Callback when data is sent
void OnDataSent(const uint8_t *mac_addr, esp_now_send_status_t status)
{
Serial.print("\r\nLast Packet Send Status:\t");
Serial.println(status == ESP_NOW_SEND_SUCCESS ? "Delivery Success" : "Delivery Fail");
if (status == 0)
{
success = "Delivery Success :)";
}
else
{
success = "Delivery Fail :(";
}
}
// Callback when data is received
void OnDataRecv(const uint8_t *mac, const uint8_t *incomingData, int len)
{
struct_message *IncomingInfo = (struct_message *)incomingData;
Serial.println(WiFi.macAddress());
// memcpy(&IncomingInfo, incomingData, sizeof(IncomingInfo));
Serial.print("Bytes received: ");
Serial.println(len);
// incomingEventID = IncomingInfo.EventID;
Serial.print((byte)IncomingInfo->EventID, HEX);
Serial.print(' ');
// incomingPageID = IncomingInfo.PageID;
Serial.print((byte)IncomingInfo->PageID, HEX);
Serial.print(' ');
// incomingObjectID = IncomingInfo.ObjectID;
Serial.print((byte)IncomingInfo->ObjectID, HEX);
Serial.print(' ');
// incomingStatusID = IncomingInfo.StatusID;
Serial.println((byte)IncomingInfo->StatusID, HEX);
}
void emptyBuffer()
{
for (byte n = 0; n < numBytes; n++)
{
receivedBytes[n] = 0;
}
}
void recvBytesWithStartMarker()
{
static boolean recvInProgress = false;
static int ndx = -1;
static byte numBytesReceived = 0;
static byte mesgLen = 0;
const byte startMarker = 0x65;
byte rc;
while (Serial2.available() > 0 && newData == false)
{
rc = Serial2.read();
receivedBytes[numBytesReceived] = rc;
numBytesReceived++;
if (numBytesReceived > 33)
{
Serial.println("Error Rx : RESET !!");
Serial.println();
emptyBuffer();
numBytesReceived = 0;
newData = false;
}
if (recvInProgress == true)
{
if (numBytesReceived == typeBytePosition)
{
ndx = 0; // enable saving of data (anticipate good data)
if (rc == requestType)
{
mesgLen = requestLength;
}
else
{
recvInProgress = false; // abort - invalid request type
ndx = -1;
}
}
if (ndx >= 0)
{
ndx++;
}
if (numBytesReceived >= (mesgLen + typeBytePosition))
{ // got the whole message
recvInProgress = false;
newData = true;
}
}
else if (rc == startMarker)
{
emptyBuffer();
recvInProgress = true;
numBytesReceived = 0;
ndx = -1; // prevent counting valid bytes for the moment
}
}
}
// void getNexInfo()
// {
// NexEventID = receivedBytes[0];
// NexPageID = receivedBytes[1];
// NexObjectID = receivedBytes[2];
// NexStatusID = receivedBytes[3];
// // Serial.print(NexEventID&&NexPageID&&NexObjectID&&NexStatusID);
// }
void showNewData()
{
if (newData == true)
{
Serial.println(WiFi.macAddress());
Serial.print(requestType, HEX);
Serial.print(' ');
for (byte n = 0; n < typeBytePosition; n++) // n < numBytes
{
if (n == 0)
{
NexEventID = receivedBytes[n];
}
else if (n == 1)
{
NexPageID = receivedBytes[n];
}
else if (n == 2)
{
NexObjectID = receivedBytes[n];
}
else if (n == 3)
{
NexStatusID = receivedBytes[n];
}
Serial.print(receivedBytes[n], HEX);
Serial.print(' ');
}
//Serial.print(getNexInfo(),HEX);
Serial.println();
// Send message via ESP-NOW
esp_err_t result = esp_now_send(broadcastAddress, (uint8_t *)&NextionInfo, sizeof(NextionInfo));
if (result == ESP_OK)
{
Serial.println("Sent with success");
}
else
{
Serial.println("Error sending the data");
}
newData = false;
}
}
void sendNewData()
{
// Set values to send
NextionInfo.EventID = NexEventID;
NextionInfo.PageID = NexPageID;
NextionInfo.ObjectID = NexObjectID;
NextionInfo.StatusID = NexStatusID;
// Send message via ESP-NOW
esp_err_t result = esp_now_send(broadcastAddress, (uint8_t *)&NextionInfo, sizeof(NextionInfo));
if (result == ESP_OK)
{
Serial.println("Sent with success");
}
else
{
Serial.println("Error sending the data");
}
}
void setup()
{
pinMode(LED, OUTPUT);
Serial.begin(250000);
Serial2.begin(115200);
while (!Serial)
{
;
}
// Set device as a Wi-Fi Station
WiFi.mode(WIFI_STA);
// Init ESP-NOW
if (esp_now_init() != ESP_OK)
{
Serial.println("Error initializing ESP-NOW");
return;
}
// Once ESPNow is successfully Init, we will register for Send CB to
// get the status of Trasnmitted packet
esp_now_register_send_cb(OnDataSent);
// Register peer
esp_now_peer_info_t peerInfo;
memcpy(peerInfo.peer_addr, broadcastAddress, 6);
peerInfo.channel = 0;
peerInfo.encrypt = false;
// Add peer
if (esp_now_add_peer(&peerInfo) != ESP_OK)
{
Serial.println("Failed to add peer");
return;
}
// Register for a callback function that will be called when data is received
esp_now_register_recv_cb(OnDataRecv);
//Setup has completed
Serial.println("<ESP32 is ready>");
}
void loop()
{
recvBytesWithStartMarker();
showNewData();
}
Most of this has come from various tutorials across the web including YouTube and RandomNerdTutorials. I am by no means a master programmer, but I am trying to learn.
Here are the results I get from the serial monitors:
ESP32 1:
�<ESP32 is ready>
AC:67:B2:36:AA:A8
65 1 2 1 FF FF FF
Sent with success
Last Packet Send Status: Delivery Success
ESP32 2:
AC:67:B2:35:19:D8
Bytes received: 4
0 0 0 0
Note that I am only sending 4 bytes of info (65 1 2 1), so it seems to match up (I'm not sending FF FF FF)...
Thanks again for any help!!
I was able to resolve my issue. Below is the code that displayed the same information that was sent from the host and received by the slave ESP32.
#include <Arduino.h>
#include <esp_now.h>
#include <WiFi.h>
// Example to receive data with a start marker and length byte
// For ease of testing this uses upper-case characters A and B
// for the request and response types
// and Z for the start marker
// this version saves all the bytes after the start marker
// if the TYPE byte is wrong it aborts
const byte numBytes = 32;
byte receivedBytes[numBytes];
const byte typeBytePosition = 6; // Position after the start byte
const byte requestType = 0x65;
const byte requestLength = 7;
boolean newData = false;
int LED = 2;
// REPLACE WITH THE MAC Address of your receiver
uint8_t broadcastAddress[] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
// Variable to store if sending data was successful
String success;
struct __attribute__((packed)) dataPacket
{
byte EventID;
byte PageID;
byte ObjectID;
byte StatusID;
} packet, *packetPtr;
const dataPacket onLoc1R = {0x65, 0x01, 0x01, 0x01};
const dataPacket offLoc1R = {0x065, 0x01, 0x01, 0x00};
const dataPacket onLoc1L = {0x65, 0x01, 0x02, 0x01};
const dataPacket offLoc1L = {0x65, 0x01, 0x02, 0x00};
// Callback when data is sent
void OnDataSent(const uint8_t *mac_addr, esp_now_send_status_t status)
{
Serial.print("\r\nLast Packet Send Status:\t");
Serial.println(status == ESP_NOW_SEND_SUCCESS ? "Delivery Success" : "Delivery Fail");
}
void emptyBuffer()
{
for (byte n = 0; n < numBytes; n++)
{
receivedBytes[n] = 0;
}
}
void recvBytesWithStartMarker()
{
static boolean recvInProgress = false;
static int ndx = -1;
static byte numBytesReceived = 0;
static byte mesgLen = 0;
const byte startMarker = 0x65;
byte rc;
while (Serial2.available() > 0 && newData == false)
{
rc = Serial2.read();
receivedBytes[numBytesReceived] = rc;
numBytesReceived++;
if (numBytesReceived > 33)
{
Serial.println("Error Rx : RESET !!");
Serial.println();
emptyBuffer();
numBytesReceived = 0;
newData = false;
}
if (recvInProgress == true)
{
if (numBytesReceived == typeBytePosition)
{
ndx = 0; // enable saving of data (anticipate good data)
if (rc == requestType)
{
mesgLen = requestLength;
}
else
{
recvInProgress = false; // abort - invalid request type
ndx = -1;
}
}
if (ndx >= 0)
{
ndx++;
}
if (numBytesReceived >= (mesgLen + typeBytePosition))
{ // got the whole message
recvInProgress = false;
newData = true;
}
}
else if (rc == startMarker)
{
emptyBuffer();
recvInProgress = true;
numBytesReceived = 0;
ndx = -1; // prevent counting valid bytes for the moment
}
}
}
void showNewData()
{
if (newData == true)
{
Serial.println(WiFi.macAddress());
Serial.print(requestType, HEX);
packet.EventID = requestType;
Serial.print(' ');
for (byte n = 0; n < typeBytePosition; n++) // n < numBytes
{
if (n == 0)
{
packet.PageID = receivedBytes[n];
}
else if (n == 1)
{
packet.ObjectID = receivedBytes[n];
}
else if (n == 2)
{
packet.StatusID = receivedBytes[n];
}
Serial.print(receivedBytes[n], HEX);
Serial.print(' ');
}
Serial.println();
// Send message via ESP-NOW
esp_err_t result = esp_now_send(broadcastAddress, (uint8_t *) &packet, sizeof(packet));
if (result == ESP_OK)
{
Serial.println("Sent with success");
}
else
{
Serial.println("Error sending the data");
}
newData = false;
}
}
// Callback when data is received
void OnDataRecv(const uint8_t *mac, const uint8_t *data, int len)
{
Serial.println(WiFi.macAddress());
memcpy(&packet, data, sizeof(packet));
Serial.printf("%x %x %x %x\n",(byte) packet.EventID,(byte) packet.PageID,(byte) packet.ObjectID,(byte) packet.StatusID);
Serial.println();
}
void sendNewData()
{
// Send message via ESP-NOW
esp_err_t result = esp_now_send(broadcastAddress, (uint8_t *) &packet, sizeof(packet));
if (result == ESP_OK)
{
Serial.println("Sent with success");
}
else
{
Serial.println("Error sending the data");
}
}
void setup()
{
pinMode(LED, OUTPUT);
Serial.begin(250000);
Serial2.begin(115200);
while (!Serial)
{
;
}
// Set device as a Wi-Fi Station
WiFi.mode(WIFI_STA);
// Init ESP-NOW
if (esp_now_init() != ESP_OK)
{
Serial.println("Error initializing ESP-NOW");
return;
}
// Once ESPNow is successfully Init, we will register for Send CB to get the status of Trasnmitted packet
esp_now_register_send_cb(OnDataSent);
// Register peer
esp_now_peer_info_t peerInfo;
memcpy(peerInfo.peer_addr, broadcastAddress, 6);
peerInfo.channel = 0;
peerInfo.encrypt = false;
// Add peer
if (esp_now_add_peer(&peerInfo) != ESP_OK)
{
Serial.println("Failed to add peer");
return;
}
// Register for a callback function that will be called when data is received
esp_now_register_recv_cb(OnDataRecv);
//Setup has completed
Serial.println("<ESP32 is ready>");
}
void loop()
{
recvBytesWithStartMarker();
showNewData();
}

Problem Reading From Thingspeak from Arduino

I am using this code to retrieve data from a Thingspeak channel in a JSON format. I am using an ESP8266 along with an Arduino Nano 33 BLE. The Nono 33 BLE Arduino board is not compatible with Thingspeak library and it doesn’t support SoftwareSerial. Alternatively, I am using serial1 to communicate with the ESP8266 and REST API method to get the data from Thingspeak as it is shown in the code. However, I cannot get it to work. It is worth noting that Serial1.available() always gives zero even after adding delays.
I have used the same code to send data to the same Thingspeak channel and it works perfectly. I have no clue why it is not reading from the channel. I would really appreciate any help with this problem
Thanks in advance.
String AP ="xxxxxxxxxxx";
String PASS ="xxxxxxxxxxxxxx";
String PORT = "80";
String field = "Id";
int countTrueCommand;
int countTimeCommand;
boolean found = false;
String Data ="";
void setup() {
Serial.begin(9600);
Serial1.begin(115200);
sendCommand("AT",5,"OK",false);
Serial1.println("AT+UART_DEF=9600,8,1,0,0");
delay(1000);
Serial1.end();
Serial1.begin(9600);
ConnectToWifi();
}
void loop() {
String getData="GET https://api.thingspeak.com/channels/1335558/fields/1?api_key=7XFZKXEGOV5HY4TQ";
sendCommand("AT+CIPMUX=1",5,"OK",false);
sendCommand("AT+CIPSTART=4,\"TCP\",\""+ HOST +"\","+ PORT,15,"OK",false);
sendCommand("AT+CIPSEND=4," +String(getData.length()+4),4,">",false);
sendCommand(getData,20,"OK",true);
delay(1500);
countTrueCommand++;
sendCommand("AT+CIPCLOSE=0",5,"OK",false);
}
bool ConnectToWifi(){
for (int a=0; a<15; a++)
{
sendCommand("AT",5,"OK",false);
sendCommand("AT+CWMODE=1",5,"OK",false);
boolean isConnected = sendCommand("AT+CWJAP=\""+ AP +"\",\""+ PASS +"\"",20,"OK",false);
if(isConnected)
{
return true;
}
}
}
bool sendCommand(String command, int maxTime, char readReplay[],boolean isGetData) {
boolean result=false;
//Test Purpose
Serial.print(countTrueCommand);
Serial.print(". at command => ");
Serial.print(command);
Serial.print(" ");
while(countTimeCommand < (maxTime*1))
{
Serial1.println(command);
if(Serial1.find(readReplay))//ok
{
if(isGetData)
{
if(Serial1.find(readReplay))
{
Serial.println("Success : Request is taken from the server");
}
while(Serial1.available())
{
char character = Serial1.read()
Data.concat(character); /
if (character == '\n')
{
Serial.print("Received: ");
Serial.println(Data);
delay(50);
Data = "";
}
}
}
result = true;
break;
}
countTimeCommand++;
}
if(result == true)
{
Serial.println("Success");
countTrueCommand++;
countTimeCommand = 0;
}
if(result == false)
{
Serial.println("Fail");
countTrueCommand = 0;
countTimeCommand = 0;
}
found = false;
return result;

Arduino, GSM shield, AT+CMGR and +CMGL don't respond with SMS text data

I am new to Arduino and GSM shield. I have been working with SMS, and I have a problem to read the SMS's text contents from the modem.
Here is the setting for the Arduino and the GSM.
#include <SoftwareSerial.h>
SoftwareSerial sim900(7, 8);
#define BUFF_SIZE 400
char buffer[BUFF_SIZE];
bool GSMconnected = false;
//functions
bool copyResponse(int* size);
void printBuff(int size);
bool readlnbuffer(int* start, int* nextline, int* size);
bool isResponseOK(int start, int endofline);
bool checkConnection();
bool checkMessage(int index);
bool checkString(int start, int end, String command);
bool checkSMS(int start, int nextline);
bool newMessage();
void setup() {
sim900.begin(9600);
Serial.begin(9600);
delay(100);
pinMode(relayIN, OUTPUT);
delay(100);
while (!GSMconnected) {
checkConnection();
delay(10000);
}
int rsize;
//text mode
sim900.print("AT+CMGF=1\r");
delay(1000);
//store message in SIM card
sim900.print("AT+CPMS=\"SM\"\r");
delay(1000);
sim900.print("AT+CMGD=0,4\r\n");
delay(1000);
copyResponse(&rsize);
printBuff(rsize);
/*
0: do not show header values
1: show the values in result codes
*/
sim900.print("AT+CSDH=1\r");
delay(1000);
//receive mode +CMTI:"SM", 1
sim900.print("AT+CNMI=2,2,0,0,0 \r");
delay(1000);
copyResponse(&rsize);
printBuff(rsize);
sim900.print("AT+CSMP=17,167,0,0\r\n");
delay(10000);
copyResponse(&rsize);
printBuff(rsize);
Serial.println("GSM is ready");
I want to check the SMS message when there is a new SMS. So I simply call the function when the GSM is available.
void loop() {
if(sim900.available()) {
newMessage();
}
}
newMessage() checks the index of the new SMS stored in the SIM card.
bool newMessage() {
int responseSize;
if (!copyResponse(&responseSize)) {
return false;
}
//+CMTI: “SM”,1
//read a line and check if there is a SMS
int start = 0;
int nextline = 0;
int index;
while (readlnbuffer(&start, &nextline, &responseSize)) {
String command = "+CMTI: \"SM\",";
if (checkString(start, start + 12, command)) {
Serial.println("SMS found");
index = int(buffer[start + 12]);
break;
}
start = nextline;
}
//debug
printBuff(responseSize);
delay(10000);
if (checkMessage(index)) {
//delete message
sim900.print("AT+CMGD=");
sim900.print(char(index));
sim900.println("\r");
delay(100);
return true;
} else {
return false;
}
checkMessage() reads SMS message and should print SMS text data.
bool checkMessage(int index) {
//listing
//sim900.print("AT+CMGL=\"REC UNREAD\"\r\n");
//reading
sim900.print("AT+CMGR=");
sim900.print(char(index));
sim900.print("\r\n");
delay(5000);
int responseSize;
if (!copyResponse(&responseSize)) {
return false;
}
delay(100);
//debug, to check what is in the buffer
printBuff(responseSize);
//read a line and check if there is a SMS
int start = 0;
int nextline = 0;
bool isSMS = false;
while (!isSMS) {
start = nextline;
if (!readlnbuffer(&start, &nextline, &responseSize)) {
Serial.println("no SMS found");
return false;
}
String command = "+CMGR:";
if (checkString(nextline, nextline + 6, command)) {
Serial.println("SMS checking");
isSMS = true;
}
}
//debug
if (!readlnbuffer(&start, &nextline, &responseSize)) {
Serial.println("cannot read the sms info");
return false;
}
Serial.println("SMS message: ");
for (int i = start; i < nextline; i++) {
//debug
Serial.print(buffer[i]);
}
Serial.println("");
}
When I run the code, it looks like the response for AT+CMGR cannot be copied completely. Here is what the Serial monitor shows.
checking the network
AT+CREG?
+CREG: 0,1
OK
AT+CMGF=1
OK
AT+CPMS="SM"
+CPMS: 1,30,1,30,1,30
OK
AT+
AT+CSDH=1
OK
AT+CNMI=2,1,0,0,0
OK
AT+CSMP=17,167,0,0
OK
GSM is ready
SMS found
+CMTI: "SM",1
AT+CMGR=1
> +CMGR: "REC UNREAD","+123456789","","17/11/26,11
SMS checking
SMS message:
AT+CMGD=1
OK
I think I went wrong with copying the response in the buffer. I am also a beginner of writing code and I need some help. Can anyone advise me on why I am getting this problem?
Thank you!
I think the problem is that you are aborting the command by using delay which you should never, never, ever do as a substitute for reading and parsing the responses from the mopdem.
For hints on removing delay usage see the linked answer (and for details on aborting command you can see V.250).

Arduino Mega: void serialEvent3() { } is not always triggering (or is it?)

Consider:
String cmdData; // Store the complete command on one line to send to sensor board.
String phResponse; // Store the pH sensor response.
boolean startOfLine = false;
boolean endOfLine = false;
boolean stringComplete = false;
void setup()
{
Serial.begin(38400);
Serial3.begin(38400);
pinMode(2, OUTPUT); // Used for temperature probe
}
void loop()
{
if (stringComplete)
{
Serial.println("Stored Response: " + phResponse);
phResponse = ""; // Empty phResponse variable to get
// ready for the next response
stringComplete = false;
}
}
void serialEvent()
{
while (Serial.available())
{
char cmd = (char)Serial.read();
if (cmd == '{')
{
startOfLine = true;
}
if (cmd == '}')
{
endOfLine = true;
}
if (startOfLine && cmd != '{' && cmd != '}')
{
cmdData += cmd;
}
if (startOfLine && endOfLine)
{
startOfLine = false;
endOfLine = false;
cmdData.toLowerCase(); // Convert cmdData value to lowercase
// for sanity reasons
if (cmdData == "ph")
{
delay(500);
ph();
}
if (cmdData == "phatc")
{
delay(500);
phATC();
}
cmdData = ""; // Empty cmdData variable to get ready for the next command
}
}
}
void serialEvent3()
{
while(Serial3.available())
{
char cmd3 = (char)Serial3.read();
phResponse += String(cmd3);
if (cmd3 == '\r')
{
stringComplete = true;
Serial.println("Carriage Command Found!");
}
}
}
float getTemp(char tempType)
{
float v_out; // Voltage output from temperature sensor
float temp; // The final temperature is stored here (this is only for code clarity)
float tempInCelcius; // Stores temperature in °C
float tempInFarenheit; // Stores temperature in °F
digitalWrite(A0, LOW); // Set pull-up resistor on analog pin
digitalWrite(2, HIGH); // Set pin 2 high, this will turn on temperature sensor
delay(2); // Wait 1 ms for temperature to stabilize
v_out = analogRead(0); // Read the input pin
digitalWrite(2, LOW); // Set pin 2 low, this will turn off temperature sensor
v_out *=. 0048; // Convert ADC points to voltage [volts] (we are using .0048
// because this device is running at 5 volts)
v_out *= 1000; // Convert unit from volts to millivolts
tempInCelcius = 0.0512 * v_out -20.5128; // The equation from millivolts to temperature
tempInFarenheit = (tempInCelcius * 9.0)/ 5.0 + 32.0; // Convert Celcius to Fahrenheit
if (tempType == 'c')
{
return tempInCelcius; // Return temperature in Celsius
}
else if (tempType == 'f')
{
return tempInFarenheit; // Return temperature in Fahrenheit
}
}
void ph()
{
Serial.println("Calculating pH sensor value in 3 seconds");
delay(3000);
Serial3.print("r\r");
}
void phATC()
{
Serial.println("pH auto temperature calibration will start in 3 seconds");
delay(3000);
float temp = getTemp('c');
char tempAr[10];
String tempAsString;
String tempData;
dtostrf(temp, 1, 2, tempAr);
tempAsString = String(tempAr);
tempData = tempAsString + '\r';
Serial3.print(tempData);
}
Why does serialEvent3() trigger after the second and sometimes the third time a command is sent to the sensor board? Once serialEvent3() finally triggers the consecutive commands work fluently. serialEvent() seems to work as expected. I have tried rearranging the functions without success. Is there a 'fail safe' time-out code to send the command again if serialEvent3 is not triggered?
Working code:
String cmdData; // Store the complete command on one line to send to sensor board.
String phResponse; // Store the pH sensor response.
boolean startOfLine = false;
boolean endOfLine = false;
boolean stringComplete = false;
boolean s3Trigger = false;
void setup()
{
Serial3.begin(38400);
Serial.begin(38400);
}
void serialEvent()
{
while (Serial.available())
{
char cmd = (char)Serial.read();
if (cmd == '{')
{
startOfLine = true;
}
if (cmd == '}')
{
endOfLine = true;
}
if (startOfLine && cmd != '{' && cmd != '}')
{
cmdData += cmd;
//Serial.println(cmdData);
}
}
}
void serialEvent3()
{
while(Serial3.available())
{
char cmd3 = (char)Serial3.read();
phResponse += String(cmd3);
if (cmd3 == '\r') // If carriage return has been found then...
{
stringComplete = true;
}
}
}
void loop()
{
if (startOfLine && endOfLine) // Both startOfLine and endOfLine must
// be true to run the command...
{
startOfLine = false;
endOfLine = false;
s3Trigger = true; // Set the s3Trigger boolean to true to check
// if data on Serial3.available() is available.
runCommand();
}
if (stringComplete)
{
Serial.println("Stored Response: " + phResponse); // Print stored response from the pH sensor.
phResponse = ""; // Empty phResponse variable to get ready for the next response
cmdData = ""; // Empty phResponse variable to get ready for the next command
stringComplete = false; // Set stringComplete to false
s3Trigger = false; // Set s3Trigger to false so it doesn't continuously loop.
}
if (s3Trigger) // If true then continue
{
delay(1000); // Delay to make sure the Serial3 buffer has all the data
if (!Serial3.available()) // If Serial3 available then execute
// the runCommand() function
{
//Serial.println("!Serial3.available()");
runCommand();
}
else
{
s3Trigger = false; // Set s3Trigger to false so it doesn't continuously loop.
}
}
}
void runCommand()
{
cmdData.toLowerCase(); // Convert cmdData value to lowercase
// for sanity reasons
if (cmdData == "ph")
{
ph();
}
}
void ph()
{
Serial.println("Calculating pH sensor value in 3 seconds");
delay(3000);
Serial3.print("r\r");
}
New working code without having to send the command twice:
/*
This software was made to demonstrate how to quickly get your
Atlas Scientific product running on the Arduino platform.
An Arduino MEGA 2560 board was used to test this code.
This code was written in the Arudino 1.0 IDE
Modify the code to fit your system.
**Type in a command in the serial monitor and the Atlas
Scientific product will respond.**
**The data from the Atlas Scientific product will come out
on the serial monitor.**
Code efficacy was NOT considered, this is a demo only.
The TX3 line goes to the RX pin of your product.
The RX3 line goes to the TX pin of your product.
Make sure you also connect to power and GND pins to power
and a common ground.
Open TOOLS > serial monitor, set the serial monitor
to the correct serial port and set the baud rate to 38400.
Remember, select carriage return from the drop down menu
next to the baud rate selection; not "both NL & CR".
*/
String inputstring = ""; // A string to hold incoming data from the PC
String sensorstring = ""; // A string to hold the data
// from the Atlas Scientific product
boolean input_stringcomplete = false; // Have we received all
// the data from the PC
boolean sensor_stringcomplete = false; // Have we received all the data from
// the Atlas Scientific product
#include <SoftwareSerial.h>
SoftwareSerial mySerial(10, 11); // RX, TX
void setup() { // Set up the hardware
// Set up the hardware
Serial.begin(38400); // Set baud rate for the hardware
// serial port_0 to 38400
mySerial.begin(38400);
inputstring.reserve(5); // Set aside some bytes for
// receiving data from the PC
sensorstring.reserve(30); // Set aside some bytes for receiving
// data from Atlas Scientific product
pinMode(12, OUTPUT);
digitalWrite(12, HIGH); // Turn on pull-up resistors
//mySerial.print("i\r");
}
void serialEvent() { // If the hardware serial port_0 receives a char
char inchar = (char)Serial.read(); // Get the char we just received
inputstring += inchar; // Add it to the inputString
if(inchar == '\r') { // If the incoming character is a <CR>, set the flag
input_stringcomplete = true;
}
}
void loop() { // Here we go....
while(mySerial.available())
{
char inchar = (char)mySerial.read(); // Get the char we just received
sensorstring += inchar; // Add it to the inputString
if(inchar == '\r') { // If the incoming character
// is a <CR>, set the flag
sensor_stringcomplete = true;
}
//Serial.print(inchar);
}
if (input_stringcomplete){ // If a string from the PC has been
// received in its entirety
//Serial.print(inputstring);
mySerial.print(inputstring); // Send that string to the Atlas Scientific product
inputstring = ""; // Clear the string:
input_stringcomplete = false; // Reset the flag used to tell if we have
// received a completed string from the PC
}
if (sensor_stringcomplete) { // If a string from the Atlas Scientific
// product has been received in its entirety
Serial.println(sensorstring); // Send that string to to the PC's serial monitor
sensorstring = ""; // Clear the string:
sensor_stringcomplete = false; // Reset the flag used to tell if
// we have received a completed string
// from the Atlas Scientific product
}
}
One thing I noticed (not the cause of the serial read problem). In a microprocessor, floating point math is expensive. It might be worth your while to combine these three lines:
v_out*=.0048;
v_out*=1000;
tempInCelcius = 0.0512 * v_out -20.5128;
into:
tempInCelcius = v_out * 0.24576 -20.2128;
I would also move tempInFarenheit = (tempInCelcius * 9.0)/ 5.0 + 32.0; right into the return statement so the calculation is only done if required. Generally splitting these lines up is encouraged for programming on a PC, but with a microprocessor I tend to compact my code and insert lots of comments.
I had a look at the example code for the sensor and I think I have something you can try. You have a while (Serial.available()) line in here. I can't be certain, but this doesn't look right to me and it might be messing you up. Try removing that block and just doing the read in the event.

Convert serial.read() into a usable string using Arduino

I'm using two Arduinos to sent plain text strings to each other using NewSoftSerial and an RF transceiver.
Each string is perhaps 20-30 characters in length. How do I convert Serial.read() into a string so I can do if x == "testing statements", etc.?
Unlimited string readed:
String content = "";
char character;
while(Serial.available()) {
character = Serial.read();
content.concat(character);
}
if (content != "") {
Serial.println(content);
}
From Help with Serial.Read() getting string:
char inData[20]; // Allocate some space for the string
char inChar = -1; // Where to store the character read
byte index = 0; // Index into array; where to store the character
void setup() {
Serial.begin(9600);
Serial.write("Power On");
}
char Comp(char* This) {
while (Serial.available() > 0) // Don't read unless there
// you know there is data
{
if(index < 19) // One less than the size of the array
{
inChar = Serial.read(); // Read a character
inData[index] = inChar; // Store it
index++; // Increment where to write next
inData[index] = '\0'; // Null terminate the string
}
}
if (strcmp(inData, This) == 0) {
for (int i=0; i<19; i++) {
inData[i] = 0;
}
index = 0;
return(0);
}
else {
return(1);
}
}
void loop()
{
if (Comp("m1 on") == 0) {
Serial.write("Motor 1 -> Online\n");
}
if (Comp("m1 off") == 0) {
Serial.write("Motor 1 -> Offline\n");
}
}
You can use Serial.readString() and Serial.readStringUntil() to parse strings from Serial on the Arduino.
You can also use Serial.parseInt() to read integer values from serial.
int x;
String str;
void loop()
{
if(Serial.available() > 0)
{
str = Serial.readStringUntil('\n');
x = Serial.parseInt();
}
}
The value to send over serial would be my string\n5 and the result would be str = "my string" and x = 5
I was asking the same question myself and after some research I found something like that.
It works like a charm for me. I use it to remote control my Arduino.
// Buffer to store incoming commands from serial port
String inData;
void setup() {
Serial.begin(9600);
Serial.println("Serial conection started, waiting for instructions...");
}
void loop() {
while (Serial.available() > 0)
{
char recieved = Serial.read();
inData += recieved;
// Process message when new line character is recieved
if (recieved == '\n')
{
Serial.print("Arduino Received: ");
Serial.print(inData);
// You can put some if and else here to process the message juste like that:
if(inData == "+++\n"){ // DON'T forget to add "\n" at the end of the string.
Serial.println("OK. Press h for help.");
}
inData = ""; // Clear recieved buffer
}
}
}
This would be way easier:
char data [21];
int number_of_bytes_received;
if(Serial.available() > 0)
{
number_of_bytes_received = Serial.readBytesUntil (13,data,20); // read bytes (max. 20) from buffer, untill <CR> (13). store bytes in data. count the bytes recieved.
data[number_of_bytes_received] = 0; // add a 0 terminator to the char array
}
bool result = strcmp (data, "whatever");
// strcmp returns 0; if inputs match.
// http://en.cppreference.com/w/c/string/byte/strcmp
if (result == 0)
{
Serial.println("data matches whatever");
}
else
{
Serial.println("data does not match whatever");
}
The best and most intuitive way is to use serialEvent() callback Arduino defines along with loop() and setup().
I've built a small library a while back that handles message reception, but never had time to opensource it.
This library receives \n terminated lines that represent a command and arbitrary payload, space-separated.
You can tweak it to use your own protocol easily.
First of all, a library, SerialReciever.h:
#ifndef __SERIAL_RECEIVER_H__
#define __SERIAL_RECEIVER_H__
class IncomingCommand {
private:
static boolean hasPayload;
public:
static String command;
static String payload;
static boolean isReady;
static void reset() {
isReady = false;
hasPayload = false;
command = "";
payload = "";
}
static boolean append(char c) {
if (c == '\n') {
isReady = true;
return true;
}
if (c == ' ' && !hasPayload) {
hasPayload = true;
return false;
}
if (hasPayload)
payload += c;
else
command += c;
return false;
}
};
boolean IncomingCommand::isReady = false;
boolean IncomingCommand::hasPayload = false;
String IncomingCommand::command = false;
String IncomingCommand::payload = false;
#endif // #ifndef __SERIAL_RECEIVER_H__
To use it, in your project do this:
#include <SerialReceiver.h>
void setup() {
Serial.begin(115200);
IncomingCommand::reset();
}
void serialEvent() {
while (Serial.available()) {
char inChar = (char)Serial.read();
if (IncomingCommand::append(inChar))
return;
}
}
To use the received commands:
void loop() {
if (!IncomingCommand::isReady) {
delay(10);
return;
}
executeCommand(IncomingCommand::command, IncomingCommand::payload); // I use registry pattern to handle commands, but you are free to do whatever suits your project better.
IncomingCommand::reset();
Here is a more robust implementation that handles abnormal input and race conditions.
It detects unusually long input values and safely discards them. For example, if the source had an error and generated input without the expected terminator; or was malicious.
It ensures the string value is always null terminated (even when buffer size is completely filled).
It waits until the complete value is captured. For example, transmission delays could cause Serial.available() to return zero before the rest of the value finishes arriving.
Does not skip values when multiple values arrive quicker than they can be processed (subject to the limitations of the serial input buffer).
Can handle values that are a prefix of another value (e.g. "abc" and "abcd" can both be read in).
It deliberately uses character arrays instead of the String type, to be more efficient and to avoid memory problems. It also avoids using the readStringUntil() function, to not timeout before the input arrives.
The original question did not say how the variable length strings are defined, but I'll assume they are terminated by a single newline character - which turns this into a line reading problem.
int read_line(char* buffer, int bufsize)
{
for (int index = 0; index < bufsize; index++) {
// Wait until characters are available
while (Serial.available() == 0) {
}
char ch = Serial.read(); // read next character
Serial.print(ch); // echo it back: useful with the serial monitor (optional)
if (ch == '\n') {
buffer[index] = 0; // end of line reached: null terminate string
return index; // success: return length of string (zero if string is empty)
}
buffer[index] = ch; // Append character to buffer
}
// Reached end of buffer, but have not seen the end-of-line yet.
// Discard the rest of the line (safer than returning a partial line).
char ch;
do {
// Wait until characters are available
while (Serial.available() == 0) {
}
ch = Serial.read(); // read next character (and discard it)
Serial.print(ch); // echo it back
} while (ch != '\n');
buffer[0] = 0; // set buffer to empty string even though it should not be used
return -1; // error: return negative one to indicate the input was too long
}
Here is an example of it being used to read commands from the serial monitor:
const int LED_PIN = 13;
const int LINE_BUFFER_SIZE = 80; // max line length is one less than this
void setup() {
pinMode(LED_PIN, OUTPUT);
Serial.begin(9600);
}
void loop() {
Serial.print("> ");
// Read command
char line[LINE_BUFFER_SIZE];
if (read_line(line, sizeof(line)) < 0) {
Serial.println("Error: line too long");
return; // skip command processing and try again on next iteration of loop
}
// Process command
if (strcmp(line, "off") == 0) {
digitalWrite(LED_PIN, LOW);
} else if (strcmp(line, "on") == 0) {
digitalWrite(LED_PIN, HIGH);
} else if (strcmp(line, "") == 0) {
// Empty line: no command
} else {
Serial.print("Error: unknown command: \"");
Serial.print(line);
Serial.println("\" (available commands: \"off\", \"on\")");
}
}
String content = "";
char character;
if(Serial.available() >0){
//reset this variable!
content = "";
//make string from chars
while(Serial.available()>0) {
character = Serial.read();
content.concat(character);
}
//send back
Serial.print("#");
Serial.print(content);
Serial.print("#");
Serial.flush();
}
If you want to read messages from the serial port and you need to deal with every single message separately I suggest separating messages into parts using a separator like this:
String getMessage()
{
String msg=""; //the message starts empty
byte ch; // the character that you use to construct the Message
byte d='#';// the separating symbol
if(Serial.available())// checks if there is a new message;
{
while(Serial.available() && Serial.peek()!=d)// while the message did not finish
{
ch=Serial.read();// get the character
msg+=(char)ch;//add the character to the message
delay(1);//wait for the next character
}
ch=Serial.read();// pop the '#' from the buffer
if(ch==d) // id finished
return msg;
else
return "NA";
}
else
return "NA"; // return "NA" if no message;
}
This way you will get a single message every time you use the function.
Credit for this goes to magma. Great answer, but here it is using c++ style strings instead of c style strings. Some users may find that easier.
String string = "";
char ch; // Where to store the character read
void setup() {
Serial.begin(9600);
Serial.write("Power On");
}
boolean Comp(String par) {
while (Serial.available() > 0) // Don't read unless
// there you know there is data
{
ch = Serial.read(); // Read a character
string += ch; // Add it
}
if (par == string) {
string = "";
return(true);
}
else {
//dont reset string
return(false);
}
}
void loop()
{
if (Comp("m1 on")) {
Serial.write("Motor 1 -> Online\n");
}
if (Comp("m1 off")) {
Serial.write("Motor 1 -> Offline\n");
}
}
If you're using concatenate method then don't forget to trim the string if you're working with if else method.
Use string append operator on the serial.read(). It works better than string.concat()
char r;
string mystring = "";
while(serial.available()){
r = serial.read();
mystring = mystring + r;
}
After you are done saving the stream in a string(mystring, in this case), use SubString functions to extract what you are looking for.
I could get away with this:
void setup() {
Serial.begin(9600);
}
void loop() {
String message = "";
while (Serial.available())
message.concat((char) Serial.read());
if (message != "")
Serial.println(message);
}
Many great answers, here is my 2 cents with exact functionality as requested in the question.
Plus it should be a bit easier to read and debug.
Code is tested up to 128 chars of input.
Tested on Arduino uno r3 (Arduino IDE 1.6.8)
Functionality:
Turns Arduino onboard led (pin 13) on or off using serial command input.
Commands:
LED.ON
LED.OFF
Note: Remember to change baud rate based on your board speed.
// Turns Arduino onboard led (pin 13) on or off using serial command input.
// Pin 13, a LED connected on most Arduino boards.
int const LED = 13;
// Serial Input Variables
int intLoopCounter = 0;
String strSerialInput = "";
// the setup routine runs once when you press reset:
void setup()
{
// initialize the digital pin as an output.
pinMode(LED, OUTPUT);
// initialize serial port
Serial.begin(250000); // CHANGE BAUD RATE based on the board speed.
// initialized
Serial.println("Initialized.");
}
// the loop routine runs over and over again forever:
void loop()
{
// Slow down a bit.
// Note: This may have to be increased for longer strings or increase the iteration in GetPossibleSerialData() function.
delay(1);
CheckAndExecuteSerialCommand();
}
void CheckAndExecuteSerialCommand()
{
//Get Data from Serial
String serialData = GetPossibleSerialData();
bool commandAccepted = false;
if (serialData.startsWith("LED.ON"))
{
commandAccepted = true;
digitalWrite(LED, HIGH); // turn the LED on (HIGH is the voltage level)
}
else if (serialData.startsWith("LED.OFF"))
{
commandAccepted = true;
digitalWrite(LED, LOW); // turn the LED off by making the voltage LOW
}
else if (serialData != "")
{
Serial.println();
Serial.println("*** Command Failed ***");
Serial.println("\t" + serialData);
Serial.println();
Serial.println();
Serial.println("*** Invalid Command ***");
Serial.println();
Serial.println("Try:");
Serial.println("\tLED.ON");
Serial.println("\tLED.OFF");
Serial.println();
}
if (commandAccepted)
{
Serial.println();
Serial.println("*** Command Executed ***");
Serial.println("\t" + serialData);
Serial.println();
}
}
String GetPossibleSerialData()
{
String retVal;
int iteration = 10; // 10 times the time it takes to do the main loop
if (strSerialInput.length() > 0)
{
// Print the retreived string after looping 10(iteration) ex times
if (intLoopCounter > strSerialInput.length() + iteration)
{
retVal = strSerialInput;
strSerialInput = "";
intLoopCounter = 0;
}
intLoopCounter++;
}
return retVal;
}
void serialEvent()
{
while (Serial.available())
{
strSerialInput.concat((char) Serial.read());
}
}
This always works for me :)
String _SerialRead = "";
void setup() {
Serial.begin(9600);
}
void loop() {
while (Serial.available() > 0) //Only run when there is data available
{
_SerialRead += char(Serial.read()); //Here every received char will be
//added to _SerialRead
if (_SerialRead.indexOf("S") > 0) //Checks for the letter S
{
_SerialRead = ""; //Do something then clear the string
}
}
}

Resources