JSON ESP8266 NONOS SDK TUTORIAL - HƯỚNG DẪN SỬ DỤNG JSON TRÊN ESP8266 NONOS SDK
Hello everyone!
Today, I will show you how to use JSON API on ESP8266 NONOS SDK.
Firstly, we need to have some things:
- ESP8266 NONOS SDK Version 3.0 (https://www.espressif.com/en/products/hardware/esp8266ex/resources).
- Compiler (I'm using SlickEdit 16.0, you can use sublime text or others ...).
- ESP8266 Lubuntu.
- Serial monitor tool.
Now, get started!
Step 1: Copy 02 files ESP8266_NONOS_SDK-3.0\examples\IoT_Demo\user\user_json.c and ESP8266_NONOS_SDK-3.0\examples\IoT_Demo\include\user_json.h to your project folder as the image below:
Step 2: go to user_main.c in user folder and edit them:
- Create JSON Objects and function to call back.
LOCAL int ICACHE_FLASH_ATTR
json_get(struct jsontree_context *js_ctx)
{
os_printf("\njson get\n");
const char *path = jsontree_path_name(js_ctx, js_ctx->depth - 1);
if (os_strncmp(path, "switch", 6) == 0) {
os_printf("\r\n write the switch \r\n");
jsontree_write_string(js_ctx, "off");
}
else if (os_strncmp(path, "thecat", 6) == 0) {
os_printf("\r\n write to thecat \r\n");
jsontree_write_string(js_ctx, "male");
}
else if (os_strncmp(path, "thedog", 6) == 0) {
os_printf("\r\n write to thedog \r\n");
jsontree_write_string(js_ctx, "black");
}
return 0;
}
json_get(struct jsontree_context *js_ctx)
{
os_printf("\njson get\n");
const char *path = jsontree_path_name(js_ctx, js_ctx->depth - 1);
if (os_strncmp(path, "switch", 6) == 0) {
os_printf("\r\n write the switch \r\n");
jsontree_write_string(js_ctx, "off");
}
else if (os_strncmp(path, "thecat", 6) == 0) {
os_printf("\r\n write to thecat \r\n");
jsontree_write_string(js_ctx, "male");
}
else if (os_strncmp(path, "thedog", 6) == 0) {
os_printf("\r\n write to thedog \r\n");
jsontree_write_string(js_ctx, "black");
}
return 0;
}
LOCAL int ICACHE_FLASH_ATTR
json_set(struct jsontree_context *js_ctx, struct jsonparse_state *parser)
{
os_printf("\n==================>json set state: %d\n", js_ctx->callback_state);
int type;
while ((type = jsonparse_next(parser)) != 0) {
if (type == JSON_TYPE_PAIR_NAME) {
char buffer[10];
os_bzero(buffer, 10);
os_printf("JSON_TYPE_PAIR\n");
jsonparse_next(parser);
jsonparse_next(parser);
uint32_t jslen = jsonparse_get_len(parser);
char buff[jslen];
jsonparse_copy_value(parser, buff, jslen + 1);
int i = 0;
for (i = 0; i < jslen + 1; i++) {
os_printf("%c", buff[i]);
}
os_printf("\r\n");
#if 0
if (jsonparse_strcmp_value(parser, "name") == 0) {
os_printf("\n---name---\n");
jsonparse_next(parser);
jsonparse_next(parser);
jsonparse_copy_value(parser, buffer, sizeof(buffer));
int i = 0;
for (i = 0; i < 10; i++) {
os_printf("%c", buffer[i]);
}
os_printf("\n");
if (!strcoll(buffer, "John")) {
os_printf("=====John==========\n");
uint32_t jslen = jsonparse_get_len(parser);
os_printf("=>>>>>>%d>>>\r\n", jslen);
}
}
if (jsonparse_strcmp_value(parser, "age") == 0) {
os_printf("\n===age\n");
uint32_t jslen = jsonparse_get_len(parser);
os_printf("=>>>>>>%d>>>\r\n", jslen);
jsonparse_next(parser);
jsonparse_next(parser);
jsonparse_copy_value(parser, buffer, sizeof(buffer));
if (!strcoll(buffer, "31")) {
os_printf("=====31==========\n");
}
}
if (jsonparse_strcmp_value(parser, "city") == 0) {
jsonparse_next(parser);
jsonparse_next(parser);
os_printf("=>>>>>>city>>>\r\n");
uint32_t jslen = jsonparse_get_len(parser);
os_printf("=>>>>>>%d>>>\r\n", jslen);
char buff[jslen];
jsonparse_copy_value(parser, buff, jslen + 1);
int i = 0;
for (i = 0; i < jslen; i++) {
os_printf("%c", buff[i]);
}
}
#endif
}
}
return 0;
}
LOCAL struct jsontree_callback switch_callback =
JSONTREE_CALLBACK(json_get, json_set);
// 01 objecs
// JSONTREE_OBJECT(switch_tree,
// JSONTREE_PAIR("switch", &switch_callback)
// );
// multy objects
JSONTREE_OBJECT(switch_tree,
JSONTREE_PAIR("thecat", &switch_callback),
JSONTREE_PAIR("switch", &switch_callback),
JSONTREE_PAIR("thedog", &switch_callback)
);
- json_get function: generater String as JSON format.
- switch_callback: call back function when set or get json format string.
- switch_tree: name of json object.
LOCAL void ICACHE_FLASH_ATTR
JSON_TEST_FUNC(void){
int i = 0;
#if 1
// test write json test
char *json_buf = NULL;
json_buf = (char *)os_zalloc(jsonSize);
os_printf("\r\n sizeof before: %d \r\n", os_strlen(json_buf));
//json_ws_send((struct jsontree_value *)&switch_tree, "devices", json_buf); // 1 object
json_ws_send((struct jsontree_value *)&switch_tree, "thecat, switch, thedog", json_buf); // multy objects
os_printf("\r\n sizeof after: %d \r\n", os_strlen(json_buf));
for (i = 0; i < os_strlen(json_buf); i++) {
os_printf("%c", json_buf[i]);
}
os_printf("\r\n");
#endif
#if 0
// read json - OK
char *data_buf = "{\"name\": \"John\", \"age\": \"31\", \"city\" : \"New York\"}";
struct jsontree_context js;
jsontree_setup(&js, (struct jsontree_value *)&switch_tree, json_putchar);
json_parse(&js, data_buf);
#endif
}
JSON_TEST_FUNC(void){
int i = 0;
#if 1
// test write json test
char *json_buf = NULL;
json_buf = (char *)os_zalloc(jsonSize);
os_printf("\r\n sizeof before: %d \r\n", os_strlen(json_buf));
//json_ws_send((struct jsontree_value *)&switch_tree, "devices", json_buf); // 1 object
json_ws_send((struct jsontree_value *)&switch_tree, "thecat, switch, thedog", json_buf); // multy objects
os_printf("\r\n sizeof after: %d \r\n", os_strlen(json_buf));
for (i = 0; i < os_strlen(json_buf); i++) {
os_printf("%c", json_buf[i]);
}
os_printf("\r\n");
#endif
#if 0
// read json - OK
char *data_buf = "{\"name\": \"John\", \"age\": \"31\", \"city\" : \"New York\"}";
struct jsontree_context js;
jsontree_setup(&js, (struct jsontree_value *)&switch_tree, json_putchar);
json_parse(&js, data_buf);
#endif
}
FULL CODE:
/*
* ESPRESSIF MIT License
*
* Copyright (c) 2016 <ESPRESSIF SYSTEMS (SHANGHAI) PTE LTD>
*
* Permission is hereby granted for use on ESPRESSIF SYSTEMS ESP8266 only, in which case,
* it is free of charge, to any person obtaining a copy of this software and associated
* documentation files (the "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the Software is furnished
* to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all copies or
* substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
*/
#include "ets_sys.h"
#include "osapi.h"
#include "user_interface.h"
#include "user_json.h"
#include "mem.h"
#if ((SPI_FLASH_SIZE_MAP == 0) || (SPI_FLASH_SIZE_MAP == 1))
#error "The flash map is not supported"
#elif (SPI_FLASH_SIZE_MAP == 2)
#define SYSTEM_PARTITION_OTA_SIZE 0x6A000
#define SYSTEM_PARTITION_OTA_2_ADDR 0x81000
#define SYSTEM_PARTITION_RF_CAL_ADDR 0xfb000
#define SYSTEM_PARTITION_PHY_DATA_ADDR 0xfc000
#define SYSTEM_PARTITION_SYSTEM_PARAMETER_ADDR 0xfd000
#define SYSTEM_PARTITION_CUSTOMER_PRIV_PARAM_ADDR 0x7c000
#elif (SPI_FLASH_SIZE_MAP == 3)
#define SYSTEM_PARTITION_OTA_SIZE 0x6A000
#define SYSTEM_PARTITION_OTA_2_ADDR 0x81000
#define SYSTEM_PARTITION_RF_CAL_ADDR 0x1fb000
#define SYSTEM_PARTITION_PHY_DATA_ADDR 0x1fc000
#define SYSTEM_PARTITION_SYSTEM_PARAMETER_ADDR 0x1fd000
#define SYSTEM_PARTITION_CUSTOMER_PRIV_PARAM_ADDR 0x7c000
#elif (SPI_FLASH_SIZE_MAP == 4)
#define SYSTEM_PARTITION_OTA_SIZE 0x6A000
#define SYSTEM_PARTITION_OTA_2_ADDR 0x81000
#define SYSTEM_PARTITION_RF_CAL_ADDR 0x3fb000
#define SYSTEM_PARTITION_PHY_DATA_ADDR 0x3fc000
#define SYSTEM_PARTITION_SYSTEM_PARAMETER_ADDR 0x3fd000
#define SYSTEM_PARTITION_CUSTOMER_PRIV_PARAM_ADDR 0x7c000
#elif (SPI_FLASH_SIZE_MAP == 5)
#define SYSTEM_PARTITION_OTA_SIZE 0x6A000
#define SYSTEM_PARTITION_OTA_2_ADDR 0x101000
#define SYSTEM_PARTITION_RF_CAL_ADDR 0x1fb000
#define SYSTEM_PARTITION_PHY_DATA_ADDR 0x1fc000
#define SYSTEM_PARTITION_SYSTEM_PARAMETER_ADDR 0x1fd000
#define SYSTEM_PARTITION_CUSTOMER_PRIV_PARAM_ADDR 0xfc000
#elif (SPI_FLASH_SIZE_MAP == 6)
#define SYSTEM_PARTITION_OTA_SIZE 0x6A000
#define SYSTEM_PARTITION_OTA_2_ADDR 0x101000
#define SYSTEM_PARTITION_RF_CAL_ADDR 0x3fb000
#define SYSTEM_PARTITION_PHY_DATA_ADDR 0x3fc000
#define SYSTEM_PARTITION_SYSTEM_PARAMETER_ADDR 0x3fd000
#define SYSTEM_PARTITION_CUSTOMER_PRIV_PARAM_ADDR 0xfc000
#else
#error "The flash map is not supported"
#endif
#define SYSTEM_PARTITION_CUSTOMER_PRIV_PARAM SYSTEM_PARTITION_CUSTOMER_BEGIN
uint32 priv_param_start_sec;
static const partition_item_t at_partition_table[] = {
{ SYSTEM_PARTITION_BOOTLOADER, 0x0, 0x1000},
{ SYSTEM_PARTITION_OTA_1, 0x1000, SYSTEM_PARTITION_OTA_SIZE},
{ SYSTEM_PARTITION_OTA_2, SYSTEM_PARTITION_OTA_2_ADDR, SYSTEM_PARTITION_OTA_SIZE},
{ SYSTEM_PARTITION_RF_CAL, SYSTEM_PARTITION_RF_CAL_ADDR, 0x1000},
{ SYSTEM_PARTITION_PHY_DATA, SYSTEM_PARTITION_PHY_DATA_ADDR, 0x1000},
{ SYSTEM_PARTITION_SYSTEM_PARAMETER, SYSTEM_PARTITION_SYSTEM_PARAMETER_ADDR, 0x3000},
{ SYSTEM_PARTITION_CUSTOMER_PRIV_PARAM, SYSTEM_PARTITION_CUSTOMER_PRIV_PARAM_ADDR, 0x1000},
};
void ICACHE_FLASH_ATTR user_pre_init(void)
{
if(!system_partition_table_regist(at_partition_table, sizeof(at_partition_table)/sizeof(at_partition_table[0]),SPI_FLASH_SIZE_MAP)) {
os_printf("system_partition_table_regist fail\r\n");
while(1);
}
}
LOCAL int ICACHE_FLASH_ATTR
json_get(struct jsontree_context *js_ctx)
{
os_printf("\njson get\n");
const char *path = jsontree_path_name(js_ctx, js_ctx->depth - 1);
if (os_strncmp(path, "switch", 6) == 0) {
os_printf("\r\n write the switch \r\n");
jsontree_write_string(js_ctx, "off");
}
else if (os_strncmp(path, "thecat", 6) == 0) {
os_printf("\r\n write to thecat \r\n");
jsontree_write_string(js_ctx, "male");
}
else if (os_strncmp(path, "thedog", 6) == 0) {
os_printf("\r\n write to thedog \r\n");
jsontree_write_string(js_ctx, "black");
}
return 0;
}
LOCAL int ICACHE_FLASH_ATTR
json_set(struct jsontree_context *js_ctx, struct jsonparse_state *parser)
{
os_printf("\n==================>json set state: %d\n", js_ctx->callback_state);
int type;
while ((type = jsonparse_next(parser)) != 0) {
if (type == JSON_TYPE_PAIR_NAME) {
char buffer[10];
os_bzero(buffer, 10);
os_printf("JSON_TYPE_PAIR\n");
jsonparse_next(parser);
jsonparse_next(parser);
uint32_t jslen = jsonparse_get_len(parser);
char buff[jslen];
jsonparse_copy_value(parser, buff, jslen + 1);
int i = 0;
for (i = 0; i < jslen + 1; i++) {
os_printf("%c", buff[i]);
}
os_printf("\r\n");
#if 0
if (jsonparse_strcmp_value(parser, "name") == 0) {
os_printf("\n---name---\n");
jsonparse_next(parser);
jsonparse_next(parser);
jsonparse_copy_value(parser, buffer, sizeof(buffer));
int i = 0;
for (i = 0; i < 10; i++) {
os_printf("%c", buffer[i]);
}
os_printf("\n");
if (!strcoll(buffer, "John")) {
os_printf("=====John==========\n");
uint32_t jslen = jsonparse_get_len(parser);
os_printf("=>>>>>>%d>>>\r\n", jslen);
}
}
if (jsonparse_strcmp_value(parser, "age") == 0) {
os_printf("\n===age\n");
uint32_t jslen = jsonparse_get_len(parser);
os_printf("=>>>>>>%d>>>\r\n", jslen);
jsonparse_next(parser);
jsonparse_next(parser);
jsonparse_copy_value(parser, buffer, sizeof(buffer));
if (!strcoll(buffer, "31")) {
os_printf("=====31==========\n");
}
}
if (jsonparse_strcmp_value(parser, "city") == 0) {
jsonparse_next(parser);
jsonparse_next(parser);
os_printf("=>>>>>>city>>>\r\n");
uint32_t jslen = jsonparse_get_len(parser);
os_printf("=>>>>>>%d>>>\r\n", jslen);
char buff[jslen];
jsonparse_copy_value(parser, buff, jslen + 1);
int i = 0;
for (i = 0; i < jslen; i++) {
os_printf("%c", buff[i]);
}
}
#endif
}
}
return 0;
}
LOCAL struct jsontree_callback switch_callback =
JSONTREE_CALLBACK(json_get, json_set);
// 01 objecs
// JSONTREE_OBJECT(switch_tree,
// JSONTREE_PAIR("switch", &switch_callback)
// );
// multy objects
JSONTREE_OBJECT(switch_tree,
JSONTREE_PAIR("thecat", &switch_callback),
JSONTREE_PAIR("switch", &switch_callback),
JSONTREE_PAIR("thedog", &switch_callback)
);
LOCAL void ICACHE_FLASH_ATTR
JSON_TEST_FUNC(void){
int i = 0;
#if 1
// test write json test
char *json_buf = NULL;
json_buf = (char *)os_zalloc(jsonSize);
os_printf("\r\n sizeof before: %d \r\n", os_strlen(json_buf));
//json_ws_send((struct jsontree_value *)&switch_tree, "devices", json_buf); // 1 object
json_ws_send((struct jsontree_value *)&switch_tree, "thecat, switch, thedog", json_buf); // multy objects
os_printf("\r\n sizeof after: %d \r\n", os_strlen(json_buf));
for (i = 0; i < os_strlen(json_buf); i++) {
os_printf("%c", json_buf[i]);
}
os_printf("\r\n");
#endif
#if 0
// read json - OK
char *data_buf = "{\"name\": \"John\", \"age\": \"31\", \"city\" : \"New York\"}";
struct jsontree_context js;
jsontree_setup(&js, (struct jsontree_value *)&switch_tree, json_putchar);
json_parse(&js, data_buf);
#endif
}
/******************************************************************************
* FunctionName : user_init
* Description : entry of user application, init user function here
* Parameters : none
* Returns : none
*******************************************************************************/
void ICACHE_FLASH_ATTR
user_init(void)
{
partition_item_t partition_item;
os_printf("SDK version:%s\n", system_get_sdk_version());
if (!system_partition_get_item(SYSTEM_PARTITION_CUSTOMER_PRIV_PARAM, &partition_item)) {
os_printf("Get partition information fail\n");
}
priv_param_start_sec = partition_item.addr/SPI_FLASH_SEC_SIZE;
JSON_TEST_FUNC();
}
* ESPRESSIF MIT License
*
* Copyright (c) 2016 <ESPRESSIF SYSTEMS (SHANGHAI) PTE LTD>
*
* Permission is hereby granted for use on ESPRESSIF SYSTEMS ESP8266 only, in which case,
* it is free of charge, to any person obtaining a copy of this software and associated
* documentation files (the "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the Software is furnished
* to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all copies or
* substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
*/
#include "ets_sys.h"
#include "osapi.h"
#include "user_interface.h"
#include "user_json.h"
#include "mem.h"
#if ((SPI_FLASH_SIZE_MAP == 0) || (SPI_FLASH_SIZE_MAP == 1))
#error "The flash map is not supported"
#elif (SPI_FLASH_SIZE_MAP == 2)
#define SYSTEM_PARTITION_OTA_SIZE 0x6A000
#define SYSTEM_PARTITION_OTA_2_ADDR 0x81000
#define SYSTEM_PARTITION_RF_CAL_ADDR 0xfb000
#define SYSTEM_PARTITION_PHY_DATA_ADDR 0xfc000
#define SYSTEM_PARTITION_SYSTEM_PARAMETER_ADDR 0xfd000
#define SYSTEM_PARTITION_CUSTOMER_PRIV_PARAM_ADDR 0x7c000
#elif (SPI_FLASH_SIZE_MAP == 3)
#define SYSTEM_PARTITION_OTA_SIZE 0x6A000
#define SYSTEM_PARTITION_OTA_2_ADDR 0x81000
#define SYSTEM_PARTITION_RF_CAL_ADDR 0x1fb000
#define SYSTEM_PARTITION_PHY_DATA_ADDR 0x1fc000
#define SYSTEM_PARTITION_SYSTEM_PARAMETER_ADDR 0x1fd000
#define SYSTEM_PARTITION_CUSTOMER_PRIV_PARAM_ADDR 0x7c000
#elif (SPI_FLASH_SIZE_MAP == 4)
#define SYSTEM_PARTITION_OTA_SIZE 0x6A000
#define SYSTEM_PARTITION_OTA_2_ADDR 0x81000
#define SYSTEM_PARTITION_RF_CAL_ADDR 0x3fb000
#define SYSTEM_PARTITION_PHY_DATA_ADDR 0x3fc000
#define SYSTEM_PARTITION_SYSTEM_PARAMETER_ADDR 0x3fd000
#define SYSTEM_PARTITION_CUSTOMER_PRIV_PARAM_ADDR 0x7c000
#elif (SPI_FLASH_SIZE_MAP == 5)
#define SYSTEM_PARTITION_OTA_SIZE 0x6A000
#define SYSTEM_PARTITION_OTA_2_ADDR 0x101000
#define SYSTEM_PARTITION_RF_CAL_ADDR 0x1fb000
#define SYSTEM_PARTITION_PHY_DATA_ADDR 0x1fc000
#define SYSTEM_PARTITION_SYSTEM_PARAMETER_ADDR 0x1fd000
#define SYSTEM_PARTITION_CUSTOMER_PRIV_PARAM_ADDR 0xfc000
#elif (SPI_FLASH_SIZE_MAP == 6)
#define SYSTEM_PARTITION_OTA_SIZE 0x6A000
#define SYSTEM_PARTITION_OTA_2_ADDR 0x101000
#define SYSTEM_PARTITION_RF_CAL_ADDR 0x3fb000
#define SYSTEM_PARTITION_PHY_DATA_ADDR 0x3fc000
#define SYSTEM_PARTITION_SYSTEM_PARAMETER_ADDR 0x3fd000
#define SYSTEM_PARTITION_CUSTOMER_PRIV_PARAM_ADDR 0xfc000
#else
#error "The flash map is not supported"
#endif
#define SYSTEM_PARTITION_CUSTOMER_PRIV_PARAM SYSTEM_PARTITION_CUSTOMER_BEGIN
uint32 priv_param_start_sec;
static const partition_item_t at_partition_table[] = {
{ SYSTEM_PARTITION_BOOTLOADER, 0x0, 0x1000},
{ SYSTEM_PARTITION_OTA_1, 0x1000, SYSTEM_PARTITION_OTA_SIZE},
{ SYSTEM_PARTITION_OTA_2, SYSTEM_PARTITION_OTA_2_ADDR, SYSTEM_PARTITION_OTA_SIZE},
{ SYSTEM_PARTITION_RF_CAL, SYSTEM_PARTITION_RF_CAL_ADDR, 0x1000},
{ SYSTEM_PARTITION_PHY_DATA, SYSTEM_PARTITION_PHY_DATA_ADDR, 0x1000},
{ SYSTEM_PARTITION_SYSTEM_PARAMETER, SYSTEM_PARTITION_SYSTEM_PARAMETER_ADDR, 0x3000},
{ SYSTEM_PARTITION_CUSTOMER_PRIV_PARAM, SYSTEM_PARTITION_CUSTOMER_PRIV_PARAM_ADDR, 0x1000},
};
void ICACHE_FLASH_ATTR user_pre_init(void)
{
if(!system_partition_table_regist(at_partition_table, sizeof(at_partition_table)/sizeof(at_partition_table[0]),SPI_FLASH_SIZE_MAP)) {
os_printf("system_partition_table_regist fail\r\n");
while(1);
}
}
LOCAL int ICACHE_FLASH_ATTR
json_get(struct jsontree_context *js_ctx)
{
os_printf("\njson get\n");
const char *path = jsontree_path_name(js_ctx, js_ctx->depth - 1);
if (os_strncmp(path, "switch", 6) == 0) {
os_printf("\r\n write the switch \r\n");
jsontree_write_string(js_ctx, "off");
}
else if (os_strncmp(path, "thecat", 6) == 0) {
os_printf("\r\n write to thecat \r\n");
jsontree_write_string(js_ctx, "male");
}
else if (os_strncmp(path, "thedog", 6) == 0) {
os_printf("\r\n write to thedog \r\n");
jsontree_write_string(js_ctx, "black");
}
return 0;
}
LOCAL int ICACHE_FLASH_ATTR
json_set(struct jsontree_context *js_ctx, struct jsonparse_state *parser)
{
os_printf("\n==================>json set state: %d\n", js_ctx->callback_state);
int type;
while ((type = jsonparse_next(parser)) != 0) {
if (type == JSON_TYPE_PAIR_NAME) {
char buffer[10];
os_bzero(buffer, 10);
os_printf("JSON_TYPE_PAIR\n");
jsonparse_next(parser);
jsonparse_next(parser);
uint32_t jslen = jsonparse_get_len(parser);
char buff[jslen];
jsonparse_copy_value(parser, buff, jslen + 1);
int i = 0;
for (i = 0; i < jslen + 1; i++) {
os_printf("%c", buff[i]);
}
os_printf("\r\n");
#if 0
if (jsonparse_strcmp_value(parser, "name") == 0) {
os_printf("\n---name---\n");
jsonparse_next(parser);
jsonparse_next(parser);
jsonparse_copy_value(parser, buffer, sizeof(buffer));
int i = 0;
for (i = 0; i < 10; i++) {
os_printf("%c", buffer[i]);
}
os_printf("\n");
if (!strcoll(buffer, "John")) {
os_printf("=====John==========\n");
uint32_t jslen = jsonparse_get_len(parser);
os_printf("=>>>>>>%d>>>\r\n", jslen);
}
}
if (jsonparse_strcmp_value(parser, "age") == 0) {
os_printf("\n===age\n");
uint32_t jslen = jsonparse_get_len(parser);
os_printf("=>>>>>>%d>>>\r\n", jslen);
jsonparse_next(parser);
jsonparse_next(parser);
jsonparse_copy_value(parser, buffer, sizeof(buffer));
if (!strcoll(buffer, "31")) {
os_printf("=====31==========\n");
}
}
if (jsonparse_strcmp_value(parser, "city") == 0) {
jsonparse_next(parser);
jsonparse_next(parser);
os_printf("=>>>>>>city>>>\r\n");
uint32_t jslen = jsonparse_get_len(parser);
os_printf("=>>>>>>%d>>>\r\n", jslen);
char buff[jslen];
jsonparse_copy_value(parser, buff, jslen + 1);
int i = 0;
for (i = 0; i < jslen; i++) {
os_printf("%c", buff[i]);
}
}
#endif
}
}
return 0;
}
LOCAL struct jsontree_callback switch_callback =
JSONTREE_CALLBACK(json_get, json_set);
// 01 objecs
// JSONTREE_OBJECT(switch_tree,
// JSONTREE_PAIR("switch", &switch_callback)
// );
// multy objects
JSONTREE_OBJECT(switch_tree,
JSONTREE_PAIR("thecat", &switch_callback),
JSONTREE_PAIR("switch", &switch_callback),
JSONTREE_PAIR("thedog", &switch_callback)
);
LOCAL void ICACHE_FLASH_ATTR
JSON_TEST_FUNC(void){
int i = 0;
#if 1
// test write json test
char *json_buf = NULL;
json_buf = (char *)os_zalloc(jsonSize);
os_printf("\r\n sizeof before: %d \r\n", os_strlen(json_buf));
//json_ws_send((struct jsontree_value *)&switch_tree, "devices", json_buf); // 1 object
json_ws_send((struct jsontree_value *)&switch_tree, "thecat, switch, thedog", json_buf); // multy objects
os_printf("\r\n sizeof after: %d \r\n", os_strlen(json_buf));
for (i = 0; i < os_strlen(json_buf); i++) {
os_printf("%c", json_buf[i]);
}
os_printf("\r\n");
#endif
#if 0
// read json - OK
char *data_buf = "{\"name\": \"John\", \"age\": \"31\", \"city\" : \"New York\"}";
struct jsontree_context js;
jsontree_setup(&js, (struct jsontree_value *)&switch_tree, json_putchar);
json_parse(&js, data_buf);
#endif
}
/******************************************************************************
* FunctionName : user_init
* Description : entry of user application, init user function here
* Parameters : none
* Returns : none
*******************************************************************************/
void ICACHE_FLASH_ATTR
user_init(void)
{
partition_item_t partition_item;
os_printf("SDK version:%s\n", system_get_sdk_version());
if (!system_partition_get_item(SYSTEM_PARTITION_CUSTOMER_PRIV_PARAM, &partition_item)) {
os_printf("Get partition information fail\n");
}
priv_param_start_sec = partition_item.addr/SPI_FLASH_SEC_SIZE;
JSON_TEST_FUNC();
}
Video tutorial:
Nhận xét
Đăng nhận xét