IOT Lock using WebSocket and Bluetooth

This is a small project using Websocket and Bluetooth to control a lock. The servo is used as a demonstration for a lock as it functionality is similar. There are 4 components in this project: the mbed, ethernet, bluetooth, and servo with external power. The main program uses two threads to handle bluetooth and internet signal. The Websocket address must match to the server address.

MBEDBluetooth
gndgnd
VU(5v)Vin
ncRTS
GndCTS
p14 (Serial RX)TXO
p13 (Serial TX)RXI
MBEDServoExternal Power(5V)
p21Signal line (yellow)
ncRed lineVin(5V)
GNDBlack lineGND
MBEDLED
p17RED
p19GREEN

/media/uploads/xemnas214/untitled.png

Control the servo via Websock and Bluetooth

#include "mbed.h"
#include "EthernetInterface.h"
#include "Websocket.h"
#include "Servo.h" 
#include "rtos.h"
Servo myservo(p21);
Serial pc(USBTX, USBRX);
Serial Blue(p13,p14);
DigitalOut myled(LED1);
DigitalOut myled4(LED4);
DigitalOut close(p17);
DigitalOut open(p19);
float position = 0;
void bluetooth(void const *args)
{
    while (1){
    if (Blue.getc()=='!') {
            if (Blue.getc()=='B') { //button data
                char bnum = Blue.getc(); //button number
                if (bnum == '1') {
                    position = 1;
                }else 
                if (bnum == '2'){
                    position = 0;
                }
            }
        }
    }
}
 
int main() {
    Thread thread1(bluetooth); 
    char recv[30];
    EthernetInterface eth;
    eth.init(); //Use DHCP
    eth.connect();
    printf("IP Address is %s\n\r", eth.getIPAddress());
 
    Websocket ws("ws://192.168.43.154:8888/ws");
    ws.connect();
    ws.send(new char('0'));
    while (1) {
        if (ws.read(recv)) {
            sscanf(recv,"%f",&position);
        }
        if (myservo != position){
            ws.send(new char(position + '0'));
        }
        myservo = position;
        open = (position > 0);
        close = (position == 0);
    }
}

The servo is run by NodeJS with ws module package with can be installed by the command

npm install ws
<</code>
The server is at port 8888 for this code
<<code title=NodeJS servo to control send data between the Web and mbed>>
var WebSocketServer = require('ws').Server
  , wss = new WebSocketServer({port: 8888});
wss.on('connection', function(ws) {
	console.log(wss.clients.length);
    ws.on('message', function(message) {
    	console.log(wss.clients.length, message);
        wss.clients.forEach(function each(client) {
	      if (client !== ws) client.send(message);
	    });
    });
    ws.send('Connected');
});
<</code>>
User use browser to control the lock to either lock or unlock
<<code title=Webpage to control the lock>>
<!doctype html>
<html>
  <head>
    <title>Wireless lock</title>
    <meta charset="utf-8" />
    <style type="text/css">
      body {
        text-align: center;
        min-width: 500px;
      }
    </style>
    <script src="http://code.jquery.com/jquery.min.js"></script>
    <script>
 
      // log function
      $(document).ready(function () {
 
        var ws = new WebSocket("ws://localhost:8888/ws");
         
        // Handle incoming websocket message callback
        ws.onmessage = function(evt) {
          if (evt.data == '1'){
              $("#status").text('Locked');
          } else {
              $("#status").text('Unlocked');
          }
        };

        // Close Websocket callback
        ws.onclose = function(evt) {
          log("***Connection Closed***");
          alert("Connection close");
         $("body").css("background", "#ff0000"); 
         $("body").css("background", "#ff0000"); 
         $("body").css("background",  "#ff0000");
          $("div#message_details").empty();

          };

        // Open Websocket callback
        ws.onopen = function(evt) { 
          $("body").css("background", "#00ff00"); 
          $("body").css("background", "#00ff00"); 
          $("body").css("background", "#00ff00");
        };

        // Send websocket message function
        $("button").click(function(evt) {
            ws.send(evt.target.value);
        });
 
      });
    </script>
  </head>
 
  <body>
    <h1>Wireless lock</h1>
    <button type="button" value="1">Lock</button>
    <button type="button" value="0">Unlock</button>
    <div id="status"></div>
  </body>
</html>

/media/uploads/xemnas214/dsc04744.jpg


Please log in to post comments.