May 4, 2017 - ESP8266学习笔记(一)

  最近被好基友拉下水,几次鼓动我在单片机上玩一下micropython。经不住诱惑买来了几片ESP8266模块玩,也赶赶物联网的大潮。

  首先要搞清楚什么是micropython(http://www.micropython.org/)。对于python的大名大部分搞程序的人应该都听说过,但micropython我也是刚听说过。问了一下度娘,原来micropython是一位名叫Damien George的计算机工程师在使用python的时候突发奇想,希望能用python语言来控制单片机。牛人果然不一般,这位老兄用了六个月的时间完成了在STM32单片机上的开发,并且开源了所有源代码。之后,micropython被移植到了很多平台,包括ESP8266。再说说ESP8266。这是一块包含了WIFI控制芯片的soc(片上系统),是乐鑫信息科技(上海)有限公司(http://espressif.com)开发的一款芯片。它的特点是体积小、功耗低、内置了功能丰富的32位单片机,还有最最重要的是廉价。某宝上有买esp8266模块已经基于ESP8266进行了二次开发,集成了外围元件,PCB的印刷天线和usb转uart串口的cp2102芯片,这些东东一起打包只卖十几块钱,简直白菜价,所以此模块不流行简直逆天。
  言归正传,便宜归便宜,我们更关心芯片的易用性如何。网上搜相关的文档,有些是讲通过物理串口烧录的,有些是讲如何烧录Nodemcu的,会让你一头雾水。经检验,此模块使用micropython特别方便,从拿到芯片到进入micropython界面只要以下几步(以windows为例),记录下来为节约大家时间:

  1. 插入usb接口。过了一会儿在windows的设备管理里面自动安装的一个串口设备(COM),记住此串口编号。
  2. 更新固件。
      本机上先安装安装此串口变sthon方便,如需要进入易用性如何, ESPtool.py。这是一个用python开发的针对ESP8266的小工具,可以实现底层的操作。它也是一个开源项目,项目在github上进行托管:https://github.com/themadinventor/esptool
      esptool是基于python2.7,所以安装esptool之前先安装python2.7。用pip安装esptool:
    pip install esptool
    因为esptool需要使用串口,所以还需要安装pyserial:
    pip install pyserial
    擦除:python c:\Python27\Scripts\esptool.py –port /COM4 erase_flash
  3. 烧录micropython固件
      到官方网站http://www.micropython.org/download ,找到Firmware for ESP8266 boards,下载最新的固件,我下载的是esp8266-20170108-v1.8.7.bin。   进入下载固件的目录,烧录命令:
      python c:\Python27\Scripts\esptool.py –port /COM4 –baud 460800 write_flash –flash_size=detect 0 esp8266-20170108-v1.8.7.bin
      成功写入固件之后,你可以用putty等telnet软件通过com口连接esp8266,设置如下。
      正常的情况下会出现如下界面:
      这时你可以在micropython的环境下进行编程了,不过还需要解决一个问题,就是本机写好的程序如何上传上去,请继续下面步骤
  4. 设置wifi 此时如果用电脑搜索热点,会发现有micropython-xxxx的热点,说明wifi也工作正常,你可以试着连接,密码是micropythoN(注意最后一个字母大写哦)。链接成功之后可以用ipconfig查看ip地址。
  5. 实现文件管理
      本来想试试中文教程上的图形工具uPyLoader,结果发现不太稳定,后来试用了一下mpfshell,感觉比较稳定,虽然是命令行界面,但也就这几条命令,使用还是so easy的。 安装mpfshell:
    https://github.com/wendlers/mpfshell 网站上下载最新版mpfshell(选择clone or download下载zip文件)。解压zip文件,在解压目录执行如下命令(注意此工具可以安装在python2或python3环境)。
    pip install pyserial
    pip install colorama
    pip install websocket_client
    python setup.py install
    

    安装完之后发现无法如手册上写的执行mpfshell,原来此文件是一个linux下的shell文件无法在windows下执行,正郁闷之时发现在解压文件的mp目录下有mpfshell.py文件,执行python mpfshell.py进入命令行搞定!
      接下来连接esp8266,在mpfshell中输入:open ws:192.168.4.1(注意:ipconfig看到的是本机的地址,此处需要连接esp8266,非本机ip),发现需要输入密码。什么密码?我可没设置过,但看到提示”webrepl password”,灵光一闪原来需要启动webrepl。在用putty登录micropython之后,输入命令import webrepl_setup,设置webrepl启动,并设定登录密码。此时在回到上面的命令,顺利连接ESP8266。mpfshell的主要命令(看着眼熟,就不多解释了):

    mpfs> ls
    mpfs> put boot.py main.py
    mpfs> mput .*\.py
    mpfs> get boot.py my_boot.py
    mpfs> mget .*\.py
    mpfs> rm boot.py
    mpfs> mrm test.*\.py(remove all remote files)
    mpfs> md test
    mpfs> cd test
    mpfs> rm test
    

  总结:通过esptool烧录micropython固件,可以用串口来执行或者调试程序。用自己喜欢的文本编辑器写好程序,通过mpfshell来上传下载文件,这样一个单片机开发环境便搭建好了。想起自己以前玩单片机,要用烧录软件将编译好的程序一次次刷进flash后再做测试,有种恍如隔世的感觉,套用一句话”科技以人为本”。

本人的更多原创文章请加入个人微信公众号。

Aug 22, 2014 - Jekyll Clean Theme

Welcome to the sample post for the Jekyll Clean theme.

A simple and clean Jekyll theme using bootstrap (not to be confused with jekyll-bootstrap) that’s easy to modify and very modular in component and element reuse.

It uses Disqus for comments and includes Google Analytics support. Both of these features are disabled by default and can be enabled via _config.yml. You can also rip this code out of the templates if you like (footer.html and post.html). The beauty of Jekyll - keep things clean… Jekyll Clean!

The theme works well on mobile phones, using a collapsable nav bar and hiding the sidebar. The links pane in the sidebar is available on mobile through the nav menu, and you can do the same thing for any other sections added to the sidebar.

Don’t forget to occassionally merge against my upstream repository so you can get the latest changes. Pull requests are encouraged and accepted!

Installation

If you don’t have a blog already on github, start by cloning this repository. Best to do that directly on github and then clone that down to your computer.

If you already do have a blog, You can certainly apply this theme to your existing blog in place, but then you won’t be able to merge as the theme changes. If you re-apply your blog history on top of this theme’s gh-pages branch, it’s then easy to update to the latest version of the theme. You also don’t want to have to deal with resolving old conflicts from your existing history, so you may wish to to push your existing master off to a new branch so you have the old history and start a new branch with this as the start, merging in your _posts and other assets (after git rm’ing the current _posts.

Not ideal, but you have to make a choice - either apply it manually or base your blog off this theme’s branch. Either way it will work, and both have their own pros and cons.

You can setup an upstream tracking repository like so:

$ git remote add upstream git@github.com:scotte/jekyll-clean.git

And now when you wish to merge your own branch onto the latest version of the theme, simply do:

$ git fetch upstream
$ git merge upstream/gh-pages

Of course you will have to resolve conflicts for _config.yml, _includes/links-list.html, and _posts, and so on, but in practice this is pretty simple.

This is how I maintain my own blog which is based on this theme. The old history is sitting in an old-master branch that I can refer to when I need to.

Running Locally

Here’s the exact set of packages I need to install on Debian to run jekyll locally with this theme for testing.

$ sudo aptitude install ruby ruby-dev rubygems nodejs
$ sudo gem install jekyll jekyll-paginate

And then it’s just a simple matter of running jekyll locally:

$ jekyll serve --baseurl=''

Now browse to http://127.0.0.1:4000

Using gh-pages

Running a jekyll site is a bit outside the scope of this doc, but sometimes it can be a bit confusing how to configure jekyll for project pages versus user pages, for example.

To start with, read through the documentation here. This will provide a good overview on how it all works. The git branch and baseurl (in _config.yml) will change depending on the sort of site deployed.

When you clone this repository, it’s set up for project pages, so the deployed branch is “gh-pages” and baseurl is configured to ‘jekyll-clean’, because that’s the name of this project.

If you plan to deploy this as user pages, the deployed branch is “master” and baseurl is configured to ‘’ (i.e. empty).

Comment Systems

Jekyll clean supports both isso and disqus comment systems.

After enabling comments, either isso or disquss must be configured. Don’t try configuring both!

Isso Comments

Isso requires running a local server, so is not suitable for hosting in github pages, for example. Isso is open source and keeps all your data local, unlike Disqus (who knows exactly what they are doing with your data).

In _config.yml you’ll need to set isso to the fully-qualified URL if your isso server (this is the value for data-isso passed to the isso JS). Make sure comments is true.

Disqus Comments

Getting Disqus to work can be a bit more work than it seems like it should be. Make sure your Disqus account is correctly configured with the right domain of your blog and you know your Disqus shortname.

In _config.yml you’ll need to set disqus to your Disqus shortname and make sure comments is true.

Finally, in posts, make sure you have comments: true in the YAML front matter.

More information on using Disqus with Jekyll is documented here.

Code Syntax Highlighting

To use code syntax highlighting, use the following syntax:

```python
import random

# Roll the die
roll = random.randint(1, 20)
print('You rolled a %d.' % roll)
``` #REMOVE

(Remove #REMOVE from the end of the last line). Which will look like this in the rendered jekyll output using the default css/syntax.css provided with this theme (which is the colorful theme from https://github.com/iwootten/jekyll-syntax):

import random

# Roll the die
roll = random.randint(1, 20)
print('You rolled a %d.' % roll)

You can, of course, use any theme you wish, see the jekyll and pygments documentation for more details.

License

The content of this theme is distributed and licensed under a License Badge Creative Commons Attribution 4.0 License

This license lets others distribute, remix, tweak, and build upon your work,
even commercially, as long as they credit you for the original creation. This
is the most accommodating of licenses offered. Recommended for maximum
dissemination and use of licensed materials.

In other words: you can do anything you want with this theme on any site, just please provide a link to the original theme on github so I get credit for the original design. Beyond that, have at it!

This theme includes the following files which are the properties of their respective owners:

Jul 22, 2014 - Lorem ipsum dolor sit amet

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed quam metus, commodo sit amet ante a, finibus efficitur lorem. Maecenas egestas purus in tempor volutpat. Sed dapibus tortor nec sem suscipit ullamcorper. Nulla nec lorem lacus. Phasellus condimentum massa quis dolor consequat viverra ut ac magna. Ut a consequat nisi. Vivamus at leo ut turpis convallis lacinia. Curabitur eu placerat quam. Donec ultricies faucibus dui, a tincidunt lorem lobortis condimentum.

Quisque aliquet consectetur justo sit amet convallis. Nunc vel aliquet ipsum, sit amet elementum justo. Vivamus id magna mi. Cras luctus est vel ipsum sagittis pellentesque. Vivamus ante elit, porttitor vitae quam quis, fermentum malesuada risus. Integer nec lectus vel lacus cursus tristique in euismod ipsum. Duis ut varius enim. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Morbi et elit eu tortor lacinia sollicitudin non in lacus.

Integer at viverra turpis. Duis aliquam mattis sapien tempor eleifend. Mauris nec eleifend risus, quis mollis neque. Vivamus non dapibus justo, vel ornare magna. Nunc in nulla venenatis, imperdiet diam in, accumsan massa. Etiam congue augue ipsum, sit amet rutrum nisi blandit quis. Cras in lectus non lorem auctor consequat a vel sem. Aliquam erat volutpat. Fusce lobortis vel orci vitae ullamcorper. Phasellus id eleifend eros. Mauris vulputate, nisi vel auctor auctor, quam enim tincidunt felis, vitae fermentum odio tortor eget est. Integer ornare blandit lectus a accumsan.