0

我正在尝试获取我的 firebase 数据并使用 Arduino 将其显示到我的 7 段,并且我正在获得 python 脚本的帮助以将数据发送到串行监视器。虽然它检测到Serial.available()循环内部,但我不知道如何获得该特定值并将其第一个数字显示到我的 7 段

目前对于测试我只是想显示 1 如果串行监视器和 arduino 内部有数据匹配

阿德鲁伊诺:

#define A PA0
#define B PA1
#define C PA2
#define D PA3
#define E PA4
#define F PA5
#define G PA6
int a = 2;

void setup() {
  Serial.begin(9600);

  pinMode(A, OUTPUT);
  pinMode(B, OUTPUT);
  pinMode(C, OUTPUT);
  pinMode(D, OUTPUT);
  pinMode(E, OUTPUT);
  pinMode(F, OUTPUT);
  pinMode(G, OUTPUT);
}

void loop() {
  if(Serial.available()){
     //  Serial.println("0 1 2 3 4 5 6 7 8 9");
//  sevenSeg(1, 1, 1, 1, 1, 1, 0); //   0
//  delay(500);

  if(Serial.readString() == Serial.readString()){
    sevenSeg(0, 0, 0, 1, 1, 0, 0); //  1
    delay(500);
  }

  }else{
  sevenSeg(1, 1, 1, 1, 1, 1, 0);
  }//   0 



}


void sevenSeg (int g, int f, int e, int d, int c, int b, int a)
{
  digitalWrite(A, a);
  digitalWrite(B, b);
  digitalWrite(C, c);
  digitalWrite(D, d);
  digitalWrite(E, e);
  digitalWrite(F, f);
  digitalWrite(G, g);

}

Python脚本:

import pyrebase
import argparse
import datetime
import time
import math
import serial
from serial import Serial

time_str = datetime.datetime.now().strftime("%Y%b%d_%I:%M:%S%p").upper()

ap = argparse.ArgumentParser()
ap.add_argument("-t", "--temp", help="Patient temperature at time of reading") #
ap.add_argument("-r", "--heart", help="Patient heart rate at the time of reading")
args = vars(ap.parse_args())


#firebase configuration
firebaseConfig = {
    "apiKey": "",
    "authDomain": "heartrate-firebase.firebaseapp.com",
    "databaseURL": "https://heartrate-firebase-default-rtdb.firebaseio.com/",
    "projectId": "heartrate-firebase",
    "storageBucket": "heartrate-firebase.appspot.com",
    "messagingSenderId": "851991577466",
    "appId": "1:851991577466:web:daaf323d9af64fd3318cc3",
}


firebase = pyrebase.initialize_app(firebaseConfig)
db = firebase.database()

if args.get("temp", None) is not None:
    data = {"Temperature": args.get("temp", None)}
    db.child("Temperature").child(time_str).set(data)

if args.get("heart", None) is not None:
    data = {"HeartRate": args.get("heart", None)}
    db.child("HeartRate").child(time_str).set(data)

user = db.child("HeartRate").get()

for x in user.each():
    date = db.child("HeartRate").child(x.key()).child("HeartRate").get()
    print("Heart Rate: " + date.val())


userTemp = db.child("Temperature").get()
for x in userTemp.each():
    date = db.child("Temperature").child(x.key()).child("Temperature").get()
    # date = db.child("Temperature").get()
    print("Temperature: " + date.val())
    

with serial.Serial('COM4', 9600, timeout=5) as ser:
    time.sleep(2)
    ser.write((date.val() + '\n').encode())
    time.sleep(1)
    y = ser.read(ser.inWaiting())
    print(y)
    # ser.close()

我最终得到的只是显示 0。非常感谢任何帮助或想法。谢谢。

4

1 回答 1

0

问题是我2\r\n从数据库中接收,所以要删除 \r\n 我曾经strip()删除过 \r\n 并且它有效。

于 2021-03-25T17:20:00.507 回答