前言

由于ESP826自带的JSON API实在太难调用,就想着能不能自己移植一个JSON库。最近做STM32的时候正好需要移植JSON库,找了一下,找到了cJSON和Jansson。这篇文章是简单总结移植cJSON到ESP8266要注意的事项。

开发环境

  • ESP8266 SDK版本:1.5.2
  • IDE:ESP8266 IDE v2.0
  • 操作系统:64位 Win10

移植步骤

2017年12月5日更新

由于写本文时间久远当初也没有记录,已经不记得是用多少版本的cJSON移植了。最新版本的cJSON移植过来不止下面一点麻烦。

然后我们只使用cJSONtest的源文件及头文件即可,导入到ESP8266工程里。

hall库连接esp8266 esp8266库如何使用_#include

点击编译,自然会出现很多错误,下面简单说明一下。

一、添加文件

在cJSON.c里添加下面头文件,这些头文件是ESP8266专用的。

#include "c_types.h"
#include "osapi.h"
#include "mem.h"
#include "ets_sys.h"
#include "osapi.h"

二、替换malloc和free

注释掉cJSON_malloccJSON_free,用ESP8266自带的os_mallocos_free代替,由于直接赋值会编译出错,所以用了宏定义的方式。

#if 0
static void *(*cJSON_malloc)(size_t sz) = os_malloc;
static void (*cJSON_free)(void *ptr) = os_free;
#else
#define cJSON_malloc(sz) os_malloc(sz)
#define cJSON_free(ptr)  os_free(ptr)
#endif

三、注释钩子函数

其后,注释掉cJSON_InitHooks里面的内容,这个一般用不到所以注释掉也没关系。

void ICACHE_FLASH_ATTR
cJSON_InitHooks(cJSON_Hooks* hooks)
{
#if 0
    if (!hooks) { /* Reset hooks */
        cJSON_malloc = malloc;
        cJSON_free = free;
        return;
    }

    cJSON_malloc = (hooks->malloc_fn)?hooks->malloc_fn:malloc;
    cJSON_free   = (hooks->free_fn)?hooks->free_fn:free;
#endif
}

四、替换其他函数

替换os_mallocos_free后,还要替换printfmemsetmemcpy等等,请对照osapi.h里的内容替换。

hall库连接esp8266 esp8266库如何使用_hall库连接esp8266_02

五、注释浮点有关语句

注释掉所有和浮点型有关的语句,ESP8266不支持浮点型,所以我们也不需要它,免得编译出错。例如注释下面的语句

/* Parse the input text to generate a number, and populate the result into item. */
static const char *ICACHE_FLASH_ATTR
parse_number(cJSON *item,const char *num)
{
    double n=0,sign=1,scale=0;int subscale=0,signsubscale=1;

    if (*num=='-') sign=-1,num++;   /* Has sign? */
    if (*num=='0') num++;           /* is zero */
    if (*num>='1' && *num<='9') do  n=(n*10.0)+(*num++ -'0');   while (*num>='0' && *num<='9'); /* Number? */
    if (*num=='.' && num[1]>='0' && num[1]<='9') {num++;        do  n=(n*10.0)+(*num++ -'0'),scale--; while (*num>='0' && *num<='9');}  /* Fractional part? */
    if (*num=='e' || *num=='E')     /* Exponent? */
    {   num++;if (*num=='+') num++; else if (*num=='-') signsubscale=-1,num++;      /* With sign? */
        while (*num>='0' && *num<='9') subscale=(subscale*10)+(*num++ - '0');   /* Number? */
    }

    //注释掉这条语句
    //n=sign*n*pow(10.0,(scale+subscale*signsubscale)); /* number = +/- number.fraction * 10^+/- exponent */

    item->valuedouble=n;
    item->valueint=(int)n;
    item->type=cJSON_Number;
    return num;
}

同样,找到有fabs函数和floor函数的语句,一并注释掉。
注释掉导入float.h的语句。

六、添加宏定义

所有的函数,函数名前面都要添加ICACHE_FLASH_ATTR宏定义,这个是让函数保存在Flash,需要用到的时候才读取到cache中运行,不加这个宏定义,会报内存不够用的错误。示例:

const char * ICACHE_FLASH_ATTR
cJSON_GetErrorPtr(void) {return global_ep;}

别忘了test.c文件的函数也要添加。

七、改函数名

把test.c里的main函数改成其他函数名,例如cJSON_TEST。之后在user_init初始化函数里调用这个函数即可。

串口打印

成功编译后,烧写代码,打开串口,可以看到如下调试信息:

hall库连接esp8266 esp8266库如何使用_ESP8266_03

如果编译后还有问题,请自行调试。

工程下载

我上传了工程,如果想看源代码或者想直接用本人移植好的cJSON都可以下载。


更新

更新cJSON版本到1.5.3,基于ESP8266 NONOS SDK 2.0测试。