def main(): global r0 global roll global yaw global pitch print ("sam") if len(words) > 2: try: pitch = -float(words[1])*grad2rad roll = -float(words[2])*grad2rad except Exception as e: print e if (roll > 1.0): r0 += 1 else: r0 =0 if r0 >=4: r0= 0 r1=0 p1=0 p2=0 ser.flushInput() time.sleep(1) function1() lcd.stringlcd(0xC0,"P:"+str(r0)+str(roll)) time.sleep(1) ser.flushInput()
def function2(): r11 = 0 p11= 0 p12 =0 rfun2 =0 pfun2=0 while (1): line = ser.readline() line = line.replace("#YPR=","") line = line.replace("#YPRAMG=","") # Delete "#YPR=" #f.write(line) # Write to the output log file words = string.split(line[4:],",") if len(words) > 2: try: pfun2 = -float(words[1])*grad2rad rfun2 = -float(words[2])*grad2rad except Exception as e: print e print ("2",rfun2,pfun2,p2) if (rfun2 > 0.5): r11 +=1 else: r11=0 if (pfun2 > 0.5): p11 +=1 else: p11=0 if (pfun2 < -0.5): p12 +=1 else: p12=0 if r11 > 3: r11=0 action2() if (p11 >= 3): p11 = 0 print ("fun3") lcd.stringlcd(0xC0,"Changed to Function3") function3() if (p12 >= 3): p12 = 0 print ("fun1") lcd.stringlcd(0xC0,"Changed to Function1") function1() ser.flushInput() time.sleep(1)
def function5(): r41 = 0 p41= 0 p42 =0 rfun5 =0 pfun5=0 while (1): line = ser.readline() line = line.replace("#YPR=","") line = line.replace("#YPRAMG=","") # Delete "#YPR=" #f.write(line) # Write to the output log file words = string.split(line[4:],",") if len(words) > 2: try: pfun5 = -float(words[1])*grad2rad rfun5 = -float(words[2])*grad2rad except Exception as e: print e print ("5",rfun5,pfun5,p2) if (rfun5 > 0.5): r41 +=1 else: r41=0 if (pfun5 > 0.5): p41 +=1 else: p41=0 if (pfun5 < -0.5): p42 +=1 else: p42=0 if r41 > 3: r41=0 print ("Doctor") gsm.Sendmsg(number,"Doctor") if (p41 >= 3): p41 = 0 print ("fun6") lcd.stringlcd(0xC0,"Changed to Function6") function6() if (p42 >= 3): p42 = 0 print ("fun4") lcd.stringlcd(0xC0,"Changed to Function4") function4() ser.flushInput() time.sleep(1)
def hello(): print 'Hello' lcd.stringlcd(0x80,"WELCOME") st = "<h1>ROBOT CONTROL</h1><br>" st = st + "<br>" + \ "<a href ='/1'>FRONT</a><br>" + \ "<a href ='/2'>BACK</a><br>" + \ "<a href ='/3'>LEFT</a><br>" + \ "<a href ='/4'>RIGHT</a><br>" + \ "<a href ='/5'>STOP</a><br>" + \ "<br></center></h2>"+"<br><br><iframe width='320' height = '240' src='http://" + ipaddr + ":8081'></iframe> " return st
def l1on(): lcd.stringlcd(0x80,"FRONT") GPIO.output(out1,True) GPIO.output(out2,False) GPIO.output(out3,True) GPIO.output(out4,False) st = "<h1>ROBOT CONTROL - FRONT</h1><br>" st = st + "<h2><center><br>" + \ "<a href ='/1'>FRONT</a><br>" + \ "<a href ='/2'>BACK</a><br>" + \ "<a href ='/3'>LEFT</a><br>" + \ "<a href ='/4'>RIGHT</a><br>" + \ "<a href ='/5'>STOP</a><br>" + \ "<br></center></h2>"+"<br><br><iframe width='320' height = '240' src='http://" + ipaddr + ":8081'></iframe> " return st
def sendftp(cs): try: ftpses = ftplib.FTP('ftp.filegenie.com','iotserver','iotserver') file = open('image.jpeg','rb') print('uploading file') ftpses.storbinary("STOR agrisystem/image_" + cs + "_" + str(datetime.datetime.now())+".jpg",file) file.close() ftpses.quit() print('Uploaded') lcd.stringlcd(0x80,"UPLOADED ") time.sleep(5) except: print('unable to upload') lcd.stringlcd(0x80,"UPLOADED FAILED") time.sleep(3)
def action6(): if GPIO.input(act6): GPIO.output(act6, False) gsm.Sendmsg(number, "Medicine") print("act6 Medicine false") os.system('mpg321 Medicine.mp3 &') lcd.lcd_init() lcd.stringlcd(0xC0, "Medicine") ser1.flushInput() elif act6 != 1: GPIO.output(act6, True) gsm.Sendmsg(number, "Medicine") os.system('mpg321 Medicine.mp3 &') lcd.lcd_init() lcd.stringlcd(0xC0, "Medicine") print("act6 Medicine true") ser1.flushInput()
def action5(): if GPIO.input(act5): GPIO.output(act5, False) gsm.Sendmsg(number, "Toilet") os.system('mpg321 Toilet.mp3 &') lcd.lcd_init() lcd.stringlcd(0xC0, "Toilet") print("act5 false") ser1.flushInput() elif act5 != 1: GPIO.output(act5, True) gsm.Sendmsg(number, "Toilet") os.system('mpg321 Toilet.mp3 &') lcd.lcd_init() lcd.stringlcd(0xC0, "Toilet") print("act5 true") ser1.flushInput()
def action2(): if GPIO.input(act2): GPIO.output(act2, False) gsm.Sendmsg(number, "act2 Light OFF") os.system('mpg321 LightOFF.mp3 &') print("act2 Light false") lcd.lcd_init() lcd.stringlcd(0xC0, "LIGHT OFF") ser1.flushInput() elif act2 != 1: GPIO.output(act2, True) gsm.Sendmsg(number, "act2 Light ON") os.system('mpg321 LightON.mp3 &') lcd.lcd_init() lcd.stringlcd(0xC0, "LIGHT ON") print("act2 Light true") ser1.flushInput()
def action1(): if GPIO.input(act1): GPIO.output(act1, False) gsm.Sendmsg(number, "act1 Fan OFF") os.system('mpg321 FanOFF.mp3 &') lcd.lcd_init() lcd.stringlcd(0xC0, "FAN OFF") print("act1 false") ser1.flushInput() DA() elif act1 != 1: GPIO.output(act1, True) gsm.Sendmsg(number, "act1 Fan ON") os.system('mpg321 FanON.mp3 &') lcd.lcd_init() lcd.stringlcd(0xC0, "FAN ON") print("act1 true") ser1.flushInput() DA()
def function6(): r51 = 0 p51= 0 p52 =0 rfun6 =0 pfun6=0 while (1): line = ser.readline() line = line.replace("#YPR=","") line = line.replace("#YPRAMG=","") # Delete "#YPR=" #f.write(line) # Write to the output log file words = string.split(line[4:],",") if len(words) > 2: try: pfun6 = -float(words[1])*grad2rad rfun6 = -float(words[2])*grad2rad except Exception as e: print e print ("6",rfun6,pfun6,p2) if (rfun6 > 0.5): r51 +=1 else: r51=0 if (pfun6 > 0.5): p51 +=1 else: p51=0 if (pfun6 < -0.5): p52 +=1 else: p52=0 if r51 > 3: r51=0 print ("Medicine") gsm.Sendmsg(number,"Medicine") lcd.stringlcd(0xC0,"Medicine") if (p51 >= 3): p51 = 0 print ("fun7") lcd.stringlcd(0xC0,"Changed to Function7") function7() if (p52 >= 3): p52 = 0 print ("fun5") lcd.stringlcd(0xC0,"Changed to Function5") function5() ser.flushInput() time.sleep(1)
def function7(): r61 = 0 p61= 0 p62 =0 rfun7 =0 pfun7=0 while (1): line = ser.readline() line = line.replace("#YPR=","") line = line.replace("#YPRAMG=","") # Delete "#YPR=" #f.write(line) # Write to the output log file words = string.split(line[4:],",") if len(words) > 2: try: pfun7 = -float(words[1])*grad2rad rfun7 = -float(words[2])*grad2rad except Exception as e: print e print ("7",rfun7,pfun7,p2) if (rfun7 > 0.5): r61 +=1 else: r61=0 if (pfun7 > 0.5): p61 +=1 else: p61=0 if (pfun7 < -0.5): p62 +=1 else: p62=0 if r61 > 3: r61=0 print ("Water") lcd.stringlcd(0xC0,"Water") gsm.Sendmsg(number,"Water") if (p61 >= 3): p61 = 0 print ("fun8") lcd.stringlcd(0xC0,"Changed to Function8") function8() if (p62 >= 3): p62 = 0 print ("fun6") lcd.stringlcd(0xC0,"Changed to Function6") function6() ser.flushInput() time.sleep(1)
def function8(): r71 = 0 p71= 0 p72 =0 rfun8 =0 pfun8=0 while (1): line = ser.readline() line = line.replace("#YPR=","") line = line.replace("#YPRAMG=","") # Delete "#YPR=" #f.write(line) # Write to the output log file words = string.split(line[4:],",") if len(words) > 2: try: pfun8 = -float(words[1])*grad2rad rfun8 = -float(words[2])*grad2rad except Exception as e: print e print ("8",rfun8,pfun8,p2) if (rfun8 > 0.5): r71 +=1 else: r71=0 if (pfun8 > 0.5): p71 +=1 else: p71=0 if (pfun8 < -0.5): p72 +=1 else: p72=0 if r71 > 3: r71=0 print ("Food") lcd.stringlcd(0xC0,"FOOD") gsm.Sendmsg(number,"FOOD") if (p71 >= 3): p71 = 0 print ("fun1") lcd.stringlcd(0xC0,"Changed to Function1") function1() if (p72 >= 3): p72 = 0 print ("fun7") lcd.stringlcd(0xC0,"Changed to Function7") function7() ser.flushInput() time.sleep(1)
def Connect_wifi(): while (1): try: lcd.stringlcd(0x80, "Checking Internet") urllib2.urlopen("http://google.com") except urllib2.URLError as e: print(e.reason) print("No Internet connection") lcd.stringlcd(0x80, "No Internet") inf = os.popen( 'sudo cat /etc/wpa_supplicant/wpa_supplicant.conf').read() print inf if ((ssid_name in inf) & (ssid_password in inf)): print 'already found ssid ' else: print 'ssid not registed adding in to network list' os.system( "echo \'network={\' | sudo tee -a /etc/wpa_supplicant/wpa_supplicant.conf" ) os.system( "echo \' ssid=\"" + ssid_name + "\"' | sudo tee -a /etc/wpa_supplicant/wpa_supplicant.conf" ) os.system( "echo \' psk=\"" + ssid_password + "\"' | sudo tee -a /etc/wpa_supplicant/wpa_supplicant.conf" ) os.system( "echo \'}\r\n\' | sudo tee -a /etc/wpa_supplicant/wpa_supplicant.conf" ) #os.system("sudo ifdown wlan0") os.system("sudo ifup wlan0") print("Connecting..") lcd.stringlcd(0x80, "Connecting..") time.sleep(5) else: print("Up and Running") break print("wifi connecting completed") lcd.stringlcd(0x80, "Connected")
def send(to, ms): lcd.stringlcd(0x80, "mail sending") server = smtplib.SMTP('smtp.gmail.com', 587) server.ehlo() server.starttls() server.login("*****@*****.**", "raspberrysudo6324") msg = "\n".join([ "From: [email protected]", "To: " + to, "Subject: Warning.....", "Date: 01/06/2016", ms ]) try: print 'Sending Mail' server.sendmail("*****@*****.**", to, msg) server.quit() print 'Mail Sent' lcd.stringlcd(0x80, "MAIL SENT") except: print 'Mail Sending Failed' lcd.stringlcd(0x80, "Mail Failed")
import RPi.GPIO as GPIO import thread import sys import time import serial import mcp3202 import gsm import datetime import wifi import webpage gps_location = "" ser = serial.Serial('/dev/ttyAMA0', 9600) lcd.lcd_init() lcd.stringlcd(0x80, "welcome") hb = 3 GPIO.setwarnings(False) GPIO.setmode(GPIO.BOARD) GPIO.setup(hb, GPIO.IN, pull_up_down=GPIO.PUD_UP) lcd.stringlcd(0x80, "WELCOME") wifi.Connect_wifi() gsm.Gsminit() def hbcal(): count = 0
def Activities(): p1 = 0 p2 = 0 p3 = 0 p4 = 0 p5 = 0 p6 = 0 p7 = 0 p8 = 0 action3() sleep = 0 while (1): x = ser1.read(1) if (x == '*'): line = ser1.readline() print(line) line = line.replace("#YPR=", "") line = line.replace("#YPRAMG=", "") # Delete "#YPR=" line = line.rstrip() raw_ypr = line.split(",") if len(raw_ypr) > 2: try: raw_y = raw_ypr[0] raw_p = raw_ypr[1] raw_r = raw_ypr[1] except Exception as e: print e #print ('yaw='+str(raw_y)+'\t'+'pitch='+str(raw_p)+'\t'+'raoll='+str(raw_r)) live_data = np.array([raw_p, raw_r]) live_data = live_data.reshape(1, 2) prediction = clf.predict(live_data) print(prediction) clf = joblib.load("filename_model.sav") line = ser1.readline() print(line) line = line.replace("#YPR=", "") line = line.replace("#YPRAMG=", "") # Delete "#YPR=" line = line.rstrip() raw_ypr = line.split(",") if len(raw_ypr) > 2: try: raw_y = raw_ypr[0] raw_p = raw_ypr[1] raw_r = raw_ypr[1] except Exception as e: print e #print ('yaw='+str(raw_y)+'\t'+'pitch='+str(raw_p)+'\t'+'raoll='+str(raw_r)) live_data = np.array([raw_p, raw_r]) live_data = live_data.reshape(1, 2) prediction = clf.predict(live_data) print(prediction) y = ''.join(prediction) #kNNpredict() print(y) if (y == '0'): if (sleep == 0): lcd.stringlcd(0x80, "Normal") else: if (sleep == 0): lcd.stringlcd(0x80, "Position: " + str(y)) time.sleep(1) if (y == '1.0'): p1 += 1 else: p1 = 0 if (y == '2.0'): p2 += 1 else: x2 = 0 ## if(y == '3.0'): ## p3 += 1 ## else: ## x3 = 0 if (y == '4.0'): p4 += 1 else: x4 = 0 if (y == '5.0'): p5 += 1 else: x5 = 0 if (y == '6.0'): p6 += 1 else: x6 = 0 if (y == '7.0'): p7 += 1 else: x7 = 0 if (y == '8.0'): p8 += 1 else: x8 = 0 #################################################### #####______________ACTIVITIES________############### #######_____activity1 if (p1 >= 5): p1 = 0 sleep = 0 action1() lcd.stringlcd(0x80, "Act1") time.sleep(2) ser1.flushInput() if (sleep == 1): continue ########_____activity2 if (p2 >= 5): p2 = 0 sleep = 0 action2() lcd.stringlcd(0x80, "Act2") time.sleep(2) ser1.flushInput() if (sleep == 1): continue ########_____activity3 if (p3 >= 5): p3 = 0 sleep = 0 action3() lcd.stringlcd(0x80, "Act3") time.sleep(2) ser1.flushInput() if (sleep == 1): continue #######_____activity4 if (p4 >= 5): p4 = 0 sleep = 0 action4() lcd.stringlcd(0x80, "Act14") time.sleep(2) ser1.flushInput() if (sleep == 1): continue ########_____activity5 if (p5 >= 5): p5 = 0 sleep = 0 action5() lcd.stringlcd(0x80, "Act5") time.sleep(2) ser1.flushInput() if (sleep == 1): continue ########_____activity6 if (p6 >= 5): p6 = 0 sleep = 0 action6() lcd.stringlcd(0x80, "Act6") time.sleep(2) ser1.flushInput() if (sleep == 1): continue ########_____activity7 if (p7 >= 5): p7 = 0 sleep = 0 action7() lcd.stringlcd(0x80, "Act7") time.sleep(2) ser1.flushInput() if (sleep == 1): continue ########_____activity8 if (p8 >= 5): p8 = 0 sleep = 0 action8() lcd.stringlcd(0x80, "Act8") time.sleep(2) ser1.flushInput() if (sleep == 1): continue
import RPi.GPIO as GPIO import gsm import os import mcp3202 from sklearn.externals import joblib import numpy as np from sklearn.model_selection import train_test_split from sklearn.model_selection import GridSearchCV from sklearn.neural_network import MLPClassifier import pandas as pd ##### INITIALIZATION gsm.Gsminit() lcd.lcd_init() lcd.stringlcd(0x80, "Welcome") time.sleep(1) default_port = '/dev/ttyUSB0' # Check your COM port and baud rate #ser = serial.Serial(port=default_port,baudrate=9600, timeout=1) ser1 = serial.Serial('/dev/ttyUSB0', 9600) number = "" ##GSM NUMBER TO TO THE GURDIAN count = 0 time.sleep(0) sleep = 0 #configure IO ports act1 = 7 act2 = 5
def function1(): r01 = 0 p01= 0 p02 =0 rfun1 =0 pfun1=0 while (1): line = ser.readline() line = line.replace("#YPR=","") line = line.replace("#YPRAMG=","") # Delete "#YPR=" #f.write(line) # Write to the output log file words = string.split(line[4:],",") print("In Function1") if len(words) > 2: try: pfun1 = -float(words[1])*grad2rad rfun1 = -float(words[2])*grad2rad except Exception as e: print e print (rfun1,pfun1,p2) if (rfun1 > 0.5): r01 +=1 else: r01=0 if (pfun1 > 0.5): p01 +=1 else: p01=0 if (pfun1 < -0.5): p02 +=1 else: p02=0 if r01 > 3: r01=0 action1() main() if (p01 >= 3): p01 = 0 print ("fun2") lcd.stringlcd(0xC0,"In Function2") function2() if (p02 >= 3): p02 = 0 print ("fun3") lcd.stringlcd(0xC0,"In Function8") function8() ser.flushInput() time.sleep(1)
import gsm import os import mcp3202 ##### INITIALIZATION gsm.Gsminit() lcd.lcd_init() lcd.stringlcd(0xC0,"welcome") default_port='/dev/ttyUSB0' # #configure IO ports act1=7 act2=5 act3=3 act4=11 GPIO.setup(act1,GPIO.IN,pull_up_down = GPIO.PUD_UP) GPIO.setup(act2,GPIO.IN,pull_up_down = GPIO.PUD_UP) GPIO.setup(act3,GPIO.IN,pull_up_down = GPIO.PUD_UP) GPIO.setup(act4,GPIO.IN,pull_up_down = GPIO.PUD_UP) GPIO.setup(act1,GPIO.OUT) GPIO.setup(act2,GPIO.OUT) GPIO.setup(act3,GPIO.OUT) GPIO.setup(act4,GPIO.OUT)
import datetime import mcp3202 import webpage import serial import thread import gsm import os import camera import ftp import thread from flask import Flask ser = serial.Serial('/dev/ttyS0',9600) os.system("sudo service motion start") lcd.lcd_init() lcd.stringlcd(0x80,"welcome") import socket import fcntl import struct def getip(ifname): s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) return socket.inet_ntoa(fcntl.ioctl( s.fileno(), 0x8915, # SIOCGIFADDR struct.pack('256s', ifname[:15]) )[20:24])