本文主要是介绍【esp8266】④esp8266对接贝壳物联平台,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
源码github地址:https://github.com/linzhongpaihuai/smartplug
①烧录方法:https://blog.csdn.net/u010177891/article/details/90348729
②esp8266实现http server服务详解:https://blog.csdn.net/u010177891/article/details/100024710
③esp8266对接天猫精灵实现语音控制:https://blog.csdn.net/u010177891/article/details/100026511
④esp8266对接贝壳物联平台详解:https://blog.csdn.net/u010177891/article/details/100058124
注册贝壳物联账号
注册地址:https://www.bigiot.net/User/index.htm
点击“添加设备”填写好设备名称点击确定。在设备列表中可以看到刚添加的设备。
点击“添加接口”,“接口名称”可以随便填写;“所属设备”选择刚刚添加的设备,这样这个接口的数据就会和这个设备绑定;“接口类型”选择接口上报的数据类型,例如如果接口上报的是开关的状态那么数据类型就可以选择“数据量接口0/1”,如果上报的是温度数据那么数据类型可以选择“模拟量接口(float)”浮点型数据。
设备添加好之后需要记录设备的ID、APIKEY、接口ID这三个数据,后边esp8266对接贝壳物联平台是需要使用。
esp8266连接至贝壳物联平台
输入esp8266的ip进入控制页面(如果不知道怎么进入参考之前的博客“烧录方法”),点击“云平台”选择“贝壳物联”填写设备ID、APIKEY、接口ID。填写好后点击“确定”。然后进入“设置”,点击“重启”使配置生效。
具体怎么使用查看上一篇博客“③esp8266对接天猫精灵实现语音控制”。下面看下代码是怎么实现的。
代码实现
因为要对接贝壳物联平台需要连接外网因此esp8266必须设置为station模式,在连接好WiFi并进入station模式后会判断配置需要进入对接阿里云的流程还是贝壳物联的流程。由于对接阿里云无法实现天猫精灵控制这里不展开讲对接阿里云的流程只关注对接贝壳物联的流程。
ucCloudPlatform = PLUG_GetCloudPlatform();
switch ( ucCloudPlatform )
{case PLATFORM_ALIYUN :MQTT_StartMqttTheard();break;case PLATFORM_BIGIOT :BIGIOT_StartBigiotTheard();break;default:LOG_OUT(LOGOUT_INFO, "Do not connect to any cloud platform");break;
}
switch会进入BIGIOT_StartBigiotTheard流程启动一个BIGIOT_BigiotTask任务来连接到贝壳物联平台。
static void BIGIOT_BigiotTask( void* para )
{int iRet = -1;char* pcDevId = 0;char* pcApiKey = 0;BIGIOT_Event_S stHeartBeat = {"", "", Bigiot_JudgeHeartBeat, "Bigiot_HeartBeatCallBack", Bigiot_HeartBeatCallBack, pstCli};BIGIOT_Event_S stSwitch = {BIGIOT_IF_SW, "", Bigiot_JudgeRelayStatus, "Bigiot_RelayStatusCallBack", Bigiot_RelayStatusCallBack, pstCli};BIGIOT_Event_S stTemp = {BIGIOT_IF_TEMP, "", Bigiot_JudgeUploadTemp, "Bigiot_UploadTempCallBack", Bigiot_UploadTempCallBack, pstCli};BIGIOT_Event_S stHumidity = {BIGIOT_IF_HUMI, "", Bigiot_JudgeUploadHumidity, "Bigiot_UploadHumidityCallBack", Bigiot_UploadHumidityCallBack, pstCli};//从配置里读出之前配置的设备IDpcDevId = PLUG_GetBigiotDevId();if ( pcDevId == 0 || pcDevId[0] == 0 || pcDevId[0] == 0xFF ){BIGIOT_LOG(BIGIOT_ERROR, "pcDevId is invalid");return;}//从配置里读出之前配置的APIKEYpcApiKey = PLUG_GetBigiotApiKey();if ( pcApiKey == 0 || pcApiKey[0] == 0 || pcApiKey[0] == 0xFF ){BIGIOT_LOG(BIGIOT_ERROR, "pcApiKey is invalid");return;}retry://等待wifi连接就绪while ( STATION_GOT_IP != wifi_station_get_connect_status() ){BIGIOT_LOG(BIGIOT_DEBUG, "wait for connect");vTaskDelay(1000 / portTICK_RATE_MS);}//分配内存,核心数据的初始化pstCli = Bigiot_New( BIGIOT_HOSTNAME, BIGIOT_PORT, pcDevId, pcApiKey);if ( pstCli == 0 ){BIGIOT_LOG(BIGIOT_ERROR, "Bigiot_New failed");goto exit;}BIGIOT_LOG(BIGIOT_INFO, "Bigiot_New success");//注册定时心跳任务,需要每隔40s向贝壳物联平台发送心跳,不然会被断开连接stHeartBeat.cbPara = pstCli;iRet = Bigiot_EventRegister( pstCli, &stHeartBeat );if ( iRet ){BIGIOT_LOG(BIGIOT_ERROR, "Register Bigiot_HeartBeatCallBack failed");goto exit;}BIGIOT_LOG(BIGIOT_INFO, "Register Bigiot_HeartBeatCallBack success");//注册开关状态变化时主动上报任务,只有当开关接口ID填写了才会注册stSwitch.cbPara = pstCli;stSwitch.pcIfId = PLUG_GetBigiotSwitchId();if ( strlen(stSwitch.pcIfId) != 0 ){iRet = Bigiot_EventRegister( pstCli, &stSwitch );if ( iRet ){BIGIOT_LOG(BIGIOT_ERROR, "Register Bigiot_RelayStatusCallBack failed");goto exit;}BIGIOT_LOG(BIGIOT_INFO, "Register Bigiot_RelayStatusCallBack success");}//注册温度数据定时上报任务,只有当温度接口ID填写了才会注册stTemp.cbPara = pstCli;stTemp.pcIfId = PLUG_GetBigiotTempId();if ( strlen(stTemp.pcIfId) != 0 ){iRet = Bigiot_EventRegister( pstCli, &stTemp );if ( iRet ){BIGIOT_LOG(BIGIOT_ERROR, "Register Bigiot_UploadTempCallBack failed");goto exit;}BIGIOT_LOG(BIGIOT_INFO, "Register Bigiot_UploadTempCallBack success");}//注册湿度数据定时上报任务,只有当湿度接口ID填写了才会注册stHumidity.cbPara = pstCli;stHumidity.pcIfId = PLUG_GetBigiotHumidityId();if ( strlen(stHumidity.pcIfId) != 0 ){iRet = Bigiot_EventRegister( pstCli, &stHumidity );if ( iRet ){BIGIOT_LOG(BIGIOT_ERROR, "Register Bigiot_UploadHumidityCallBack failed");goto exit;}BIGIOT_LOG(BIGIOT_INFO, "Register Bigiot_UploadHumidityCallBack success");}for (;;){//连接到贝壳物联平台并发送登陆命令iRet = Bigiot_Login( pstCli );if ( iRet ){BIGIOT_LOG(BIGIOT_ERROR, "Bigiot_Login failed, iRet:%d", iRet);goto exit;}BIGIOT_LOG(BIGIOT_INFO, "Bigiot_Login success");//启动一个任务来完成心跳,开关、温度、湿度等信息的上报iRet = Bigiot_StartEventTask( pstCli );if ( iRet ){BIGIOT_LOG(BIGIOT_ERROR, "Bigiot_StartEventTask failed");goto exit;}BIGIOT_LOG(BIGIOT_INFO, "Bigiot_StartEventTask success");//这里死循环主要是接收贝壳物联平台、公众号、天猫精灵发送的命令,并给与响应for ( ;; ){iRet = Bigiot_Cycle( pstCli );if ( iRet ){BIGIOT_LOG(BIGIOT_ERROR, "Bigiot_Cycle failed");goto exit;}//心跳失败时退出循环,重新进入连接贝壳物联流程if ( !pstCli->iAlived ){goto exit;}vTaskDelay( 1000/portTICK_RATE_MS );}}exit:BIGIOT_LOG(BIGIOT_INFO, "BIGIOT_BigiotTask stop");vTaskDelay( 1000/portTICK_RATE_MS );BIGIOT_Destroy( &pstCli );goto retry;vTaskDelete(NULL);return;
}
j接下来详细介绍下BIGIOT_BigiotTask里边的实现。
1,Bigiot_New的实现
BIGIOT_Ctx_S* Bigiot_New( char* pcHostName, int iPort, char* pcDevId, char* pcApiKey )
{BIGIOT_Ctx_S* pstCtx = NULL;pstCtx = malloc( sizeof(BIGIOT_Ctx_S) );if ( pstCtx == 0 ){BIGIOT_LOG(BIGIOT_ERROR, "Bigiot_New failed");return 0;}Init( pstCtx, pcHostName, iPort, pcDevId, pcApiKey);return pstCtx;
}
申请了内存用于存放核心数据,核心数据说明如下:
typedef struct tagBigiot
{char* pcHostName; //贝壳物联平台服务器的域名:www.bigiot.netint port; //贝壳物联平台服务器的端口,这里用8181char szDevName[BIGIOT_DEVNAME_LEN]; //对应贝壳物联的设备名称DEVTYPE_E eDevType; //设备类型char* pcDeviceId; //设备IDchar* pcApiKey; //APIKEYint socket; //使用tcp协议连接的socket描述符int iTimeOut; //发送和接收的超时时间,超过该时间认为发送或接收失败xTaskHandle xEventHandle; //处理注册事件任务的任务句柄int iAlived; //心跳成功标志,心跳失败时该标志会被清零BIGIOT_Event_S astEvent[BIGIOT_EVENT_NUM]; //心跳、开关状态、温度、湿度等事件会注册到这里int (*Read)(struct tagBigiot*, unsigned char*, unsigned int, unsigned int); //数据接受函数int (*Write)(struct tagBigiot*, const unsigned char*, unsigned int, unsigned int); //数据发送函数int (*Connect)(struct tagBigiot*); //连接函数void (*Disconnect)(struct tagBigiot*); //断开连接函数}BIGIOT_Ctx_S;
Init函数只是初始化了核心数据。Read、Write、Connect、Disconnect是四个通用的函数实现了数据的接收、发送、连接、断开等基本功能。
static void Init( BIGIOT_Ctx_S *pstCtx, char* pcHostName, int iPort, char* pcDevId, char* pcApiKey )
{pstCtx->socket = -1;pstCtx->pcHostName = pcHostName;pstCtx->port = iPort;pstCtx->pcDeviceId = pcDevId;pstCtx->pcApiKey = pcApiKey;pstCtx->xEventHandle = 0;pstCtx->iAlived = 0;pstCtx->iTimeOut = BIGIOT_TIMEOUT;pstCtx->Read = Read;pstCtx->Write = Write;pstCtx->Connect = Connect;pstCtx->Disconnect = Disconnect;memset( pstCtx->szDevName, 0, BIGIOT_DEVNAME_LEN );memset( pstCtx->astEvent, 0, sizeof(pstCtx->astEvent) );
}
2,Bigiot_EventRegister。心跳、开关状态、温度、湿度等事件的注册。
事件的注册机制是这样实现的,事件注册时需要提供2个函数,1个回调函数用于实现心跳、各种状态的上报等功能;另外一个函数用于判断是否需要调用回调函数。如心跳上报的间隔是40s需要判断40s的事件是否到了。
typedef struct tagBigiotEvent
{char* pcIfName; //接口的名称char* pcIfId; //贝壳物联的接口IDTriggerFun tf; //触发条件函数,调用该函数指针返回true时才会调用回调函数char* cbName; //回调函数名称CallbackFun cb; //回调函数void* cbPara; //回调函数入参}BIGIOT_Event_S;
例如心跳事件的注册:
BIGIOT_Event_S stHeartBeat = {"", "", Bigiot_JudgeHeartBeat, "Bigiot_HeartBeatCallBack", Bigiot_HeartBeatCallBack, pstCli
};
Bigiot_JudgeHeartBeat函数用于判断距离上次上报心跳是否超过40s,如果超过就调用Bigiot_HeartBeatCallBack接口重新上报心跳。
stHeartBeat.cbPara = pstCli;iRet = Bigiot_EventRegister( pstCli, &stHeartBeat );if ( iRet ){BIGIOT_LOG(BIGIOT_ERROR, "Register Bigiot_HeartBeatCallBack failed");goto exit;}BIGIOT_LOG(BIGIOT_INFO, "Register Bigiot_HeartBeatCallBack success");
int Bigiot_EventRegister( BIGIOT_Ctx_S *pstCtx, BIGIOT_Event_S* pstEvent )
{UINT i = 0;if ( pstCtx == NULL ){BIGIOT_LOG(BIGIOT_ERROR, "pstCtx is NULL");return 1;}for ( i = 0; i < BIGIOT_EVENT_NUM; i++ ){if ( pstCtx->astEvent[i].tf == 0 ){pstCtx->astEvent[i].pcIfName = pstEvent->pcIfName;pstCtx->astEvent[i].pcIfId = pstEvent->pcIfId;pstCtx->astEvent[i].tf = pstEvent->tf;pstCtx->astEvent[i].cbName = pstEvent->cbName;pstCtx->astEvent[i].cb = pstEvent->cb;pstCtx->astEvent[i].cbPara = pstEvent->cbPara;return 0;}}if ( i >= BIGIOT_EVENT_NUM ){BIGIOT_LOG(BIGIOT_ERROR, "Event is %d and full", BIGIOT_EVENT_NUM);return 1;}return 1;
}
以上只是完成事件的注册,具体事件的回调是在Bigiot_EventHanderTask这个任务中调用Bigiot_EventCallBack实现的。
void Bigiot_EventCallBack( BIGIOT_Ctx_S *pstCtx )
{int i = 0;int iRet = 0;for ( i = 0; i < BIGIOT_EVENT_NUM; i++ ){if ( pstCtx->astEvent[i].tf != NULL && pstCtx->astEvent[i].cb != NULL ){if ( pstCtx->astEvent[i].tf() ){iRet = pstCtx->astEvent[i].cb( pstCtx->astEvent[i].cbPara );if ( iRet ){BIGIOT_LOG(BIGIOT_ERROR, "EventCallBack %s failed", pstCtx->astEvent[i].cbName);}}}}
}
3,Bigiot_Login。连接并登陆到贝壳物联平台。
int Bigiot_Login( BIGIOT_Ctx_S *pstCtx )
{char szMess[100] = { 0 };char szValue[100] = { 0 };char* pcMethod = 0;char* pcContent = 0;int iLen = 0;int iRet = 0;//先连接至贝壳物联平台iRet = pstCtx->Connect( pstCtx );if ( iRet ){BIGIOT_LOG(BIGIOT_ERROR, "Connect failed");return 1;}//拼装并发送登陆指令iLen = snprintf(szMess, sizeof(szMess), "{\"M\":\"checkin\",\"ID\":\"%s\",\"K\":\"%s\"}\n",pstCtx->pcDeviceId, pstCtx->pcApiKey );iRet = pstCtx->Write( pstCtx, szMess, iLen, pstCtx->iTimeOut );if ( iRet != iLen ){BIGIOT_LOG(BIGIOT_ERROR, "Bigiot_Login failed");return 2;}//登陆成功会返回checkinok和设备名称等字段iRet = pstCtx->Read( pstCtx, szMess, sizeof(szMess), pstCtx->iTimeOut );if ( iRet < 0 ){return 3;}else if ( iRet == 0 ){Bigiot_Logout(pstCtx);return 4;}pcMethod = BigiotParseString(szMess, "M", szValue, sizeof(szValue));if ( 0 != pcMethod && 0 == strcmp(pcMethod, BIGIOT_LOGINT_OK) ){pcContent = BigiotParseString(szMess, "NAME", szValue, sizeof(szValue));if ( 0 != pcContent ){strncpy( pstCtx->szDevName, pcContent, BIGIOT_DEVNAME_LEN );}return 0;}BIGIOT_LOG(BIGIOT_ERROR, "Bigiot_Login failed, unknown method:%s", pcMethod);return 6;
}
4,Bigiot_StartEventTask。启动注册事件的任务
static int Bigiot_StartEventTask( BIGIOT_Ctx_S *pstCtx )
{if ( pdPASS != xTaskCreate( Bigiot_EventHanderTask,"Bigiot_EventHanderTask",256,(void*)pstCtx,uxTaskPriorityGet(NULL),&(pstCtx->xEventHandle))){return 1;}return 0;
}
5,Bigiot_Cycle。死循环接收并处理数据
int Bigiot_Cycle( BIGIOT_Ctx_S *pstCtx )
{char szMess[150] = { 0 };char szValue[100] = { 0 };char* pcMethod = 0;char* pcContent = 0;int iRet = 0;//判断是否有数据发送过来iRet = pstCtx->Read( pstCtx, szMess, sizeof(szMess), pstCtx->iTimeOut );if ( iRet < 0 ){BIGIOT_LOG(BIGIOT_ERROR, "Read failed");return iRet;}if ( iRet > 0 ){//有指令发送过来pcMethod = BigiotParseString(szMess, "M", szValue, sizeof(szValue));if ( 0 != pcMethod && 0 == strcmp(pcMethod, BIGIOT_SAY) ){pcContent = BigiotParseString(szMess, "C", szValue, sizeof(szValue));if ( 0 != pcContent ){//打开开关的指令if ( 0 == strcmp(pcContent, BIGIOT_ON) ){BIGIOT_LOG(BIGIOT_INFO, "Conent: %s", pcContent);PLUG_SetRelayByStatus( 1, 1 );}//关闭开关的指令else if ( 0 == strcmp(pcContent, BIGIOT_OFF) ){BIGIOT_LOG(BIGIOT_INFO, "Conent: %s", pcContent);PLUG_SetRelayByStatus( 0, 1 );}//其他指令else{BIGIOT_LOG(BIGIOT_INFO, "recv content:%s", pcContent);}}else{BIGIOT_LOG(BIGIOT_ERROR, "BigiotParseString failed, szMess:%s", szMess);}}//有其他途径登陆到贝壳物联平台如通过页面或者微信公众号等会收到上线的通知else if ( 0 != pcMethod && 0 == strcmp(pcMethod, BIGIOT_LOGIN) ){pcContent = BigiotParseString(szMess, "NAME", szValue, sizeof(szValue));if ( 0 != pcContent ){BIGIOT_LOG(BIGIOT_INFO, "%s has login", pcContent);//有用户登陆马上上报开关状态Bigiot_RelayStatusCallBack( pstCtx );}}//有用户离线通知else if ( 0 != pcMethod && 0 == strcmp(pcMethod, BIGIOT_LOGOUT) ){pcContent = BigiotParseString(szMess, "NAME", szValue, sizeof(szValue));if ( 0 != pcContent ){BIGIOT_LOG(BIGIOT_INFO, "%s has logout", pcContent);}}}return 0;
}
这篇关于【esp8266】④esp8266对接贝壳物联平台的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!