1
\$\begingroup\$

I am trying to interface the PSOC5 LP097 with PIR sensor to trigger the stepper motor for my Pet Feeder System. In addition, everytime the PIR sensor detects a movement, I try to send a mail using PySerial.

The problem I am currently facing is that the output of PIR sensor is always high when I try to read it via software. I tried to connect a LED to the PIR sensor output and it gets activated only when there is a movement in front of sensor. However, I do not understand why the software reads it as HIGH always and keeps triggering the stepper motor

Below is my code:

#define MaxCountForTurns 300 //count for 2 rotations of the motor

int main(void)
{
CyGlobalIntEnable; /* Enable global interrupts. */


uint16 onCount = 0x00;
uint16 offCount = 0x00;
uint8 gateClose = 0x00;
uint8 motorON = 0x00;
uint8 pirValue = 0x00;


UART_Start(); 

for(;;)
{
    if (gateClose == 0x00) {
        pirValue |=  pirInput_Read();           
    }

    if ((pirValue & 0x01) && (motorON == 0)) {
        motorON = 0x01;
        UART_PutString("Pet movement detected : Motor will be turned ON\n");
        CyDelay(10); 
    }
    if(motorON) {
        onCount++;
        Control_Reg_Write(0x0c);
        CyDelay(10);          
        Control_Reg_Write(0x06);
        CyDelay(10);           
        Control_Reg_Write(0x03);
        CyDelay(10);          
        Control_Reg_Write(0x09);
        CyDelay(10);            
     }   

        if (gateClose) {
            offCount++;
            Control_Reg_Write(0x09);
            CyDelay(10);          
            Control_Reg_Write(0x03);
            CyDelay(10);           
            Control_Reg_Write(0x06);
            CyDelay(10);          
            Control_Reg_Write(0x0c);
            CyDelay(10);                
        }

        // turns in fwd direction
        if(onCount > MaxCountForTurns) {
            motorON = 0x00;
            onCount = 0x00;
            pirValue = 0x00;
            CyDelay(50);
            gateClose = 0x01;
        }

        // turns in reverse direction
        if(offCount > MaxCountForTurns) {                
            gateClose &= 0x00;
        }
    }        
}

The python code for sending the mail:

    import time
    import serial
    import smtplib
    TO = '[email protected]'
    GMAIL_USER = '[email protected]'
    GMAIL_PASS = 'psoc_pir_pet_feeder'
    SUBJECT = 'Pet Feeder System Alert!!'
    TEXT = 'PIR sensor detected pet movement, Motor will be turned ON'
    ser = serial.Serial('COM2', 9600)
    def send_email():
        print("Sending Email")
        smtpserver = smtplib.SMTP("smtp.gmail.com",587)
        smtpserver.ehlo()
        smtpserver.starttls()
        smtpserver.ehlo
        smtpserver.login(GMAIL_USER, GMAIL_PASS)
        header = 'To:' + TO + '\n' + 'From: ' + GMAIL_USER
        header = header + '\n' + 'Subject:' + SUBJECT + '\n'
        print header
        msg = header + '\n' + TEXT + ' \n\n'
        smtpserver.sendmail(GMAIL_USER, TO, msg)
        smtpserver.close()

    while True:
        message = ser.readline()
        print(message)
        if message[0] == 'P' :
            send_email()
        time.sleep(0.5)

I have tried doing the same project with arduino UNO and it worked perfectly. However here I see the following observations:

  1. The motor turns in fwd direction once and reverse direction once.

  2. Later on the motor keeps turning in fwd direction.

  3. on PySerial :Pet movement detected : Motor will be turned ON message is displayed continuously. The mail is not being sent to the email id.

I realise that the PIR read which I try to do is not correct. However I dont seem to realise how to do it efficiently. Second question is related to the pyserial- I assume the serial port is busy with the response received from PSOC5 and hence does not proceed to sending an email to the specified address. Could anyone of you help?

Edit: I have fixed the motor turn issues, a simple bug in the implementation related to the offCount. However I am still stuck on the pyserial part of the project. There is no email being sent to the email address. Suggestions are highly appreciated.

\$\endgroup\$
2
  • \$\begingroup\$ I don't know that "|=" is a valid operator. Try this: pirValue = (pirInput_Read() & 0x01); ....of course the return from pirInput_Read should already be constrained and typed if it is required. \$\endgroup\$ Mar 19, 2017 at 15:16
  • \$\begingroup\$ @JackCreasey- tried doing it, however still the motor keeps rotating continuosly. \$\endgroup\$
    – gst
    Mar 19, 2017 at 16:20

1 Answer 1

1
\$\begingroup\$

Instead of reading from the PSOC, I'd read from a simple digital port and put a switch on it. Then you can debug your code before finally introducing the PSOC register reads.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.