Spivey
Published © MIT

How We Built Our Facial Recognition Ferris Wheel

At Coolest Projects 2018, we showcased the Wia platform with a facial recognition Ferris wheel!

IntermediateFull instructions provided2 hours5,484
How We Built Our Facial Recognition Ferris Wheel

Things used in this project

Story

Read more

Code

Camera

JavaScript
'use strict';

var wia = require('wia')('device-secret-key');
var fs = require('fs');
var RaspiCam = require("raspicam");

wia.events.publish({
	name: "started"
});

// Setup the camera
var camera = new RaspiCam({
  mode: 'photo',
  output: __dirname + '/photo.jpg',
  encoding: 'jpg'
});

// Listen for the "start" event triggered when the start method has been successfully initiated
camera.on("start", function(){
  console.log("Starting to take photo.");
});

// Listen for the "read" event triggered when each new photo/video is saved
camera.on("read", function(err, timestamp, filename){
  console.log("New photo created.", timestamp, filename);

  // Publish the photo to Wia
  wia.events.publish({
    name: 'photo',
    file:  fs.createReadStream(__dirname + '/' + filename)
  });
});

wia.stream.on("connect", function() {
  console.log("Connected to stream.");
  wia.events.publish({name: "connected"});
});

setTimeout(function() {
  wia.commands.subscribe({
    slug: 'take-photo'
  }, function(err, data) {
    wia.events.publish({name:"gotCommand"});

    console.log("In command callback. Taking photo.");

    // Take a photo
    camera.start();
  });
}, 12500);

wia.stream.connect();

Ferris Wheel

Arduino
#include <WiFi101.h>
#include <MQTT.h>

const char WIFI_SSID[] = "wifi-ssid"; // WiFI ssid 
const char WIFI_PASS[] = "wifi-password"; //WiFI password

// get this from the wia dashboard. it should start with `d_sk`
const char* device_secret_key = "device-secret-key";

//WiFiSSLClient ipCloudStack;
WiFiClient wifiClient;
MQTTClient mqttClient;

int status = WL_IDLE_STATUS;

// Wia Cloud MQTT params
char mqttCloudServer[]     = "api.wia.io";
int  mqttCloudPort         = 1883;
char mqttCloudUsername[]   = "device_secret_key"; 
char mqttCloudPassword[]   = " "; 

// Wia API parameters
char server[] = "api.wia.io";
const int mqttPort = 1883;  // Default MQTT port

const String deviceId = "device-id";   // starts with dev_, found in Wia Dashboard
const String commandName = "take-photo";  // Configured in Wia Dashboard

// Topics
String takePhotoCommandTopic = "devices/" + deviceId + "/commands/take-photo/run";
String rotateForFiveCommandTopic = "devices/" + deviceId + "/commands/rotate-for-5-seconds/run";
String startMovingCommandTopic = "devices/" + deviceId + "/commands/start-moving/run";
String stopMovingCommandTopic = "devices/" + deviceId + "/commands/stop-moving/run";

//L293D
const int motorPin1  = 7;
const int motorPin2  = 6;


void messageReceived(String &topic, String &payload) {
  Serial.println("incoming: " + topic + " - " + payload);
  if (topic.equals(rotateForFiveCommandTopic)) {
    Serial.println("Rotating for 5 seconds.....");
    // Start rotating
    digitalWrite(motorPin1, HIGH);
    digitalWrite(motorPin2, LOW);
    // wait 5 seconds for connection:
    delay(5000);
    // Stop rotating
    digitalWrite(motorPin1, LOW);
    digitalWrite(motorPin2, LOW);
  } else if (topic.equals(startMovingCommandTopic)) {
    Serial.println("Start rotating.....");
    // Start rotating
    digitalWrite(motorPin1, HIGH);
    digitalWrite(motorPin2, LOW);
  } else if (topic.equals(stopMovingCommandTopic)) {
     Serial.println("Stop rotating.....");
    // Stop rotating
    digitalWrite(motorPin1, LOW);
    digitalWrite(motorPin2, LOW);
  } else {
    Serial.println("Unhandled topic");
  }
}

void connect() {
  
 // 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(WIFI_SSID);
    // Connect to WPA/WPA2 network. Change this line if using open or WEP network:
    status = WiFi.begin(WIFI_SSID, WIFI_PASS);

    // wait 5 seconds for connection:
    delay(5000);
  }
  Serial.print("\nconnecting...");

  Serial.println("\nconnected!\n");
  Serial.print("\nIP address: ");
  Serial.println(WiFi.localIP());

  // You need to set the IP address directly.
  mqttClient.begin(mqttCloudServer, mqttCloudPort, wifiClient);

  Serial.println("start wia connect"); Serial.println();
  
  while (!mqttClient.connect("wiatest", mqttCloudUsername, mqttCloudPassword)) {
    Serial.print("*");
    delay(500);    
  }

  Serial.println("Connected to MQTT");

  mqttClient.onMessage(messageReceived);
  
  mqttClient.subscribe(takePhotoCommandTopic);
  mqttClient.subscribe(rotateForFiveCommandTopic);
  mqttClient.subscribe(startMovingCommandTopic);
  mqttClient.subscribe(stopMovingCommandTopic);
}

void setup() {
  Serial.begin(115200);

  pinMode(motorPin1, OUTPUT);
  pinMode(motorPin2, OUTPUT);

  digitalWrite(motorPin1, LOW);
  digitalWrite(motorPin2, LOW);  

  connect();
}

void loop() {
  mqttClient.loop();
  delay(1000);
  
  if (!wifiClient.connected()) {
    connect();
  }

}

Credits

Spivey

Spivey

82 projects • 59 followers
Tourist in a Tutu || US Born || Melbourne/Mexico/California Raised || New Yorker at ❤️ || SF to Dublin to be COO of Wia the best IoT startup

Comments