Python Forum
Need help in coding errors - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Need help in coding errors (/thread-3096.html)



Need help in coding errors - Shubham125211 - Apr-28-2017

Below is the given code in which i need u guys help, plz tell me the logical and syntax errors of the following code
This program is for raspberry pi 3 for switching on lights through voice command, the voice command will be given by amr voice recognition app through Bluetooth to an external Bluetooth module connected to raspberry pi gpio pins.... Plz help me guys thnxs for ur support in advance
As the compliation is showing errors, so i would advice to complie it first  Shy


import serial
import RPi.GPIO as GPIO      
import os, time
led1=17
led2=27
led3=22
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)
GPIO.setup(led1, GPIO.OUT)
GPIO.setup(led2, GPIO.OUT)
GPIO.setup(led3, GPIO.OUT)
GPIO.output(led1 , 0)
GPIO.output(led2 , 0)
GPIO.output(led3 , 0)
Serial = serial.Serial("/dev/ttyS0", baudrate=9600, timeout=2)
data1=""
data=''
while 1:
  while data != '#':
    data = Serial.read(1)
    data1+=data
  print (data1)
  if data1.find("blue light on")>0:
      GPIO.output(led1 , 1)
      print ("Blue Light on")
  if data1.find("blue light off")>0:
      GPIO.output(led1 , 0)
      print ("Blue Light Off")
  if data1.find("red light on")>0:
      GPIO.output(led2 , 1)
      print ("Red Light on")
  if data1.find("red light off")>0:
      GPIO.output(led2 , 0)
      print ("red Light Off")
  if data1.find("green light on")>0:
      GPIO.output(led3 , 1)
      print ("Green Light on")
  if data1.find("green light off")>0:
      GPIO.output(led3 , 0)
      print ("Green Light Off")
  if data1.find("all lights on")>0:
      GPIO.output(led1 , 1)
      GPIO.output(led2 , 1)
      GPIO.output(led3 , 1)
      print ("All Lights on")
  if data1.find("all lights off")>0:
      GPIO.output(led1 , 0)
      GPIO.output(led2 , 0)
      GPIO.output(led3 , 0)
      print ("All Light Off")
  if data1.find("blink")>0:
      for k in range (100):
        GPIO.output(led1 , 1)
        GPIO.output(led2 , 1)
        GPIO.output(led3 , 1)
        time.sleep(0.1)
        GPIO.output(led1 , 0)
        GPIO.output(led2 , 0)
        GPIO.output(led3 , 0)
        time.sleep(0.1)
  
  Serial.flush();
  data="";
  data1="";



Need help for finding errors - Shubham125211 - Apr-28-2017

Need help to solve errors in the below program arriving after compilation, syntax errors and logical errors are also there
This program is for operation of led by voice command given through mobile to pi through Bluetooth.. In pi end external Bluetooth adapter is ued while in mobile for voice recognition amr voice recognition app is used. 



import serial
import RPi.GPIO as GPIO      
import os, time
led1=17
led2=27
led3=22
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)
GPIO.setup(led1, GPIO.OUT)
GPIO.setup(led2, GPIO.OUT)
GPIO.setup(led3, GPIO.OUT)
GPIO.output(led1 , 0)
GPIO.output(led2 , 0)
GPIO.output(led3 , 0)
Serial = serial.Serial("/dev/ttyS0", baudrate=9600, timeout=2)
data1=""
data=''
while 1:
  while data != '#':
    data = Serial.read(1)
    data1+=data
  print (data1)
  if data1.find("blue light on")>0:
      GPIO.output(led1 , 1)
      print ("Blue Light on")
  if data1.find("blue light off")>0:
      GPIO.output(led1 , 0)
      print ("Blue Light Off")
  if data1.find("red light on")>0:
      GPIO.output(led2 , 1)
      print ("Red Light on")
  if data1.find("red light off")>0:
      GPIO.output(led2 , 0)
      print ("red Light Off")
  if data1.find("green light on")>0:
      GPIO.output(led3 , 1)
      print ("Green Light on")
  if data1.find("green light off")>0:
      GPIO.output(led3 , 0)
      print ("Green Light Off")
  if data1.find("all lights on")>0:
      GPIO.output(led1 , 1)
      GPIO.output(led2 , 1)
      GPIO.output(led3 , 1)
      print ("All Lights on")
  if data1.find("all lights off")>0:
      GPIO.output(led1 , 0)
      GPIO.output(led2 , 0)
      GPIO.output(led3 , 0)
      print ("All Light Off")
  if data1.find("blink")>0:
      for k in range (100):
        GPIO.output(led1 , 1)
        GPIO.output(led2 , 1)
        GPIO.output(led3 , 1)
        time.sleep(0.1)
        GPIO.output(led1 , 0)
        GPIO.output(led2 , 0)
        GPIO.output(led3 , 0)
        time.sleep(0.1)
  
  Serial.flush();
  data="";
  data1="";



RE: Need help for finding errors - volcano63 - Apr-28-2017

Could you please show the errors?

I will hazard a guess and presume that one of them looks like that
In [2]: a = ''

In [3]: a += b'a'
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-3-13f74cfaaae2> in <module>()
----> 1 a += b'a'

TypeError: Can't convert 'bytes' object to str implicitly
Communication interfaces usually yield byte objects - and you are adding to string data1 the value returned by Serial.read()

Here's one more thing to consider - "identical" byte and string objects are not
In [6]: b'#' == '#'
Out[6]: False



RE: Need help for finding errors - idontreallywolf - Apr-28-2017

[DELETED]