自己编译NodeMCU固件 & 提交数据到服务器

来源:互联网 发布:幼儿教师网络研修作业 编辑:程序博客网 时间:2024/05/22 02:33

自己编译NodeMCU固件

之前这篇文章nodeMCU使用小记里用到的固件是比较老的固件,不带http模块,所以无法实现http的post请求。所以……在云构建固件没法用的情况下,还得自己编译固件啊。。
编译固件的系统环境是ubuntu-14.04.4-desktop-i386,步骤如下:
先安装编译工具链esp-open-sdk:

sudo apt-get install make unrar-free autoconf automake libtool gcc g++ gperf flex bison texinfo gawk ncurses-dev libexpat-dev python-dev python python-serial sed git unzip bash help2man wget bzip2git clone --recursive https://github.com/pfalcon/esp-open-sdk.gitcd esp-open-sdkmake最后按照提示把环境变量加到/etc/profile里,再source一下

再克隆nodemcu项目进行编译:

git clone https://github.com/nodemcu/nodemcu-firmware.gitcd nodemcu-firmware(有很多modules是可选项,可在app/include/user_modules.h将其注释掉)(波特率在app/include/user_config.h里定义)(Integer build选项也在app/include/user_config.h里定义)(在app/include/user_version.h里自定义个人签名)make

最后刷固件:

# 首选把自己加入对话群,不然没法访问串口sudo adduser ph dialout# 把固件上载到nodemcu里make flash #这里提示是512kb还是4m,自己选

到这里固件就刷好了,效果如图:
这里写图片描述


参考:
esp-open-sdk
物联网应用——从源代码为ESP8266创建NodeMCU
NodeMCU Build Options
ubuntu 将当前用户加入到串口所在组

NodeMCU提交数据到服务器

NodeMCU端init.lua:

-- init.luaprint(wifi.sta.getip())print('Setting up WIFI...')wifi.setmode(wifi.STATION)wifi.sta.config('test', '11223344')wifi.sta.connect() -- 要想提交数据到远程服务器,首先得连上互联网啊2333tmr.alarm(1, 1000, tmr.ALARM_AUTO, function()    if wifi.sta.getip() == nil then        print('Waiting for IP ...')    else        print('IP is ' .. wifi.sta.getip())            http.post('http://192.168.223.193/nodemcu_data_process.php', -- 服务器ip是192.168.223.193                    'Content-Type: application/json\r\n',                    '{"name":"hello"}',                    function(code, data)                        if (code < 0) then                            print("HTTP request failed")                        else                             print(code, data)                        end            end)        tmr.stop(1)    endend)

PHP服务端代码:

<?php$str = file_get_contents ( "php://input" );$args = (json_decode ( $str ));$user_name = isset ( $args->name ) ? $args->name : null;$message = array (        "type" => 0,        "name" => $user_name );$json_string=json_encode($message);error_log($json_string,3,'/opt/lampp/logs/nodemcu.log');echo $json_string;?>

效果如图:
nodemcu端:
这里写图片描述
xampp端:
这里写图片描述


参考:
NodeMCU教程 http.post请求及服务端接收不到参数解决方案
项目部署到远程Linux服务器