Hack N Roll 2018

> 3 min read

It’s been a while since I last joined a hackathon, and the one I attended recently is something familiar: Hack N Roll 2018! Held in a different location @ Cinnamon-Tembusu Dining Hall instead (the past few times I attended was held at the NUS School of Computing), there were much more people and (unfortunately) less location of where you could do your work. (I prefer the previous location instead of the new one, where it’s much more spacious and need not compete for the power points/plugs.) With that aside, let me share what idea I worked on this time round.


The Idea

Since I have not being coding or playing with electronics for some time (due to other commitments), I decided that I would work the following criteria:

  • Simple, Interesting & Fun, and New (yet to try)

While searching through my cabinet, I realized that there are still some electronic components and micro controller that I have yet to test out, so I thought this would be a good time to give it a try in this hackathon, creating a simple application for the controller. And the components used for this project consists of:

  • RGB LED Ring
  • NodeMCU (ESP8266)
  • TXB0104 Bi-Directional Level Shifter [Borrowed from site]

Since the NodeMCU is powered by ESP8266, it could be considered a IOT device which could be connected to the internet. But instead of using it to communicate or grab data from the internet, I decided to do something simpler: To use the NodeMCU (running a simple local server) with a webpage to control the RGB LED ring with basic “toggles”.

Setup

As the NodeMCU communicates in at 3.3V, we’ll need a bidirectional level shifter to communicate with the RGB LED ring, which is operated at 5V.

 

Code

#include <ESP8266WiFi.h>

#include <Adafruit_NeoPixel.h>

#define PIN            14 //D5 of nodemcu
#define LEDPIN         D0
#define NUMPIXELS      24
#define BRIGHTNESS      15
Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);

const char* ssid = "some-ap";
const char* password = "00012340";

WiFiServer  server(80);

void setup() {
  Serial.begin(115200);
  pixels.begin(); // This initializes the NeoPixel library.
  pinMode(LEDPIN, OUTPUT);
  delay(10);
  //Off all led (If any is on)
  digitalWrite(LEDPIN, LOW);
  for (uint16_t i = 0; i < pixels.numPixels(); i++) {
    pixels.setPixelColor(i, pixels.Color(0, 0, 0)); // Moderately bright green color.
  }
  pixels.show();
  delay(10);

  //Start connection
  // Connect to WiFi network
  Serial.println();
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);

  WiFi.begin(ssid, password);

  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.println("WiFi connected");

  // Start the server
  server.begin();
  Serial.println("Server started");

  // Print the IP address
  Serial.print("Use this URL to connect: ");
  Serial.print("http://");
  Serial.print(WiFi.localIP());
  Serial.println("/");

}


void offleds() {
  for (int i = 0; i < NUMPIXELS; i++) {
    pixels.setPixelColor(i, pixels.Color(0 , 0 , 0 ));
  }
  pixels.show();
  delay(100);
}

int r = 0, g = 0, b = 0;

void loop() {
  // Check if a client has connected
  WiFiClient client = server.available();
  if (!client) {
    return;
  }

  // Wait until the client sends some data
  Serial.println("new client");
  while (!client.available()) {
    delay(1);
  }

  // Read the first line of the request
  String request = client.readStringUntil('r');
  Serial.println(request);
  client.flush();

  // Match the request
  int value = LOW;
  if (request.indexOf("?r=1") != -1) {
    digitalWrite(LEDPIN, LOW);
    r = BRIGHTNESS;
    value = HIGH;
  }

  if (request.indexOf("?g=1") != -1) {
    digitalWrite(LEDPIN, LOW);
    g = BRIGHTNESS;
    value = HIGH;
  }

  if (request.indexOf("?b=1") != -1) {
    digitalWrite(LEDPIN, LOW);
    b = BRIGHTNESS;
    value = HIGH;
  }

  if (request.indexOf("?r=0") != -1) {
    digitalWrite(LEDPIN, LOW);
    r = 0;
    value = HIGH;
  }

  if (request.indexOf("?g=0") != -1) {
    digitalWrite(LEDPIN, LOW);
    g = 0;
    value = HIGH;
  }

  if (request.indexOf("?b=0") != -1) {
    digitalWrite(LEDPIN, LOW);
    b = 0;
    value = HIGH;
  }

  if (request.indexOf("?all=0") != -1 ) {
    digitalWrite(LEDPIN, HIGH);
    r = g = b = 0;
    value = LOW;
  }

  for (uint16_t i = 0; i < pixels.numPixels(); i++) {
    pixels.setPixelColor(i, pixels.Color(r, g, b)); // Moderately bright green color.
  }
  pixels.show(); // This sends the updated pixel color to the hardware.

  delay(10); // Delay for a period of time (in milliseconds).


  // Set ledPin according to the request
  //digitalWrite(ledPin, value);

  // Return the response
  client.println("HTTP/1.1 200 OK");
  client.println("Content-Type: text/html");
  client.println(""); // do not forget this one
  client.println("<!DOCTYPE HTML>");
  client.println("<html>");


  client.print("Led pin is now: ");

  if (value == HIGH) {
    client.print("On");
  } else {
    client.print("Off");
  }
  client.println("<br><br>");
  client.println("<p>R: <a href="?r=1"><button>ON</button></a>&nbsp;<a href="?r=0"><button>OFF</button></a></p>");
  client.println("<p>G: <a href="?g=1"><button>ON</button></a>&nbsp;<a href="?g=0"><button>OFF</button></a></p>");
  client.println("<p>B: <a href="?b=1"><button>ON</button></a>&nbsp;<a href="?b=0"><button>OFF</button></a></p>");
    client.println("<p> <a href="?all=0"><button>OFF ALL</button></a></p>");
  client.println("</html>");
  delay(1);
  Serial.println("Client disonnected");
  Serial.println("");

}

 

You may also like...