Sunfounder Sensor Kit V2.0 for Raspberry PI + Golang: Dual Color LED

Today, I want to start a small series of articles about the Sensor Kit V2.0 provided by www.sunfounder.com. I am planning to use Golang on a Raspberry Pi to interact with the sensors and outputs. As a Golang Library I am using periph.io. I am trying to find interesting use cases for each sensor. The goal of this project is to have a proof of concept for as many sensors as possible.

The idea of today’s project is to switch the Dual-Color LED using a simple HTML form. There are 3 buttons: “Red”, “Green” and off. Each button changes the state of the Dual-Color LED. Maybe the beginning of a new Smart Home?

The code of this example is hosted at github:
https://github.com/lnitram/go-sunfouder-sensor-kit/tree/master/cmd/light-app

The files might change over time whenever I add improvements, but the concept will stay the same as described in this article.

The app is built out of the following parts:

main.go

  • Interacts with the hardware using periph.io
  • Provides a fileserver for the html/js/css in the ./static/ folder
  • Provided 3 API endpoints (red, green, off) that change the states of the LED

static/index.html
Simple website with 3 Buttons: Red, Green, Off

static/js/main.js
Javascript to call the API when a button is clicked

static/css/main.css
Some styles (still quite ugly)

That’s all. Once the app is started, the website can be reached at http://ip-of-raspberry:8080/

Now the LED can be switched with a browser.

Leave a Reply

Your email address will not be published.