Alex Glow
Published

The Ugliest Sweater

In the maker world, only boredom is truly ugly. Prevent boredom with horrible, smart flashy lights!!!

IntermediateFull instructions provided5 hours1,753
The Ugliest Sweater

Things used in this project

Hardware components

Arduino MKR1000
Arduino MKR1000
×1
LED string lights (battery-powered)
×1
General Purpose Transistor NPN
General Purpose Transistor NPN
I used the PN2222A (bought from Adafruit).
×1
5V USB Battery Pack
×1

Story

Read more

Schematics

Sweater circuit

Code

Arduino sketch

C/C++
Adapted from Vincent Wong's project: https://www.hackster.io/wesee/control-your-mkr1000-with-arest-framework-ba6619
// Import required libraries
#include <SPI.h>
#include <WiFi101.h>

#define DEBUG_MODE 0

#include <aREST.h>

// Create aREST instance
aREST rest = aREST();

char ssid[] = "YOURSSID";      // your network SSID (name)
char pass[] = "YOURPASS";   // your network password
int keyIndex = 0;                 // your network key Index number (needed only for WEP)

int status = WL_IDLE_STATUS;

WiFiServer restServer(80);

const int ledPin = 13;
int between = 60000;

void setup(void)
{
  // Function to be exposed
  rest.function("led",ledControl);

  // Give name and ID to device
  rest.set_id("007");
  rest.set_name("bored_cat");

  // Start Serial
  Serial.begin(115200);

  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only

  }

  // check for the presence of the shield:
  if (WiFi.status() == WL_NO_SHIELD) {
    Serial.println("WiFi shield not present");
    // don't continue:
    while (true);
  }

  // attempt to connect to Wifi network:
  while ( status != WL_CONNECTED) {
    Serial.print("Attempting to connect to SSID: ");
    Serial.println(ssid);
    // Connect to WPA/WPA2 network. Change this line if using open or WEP network:
    status = WiFi.begin(ssid, pass);

    // wait 10 seconds for connection:
    delay(10000);
  }

  Serial.println();

  // you're connected now, so print out the status:
  printWifiStatus();

  // Start server
  restServer.begin();
  Serial.println(F("Listening for connections..."));

  pinMode(ledPin, OUTPUT);

  // Enable watchdog
  //wdt_enable(WDTO_4S);
}

void loop() {

  // Handle REST calls
  WiFiClient client = restServer.available();
  rest.handle(client);

  // flash to start
  digitalWrite(ledPin, 1);
  delay(1000);
  digitalWrite(ledPin,0);

  // pause for the delay; flash for a second; divide delay by 3; end loop
  delay(between);
  digitalWrite(ledPin, 1);
  delay(1000);
  digitalWrite(ledPin, 0);
  between = between/3;
}


// Custom function accessible by the API
int ledControl(String command) {

  Serial.println(command);
  
  // Get state from command
  int state = command.toInt();

  // reset delay length; quick double-flash to acknowledge
  between = 60000;
  digitalWrite(ledPin, 0);
  delay(100);
  digitalWrite(ledPin, 1);
  delay(100);
  digitalWrite(ledPin, 0);
  delay(100);
  digitalWrite(ledPin, 1);
  delay(100);
  digitalWrite(ledPin, 0);
  return 1;
}

void printWifiStatus() {
  // print the SSID of the network you're attached to:
  Serial.print("SSID: ");
  Serial.println(WiFi.SSID());

  // print your WiFi shield's IP address:
  IPAddress ip = WiFi.localIP();
  Serial.print("IP Address: ");
  Serial.println(ip);

  IPAddress subnet = WiFi.subnetMask();
  Serial.print("Netmask: ");
  Serial.println(subnet);

  IPAddress gateway = WiFi.gatewayIP();
  Serial.print("Gateway: ");
  Serial.println(gateway);

  // print the received signal strength:
  long rssi = WiFi.RSSI();
  Serial.print("signal strength (RSSI):");
  Serial.print(rssi);
  Serial.println(" dBm");
}

Test page with On and Off buttons

HTML
Simply toggles digital pin 13 on and off.
<!DOCTYPE html>
<html>
<head>
    <script src="jquery-2.1.1.js"></script>
    <script>
        //Usually, you put script-tags into the head
        function pokeOn() {
            //This performs a POST-Request.
            //Use "$.get();" in order to perform a GET-Request (you have to take a look in the rest-API-documentation, if you're unsure what you need)
            //The Browser downloads the webpage from the given url, and returns the data. Update with your own Arduino's IP
            $.post( "http://192.168.1.252/digital/13/1", function( data ) {
                 //As soon as the browser finished downloading, this function is called.
                 $('#demo').html(data);
            });
        }
		
        function pokeOff() {
            //This performs a POST-Request.
            //Use "$.get();" in order to perform a GET-Request (you have to take a look in the rest-API-documentation, if you're unsure what you need)
            //The Browser downloads the webpage from the given url, and returns the data.
            $.post( "http://192.168.1.252/digital/13/0", function( data ) {
                 //As soon as the browser finished downloading, this function is called.
                 $('#demo').html(data);
            });
        }
    </script>
</head>
<body>

    <p>Click the button to trigger a function.</p>

    <button onclick="pokeOn()">On</button>
    <button onclick="pokeOff()">Off</button>

    <p id="demo"></p>    
</body>
</html>

"Poke" web page

HTML
You'll have to update this with the actual IP address that your Arduino shows in the serial monitor.
<!DOCTYPE html>
<html>
<head>
    <script src="jquery-2.1.1.js"></script>
    <script>
        //Usually, you put script-tags into the head
        function pokeFunction() {
            //This performs a POST-Request.
            //Use "$.get();" in order to perform a GET-Request (you have to take a look in the rest-API-documentation, if you're unsure what you need)
            //The Browser downloads the webpage from the given url, and returns the data. Update with your own Arduino's IP
            $.post( "http://192.168.1.252/led?params=0", function( data ) {
                 //As soon as the browser finished downloading, this function is called.
                 $('#demo').html(data);
            });
        }
    </script>
</head>
<body>

    <p>Poke.</p>

    <button onclick="pokeFunction()">Poke</button>

    <p id="demo"></p>    
</body>
</html>

Credits

Alex Glow

Alex Glow

145 projects • 1568 followers
The Hackster team's resident Hardware Nerd. I love robots, music, EEG, wearables, and languages. FIRST Robotics kid.

Comments