Advertisement
Guest User

Untitled

a guest
Dec 12th, 2019
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.19 KB | None | 0 0
  1. import RPi.GPIO as GPIO
  2. import time
  3. import datetime
  4. import socket
  5.  
  6. # Create a TCP/IP socket
  7.  
  8. GPIO.setmode(GPIO.BCM) #Set GPIO to pin numbering
  9. pir = 18 #Assign pin 8 to PIR
  10.  
  11. GPIO.setup(pir, GPIO.IN) #Setup GPIO pin PIR as input
  12.  
  13. print ("Sensor initializing . . .")
  14. time.sleep(2) #Give sensor time to startup
  15. print ("Active")
  16. print ("Press Ctrl+c to end program")
  17.  
  18. IOT_Client="Motion detected at "
  19. if(pir == False):
  20. print("Fel")
  21.  
  22. while True:
  23. if GPIO.input(pir) == True: #If PIR pin goes high, motion is detected
  24. print("pir")
  25. now = datetime.datetime.now()
  26. fb = open("/home/pi/Desktop/IOT_Client.txt", "a+")
  27. #print(now.strftime("%Y-%m-%d %H:%M:%S"))
  28. print(2)
  29. fb.write(IOT_Client)
  30. fb.write(now.strftime("%Y-%m-%d %H:%M:%S"))
  31. fb.write("\n")
  32. fb.close()
  33. s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  34. s.connect(('172.16.0.20', 13000))
  35. s.send(IOT_Client)
  36. s.send(now.strftime("%Y-%m-%d %H:%M:%S"))
  37. s.send("\n")
  38. s.close()
  39. #time.sleep(2) #Keep LED on for 2 seconds
  40. #time.sleep(0.1)
  41. print(s)
  42. GPIO.cleanup() #reset all GPIO
  43. print ("Program ended")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement