#off-topic jarvis (chatbot) using python &mit app inventor app

hello guys, today I am going to share my recent project that is chatbot.you will get some clear idea what I am talking about after watching this video.I think this is off topic but this will be helpful to others.but we can also integrate Blynk to make it more advanced & powerful.for this project, I have spent 2-3 days & sleepless nights.
I have made simple python code for the server which will handle all the clients & send response back to the app. the app is made in MIT app inventor.



server.py

import socket
import sys
import RPi.GPIO as GPIO
from thread import *
import datetime
import random

GPIO.setmode(GPIO.BOARD)
GPIO.setup(11,GPIO.OUT)

host = ''
port = 8220
address = (host, port)

server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_socket.bind(address)
server_socket.listen(5)
##############################################################################
#small database of our bot
greetings = ['hola', 'hello', 'hi', 'hey']
questions = ['how are you', 'how are you doing']
responses = ['okay', 'i am fine']
database={
  'jarvis':'hello,sir how can i help you',
  'name':'jarvis',
  'what is your name':'my name is jarvis',
  'hello jarvis':'hello,sir how can i help you',
  'what can you do for me':'i can do many things..'
}

print "Listening for client . . ."
###############################################################################
#chatbot code here
def chatboat(data):
  if data in database:
  print(database[data])
  conn.send(database[data])
  elif data in questions:
  random_response = random.choice(responses)
  print(random_response)
  conn.send(random_response)
  elif data in greetings:
  random_greeting = random.choice(greetings)
  print(random_greeting)
  conn.send(random_greeting)
  elif 'light on'in data:
  conn.send("light turn on ")
  GPIO.output(11,True)
  elif 'light of' in data:
  conn.send("light turn off")
  GPIO.output(11,False)
  elif 'time' in data:
  now = datetime.datetime.now()
  time=str(now.hour)+str("hours")+str(now.minute)+str("minutes")
  print(time)
  conn.send(time)
  elif 'date'in data:
  now = datetime.datetime.now()
  date=str("%s/%s/%s" % (now.day, now.month,now.year))
  print(date)
  conn.send(date)
  else:
  conn.send("sorry please repeat..")
##############################################################################
#server code here
def clientthread(conn):
#infinite loop so that function do not terminate and thread do not end.
  while True:
  output = conn.recv(2048);
  if output.strip() == "disconnect":
  conn.close()
  sys.exit("Received disconnect message. Shutting down.")
  conn.send("dack")
  elif output:
  print "Message received from client:"
  print output
  data=str(output).lower()
  print data
  chatboat(data)
  
while True:

#Accepting incoming connections
  conn, address = server_socket.accept()
  print "Connected to client at ", address
#Creating new thread. Calling clientthread function for this function and passing conn as argument.
  start_new_thread(clientthread,(conn,)) #start new thread takes 1st argument as a function name to be run, second is the tuple of arguments to the function.
 
conn.close()
sock.close() 

we can also trigger google voice recognizer in the app from server side by sending strings containing. ..
we can also control Blynk devices by using blynk API.
& using request module python.import requests
applink–https://drive.google.com/open?id=0B_7VHGHWspGcMTNBOGRiUDE0d0E
app aia file–https://drive.google.com/open?id=0B_7VHGHWspGceHpCTFdNaUN4bDQ
I am waiting for Blynk voice control widget.
guys, I need some idea &help.I am not a coder.I don’t know is this right way to do this.

3 Likes

Nice project!
I think, benefits of Blynk are much more clear now! Ha :wink:

1 Like

waiting for speech recognition widget…