I'm currently trying to read serial data from an ESP8266 NodeMCU module to a Beaglebone Black. Essentially I trigger the NodeMCU to send a string via UART from the BBB and print the string to the BBB terminal. The following is the code on the BBB:
#include<iostream>
#include<fstream>
#include<string>
#include<unistd.h>
#include<termios.h>
#include<stdio.h>
#include<fcntl.h>
using namespace std;
int main(){
std::fstream fs;
// Setup UART
int file, count;
if ((file = open("/dev/ttyO4", O_RDWR | O_NOCTTY | O_NDELAY))<0){
perror("UART: Failed to open the file.\n");
return -1;
}
struct termios options; // the termios structure is vital
tcgetattr(file, &options); // sets the parameters associated with file
// Set up the communications options:
// 115200 baud, 8-bit, enable receiver, no modem control lines
options.c_cflag = B115200 | CS8 | CREAD | CLOCAL;
options.c_iflag = IGNPAR | ICRNL; // ignore partity errors, CR -> newline
tcflush(file, TCIFLUSH); // discard file information not transmitted
tcsetattr(file, TCSANOW, &options); // changes occur immmediately
fs.open("/sys/class/gpio/export");
fs << "48";
fs.close();
fs.open("/sys/class/gpio/gpio48/direction");
fs << "out";
fs.close();
fs.open("/sys/class/gpio/gpio48/value");
fs << "1"; // "0" for off
fs.close();
usleep(1000000);
fs.open("/sys/class/gpio/gpio48/value");
fs << "0";
fs.close();
unsigned char receive[1000]; // declare a buffer for receiving data
int characters = read(file, (void*)receive, 1000);
if (characters == -1){ // receive the data
perror("Failed to read from the input\n");
return -1;
}
if (characters==0) printf("There was no data available to read!\n");
else {
receive[characters] = 0;
printf("%s\n",receive);
}
close(file);
return 0;
}
The following is the code on the NodeMCU:
void setup() {
// put your setup code here, to run once:
Serial.begin(115200, SERIAL_8N1);
Serial.println();
// Set D2 as input
pinMode(4, INPUT);
}
void loop() {
// put your main code here, to run repeatedly:
if(digitalRead(4)==1){
String msg = "Hola!";
while(digitalRead(4)==1){
}
Serial.print(msg);
}
}
When I run the C script, I get the following error:
Failed to read from the input
: Resource temporarily unavailable
However, I can open a Minicom terminal on the BBB on ttyO4 and the "Hola!" string gets printed once as expected.
How can I overcome this error?