1

I'm building a security camera system for my home out of a number of raspberry pi's. One of the things each camera system has will be a light to show a internet connection is present. For this I've used the code found at python check to see if host is connected to network and modified it to.

#! /usr/bin/python

import socket
import fcntl
import struct
import RPi.GPIO as GPIO
import time
pinNum = 8
GPIO.setmode(GPIO.BCM) #numbering scheme that corresponds to breakout board and pin layout
GPIO.setup(pinNum,GPIO.OUT) #replace pinNum with whatever pin you used, this sets up that pin as an output
#set LED to flash forever

def check_connection():

    ifaces = ['eth0','wlan0']
    connected = []

    i = 0
    for ifname in ifaces:

        s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
        try:
            socket.inet_ntoa(fcntl.ioctl(
                    s.fileno(),
                    0x8915,  # SIOCGIFADDR
                    struct.pack('256s', ifname[:15])
            )[20:24])
            connected.append(ifname)
            print "%s is connected" % ifname 
            while True:
                GPIO.output(pinNum,GPIO.HIGH)
                time.sleep(0.5)
                GPIO.output(pinNum,GPIO.LOW)
                time.sleep(0.5)
                GPIO.output(pinNum,GPIO.LOW)
                time.sleep(2.0)

        except:
            print "%s is not connected" % ifname

        i += 1

    return connected

connected_ifaces = check_connection()

However when I run the code through my Pi the error reads:

pi@raspberrypi ~/Desktop $     while True:
>               ^
> TabError: inconsistent use of tabs and spaces in indentation
> ^C

Anyone know the issue? Apologies if it's a basic one, I'm new to Python Programming. In short my hope is that when an internet connection is present the light on pin 8 will turn on.

Thanks!

Community
  • 1
  • 1
Media Street
  • 11
  • 1
  • 3
  • It looks like you are mixing tabs and spaces for indentation. Do not do that either use tabs or (better) spaces for indentation. Try to run the code using `python -tt` – jfs Apr 17 '14 at 21:21
  • That 'python -tt' is good, shows me what you said 'TabError: inconsistent use of tabs and spaces in indentation' . i don't see what's wrong with the code though. It is using tabs all the way now. I've updated my code above. The error I'm getting is for the 'while true;' line. – Media Street Apr 17 '14 at 21:29
  • *"i don't see what's wrong with the code"* -- don't mix tab and spaces for indentation: it is a part of the syntax in Python. I see that the code in your question uses both spaces and tabs for indentation – jfs Apr 17 '14 at 21:58
  • @MediaStreet can you edit your answer to show the new error you mentioned? – Winny Apr 18 '14 at 09:18
  • Hi @Winny sorry for the delay. If you go to https://www.dropbox.com/sh/fxa3vluhdopac29/GCbQYF15fs you can view the Py file and error I'm getting. Thanks in advance! – Media Street Apr 18 '14 at 23:19

1 Answers1

0

Python is very picky about indentation as it uses the indentation to group code blocks. It seems that the code is using 4 spaces (or a tab) for indentation everywhere except for line 30 through 35 where it is only two spaces. Make sure the indentation is consistent throughout your code and it should solve your problem.

Updated: With your new error, make sure that you're consistent with whether you're using spaces or tabs. Go through each line of the code and either use 4 spaces per tab, or make sure it's using tabs to reach the correct indentation.

More information on fixing indentation: How to fix python indentation Also check this out: http://www.annedawson.net/Python_Spaces_Indentation.html

Try googling for more information on indentation and Python as you fix up this code.

Community
  • 1
  • 1
Tyler Ferraro
  • 3,753
  • 1
  • 21
  • 28
  • Thanks @Tyler Ferraro - I updated my question above, your solution has kind of worked, it shows a different error now. – Media Street Apr 17 '14 at 21:19