0

I am doing a project for which I need to make an LED ON when a device connects with Raspberry Pi. Raspberry Pi will act as access point and will provide network to other devices. I found a example on Raspberry Pi forum but this is different. I tried it also (after making some changes) when Raspberry Pi connects with the access point but it didn't work. Please help!

EDIT: It worked now, I was doing wrong first. Now how can I change it to make it work when Raspberry Pi act as access point and turn ON an LED when a device connects with it? I followed this guide to make raspberry pi access point.

I have connected an LED with GPIO17. I wrote a simple Python script and name it wifiLED.py:

#!/bin/env python

import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)
GPIO.setup(17, GPIO.OUT)
GPIO.output(17, True)

I put this script into following bash script wifi_LED.sh. I found my gateway address by $route -n:

#!/bin/bash

# ping the gateway address
/bin/ping -q -c 192.168.0.1 > /dev/null

if [ $? -eq  0 ]
then
  echo "Network active"
  sudo python /home/pi/wifiLED.py  

else
  echo "Network down"
fi

When I do sh wifi_LED.sh, it turn ON LED connected with GPIO17.

2
  • Code? Wiring? Photos?
    – joan
    Commented Jan 26, 2017 at 11:20
  • I edited my question sir.
    – jHz
    Commented Jan 26, 2017 at 11:54

1 Answer 1

2

I know it won't answer your question but I'll give you a hint as I don't know hostapd. Enable logging in hostapd.conf and use the generated log to capture connect events from a script. You might want to run a consumer script like this, for instance:

my-log-analyzer.sh < /var/log/hostapd.log

Assuming hostapd.conf allows you to specify a custom log file path.

A typical pseudo code would look like this:

#!/bin/sh
while read LINE; do
    # Detect connect events in $LINE, which is read from the log
    ...
    # If connect event detected then run script to light the LED
done

As the log file might be really verbose an enhanced version would filter connect events prior to run the log analyser:

grep 'connect' /var/log/hostapd.log | my-log-analyzer.sh

If hostapd allows connect hooks then it's trivial as you'd run a script every time a client connects to the access point.

4
  • Thank you. I also have problem with this guide setup. When I use RPi as AP, I can't connect with the internet anymore. Can you suggest me any other method or guide through which I can solve both problems?
    – jHz
    Commented Jan 26, 2017 at 13:29
  • From where can you not connect to the internet anymore? The Pi or other wireless clients (that are connected to the Pi)? Otherwise here's another guide, much simpler, which takes advantage of dnsmasq a full-fledged DHCP and DNS forwarder.
    – user29510
    Commented Jan 26, 2017 at 13:47
  • Yes the RPi can't connect with my existing network. I also want to use Internet on RPi while it is also providing network to clients.
    – jHz
    Commented Jan 27, 2017 at 2:26
  • «Yes the RPi can't connect with my existing network» — well I'm not sure I follow you now. Can you ping the Pi from a client wirelessly connected to your Raspberry Pi's SSID (aka the wireless network ID that you defined in hostapd.conf)?
    – user29510
    Commented Jan 30, 2017 at 19:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.