【ESP8266】移植cJSON到ESP8266
前言由于ESP826自带的JSON API实在太难调用,就想着能不能自己移植一个JSON库。最近做STM32的时候正好需要移植JSON库,找了一下,找到了cJSON和Jansson。这篇文章是简单总结移植cJSON到ESP8266要注意的事项。 开发环境
移植步骤首先到Github上下载cJSON代码:https://github.com/DaveGamble/cJSON
然后我们只使用cJSON和test的源文件及头文件即可,导入到ESP8266工程里。 点击编译,自然会出现很多错误,下面简单说明一下。 一、添加文件在cJSON.c里添加下面头文件,这些头文件是ESP8266专用的。 #include "c_types.h"
#include "osapi.h"
#include "mem.h"
#include "ets_sys.h"
#include "osapi.h"
二、替换malloc和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
三、注释钩子函数其后,注释掉 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
}
四、替换其他函数替换 五、注释浮点有关语句注释掉所有和浮点型有关的语句,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;
}
同样,找到有 六、添加宏定义所有的函数,函数名前面都要添加ICACHE_FLASH_ATTR宏定义,这个是让函数保存在Flash,需要用到的时候才读取到cache中运行,不加这个宏定义,会报内存不够用的错误。示例: const char * ICACHE_FLASH_ATTR
cJSON_GetErrorPtr(void) {return global_ep;}
别忘了test.c文件的函数也要添加。 七、改函数名把test.c里的main函数改成其他函数名,例如 串口打印成功编译后,烧写代码,打开串口,可以看到如下调试信息: 如果编译后还有问题,请自行调试。 工程下载我上传了工程,如果想看源代码或者想直接用本人移植好的cJSON都可以下载。 另外放了主要工程在Github:(该链接已删,请参考后文的链接) 其他如果想了解如何使用ESP8266官方SDK里的JSON API,可以参考这篇文章:http://www.52php.cn/article/p-hztgdldw-kd.html 更新更新cJSON版本到1.5.3,基于ESP8266 NONOS SDK 2.0测试。 Github:https://github.com/AngelLiang/ESP8266-Demos 本文最后编辑时间:2017年12月 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |