Raspberry and Arduino communication question!

Asked 1 years ago, Updated 1 years ago, 77 views

I am communicating with Raspberry Pi by attaching a fingerprint recognition sensor to Arduino. While coding, I didn't receive the value of the fingerprint recognition sensor from Raspberry Pi, so I'm asking if the coding is wrong.

It's raspberry pie sauce.

import time
import picamera
import datetime
import requests
import os
from flask import Flask, redirect, url_for
from werkzeug import secure_filename
import serial


port = "/dev/ttyACM0"
serialFromArduino = serial.Serial(port, 9600)
serialFromArduino.flushInput()



while True:
    if(serialFromArduino.inWaiting() > 0):
        input = serialFromArduino.read(1)
#        #        input = serialFromArduino.read(1)
       # # print(input)
    if input=='a':
        with picamera.PiCamera() as camera:
                camera.brightness = 60
                camera.start_preview(fullscreen=False,window=(100,20,1024,768))
                time.sleep(5)
                camera.capture("A.jpg")
                print("AOK")
                camera.stop_preview()
                camera.close()
    if input=='b':
        with picamera.PiCamera() as camera:
                camera.brightness = 60
                camera.start_preview(fullscreen=False,window=(100,20,1024,768))
                time.sleep(5)
                camera.capture("B.jpg")
                print("BOK")
                camera.stop_preview()
                camera.close()

It's Arduino sauce.

#include <Adafruit_Fingerprint.h>

#include <Wire.h>                            // I2C control library

#include <LiquidCrystal_I2C.h>          // LCD library 

#include <SoftwareSerial.h>

SoftwareSerial mySerial(18, 19);

#define SHOCK 5

LiquidCrystal_I2C lcd(0x27,16,2);



Adafruit_Fingerprint finger = Adafruit_Fingerprint(&mySerial);

/////////////// switch////////////////////////////////////////

int inputPin = 2; // sensor signal pin

int pirState = LOW; // PIR initial state

int val = 0; // Signal Input



int RELAY=8;



/////////////// number of fingerprints/////////////////

int a=0;

//////////////////



void setup()  

{

Serial.begin(9600);

////////////////////////////////////////////////



 pinMode(A0,INPUT);

  pinMode(A1,INPUT);

  ////////////// shock detection//////////////

  pinMode(SHOCK, INPUT);





 ///////////////relay+electronic solenoid////////////

  pinMode(RELAY,OUTPUT);

 pinMode(3,OUTPUT);

 pinMode(4,OUTPUT);

  digitalWrite(RELAY,LOW); 





  /////////////////// power control switch/////////////////////////////////

 pinMode(inputPin, INPUT);



  ///////////////// fingerprint sensor//////////////////////////////////////////////////////// 

  while (!Serial);  // For Yun/Leo/Micro/Zero/...

  delay(100);

  Serial.println("\n\nAdafruit finger detect test");



  // // set the data rate for the sensor serial port

  finger.begin(57600);



  if (finger.verifyPassword()) {

    Serial.println("Found fingerprint sensor!");
  } 
  else {

    Serial.println("Did not find fingerprint sensor :(");
  }



  finger.getTemplateCount();

  Serial.print("Sensor contains "); Serial.print(finger.templateCount); Serial.println(" templates");

  Serial.println("Waiting for valid finger...");

  /////////////////////////////////////////////////////////////



}



void loop()                     // run over and over again

{

  ////////////// shock detection//////////////

  if (digitalRead(SHOCK ) == HIGH) 
  {

    Serial.println('b');

  }



  val = digitalRead(inputPin);

  ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////



  if(val == HIGH)

  {

  lcd.init();              

  lcd.setCursor(0,0);

  lcd.print("Welcome DDL!");

  lcd.setCursor(0,1);

  lcd.print("Put your finger!");

  lcd.backlight();

  Serial.println('a');



   /////////////// fingerprint sensor//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

   getFingerprintIDez();





    if(a>=0 && a<=2)

    {

      digitalWrite(3,LOW);

      digitalWrite(4,LOW);

    }   

  }



  else 

  {         
    Serial.println('c');

    lcd.noBacklight();  // turn on backlight  

    lcd.noDisplay();

  }



}

// // returns -1 if failed, otherwise returns ID #

int getFingerprintIDez() {

  digitalWrite(RELAY,LOW);

  uint8_t p = finger.getImage();

  if (p != FINGERPRINT_OK)  return -1;



  p = finger.image2Tz();

  if (p != FINGERPRINT_OK)  return -1;



  p = finger.fingerFastSearch();

  if (p != FINGERPRINT_OK) {

 a++;



 if(a>=3)

 {

  /////////EM Lock///////////////////////////

  digitalWrite(3,LOW);

  digitalWrite(4,HIGH);

///////////////////////////////////////////////////



  lcd.init();  

  lcd.setCursor(0,0);

  lcd.print("close the door");



  }



 lcd.init();  

 lcd.setCursor(0,0);

 lcd.print("Unsaved        ");

 lcd.setCursor(0,1);

 lcd.print("Fingerprint     ");

  return -1;

  }

  //If you get the fingerprint wrong//



  a=0;//number of times wrong



  lcd.init();  

  lcd.setCursor(0,0);

  lcd.print("Open the door");

  delay(100);

  lcd.setCursor(0,1);

  lcd.print("                ");

  delay(100);

  lcd.setCursor(0,0);

  lcd.print("Welcome DDL!   ");

  lcd.setCursor(0,1);

  lcd.print("Put your finger!");

  // // found a match!

  Serial.print("Found ID #"); Serial.print(finger.fingerID); 

  Serial.print(" with confidence of "); Serial.println(finger.confidence);

   digitalWrite(RELAY,HIGH);

  delay(300);

  return finger.fingerID; 

}

When you do these two, Arduino does the action. But I don't think Raspberry will accept the price of Arduino.

Upload as an execution error to a picture.

Other than this, sometimes SerialFromArduino.inWaiting() > 0 There is also an error in this part, but the error did not appear when I first wrote it... I don't know what's wrong. I ask for your help me.

Is there a conflict between Jimoo and Raspberry Pie in Uart communication?

raspberry-pi python arduino-mega fingerprint-sensor

2022-09-21 17:29

1 Answers

I can't give you an accurate answer because I can't have an environment I'll write a few words as I pass by.

As soon as it appears in the error log

Try it

If that's not the case... The problem is that an error has occurred and it stops, so the I/O (read) part is Try... Why don't you cover it with an exception statement and make an exception.

It's a code that goes around the Raspberry Pi infinite loop and polls the input Even if an exception occurs, I think I can get it again in the next tick? h


2022-09-21 17:29

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.