我通过USB连接了Raspberry Pi和Arduino . Arduino通过传感器(EC和温度传感器)从世界获取数据并将此数据写入串行数据 . Raspberry正在将这些数据写入数据库 .
Arduino草图:
#include <OneWire.h>
#include <DallasTemperature.h>
int R1= 500;
int Ra=25; //Resistance of powering Pins
int ECPin= A0;
int ECGround=A1;
int ECPower =A4;
float PPMconversion=0.7;
float TemperatureCoef = 0.019;
float K=2.88;
#define ONE_WIRE_BUS 10 // Data wire For Temp Probe is plugged into pin 10 on the Arduino
const int TempProbePossitive =8; //Temp Probe power connected to pin 9
const int TempProbeNegative=9; //Temp Probe Negative connected to pin 8
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);// Pass our oneWire reference to Dallas Temperature.
float Temperature=10;
float EC=0;
float EC25 =0;
int ppm =0;
float raw= 0;
float Vin= 5;
float Vdrop= 0;
float Rc= 0;
float buffer=0;
void setup()
{
Serial.begin(9600);
pinMode(TempProbeNegative , OUTPUT ); //seting ground pin as output for tmp probe
digitalWrite(TempProbeNegative , LOW );//Seting it to ground so it can sink current
pinMode(TempProbePossitive , OUTPUT );//ditto but for positive
digitalWrite(TempProbePossitive , HIGH );
pinMode(ECPin,INPUT);
pinMode(ECPower,OUTPUT);//Setting pin for sourcing current
pinMode(ECGround,OUTPUT);//setting pin for sinking current
digitalWrite(ECGround,LOW);//We can leave the ground connected permanantly
delay(100);// gives sensor time to settle
sensors.begin();
delay(100);
R1=(R1+Ra);// Taking into acount Powering Pin Resitance
};
void loop()
{
GetEC();
PrintReadings(); // Cals Print routine [below main loop]
delay(20000);
}
void GetEC(){
sensors.requestTemperatures();// Send the command to get temperatures
Temperature=sensors.getTempCByIndex(0); //Stores Value in Variable
digitalWrite(ECPower,HIGH);
raw= analogRead(ECPin);
raw= analogRead(ECPin);// This is not a mistake, First reading will be low beause if charged a capacitor
digitalWrite(ECPower,LOW);
Vdrop= (Vin*raw)/1024.0;
Rc=(Vdrop*R1)/(Vin-Vdrop);
Rc=Rc-Ra; //acounting for Digital Pin Resitance
EC = 1000/(Rc*K);
EC25 = EC/ (1+ TemperatureCoef*(Temperature-25.0));
ppm=(EC25)*(PPMconversion*1000);
}
void PrintReadings(){
Serial.print("Rc: ");
Serial.print(Rc);
Serial.print(" EC: ");
Serial.print(EC25);
Serial.print(" Simens ");
Serial.print(ppm);
Serial.print(" ppm ");
Serial.print(Temperature);
Serial.println(" *C ");
Serial.print("Vdrop: ");
Serial.println(Vdrop);
Serial.print("Rc: ");
Serial.println(Rc);
Serial.print(EC);
Serial.println("Siemens");
};
Raspberry Pi上的代码:
import serial
import time
import re
import sqlite3
for com in range(0,4):
try:
PORT = '/dev/ttyACM'+str(com)
BAUD = 9600
board = serial.Serial(PORT,BAUD)
board.close()
break
except:
pass
DEVICE = '/dev/ttyACM'+str(com)
BAUD = 9600
s = serial.Serial(DEVICE, BAUD)
conn=sqlite3.connect('mydatabase.db')
cursor=conn.cursor()
#s.open()
time.sleep(5) # der Arduino resettet nach einer Seriellen Verbindung, daher muss kurz gewartet werden
#s.write("test");
while True:
response = s.readline()
numbers = re.findall(r"[-+]?\d*\.\d+|\d+", response)
if len(numbers) == 4:
temp = numbers[3]
ec = numbers[1]
result = cursor.execute("INSERT INTO sensordata (temp, ec) VALUES ({temp}, {ec})".form$
conn.commit()
print response
在Raspberry端写入数据大约24小时,然后我不再从Arduino获得串行输出 . 当我再次重启python脚本时出现同样的问题 . 当我重新启动python脚本并再次启动串行通信时,Arduino将重置 . 我没有更改此默认行为 . 我仍然没有通过串口获取数据这一事实告诉我,Arduino方面没有内存问题 . 还有一个提示,它一定是Raspberry的一个问题,我是否可以从重新启动Raspberry解决问题并且数据再记录24小时这一事实 .
有人好奇地给我一个暗示,如何 Build 稳固的沟通?
1 回答
我的问题解决了 .
当我在Arduino端添加大量串行打印时,我发现Raspberry端没有收到任何东西,但更不用说了,所以我的python程序无法解析Arduino草图发送的内容 . 另一个观察是:
当我看着串口设备时
我得到了一个非常相似的效果,可以重现这个问题 . 由于我在Arduino端添加了大量的调试打印,我看到一些字符通过pyserial收到我的python脚本正在打印但是这个屏幕命令严重损害了通信 . 通过屏幕观看串行设备,我看到更多的角色,好像屏幕偷走了节目 . 这是一个混乱,我没有设法清理,我不得不重新启动Raspberry . 但告诉我,问题必须在Raspberry方面 .
什么解决了我的问题是尝试这种沟通模式:
https://github.com/gskielian/Arduino-DataLogging/blob/master/PySerial/README.md
完全不明白作者的意思是'但是,小心Arduino不会经常向你的程序发送数据 - 你可能会遇到来自缓冲区溢出的错误 .
现在要求Arduino提供数据并做出响应而不是连续发送 .
这对我来说仍然是神秘的,但我的问题得到解答,Raspberry现在成功接收传感器数据6天 .