一个软件工程师的硬件修养:ESP8266 入门(普通动感单车-变智能)

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

一个软件工程师的硬件修养:ESP8266 入门(普通动感单车-变智能)

Human_nature   2021-03-25 我要评论

前言

一直在开发软件。今日突然心血来潮想尝试一下硬件。

于是就买了这样一个板子:

 

买的淘宝上大佬帮忙找的一个套装。 除了板子之外还有一些线和其他配件:温湿度传感器,气压传感器,光线传感器,小屏幕。

板子的型号是 ESP8266 CH340G 。

另外还买了一个霍尔传感器:

 

 

 

尝试入门

 

按照网上的很多教程,拿到板子之后第一步先刷固件。

参考  https://blog.csdn.net/qq_41736609/article/details/80980954

这里就不多讲了。

然后,随后的事情令我猝不及防:我的板子插到我笔记本上的时候不能识别为COM串口,只能识别成“未知设备”..... 这令我费解,我查了很多资料,都说是驱动没有安装。我随机拿着板子插到家里的台式机上,同样都是win10系统,台式机立马正确识别COM端口,驱动正常。???!!! なに???(纳尼)

这令我费解,然后开始找驱动程序,找了半天终于找到了很多驱动,结果却都不能用.... 

最后我上网搜了一下 未知设备 的名字,什么FT232R之类的,我忘记了,当时没有截图。结果真的有响应的驱动:

 

 

 哈哈哈哈,然后直接从设备管理器里面安装驱动,要安装两边 。我看了一个国外的网站里面有类似的教程 :

参考 : https://www.usb-drivers.org/ft232r-usb-uart-driver.html

防止丢失,我把内容都贴出来了

 

Step 1:

Download the driver file and extract it to your hard driver where you can find it

FT232R USB UART Driver Download

Step 2:

You first need to uninstall the incorrect drivers on your computer. In Windows, go to Control Panel then to Device Manager

 

Now press the delete button on your computer and it will ask for confirmation.

Collapse the Ports menu and now click on the USB controller at the bottom of the list.

Now you need to uninstall the existing USB Serial Convertor.

Disconnect the USB cable from your PC to KMX1 or KMX2

reconnect the USB cable from your PC to KMX1 or KMX2

A new yellow flagged item will appear FT232R USB UART

Right Click on FT232R USB UART

Select Update Driver Software

 

Now you need to find where you extracted the drivers from the file you downloaded above and point the software to the directory.

The driver software for USB Serial Converter has been installed

The USB Serial Convertor is now installed. Next you have to install the port. If you are old enough to remember software modems then you will recall that it was done in the same manner.

The yellow flagged item will have changed name to USB Serial Port

Right Click on USB Serial Port
Select Update Driver Software

 

The driver software for the Port is in the same directory where you had the other drivers which you had just installed.

The Serial Port will now install and that will be the final installation. There should be no yellow flags any more.

Done!

 

上面是我引用的内容。

 

--------------------------------------------------------------------------分割线 --------------------------------------------------------------------------------

驱动搞完了,然后去nodemcu官网构建自己的固件,打开下面的网址

https://nodemcu-build.com/

我自己不懂,就勾选了这些

 

 

 

然后开始教程里面的刷固件,那个教程也有对应的软件下载,再次感谢他的奉献 

开发的过程中,主要用到两个软件:

1. 烧写固件工具 ESP8266Flasher

2. 烧写程序工具 ESPlorer

 

 嗯.... 硬件里面都叫烧写,要么烧录 都是发烧友... 

 

接着 ,我连上了套件里送的 温湿度传感器。具体的连接方法: https://www.qutaojiao.com/233.html

贴出最重要的东西:

DHT11上标有(+或VCC)引脚的连接nodemcu的+ 3V引脚。

DHT11上标有(S或OUT)引脚的连接nodemcu的D4V引脚。

DHT11上标有(-或GND)引脚的连接nodemcu的GND引脚。

 

至此,get到新知识 :VCC连接电源正极,GND连接地线,其他基本都是数据传输用的。

 

 

 

然后使用刚说的第二个软件 ESPlorer 烧写程序:

运行结果如下:

 

 不知道为啥....   温湿度数值都不太正常,可能时配件失灵了... 

 

 

新的想法

 做到上一步,基本已经熟悉了一点皮毛了。可以开始着手实现idea了

之前买了一个霍尔传感器,就是想把我家的普通动感单车改造成一个智能的,可以传输数据的动感单车 。基本原理是

用霍尔传感器测出转速,然后传输到手机或者电脑上,然后写一个程序比如,吹气球。 实现单车控制吹气球的速度。哈哈哈哈哈,想想就好玩

第一步:连线

 

 

连好之后是这样的,这个传感器有两个灯,一个是电源指示灯,一个是 开关指示灯。 开关指示灯在霍尔传感器被激活的时候就会亮。

不过目前我还不知道霍尔传感器怎么被激活......

霍尔传感器的D0接口,连接的是板子的D1或者D2 或者其他IO引脚。都可以 

第二步:操作IO接口,获取数据

这一步卡了很久  连上之后不管怎么都没反应 。下面是我尝试时的视频,为此我还专门去旁边的五金店买了一个磁铁 7元钱,真贵。

算了  不能直接上传视频 ,弄个图片吧

 

 

 然后,找呀找 卡在这里很痛苦,睡觉都睡不好。 结果在小破站找到了一个大佬的视频:

https://www.bilibili.com/video/BV1Zv411t7xk

发现他用的板子,和传感器跟我的一模一样,这对于一个新手来说简直是天大的福利。 能比葫芦画瓢的学习,最好了  哈哈哈哈 ,要是有老师更好 

如果你也在开发这个,请看他的视频...

 

第三步:编写程序读取霍尔传感器数据

参考官方文档:https://nodemcu.readthedocs.io/en/release/modules/gpio

这里有对gpio这个模块的详细解释,不过是英文的,一点点啃吧。 

inputPin = 4
gpio.mode(inputPin, gpio.OUTPUT)
gpio.write(0, gpio.LOW)

gpio.mode(inputPin, gpio.INPUT)
tmr.create():alarm(50, tmr.ALARM_AUTO, function()
    print(gpio.read(inputPin), tmr.time())
end)

 

哦对了,这里有个很重要的知识点,也是卡我很久重大原因,就是关于引脚对应的GPIO编号,和index值 这个一开始我一直搞混了....  

放一个图:

 

 

 

 

 

 

 

 

 

参考: https://www.jianshu.com/p/c3ef61c9c59b

然后,正常获取引脚的数据了。 

 

 

 

第五步:编写websocket 上传数据

使用websocket是我开发javaWeb这么多年 必选的,当然可以用其他方式交互数据,当然也是恰好官网上有websocket对这个固件的支持:

 

 --- 2021-03-25 更新

首先,本地用spring 跑一个webSocket服务 。Java写的 

@Override
    public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
        registry
        .addHandler(getNodemcuHandle(), "/websocket/nodemcu")
        .addInterceptors(new ClientSocketInterceptor())
        .setAllowedOrigins("*");
    }

 

然后我电脑的IP是  192.168.3.246 ,监听端口是 8081 ,接着

那就直接上一下 websocket代码吧

-- websocket
ws = websocket.createClient()
ws:on("connection", function()
    print('got ws connection')

    function hallInterupt(level, when, eventcount)
        gpio.trig(inputPin)
        -- tmr.delay(70000) -- wait 700 ms
        print('level:',level) -- print level of on pin
        print('when(us):',tmr.now() - when  ) -- print timestamp while interrupt occur
        print('eventcount:', eventcount)--eventcount

        sendData = '{"actionName":"hallData","data":"1"}'
        ws:send(sendData)
        
        gpio.trig(inputPin,"up", hallInterupt)
    end
    
    gpio.trig(inputPin,"up", hallInterupt)
    
end)
ws:on("receive", function(_, msg, opcode)
  print('got message:', msg, opcode) -- opcode 为 1 则是文本消息, 为 2 则是二进制消息
end)
ws:on("close", function(_, status)
  print('connection closed', status)
  connectServer(myIp)
  --ws = nil -- 需要lua释放回收websocket客户端实例
  hallTmr:stop()
end)

ws:connect('ws://192.168.3.246:8081/server/websocket/nodemcu?clientId=testId')

 

 

在ws的 connection事件里面,监听IO的中断,不过目前为止我还不知什么叫中断,毕竟才是一个刚买板子3天的小白。

虽然不懂,但是起码知道 如果霍尔传感器有感应到数据,IO中断就会被触发 。 类似监听器一样 。注意看上面的hallInterupt方法。里面有三个参数,参考官方解释:

callback function when trigger occurs. The level of the specified pin at the interrupt passed as the first parameter to the callback. The timestamp of the event is passed as the second parameter. This is in microseconds and has the same base as for tmr.now(). This timestamp is grabbed at interrupt level and is more consistent than getting the time in the callback function. This timestamp is normally of the first interrupt detected, but, under overload conditions, might be a later one. The eventcount is the number of interrupts that were elided for this callback. This works best for edge triggered interrupts and enables counting of edges. However, beware of switch bounces -- you can get multiple pulses for a single switch closure. Counting works best when the edges are digitally generated. The previous callback function will be used if the function is omitted.

我懒得翻译了。

下面三个print 方法,分别打印了这三个参数.

然后ws:send 是向服务器发送数据 。 websocket 程序至此完毕。

  --- 2021-03-25 更新

第六步:根据获取的数据处理程序

 

 2021-03-24 首次编辑:

不过目前我只做到了第四步。敬请期待吧... 

2021-03-25 更新:

 已经做到第五步了

 

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们