espush_sdk library 使用指南

espush_sdk library 使用指南

快速开始

git clone https://github.com/pushdotccgzs/espush_sdk.git

例程中的 user_init 函数既是固件的入口函数,最简化的代码示例如下:

void ICACHE_FLASH_ATTR user_init(void)
{
  //首先初始化串口传输;
    uart_init(BIT_RATE_115200, BIT_RATE_115200);

  //配置WIFI热点信息,此处写死,设备固件可使用SmartConfig等方式配置设备网络信息
    struct station_config config;
    os_strcpy(config.ssid, "YOUR_WIFI_AP");
    os_strcpy(config.password, "WIFI_AP_PASSWORD");

  //连接到网络
    wifi_set_opmode(STATION_MODE);
    wifi_station_set_config(&config);
    wifi_station_set_auto_connect(1);
    wifi_station_dhcpc_start();

  //注册到espush平台,指定接收数据的回调函数,此处换APPID与APPKE为帐号相关
  //此处消息回调函数指定为NULL,表明不关注推送消息。
  espush_register(APPID, "APPKEY", "", VER_SDK, NULL);
}

如果需要在收到推送数据时得到通知,可按头文件的方式定义函数,并将函数名置入最后一个参数,即可。函数声明形式:typedef void(*msg_cb)(uint8* pdata, uint32 len);,如以下示例,定义一个数据回调函数:

void esp_msgcb(uint8* pdata, uint32 len)
{
  //输出接收到的数据字节数,最大4K字节。
  os_printf("RECV [%d] bytes.\n\r", len);
}

头文件解析

文件https://github.com/pushdotccgzs/espush_sdk/blob/master/app/include/push.h定义了espush library 引出的函数,常用的如下:

  • 连入espush平台,执行此命令后设备将尽可能的与平台保持TCP长连接
void ICACHE_FLASH_ATTR espush_register(uint32 appid, char appkey[32], char devid[32], enum VERTYPE type, msg_cb msgcb);
  • 从espush平台断开连接
//断开连接,并不再重连
void ICACHE_FLASH_ATTR espush_unregister();
  • 获得当前服务器时间
//平台每一次心跳都会与服务器同步时间戳,此函数可快速获得当前时间,为32位Unix时间戳
uint32 ICACHE_FLASH_ATTR get_timestamp();
  • 设置GPIO电平态
uint8 ICACHE_FLASH_ATTR set_gpio_edge(uint8 pin, uint8 edge);
  • 与平台的连接状态
//同AT+PUSH? 指令,返回到平台的连接状态
sint8 ICACHE_FLASH_ATTR espush_server_connect_status();
  • 主动的数据推送
//立即的数据推送,若设备未在线,推送失败
sint8 ICACHE_FLASH_ATTR espush_msg(uint8* buf, uint16 len);
//计划中的数据推送,若设备离线,则缓存到flash以待连接后发送,_timestamp可置入当下timestamp
sint8 ICACHE_FLASH_ATTR espush_msg_plan(uint8* buf, uint16 len, uint32 _timestamp);
  • 数据回调
typedef void(*msg_cb)(uint8* pdata, uint32 len);

编译与执行

直接在代码根目录执行make指令可快速编译,执行make clean可清除编译中间文件,强制重新编译,通过使用不同的命令行参数可以编译不同的固件,如

#如下指令可编译支持OTA的固件,相关资料,可参考乐鑫官方文档
make BOOT=new APP=1
make BOOT=new APP=2