【ESP8266】移植cJSON到ESP8266

来源:互联网 发布:淘宝企业店个体工商户 编辑:程序博客网 时间:2024/06/06 09:05

前言

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

开发环境

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

移植步骤

首先到Github上下载cJSON代码:https://github.com/DaveGamble/cJSON

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

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

一、添加文件

在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 0static 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_ATTRcJSON_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里的内容替换。
osapi.h

五、注释浮点有关语句

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

/* Parse the input text to generate a number, and populate the result into item. */static const char *ICACHE_FLASH_ATTRparse_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_ATTRcJSON_GetErrorPtr(void) {return global_ep;}

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

七、改函数名

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

串口打印

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

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

工程下载

我上传了工程,如果想看源代码或者想直接用本人移植好的cJSON都可以下载。
http://download.csdn.net/detail/u012163234/9644254

另外放了主要代码在Github:
Github:https://github.com/AngelLiang/cJSON-for-ESP8266

其他

如果想了解如何使用ESP8266官方SDK里的JSON API,可以参考这篇文章:http://blog.csdn.net/yannanxiu/article/details/50911357


本文最后编辑时间:2017年1月

1 0
原创粉丝点击