源碼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};
//從配置裏讀出之前配置的設備ID
pcDevId = PLUG_GetBigiotDevId();
if ( pcDevId == 0 || pcDevId[0] == 0 || pcDevId[0] == 0xFF )
{
BIGIOT_LOG(BIGIOT_ERROR, "pcDevId is invalid");
return;
}
//從配置裏讀出之前配置的APIKEY
pcApiKey = 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.net
int port; //貝殼物聯平臺服務器的端口,這裏用8181
char szDevName[BIGIOT_DEVNAME_LEN]; //對應貝殼物聯的設備名稱
DEVTYPE_E eDevType; //設備類型
char* pcDeviceId; //設備ID
char* pcApiKey; //APIKEY
int 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; //貝殼物聯的接口ID
TriggerFun 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;
}