ESP8266 联网后执行在线代码

espruino/esp8266 加载远程代码

今天玩esp8266的时候突然想到, 是不是可以直接运行线上代码, 这样的话就可以灵活的配置esp8266执行什么操作了, 也不用每次更新代码都要把设备拆下来搞到电脑上去下载程序, 因为本身esp8266就需要联网工作, 所以就想到了联网后获取在线代码进行执行, 如果代码更新只需要重启设备就可以了, 不用每次都插到电脑上去下载代码. 后来翻了一下espruino的文档发现有提供加载线上模块的方法, 于是就实现了一下, 下面是加载远程代码的逻辑.

烧录espruino固件并下载代码

  1. 配置wifi和线上代码地址 (注意需要使用http协议, 不支持https协议)
  2. 将代码写入esp8266
  3. 编写线上代码
  4. 重启设备
  5. 重启后设备就会自动联网和拉取指定的代码并执行了.
  1. var wifi = require('Wifi');
  2. var app = {
  3. config: {
  4. wifi: {
  5. ssid: '你的wifi名称',
  6. password: '你的wifi密码'
  7. },
  8. // 你的线上代码地址
  9. online: 'http://espjs.admin-ui.cn/ota.js'
  10. },
  11. init: function () {
  12. this.wifi();
  13. },
  14. wifi: function () {
  15. var self = this;
  16. console.log('wifi connecting...');
  17. wifi.connect(self.config.wifi.ssid, { password: self.config.wifi.password }, err => {
  18. if (err) {
  19. console.log('wifi connect error: ', err);
  20. return;
  21. }
  22. wifi.getIP(function (err, result) {
  23. self.onWifiConnected(result);
  24. });
  25. });
  26. },
  27. run: function () {
  28. this.init();
  29. },
  30. onWifiConnected: function (result) {
  31. console.log('wifi connected: ', result);
  32. this.loadModule(this.config.online);
  33. },
  34. loadModule: function (url, callback) {
  35. if (url.substr(0, 4) !== 'http') {
  36. url = 'http://www.espruino.com/modules/' + url + '.min.js';
  37. }
  38. require("http").get(url, function (res) {
  39. var contents = "";
  40. res.on('data', function (data) { contents += data; });
  41. res.on('close', function () {
  42. Modules.addCached(url, contents);
  43. if (callback) callback();
  44. });
  45. }).on('error', function (e) {
  46. console.log("ERROR", e);
  47. });
  48. }
  49. };
  50. app.run();

案例

比如我这里用了自己的域名, 线上代码是一个获取温湿度和显示的一个例子.
代码地址: http://espjs.admin-ui.cn/ota.js

  1. var demo = {
  2. config: {
  3. dht: {
  4. pin: NodeMCU.D1
  5. },
  6. oled: {
  7. scl: NodeMCU.D5,
  8. sda: NodeMCU.D4,
  9. width: 128,
  10. height : 64
  11. },
  12. led: {
  13. pin: NodeMCU.D2,
  14. }
  15. },
  16. device: {
  17. dht: null,
  18. oled: null,
  19. led: null
  20. },
  21. init: function () {
  22. this.led();
  23. this.oled();
  24. this.dht(2000);
  25. },
  26. led: function () {
  27. var self = this;
  28. app.loadModule('http://espjs.admin-ui.cn/libs/led.js', function (LED) {
  29. self.device.led = new LED(self.config.led.pin);
  30. self.device.led.close();
  31. });
  32. },
  33. dht: function (time) {
  34. var self = this;
  35. app.loadModule('DHT11', function (dht11) {
  36. self.device.dht = dht11.connect(self.config.dht.pin);
  37. setInterval(function () {
  38. self.device.dht.read(function (result) {
  39. self.onDht(result.temp, result.rh);
  40. });
  41. }, time);
  42. });
  43. },
  44. oled: function () {
  45. var self = this;
  46. var i2c = new I2C();
  47. i2c.setup({
  48. scl: this.config.oled.scl,
  49. sda: this.config.oled.sda,
  50. bitrate: 100000
  51. });
  52. var config = {
  53. width: this.config.oled.width,
  54. height: this.config.oled.height
  55. };
  56. app.loadModule('SH1106', function (ssd1306) {
  57. self.device.oled = ssd1306.connect(i2c, function () {
  58. self.onOledInit();
  59. }, config);
  60. })
  61. },
  62. run: function () {
  63. this.init();
  64. },
  65. onDht: function (temp, rh) {
  66. console.log('Temp is ' + temp + ' and RH is ' + rh);
  67. if (this.device.oled) {
  68. this.device.oled.clear(true);
  69. this.device.oled.drawString('Temp: ' + temp , 40 , 20);
  70. this.device.oled.drawString('RH: ' + rh, 40 , 40);
  71. this.device.oled.flip();
  72. }
  73. },
  74. onWifiConnected: function (result) {
  75. console.log('wifi connected: ', result);
  76. },
  77. onOledInit: function () {
  78. this.device.oled.clear(true);
  79. this.device.oled.drawString("Hello World!", 50 , 40);
  80. this.device.oled.flip();
  81. console.log('oled init finish');
  82. }
  83. };
  84. demo.run();

效果是这样的:

文章不错, 赏你二两银子

如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续努力!