Arduinoのプログラムをネット経由で書き込み

このエントリーをはてなブックマークに追加

How does it works

arduinoはUART経由でプログラムを書き込みますが、obnizを2台使うことでUARTをインターネット経由で転送してワイヤレス化します。

Program

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">

  <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">
  <script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js" integrity="sha384-ZMP7rVo3mIykV+2+9J3UJ46jBk0WLaUAdn689aCwoqbBJiSnjAK/l8WvCWPIPm49" crossorigin="anonymous"></script>
  <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js" integrity="sha384-ChfqqxuZUCnJSK3+MXmPNIyE6ZbWh2IMqE241rYiqJxyMiZ6OW/JmZQ5stwEULTy" crossorigin="anonymous"></script>
  
  <script src="https://unpkg.com/obniz@1.9.1/obniz.js"></script>
</head>

<body>
<div id="obniz-debug"></div>
<br>
<div class="text-center">
  <h1> Arduino Flasher </h1>
</div>

<button id="on" class="btn btn-primary">ON</button>
<button id="off" class="btn btn-primary">OFF</button>

<script>

var obnizA = new Obniz("OBNIZ ID HERE"); //PC-side obniz
obnizA.onconnect = async function () {
  var obnizB = new Obniz("OBNIZ ID HERE"); //Arduino-side obniz
  obnizB.onconnect = async function(){
    var baud = 115200; // configration for UNO
    obnizA.uart0.start({tx: 0, rx: 1, baud:baud });  
    obnizB.uart0.start({tx: 0, rx: 1, baud:baud });  
    obnizA.io3.output(false); //USB-UART GND
    obnizB.io4.output(true); //Arduino Power
    obnizB.io3.output(false); //Arduino GND
    obnizB.io2.output(true); //DTR(Arduino Reset)
    
    //Arduino GND
    $("#on").click(function(){
      obnizB.io3.output(false);
    })
    $("#off").click(function(){
      obnizB.io3.output(true);
    })
    
    var lastTime = new Date().getTime();
    obnizA.uart0.onreceive = async function(data, text) {
      // PC to arduino
      obnizB.uart0.send(data);

      // automatic Reset to turn arduino to upload-mode
      var now = new Date().getTime();
      if (now - lastTime > 1000){
        obnizB.io2.output(false);
        obnizB.wait(10);
        obnizB.io2.output(true);
      }
      lastTime = new Date().getTime();
    }
    
    obnizB.uart0.onreceive = async function(data, text) {
      // arduino to pc
      obnizA.uart0.send(data);
    }   
  }
}
    
</script>
</body>
</html>