The 2020 Raspbian distribution for the AVNET SmartEdge IIOT Gateway uses systemd services to manage the hardware customisations.

In this series, I review what they do, how they interact.

Part 2: the led service, part of the functionality to control the front LEDs.

 

 

ledservice

 

The SmartEdge has 3* custom LEDs.  They are attacked from several angles.

From the reboot, watchdog, IoTConnect and led service.

From the onboard ATTiny , and maybe from some other places that I find during my investigation.

But ledservice itself is the heart to keep the LEDs under control, and initially flags that your new SmartEdge hasn't been WiFi-provisioned yet.

 

*It looks like there are 2 LEDs, but the lower one is a green and orange LED in a single housing. The top LED is a common red one.
The lower orange LED is not addressable. It's connected to a pin that's set up to report board activity. Check the overlay definition for that.

 

Here's the service registry content:

 

# ...
[Service]
ExecStart=/bin/bash /opt/avnet-iot/iotservices/led   
ExecStop=/bin/echo 0 >/sys/class/leds/green/brightness
Restart=always
RestartSec=60
[Install]
WantedBy=default.target

 

This script has an action when it's started and when the service stops.

 

When the service is stopped, it dims the lower green LED:

 

ExecStop=/bin/echo 0 >/sys/class/leds/green/brightness

 

When it starts, it executes a shell script .

Startup script:

 

/opt/avnet-iot/iotservices/led

 

Content:

 

# ...
cd /opt/avnet-iot/iotservices

# ...
echo "Starting Smartedge-iiot-gateway ConfigurationMode LED service"
#wait for device
while [ ! -d /sys/class/leds/green ];
do
    sleep 2
    echo "waiting for green brightness system device"
done
# wait for device
while [ ! -d /sys/class/leds/red ];
do
    sleep 2
    echo "waiting for red brightness system device"
done
# wait for device
while [ ! -d /sys/class/leds/smartedge_led_duty ];
do
    sleep 2
    echo "waiting for smartedge_led_duty system device"
done

/bin/bash /opt/avnet-iot/iotservices/stopwd
python -u /opt/avnet-iot/iotservices/led.py >/dev/null
echo "Led python restart" >>/var/log/led.log

 

It waits for Linux to load the green led and the red-led-brightness-and-flasher interfaces.

I will go into the red led's functionality later. For now, it's enough to know that you can set its brightness and blink rate.

At that point, the watchdog is stopped (exercise for the reader that can't wait until the watchdog blog is written: check the stopwd script in the same directory).

Then, the python script to manage the LEDs is called.

 

python script:

 

/opt/avnet-iot/iotservices/led.py

 

Content:

 

#...

ApMode = 0
GreenThisTime = 0
RedThisTime = 0


GREEN_LED ='/sys/class/leds/green/brightness'
RED_LED = '/sys/class/leds/red/brightness'

# ...

def green_led_on():
    os.system('echo 1 > /sys/class/leds/green/brightness')
    print("GON")
    sleep(0.2)

def green_led_off():
    os.system('echo 0 > /sys/class/leds/green/brightness')
    print("GOF")
    sleep(0.2)

def red_led_on():
    os.system('echo 1 > /sys/class/leds/red/brightness')
    print("RON")
    sleep(0.2)

def red_led_off():
    os.system('echo 0 > /sys/class/leds/red/brightness')
    print("ROF")
    sleep(0.2)

def get_ap_mode():
    global ApMode
    try:
        hostapd = subprocess.call(['systemctl', 'is-active', 'hostapd.service'], stdout=None , stderr=None)
        if hostapd == 0:
            ApMode = 1
        else:
            ApMode = 0
    except Exception as ex:
        print(ex)
        ApMode = 0
    print("AP status" + str(ApMode))
    return ApMode

def check_switch():
    global GreenThisTime
    global RedThisTime
    global ApMode
    GreenFirst = 1
    time.sleep(3)
    try:
        while 1:
            get_ap_mode()
            time.sleep(1)
            if (ApMode == 1):
                if RedThisTime == 1:
                    RedThisTime = 0
                    red_led_off()
                    green_led_on()
                else:
                    RedThisTime = 1
                    green_led_off()
                    red_led_on()
            else:
                 if(GreenFirst == 1):
                    red_led_off()
                    GreenFirst == 0
                if GreenThisTime == 1:
                    GreenThisTime = 0
                    green_led_on()
                else:
                    GreenThisTime = 1
                    green_led_off()
    except Exception as ex:
        print(ex)

if __name__ == '__main__':
    print("Starting LED service")
    ApMode = 1
    red_led_off()
    green_led_on()
    GreenThisTime = 1
    t2 = threading.Thread(name='child procs', target=check_switch)
    t2.start()
    while 1:
        time.sleep(60*60)
    red_led_off()
    green_led_off()

 

This is a little state machine in python. It checks access point (WiFi) status, and based on that entertains you with differently flashing LEDs.

A little guide to follow along:  hostapd == 0 indicates that the linux hostapd.service is active.

That happens when you have a fresh kit that hasn't been provisioned yet. It acts as an access point until you connect it to your router via the IoTConnect app.

 

Side effects:

It stops the watchdog

 

Related Blog
Industrial I/O
Use the Industrial I/O
Control Industrial I/O directly from GPIO Pins
C Program for the Industrial I/O
Control Industrial I/O directly from Node-RED
Display Industrial I/O on Node-RED Dashboard
CAN Bus
Use the Isolated CAN
CAN programming in C on Linux: Filter and Mask
2020 Linux Image
The 2020 Linux Image and IoTConnect Scripts
Understand the Custom Scripts and Services (Pt. 1: intro and reset button service)
Understand the Custom Scripts and Services (Pt. 2: led service)
Understand the Custom Scripts and Services (Pt. 3: IoT Connect service)