I have a question for Python code. Two actions do not work at the same time. (Sockets, Servers, Vibration Sensors)

Asked 2 years ago, Updated 2 years ago, 58 views

Hello. I use the Python cord for jjamppong, but if one works, one doesn't work... I don't know what the problem is.

I don't really know Python, so I'll look for a lot of things and order jjamppong

When the vibration is detected in Raspberry Pi, I set it up to send a message through twilio that the vibration was detected on my cell phone, and

I made Raspberry Pie a server and sent the data I set "picture" from the client side (cell phone application) to the Raspberry Pi server, so that the camera was taken from Raspberry to the camera module.

If you run it separately once, it works well. If you run it separately twice, it works well But if you order two and Raspberry Pie, If number 1 moves, number 2 doesn't move If No. 2 is working, No. 1 is not working.

Python antiquities, I'd appreciate it if you could tell me what the problem is and how to solve it. Thank you for reading it. I will attach the code below.

I deleted the mobile phone number and token corresponding to the ip and personal information.

This is the source of the blog I referenced by me https://blog.naver.com/PostView.nhn?blogId=cosmosjs&logNo=220805719737&proxyReferer=https:%2F%2Fwww.google.com%2F --> Vibration detection

https://m.blog.naver.com/cosmosjs/220834751822 --> Create a server and communicate with the socket

import RPi.GPIO as GPIO
import time
from twilio.rest import Client
from subprocess import call
import socket
from picamera import PiCamera
from time import sleep
import datetime

account_sid = '***********************'
auth_token = '*********************'
client = Client(account_sid, auth_token)

ip = '172.30.xxx.xxx'
port = 8888
camera = PiCamera()
camera.rotation = 180

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
print('Socket created') 
s.bind((ip, port))
print('Socket bind complete')
s.listen(60)
print('Socket now listening')

def camera_picture(input_string):

    if input_string == "picture":
        dateString = datetime.datetime.now().strftime('%b %d %H:%M:%S')
        saveFileName = datetime.datetime.now().strftime('%y%m%d-%H%M%S') +'.jpg'
        input_string = "Take a picture , " + saveFileName
        camera.start_preview()
        camera.annotate_text = dateString
        sleep(5)
        camera.capture(saveFileName)
        camera.stop_preview()

    elif input_string == "video":
        input_string = "Take a video"
        camera.start_preview()
        camera.start_recording('/home/pi/Desktop/server_video.h264')
        sleep(5)

        camera.stop_recording()
        camera.stop_preview()

    else:
        input_string = input_string + "unknown command"

    return input_string

while True:

    conn, addr = s.accept()
    print("Connected by ", addr)
    data = conn.recv(1024)
    data = data.decode("utf8").strip()

    if not data: break
    print("Received : " + data)
    res = camera_picture(data)
    print("ras pi action : " + res)
    conn.sendall(res.encode("utf-8"))

    conn.close()

s.close()

class Sw420(object):

    def __init__(self, pin, led):
        self.led = led
        GPIO.setmode(GPIO.BCM)
        GPIO.setup(self.led, GPIO.OUT)
        self.pin = pin
        GPIO.setup(self.pin, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
        GPIO.add_event_detect(self.pin, GPIO.RISING, callback=self.callback, bouncetime=1)
        self.count = 0

    def callback(self, pin):
        self.count += 1        
    def LedOn(self):
        GPIO.output(self.led, 1)
        print("Vibration Detected")        
    def LedOff(self):
        GPIO.output(self.led, 0)
        print("NOT Vibration Detected")  

def main():

    sensor = Sw420(17,16)

    try:
        while True:
            time.sleep(1)
            if sensor.count >= 30:
                sensor.LedOn()
                message = client.messages\
                                .create(
                                    body="Vibration Detected from raspi",
                                    from_='+1212341234',
                                    to='+821012341234'
                                )
                print(message.sid)
            else:
                sensor.LedOff()               
            sensor.count = 0

    except KetboardInterrupt:
        GPIO.cleanup()

if __name__ == '__main__':
    main()

Masters, thank you for reading this.

python function

2022-09-20 22:26

1 Answers

It's my personal opinion

Based on the code written in the socket accept waiting and the vibration detection while statement, it seems natural that the problem will occur.

Raise the vibration detection part to thread and send an alarm when detecting vibration

In the main thread part, it seems better to wait with accept.


2022-09-20 22:26

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.