Nodemcu上使用Mongoose OS的坑(1)

下载了微软的Code做编辑环境,其实可以用Visual Studio(官方例子就是VS项目文件)。

{
    // See https://go.microsoft.com/fwlink/?LinkId=733558
    // for the documentation about the tasks.json format
    "version": "2.0.0",
    "tasks": [
        {
            "taskName": "Mongoose build",
            "type": "shell",
            "command": "mos",
            "args": ["build --arch esp8266"],
            "problemMatcher": {
                "owner": "c",
                "fileLocation": ["relative", "${workspaceRoot}"],
                "pattern": {
                    "regexp": "^(.*):(\\d+):(\\d+):\\s+(warning|error):\\s+(.*)$",
                    "file": 1,
                    "line": 2,
                    "column": 3,
                    "severity": 4,
                    "message": 5
                }
              }
        }
    ]
}
tasks.json内容如上。


第一个坑:include路径。


{
            "name": "Win32",
            "includePath": [
                "${workspaceRoot}",
                "${workspaceRoot}/src",
                "${workspaceRoot}/build/gen",
                "D:/mongoose-os-1.5",
                "D:/mongoose-os-1.5/frozen",
                "D:/mongoose-os-1.5/fw/src",
                "D:/mongoose-os-1.5/fw/common",
                "C:/Program Files (x86)/CodeBlocks/MinGW/lib/gcc/mingw32/4.9.2/include",
                "C:/Users/y/.mos/libs-1.14/http-server-1.14/src",
                "C:/Program Files (x86)/CodeBlocks/MinGW/include",
                "C:/Users/y/.mos/libs-1.14"
            ],
            "defines": [
                "_DEBUG",
                "UNICODE"
            ],
            "intelliSenseMode": "msvc-x64",
            "browse": {
                "path": [
                    "${workspaceRoot}",
                    "${workspaceRoot}/src",
                    "${workspaceRoot}/build/gen",
                    "D:/mongoose-os-1.5",
                    "D:/mongoose-os-1.5/frozen",
                    "D:/mongoose-os-1.5/fw/src",
                    "D:/mongoose-os-1.5/fw/common",
                    "C:/Program Files (x86)/CodeBlocks/MinGW/lib/gcc/mingw32/4.9.2/include",
                    "C:/Users/y/.mos/libs-1.14/http-server-1.14/src",
                    "C:/Program Files (x86)/CodeBlocks/MinGW/include",
                    "C:/Users/y/.mos/libs-1.14"
                ],
                "limitSymbolsToIncludedHeaders": true,
                "databaseFilename": ""
            }
        }


是在win7下使用,所以只修改win32这部分。注意 workspaceRoot是项目文件夹的Root。

路径不全,则编译时会提示 未定义的引用。在IDE中也会有波浪下划线,点击#include...,左侧有图标小灯可跳。

正确包含文件后,颜色、样式如下:

Nodemcu上使用Mongoose OS的坑(1)_第1张图片


第2个坑,针脚映射。

注意,MGS是for esp8266等,不是for nodemcu。 所以在函数里访问GPIO的Pin,不能按照Nodemcu板子上的D0, D1针脚来用。

Nodemcu上使用Mongoose OS的坑(1)_第2张图片

如D0是用户指示灯,让它闪烁时,函数mgos_gpio_toggle(16),显然要使用GPIO16 。

Pin号用错了,结果是程序不执行,也不报错(编译可以通过)。

一个完整的main.c如下:

#include "mgos_timers.h"
#include "mgos.h"
#include "common/cs_dbg.h"
#include "common/platform.h"
#include "frozen/frozen.h"
#include "mgos_app.h"
#include "mgos_mongoose.h"
#include "mgos_uart.h"
#include "mgos_gpio.h"
#include "mgos_mongoose.h"
#include "sht1x_v3.h"

#define UART_NO 0
#define led_0 16
#define btn_set 4
#define btn_reset 5

//const struct sys_config_wifi_sta *ap;
char mode_temp = 0x3;
char mode_hum = 0x5;
char info[50];
SHT1x_V3 shtx_v3 = {0, 0, 0.0, 0.0, 0x3, 0, 0};
//ip请求处理
static void ip_handler(struct mg_connection *c, int ev, void *p, void *user_data)
{
  if (ev == MG_EV_HTTP_REPLY)
  {
    struct http_message *hm = (struct http_message *)p;
    if (hm->resp_code == 200)
    {
      int c = json_scanf(hm->message.p, hm->message.len, "ip:%Q", &info);
      if (c > 0)
      {
        //mgos_uart_printf(UART_NO, info);
        printf("get ip:%s", info);
      }
    }
    else
    {
      //mgos_uart_printf(UART_NO, "connect failed\n");
      printf("connect failed");
    }
  }
  (void)user_data;
  (void)c;
}
//ip 定时器
static void cb_ddns_timer(void)
{
  const char *url = "http://ip.chinaz.com/getip.aspx";
  mgos_connect_http(url, ip_handler, NULL);
  //(void)arg;
}

//按键
static void cb_btnset(int pin, void *arg)
{
  //printf("button set \n");
  printf("button set \n");
  (void)pin;
  (void)arg;
}

static void cb_btnreset(int pin, void *arg)
{
  //printf("button reset \n");
  printf("button reset \n");
  (void)pin;
  (void)arg;
}

//心跳信号
static void cb_led_heartbeat(void *arg)
{
  mgos_gpio_toggle(led_0);
  printf("I am here,...\n");
  (void)arg;
}

//读传感器
static void cb_read_sht1x(void *arg)
{
  measure(&shtx_v3, *(char *)arg);
  //(void)arg;
}

/* main entry */
enum mgos_app_init_result mgos_app_init(void)
{
  //timers init
  int result = mgos_timers_init();
  printf("timer init:%d", result);
  mgos_uptime_init();

  //定时取外网IP
  //mgos_set_timer(20000, true, cb_ddns_timer, NULL);
  cb_ddns_timer();
  //更新ddns

  //注册按键
  mgos_gpio_set_button_handler(btn_set, MGOS_GPIO_PULL_UP, MGOS_GPIO_INT_EDGE_POS, 100, cb_btnset, NULL);
  mgos_gpio_set_button_handler(btn_reset, MGOS_GPIO_PULL_UP, MGOS_GPIO_INT_EDGE_POS, 100, cb_btnreset, NULL);

  //heartbeat
  mgos_gpio_set_mode(led_0, MGOS_GPIO_MODE_OUTPUT);
  mgos_set_timer(2000, true, cb_led_heartbeat, NULL);

  //读取温湿度
  connectionreset();
  int id = mgos_set_timer(15 * 1000, true, cb_read_sht1x, &mode_temp);
  printf("read temp timer id:%d", id);
  mgos_set_timer(10 * 1000, true, cb_read_sht1x, &mode_hum);

  // 发送

  return MGOS_APP_INIT_SUCCESS;
}

对于回调函数里的void*参数,参见前一篇的翻译:C的void指针。

你可能感兴趣的:(电脑网络)