简体   繁体   English

在 HTML 的 javascript 文件中包含 Arduino 的 function 和 Johnny-Five

[英]Include Arduino's function with Johnny-Five in HTML's javascript file

I'm a beginner with the javascript language, I wanted to create a project where by pressing a button on an HTML page I could turn on a connected LED on an arduino board.我是 javascript 语言的初学者,我想创建一个项目,通过按下 HTML 页面上的按钮,我可以打开 arduino 板上连接的 LED。 I have seen that it is possible to use arduino functions through a javascript library called "johnny-five".我已经看到可以通过名为“johnny-five”的 javascript 库使用 arduino 函数。 The problem is that when I've wrote in the javascript code included in the HTML file: var five = require("Johnny-five");问题是,当我在 HTML 文件中包含的 javascript 代码中编写时: var five = require("Johnny-five"); an error occurs in the web page console: require is not defined... . web 页面控制台出现错误: require is not defined... I've read something on the internet, but I didn't understand much.我在互联网上读过一些东西,但我不太了解。 I'd be happy to know how to fix it, thanks!`我很高兴知道如何解决它,谢谢!

Your johnny-five library import and all the LED logic will go into a.js file, not into HTML.您的 johnny-5 库导入和所有 LED 逻辑将 go 转换为 .js 文件,而不是 HTML。 The first step is to have Nodejs and the johnny-five npm package installed.第一步是安装 Nodejs 和 johnny-5 npm package。 This is an easy way to get started: http://johnny-five.io/#hello-world这是一个简单的入门方法: http://johnny-five.io/#hello-world

Next, the HTML file with the button will need to call a function on its onclick event,which connects to your johnny-five logic via event-listeners/sockets.接下来,带有按钮的 HTML 文件将需要在其 onclick 事件上调用 function,该事件通过事件侦听器连接到您的 johnny-five 逻辑。 You may find this useful您可能会发现很有用

I put together a quick project that does exactly what you want: Clicking on a button in a HTML page triggers LED blinking.我整理了一个快速项目,完全符合您的要求:单击 HTML 页面中的按钮会触发 LED 闪烁。 Check it out here .在这里查看 I've given detailed instructions on how to run it as well.我也给出了如何运行它的详细说明。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM