{{toaster.title}}

{{ toaster.text }}

Wifi Locked Box

A box with a self-locking mechanism; it can only be opened by entering a password on its website.

Albert van der Meer 2016-02-07 00:00:00

Wifi Locked Box, Internet of Things, Electronics,

Wifi Locked Box

Description

A little box that locks itself from the inside; can be opened via a website, using an ESP-8266 v12 module. It is used as a box containing a letter, which can only be opened by solving a puzzle on a webpage.

image

Hardware

locking mechanism

The locking system is a pin attached to a servo in such a way that the pin can be pushed for- and backwards. This makes it possible to hold two rings (one for the box, one for the cover) together. To make sure the servo does not suffer from tensile force, the attachment of the pin is flexible.

gif

electronics

The core is an ESP-8266 v12 module; In order to use it with a 9v battery, a conversion to both 5v (for servo) and 3.3v (for ESP) is required. Therefore, two voltage regulaters are used: one for 5v, which output is used for both the servo and the ESP. The ESP breakout board has an build-in 3.3v voltage regulator; to use it, the second resistor (out of three) has to be desoldered. To prevent a battery draw when the box is unused, a power switch is attached. Furthermore, when the box is unlocked, a little led lights up.

image

Software

Webpage

For the webpage, I designed a drag and drop puzzle. When the puzzle is solved, the webpage requested by the ESP gets updated, so the page shows the command to open the box. On the other hand, the script updates the page in order to lock the box when the puzzle is solved incorrectly. Furthermore, I added some methods for checking whether the device is turned on by logging the requests by the ESP. This is used to give an alert on the puzzle-webpage, saying the connection with the device is lost/not established.

ESP

The ESP is programmed using the arduino IDE; There are a few tasks:

  • connecting to the right network
  • requesting a certain webpage with a 5 sec. interval
  • act on the response: opening/closing the lock by turning the servo

Code

#include <ESP8266WiFi.h>
#define servopin 14
#define ledpin 5
#include <Ticker.h>
#include "myServo.h"

Servo servo;

const char* ssid     = "NetworkName";
const char* password = "NetworkPassword";

const char* host = "avdm.nl";

void setup() {
 // pinMode(A0, INPUT); //analog pin is input
  pinMode(ledpin, OUTPUT); 
  servo.attach(servopin); //start servo
  servo.write(000);
  WiFi.mode(WIFI_STA);
  Serial.begin(115200);
  delay(100);

  // We start by connecting to a 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");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());
}

int value = 0;
int pos;
String response;
bool closed = true;
int j = 7;
int maxLoop = 8;

void loop() {
 /* Serial.println(analogRead(A0));  if(analogRead(A0)>600){    digitalWrite(ledpin, HIGH);  }  else{    digitalWrite(ledpin, LOW);  }*/
  delay(500);
  ++value;
  ++j;

  if (j == maxLoop) {
    Serial.print("connecting to ");
    Serial.println(host);

    // Use WiFiClient class to create TCP connections
    WiFiClient client;
    const int httpPort = 80;
    if (!client.connect(host, httpPort)) {
      Serial.println("connection failed");
      return;
    }


    // We now create a URI for the request
    String url = "/my/check/url";
    Serial.print("Requesting URL: ");
    Serial.println(url);

    // This will send the request to the server
    client.print(String("GET ") + url + " HTTP/1.1\r\n" +
                 "Host: " + host + "\r\n" +
                 "User-Agent: ArduinoWiFi/1.1\r\n" +
                 "Connection: close\r\n\r\n");
    delay(500);

    // Read all the lines of the reply from server and print them to Serial
    response = "";
    while (client.available()) {
      // String line = client.readStringUntil('\r');
      // Serial.print(line);
      char c = client.read();
      response += c;
    }
    // Serial.println(response+"|");
    response = response.substring(response.length() - 10, response.length() - 7);

    Serial.println(response);
    if (response == "000") {
      closed = true;
      digitalWrite(ledpin, LOW);
    }
    if (response == "360") {
      closed = false;
      digitalWrite(ledpin, HIGH);
    }

    if (closed) {
      servo.write(0);
    }
    else {
      servo.write(360);
    }
    Serial.println(closed);
    Serial.println("closing connection");
    j = 0;
    //client.close();
  }
  if(j>maxLoop){
    ESP.restart();
  }

}



the Facts

built for Albert
owner Albert
state finished (january 2016)
method wifi