Python Forum
Thread Rating:
  • 1 Vote(s) - 5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Need help in coding errors
#1
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="";
Reply
#2
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="";
Reply
#3
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
Test everything in a Python shell (iPython, Azure Notebook, etc.)
  • Someone gave you an advice you liked? Test it - maybe the advice was actually bad.
  • Someone gave you an advice you think is bad? Test it before arguing - maybe it was good.
  • You posted a claim that something you did not test works? Be prepared to eat your hat.
Reply
#4
[DELETED]
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020