1 Reply Latest reply on May 3, 2017 9:07 AM by Intel Corporation

    connect node js with html button

    morire

      hi everyone

      i'm working on robot has Intel Edison board and i need to connect the html button and make it a server can anyone help me this is the node code and html code :

      app.js:

       

      /*Http server*/

      var express = require("express");

      var app = express();

      var http = require('http').Server(app);

      var path = __dirname + '/';

      var io = require('socket.io')(http);

      var myip = require('quick-local-ip');

       

       

      var PORT = 4000;

       

       

      // INTEL EDISON CONTROLL PART----------------------------------------------------

      //MRAA Initialization

      var m = require("mraa");

       

       

       

       

      var u = new m.Uart(0); //Default

      u.setBaudRate(9600);

      u.setMode(8,0,1);

      u.setFlowcontrol(false, false);

      sleep(200);   

      var command = new Buffer(4);

      command[0] = 0x22;

      command[1] = 0x00;

      command[2] = 0x00;

      command[3] = 0x22;

       

       

       

       

      function isObjectClose(threshold) {

          var rxBuf;

          var result;

          u.write(command);

          sleep(200);

          rxBuf = u.read(4);

          sleep(200);

       

          if (rxBuf[3] == (rxBuf[0]+rxBuf[1]+rxBuf[2])) {

              result = (rxBuf[1]<<8) | rxBuf[2];

       

       

              if (result < threshold)

                  return true;

              else

                  return false;

          }

       

       

          else

              return true;

      }

       

       

       

       

       

       

       

       

      var x = new m.I2c(0);

      x.address(4);

      var buf = new Buffer(5);

      buf[0] = 0x55;  //Header 1

      buf[1] = 0xaa;  //Header 2

      function tankForward(speed) {

          if (speed > 0xFF)

              speed = 0xFF;

       

       

          //Left Motor CounterClockwise

          buf[2] = 0xB1;

          buf[3] = 0x1;

          buf[4] = (buf[0]+buf[1]+buf[2]+buf[3]) & 0xFF;

          x.write(buf);

       

       

          //Right Motor CounterClockwise

          buf[2] = 0xB2;

          buf[3] = 0x1;

          buf[4] = (buf[0]+buf[1]+buf[2]+buf[3]) & 0xFF;

          x.write(buf);

       

       

          //Left Motor Speed

          buf[2] = 0xC1;

          buf[3] = speed;

          buf[4] = (buf[0]+buf[1]+buf[2]+buf[3]) & 0xFF;

          x.write(buf);

       

       

          //Right Motor Speed

          buf[2] = 0xC2;

          buf[3] = speed;

          buf[4] = (buf[0]+buf[1]+buf[2]+buf[3]) & 0xFF;

          x.write(buf);

      }

       

       

      function tankBackward(speed) {  

          if (speed > 0xFF)

              speed = 0xFF;

       

       

          //Left Motor Clockwise

          buf[2] = 0xB1;

          buf[3] = 0x0;

          buf[4] = (buf[0]+buf[1]+buf[2]+buf[3]) & 0xFF;

          x.write(buf);

       

       

       

          //Right Motor Clockwise

          buf[2] = 0xB2;

          buf[3] = 0x0;

          buf[4] = (buf[0]+buf[1]+buf[2]+buf[3]) & 0xFF;

          x.write(buf);

       

       

          //Left Motor Speed

          buf[2] = 0xC1;

          buf[3] = speed;

          buf[4] = (buf[0]+buf[1]+buf[2]+buf[3]) & 0xFF;

          x.write(buf);

       

       

       

       

          //Right Motor Speed

          buf[2] = 0xC2;

          buf[3] = speed;

          buf[4] = (buf[0]+buf[1]+buf[2]+buf[3]) & 0xFF;

          x.write(buf);

          sleep(2000);

      }

       

      app.get('/', function(req, res){

        // console.log(res)

        res.sendFile(__dirname + '/index.html');

      });

       

       

      http.listen(PORT, function(){

        console.log('--------------------------------------------------------------');

        console.log('-------------------------Loading Server-----------------------');

        console.log('--------------------------------------------------------------\n');

        console.log('Server IP : '+myip.getLocalIP4()+'  Port : '+PORT);

       

       

      });

      -------------------------------------------------------------------------

      and html is:

      <!DOCTYPE html>

       

       

      <html>

       

       

        <head>

          <title>Robot as a Website</title>

          <meta charset="utf-8">

          <link rel="stylesheet" href="css/style.css">

      <script src="script/script.js"></script>

        </head>

       

       

      <body>

      <!-- make the header for the website to write the name for the project-->

       

        <div>

       

        <h1 id="headerWeb">rbot as a websit</h1>

       

      <!-- <img src="img/robot.jpg" id="img"> -->

       

      </div>

       

       

      <div id="but">

       

       

      <input onclick="moveForward();" type="button" value="Forward" id="forwardButton"></input>

       

       

       

       

       

       

       

       

       

       

      <div id="messages"></div>

       

      </div>

       

      </body>

       

       

       

       

      </html>