Hello,
Context
I am running into what I imagine is a fairly straightforward SoftwareSerial issue, but have so far been unsuccessful in resolving. I have a CO2 sensor (instructions sheet attached) that has a simple RX/TX connection. The example the supplier provided was for R.Pi, but translating to Arduino doesn't seem too hefty of a task (R.Pi and Arduino translation we made posted below). However, when we run this script, we have been running into the following list of errors:
avrdude: stk500v2_ReceiveMessage(): timeout
avrdude: stk500v2_ReceiveMessage(): timeout
avrdude: stk500v2_ReceiveMessage(): timeout
avrdude: stk500v2_ReceiveMessage(): timeout
avrdude: stk500v2_ReceiveMessage(): timeout
avrdude: stk500v2_ReceiveMessage(): timeout
avrdude: stk500v2_getsync(): timeout communicating with programmer
In my attempts to troubleshoot, I've tried sending/receiving simple messages ("hello world") via RX/TX with the Arduino hooked up to itself (pin 0 --> pin 1). This resulted in the same error. To isolate the problem, I pulled up the SoftwareSerialExample and TwoPortReceive examples. While I no longer ran into the errors, I was not seeing the apparent messages that should be relayed to Serial.print (I was sifting through the appropriate Baud rates; only getting a static set of "?????"). I also noticed that I had a growing list of "COM3", which I couldn't clear, and temporarily resolved by just restarting my computer.
Ultimately, the plan is to have two of these sensors hooked up to the Arduino (MEGA). I mention that now in case that significantly affects how we move forward, but otherwise that seems like something we can address later. So, in summary...
The Ask
I'd greatly appreciate help with:
1). Identifying what we are doing wrong with our Serial Port management.
2). Checking over our translation of the R.Pi script to Arduino (below).
Raspberry Pi
#rpi serial connections
#Python app to run a S8 Sensor
import serial
import time
# RPi pin connextions:
#pin 6 GND
#pin 4 5v
#pin 8 TXD: UART data to S8
#pin 10 RXD: UART data from S8
ser = serial.Serial("/dev/ttyS0",baudrate =9600,timeout = .5)
print " AN-168: Raspberry Pi3 to S8 Via UART\n"
ser.flushInput()
time.sleep(1)
for i in range(1,21): # Print 20 readings from sensor
ser.flushInput()
ser.write("\xFE\x44\x00\x08\x02\x9F\x25")
time.sleep(1.9)
resp = ser.read(7)
high = ord(resp[3])
low = ord(resp[4])
co2 = (high*256) + low
print " CO2 = " +str(co2)
time.sleep(.1)
Arduino
#include <SoftwareSerial.h>
#define rxPin1 0
#define txPin1 1
SoftwareSerial co21Ser(rxPin1, txPin1);
int co2;
char resp = '0';
void setup() {
pinMode(rxPin1, INPUT);
pinMode(txPin1, OUTPUT);
Serial.begin(9600);
co21Ser.begin(9600);
}
void loop() {
if (co21Ser.available()){
co21Ser.write("\xFE\x44\x00\x08\x02\x9F\x25");
delay(1900)
resp = co21Ser.read();
Serial.print("resp = ");
Serial.println(resp);
int high = atoi(resp[3]);
int low = atoi(resp[4]);
co2 = (high*256) + low;
Serial.print("CO2 = ");
Serial.println(co2);
}
else{
Serial.println("Nothing...");
}
delay(100);
}
Thank you.
All the best,
m3yre
CO2Meter_CO2Circuit_Instructions.pdf (423 KB)