Ive connected my arduino nano to mlx90614 sensor and trying to have created an executable for printing fahrenheit temperature. Since I had no idea how to use the arduino code in my C++ code, I made an executable to read the serial output.
NANO CODE
#include <Wire.h>
#include <Adafruit_MLX90614.h>
Adafruit_MLX90614 mlx = Adafruit_MLX90614();
const int buzzer=2;
const int threshold=38;
void setup() {
Serial.begin(9600);
pinMode(2,OUTPUT);
mlx.begin();
}
void loop() {
Serial.print(mlx.readObjectTempF());
Serial.println();
if (mlx.readObjectTempC()>threshold){
digitalWrite(buzzer, HIGH);
} else {
digitalWrite(buzzer, LOW);
}
delay(10);
}
C++ CODE TO READ SERIAL OUTPUT
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <fcntl.h> // Contains file controls like O_RDWR
#include <errno.h> // Error integer and strerror() function
#include <termios.h> // Contains POSIX terminal control definitions
#include <unistd.h> // write(), read(), close()
using namespace std;
string temp() {
int serial_port = open("/dev/ttyUSB0", O_RDWR);
struct termios tty;
if(tcgetattr(serial_port, &tty) != 0) {
printf("Error %i from tcgetattr: %s\n", errno, strerror(errno));
}
tty.c_cflag &= ~PARENB; // Clear parity bit, disabling parity (most common)
tty.c_cflag &= ~CSTOPB; // Clear stop field, only one stop bit used in communication (most common)
tty.c_cflag &= ~CSIZE; // Clear all bits that set the data size
tty.c_cflag |= CS8; // 8 bits per byte (most common)
tty.c_cflag &= ~CRTSCTS; // Disable RTS/CTS hardware flow control (most common)
tty.c_cflag |= CREAD | CLOCAL; // Turn on READ & ignore ctrl lines (CLOCAL = 1)
tty.c_lflag &= ~ICANON;
tty.c_lflag &= ~ECHO; // Disable echo
tty.c_lflag &= ~ECHOE; // Disable erasure
tty.c_lflag &= ~ECHONL; // Disable new-line echo
tty.c_lflag &= ~ISIG; // Disable interpretation of INTR, QUIT and SUSP
tty.c_iflag &= ~(IXON | IXOFF | IXANY); // Turn off s/w flow ctrl
tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP|INLCR|IGNCR|ICRNL); // Disable any special handling of received bytes
tty.c_oflag &= ~OPOST; // Prevent special interpretation of output bytes (e.g. newline chars)
tty.c_oflag &= ~ONLCR; // Prevent conversion of newline to carriage return/line feed
tty.c_cc[VTIME] = 10; // Wait for up to 1s (10 deciseconds), returning as soon as any data is received.
tty.c_cc[VMIN] = 0;
// Set in/out baud rate to be 9600
cfsetispeed(&tty, B9600);
cfsetospeed(&tty, B9600);
// Save tty settings, also checking for error
if (tcsetattr(serial_port, TCSANOW, &tty) != 0) {
printf("Error %i from tcsetattr: %s\n", errno, strerror(errno));
}
char read_buf [256];
memset(&read_buf, '\0', sizeof(read_buf));
int num_bytes = read(serial_port, &read_buf, sizeof(read_buf));
if (num_bytes < 0) {
printf("Error reading: %s", strerror(errno));
}
// printf("Read %i bytes. Received message: %s", num_bytes, read_buf);
close(serial_port);
return read_buf; // success
}
MAIN.CPP
#include <stdio.h>
#include <string.h>
#include <iostream>
using namespace std;
string temp();
int main(int argc, char** argv) {
string tem;
tem=temp();
cout << tem;
return 0;
}
g++ main.cpp temp.cpp -o temp
Now when I execute ./temp I sometimes get temperature, while the other times its junk values
ns@ns-desktop:~/Arduino$ ./temp
61.72 (WORKS)
ns@ns-desktop:~/Arduino$ ./temp
(NO VALUE PRINTED)
ns@ns-desktop:~/Arduino$ ./temp
� (GARBAGE VALUE)
ns@ns-desktop:~/Arduino$ ./temp
61.43� (VALUE READ BUT ALONG WITH GARBAGE )
ns@ns-desktop:~/Arduino$ ./temp
� (GARBAGE)
ns@ns-desktop:~/Arduino$ ./temp
� (GARBAGE)
What could be the issue ? I suspect its something to do with the arduino code or the sensor. How to avoid this garbage value ?