Jump to content

I've been having trouble with a python project I've been working on. I got my pi to turn on and off the GPIO pins from a webpage following a guide but now I'm trying to get it to work with PWM controls. I'm pretty new to programming and I don't know a how all of this program works so I tried to change it as little as i could to change PWM % instead of turning it on and off. When i try to run the code this error comes up 

 

 

AttributeError: 'module' object has no attribute 'setmode'

I've tried updating everything and I have the newest software.

This is my code

 

from flask import Flask, render_template

import datetime
import RPi.GPIO as GPIO
from time import sleep
app = Flask(__name__)
GPIO.setmode(GPIO.BCM) #line that causes problems
 
GPIO.setup(7, GPIO.OUT)
GPIO.setup(11, GPIO.OUT)
GPIO.setup(12, GPIO.OUT)
GPIO.setup(13, GPIO.OUT)
GPIO.setup(15, GPIO.OUT)
GPIO.setup(16, GPIO.OUT)
GPIO.setup(18, GPIO.OUT)
GPIO.setup(22, GPIO.OUT)
seven=GPIO.PWM(7, 100)
eleven=GPIO.PWM(11, 100)
twelve=GPIO.PWM(12, 100)
thirteenGPIO.PWM(13, 100)
fifteen=GPIO.PWM(15, 100)
sixteen=GPIO.PWM(16, 100)
eighteen=GPIO.PWM(18, 100)
twentytwo=GPIO.PWM(22,100)
seven.start(0)
eleven.start(0)
twelve.start(0)
thirteen.start(0)
fifteen.start(0)
sixteen.start(0)
eighteen.start(0)
twentytwo.start(0)
 
now = datetime.datetime.now()
timeString = now.strftime("%Y-%m-%d %H:%M")
 
@app.route("/")
def hello():
   templateData = {
      'title' : 'RPi GPIO Control',
      'time': timeString
      }
   return render_template('gpioweb.html', **templateData)
 
@app.route("/4/on")
def action4on():
    seven.ChangeDutyCycle(50)
    message = "GPIO 4 was Set to 50%."
    templateData = {
        'message' : message,
        'time' : timeString
    }
    return render_template('gpioweb.html', **templateData)
    
@app.route("/4/off")
def action4off():
    seven.ChangeDutyCycle(10)
    message = "GPIO 4 was set to 10."
    templateData = {
        'message' : message,
        'time' : timeString
    }
    return render_template('gpioweb.html', **templateData)
 
@app.route("/17/on")
def action17on():
    GPIO.output(11, True)
    message = "GPIO 17 was turned on."
    templateData = {
        'message' : message,
        'time' : timeString
    }
    return render_template('gpioweb.html', **templateData)
 
@app.route("/17/off")
def action17off():
    GPIO.output(11, False)
    message = "GPIO 17 was turned off."
    templateData = {
        'message' : message,
        'time' : timeString
    }
    return render_template('gpioweb.html', **templateData)
 
@app.route("/18/on")
def action18on():
    GPIO.output(12, True)
    message = "GPIO 18 was turned on."
    templateData = {
        'message' : message,
        'time' : timeString
    }
    return render_template('gpioweb.html', **templateData)
 
@app.route("/18/off")
def action18off():
    GPIO.output(12, False)
    message = "GPIO 18 was turned off."
    templateData = {
        'message' : message,
        'time' : timeString
    }
    return render_template('gpioweb.html', **templateData)
 
@app.route("/21/on")
def action21on():
    GPIO.output(13, True)
    message = "GPIO 21 was turned on."
    templateData = {
        'message' : message,
        'time' : timeString
    }
    return render_template('gpioweb.html', **templateData)
 
@app.route("/21/off")
def action21off():
    GPIO.output(13, False)
    message = "GPIO 21 was turned off."
    templateData = {
        'message' : message,
        'time' : timeString
    }
    return render_template('gpioweb.html', **templateData)
 
@app.route("/22/on")
def action22on():
    GPIO.output(15, True)
    message = "GPIO 22 was turned on."
    templateData = {
        'message' : message,
        'time' : timeString
    }
    return render_template('gpioweb.html', **templateData)
 
@app.route("/22/off")
def action22off():
    GPIO.output(15, False)
    message = "GPIO 22 was turned off."
    templateData = {
        'message' : message,
        'time' : timeString
    }
    return render_template('gpioweb.html', **templateData)
 
@app.route("/23/on")
def action23on():
    GPIO.output(16, True)
    message = "GPIO 23 was turned on."
    templateData = {
        'message' : message,
        'time' : timeString
    }
    return render_template('gpioweb.html', **templateData)
 
@app.route("/23/off")
def action23off():
    GPIO.output(16, False)
    message = "GPIO 23 was turned off."
    templateData = {
        'message' : message,
        'time' : timeString
    }
    return render_template('gpioweb.html', **templateData)
 
@app.route("/24/on")
def action24on():
    GPIO.output(18, True)
    message = "GPIO 24 was turned on."
    templateData = {
        'message' : message,
        'time' : timeString
    }
    return render_template('gpioweb.html', **templateData)
 
@app.route("/24/off")
def action24off():
    GPIO.output(18, False)
    message = "GPIO 24 was turned off."
    templateData = {
        'message' : message,
        'time' : timeString
    }
    return render_template('gpioweb.html', **templateData)
 
@app.route("/25/on")
def action25on():
    GPIO.output(22, True)
    message = "GPIO 25 was turned on."
    templateData = {
        'message' : message,
        'time' : timeString
    }
    return render_template('gpioweb.html', **templateData)
 
@app.route("/25/off")
def action25off():
    GPIO.output(22, False)
    message = "GPIO 25 was turned off."
    templateData = {
        'message' : message,
        'time' : timeString
    }
    return render_template('gpioweb.html', **templateData)
 
@app.route("/all/on")
def actionallon():
    GPIO.output(7, True)
    GPIO.output(11, True)
    GPIO.output(12, True)
    GPIO.output(13, True)
    GPIO.output(15, True)
    GPIO.output(16, True)
    GPIO.output(18, True)
    GPIO.output(22, True)
    message = "All GPIO pins were turned on."
    templateData = {
        'message' : message,
        'time' : timeString
    }
    return render_template('gpioweb.html', **templateData)
 
@app.route("/all/off")
def actionalloff():
    GPIO.output(7, False)
    GPIO.output(11, False)
    GPIO.output(12, False)
    GPIO.output(13, False)
    GPIO.output(15, False)
    GPIO.output(16, False)
    GPIO.output(18, False)
    GPIO.output(22, False)
    message = "All GPIO pins were turned off."
    templateData = {
        'message' : message,
        'time' : timeString
    }
    return render_template('gpioweb.html', **templateData)
 
if __name__ == "__main__":
   app.run(host='0.0.0.0', port=80, debug=True)

Any help would be appreciated 

CPU: Intel i5 2310 Mobo: MSI Z77A-G41 GPU: MSI 760 Twin Frozr 4GB RAM: 8GB ADATA XPG Gaming Series 


Storage: OCZ Aglility 3 120 GB SSD, 2 250GB Segate HDDs Case: Cooler Master HAF 912 PSU: Antec NEO ECO 520W


 

Link to comment
https://linustechtips.com/topic/230788-noob-raspberry-pi-python-errors/
Share on other sites

Link to post
Share on other sites

You most likely are using an old library version..

CPU: i7 4770k | GPU: Sapphire 290 Tri-X OC | RAM: Corsair Vengeance LP 2x8GB | MTB: GA-Z87X-UD5HCOOLER: Noctua NH-D14 | PSU: Corsair 760i | CASE: Corsair 550D | DISPLAY:  BenQ XL2420TE


Firestrike scores - Graphics: 10781 Physics: 9448 Combined: 4289


"Nvidia, Fuck you" - Linus Torvald

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×