请教,coap连接阿里云失败,求解

发表在 网络学习营2019-6-12 10:22 [复制链接] 3 157

本帖最后由 klickauf 于 2019-6-12 10:27 编辑 : H5 g" W/ t8 W# _& p" L$ q1 N- j
$ h& G2 e# a& q; x4 O' H
打印信息:
9 G. Q0 [9 u7 q2 w/ y: r- nali_coap_test
7 m0 ~) e! M) P8 S[COAP-Client]: Enter Coap Client
; d" @7 _  K$ q*****The Product Key  : a1Lb1FNnDns *****
' w3 h& ?8 \( l) k7 X. x*****The Device Name  : LED1_1 *****
- L2 H) ?: @/ P7 v0 [$ [4 N*****The Device ID    : a1Lb1FNnDns.LED1_1 *****4 B2 N! S- i7 q& t; F, _
[dbg] CoAPUri_parse(72): The endpoint type is: 15 N; l4 k+ G- _3 J4 ?8 R3 N% ~
[dbg] CoAPUri_parse(107): The host name is: a1Lb1FNnDns.coap.cn-shanghai.link.aliyuncs.com
/ k: D5 z4 V' l$ r, f; B6 b( @% L[dbg] CoAPUri_parse(133): The port is: 5684
/ E$ T* `( F% T2 z0 K9 B<INFO>  [packages\ali-iotkit-latest\ports\ssl\mbedtls\HAL_DTLS_mbedtls.c #156]   HAL_DTLSSession_init success  z7 W1 q5 i1 L2 X
<TRACE> [packages\ali-iotkit-latest\ports\ssl\mbedtls\HAL_DTLS_mbedtls.c #64]   Call mbedtls_ssl_conf_authmode3 m  G+ O! f  E- l" Y' `6 {1 z
<TRACE> [packages\ali-iotkit-latest\ports\ssl\mbedtls\HAL_DTLS_mbedtls.c #66]   x509 ca cert pem len 1281
2 Y9 X# b/ r$ G$ ?4 D
9 D: f( ]2 G/ j0 S-----BEGIN CERTIFICATE-----
& X8 d- Q4 D$ F) m7 ^5 aMIIDdTCCAl2gAwIBAgILBAAAAAABFUtaw5QwDQYJKoZIhvcNAQEFBQAwVzELMAkG4 F3 E) H5 L- ]8 l  u
A1UEBhMCQkUxGTAXBgNVBAoTEEdsb2JhbFNpZ24gbnYtc2ExEDAOBgNVBAsTB1Jv& b8 T) `* ]* s$ ^3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yj1hTdNGCbM+w6DjY1Ub8rrvrTnhQ7k4o+YviiY776BQVvnGCv04zcQLcFGUl5gE- A6 C# A7 ~2 o; `' S- v
38NflNUVyRRBnMRddWQVDf9VMOyGj/8N7yy5Y0b2qvzfvGn9LhJIZJrglfCm7ymP2 y+ X7 s0 }0 X8 x
AbEVtQwdpf5pLGkkeB6zpxxxYu7KyJesF12KwvhHhm4qxFYxldBniYUr+WymXUad+ ?8 S% a* Z: S& i7 A1 l, c
DKqC5JlR3XC321Y9YeRq4VzW9v493kHMB65jUr9TU/Qr6cf9tveCX4XSQRjbgbME
0 A5 ~' ~  `$ Q6 aHMUfpIBvFSDJ3gyICh3WZlXi/EjJKSZp4A==
/ g% `  l# r6 g8 \  e4 F6 |; P-----END CERTIFICATE-----# ~/ c- ?, F+ S6 t7 z; A
' J: X7 v- t, _7 l! P
<TRACE> [packages\ali-iotkit-latest\ports\ssl\mbedtls\HAL_DTLS_mbedtls.c #71]   mbedtls_x509_crt_parse result 0x00000 r& d$ {2 D, a% O6 l! N9 p; g! p5 E+ M
msh /><TRACE> [packages\ali-iotkit-latest\ports\ssl\mbedtls\HAL_DTLS_mbedtls.c #99]   mbedtls_ssl_setup result 0x0000# |& |# a' S9 ^% E
<TRACE> [packages\ali-iotkit-latest\ports\ssl\mbedtls\HAL_DTLS_mbedtls.c #112]   mbedtls_ssl_set_hostname a1Lb1FNnDns.coap.cn-shanghai.link.aliyuncs.com
. M1 l: F( Z1 _' a<TRACE> [packages\ali-iotkit-latest\ports\ssl\mbedtls\HAL_DTLS_mbedtls.c #120]   mbedtls_ssl_set_bio result 0x0000' Y& ~: H$ U0 F8 l' {: L# ]# x& @7 x
<TRACE> [packages\ali-iotkit-latest\ports\ssl\mbedtls\HAL_DTLS_mbedtls.c #126]   mbedtls_ssl_handshake result -0x6800
4 b. V8 V+ A$ B. N<ERROR> [packages\ali-iotkit-latest\ports\ssl\mbedtls\HAL_DTLS_mbedtls.c #266]   DTLSVerifyOptions_set result 0x1000004
. d* S  @" m8 q9 {[err] IOT_CoAP_Init(625):  Create coap context failed
+ }# {7 G( Q* N* Q; U/ eIoTx CoAP init failed3 X$ H7 n& Q3 C
" k- j7 _  e$ `* [' o! ^
具体代码如下:
! f+ g! A" |9 D) D' `6 _3 V#include <stdio.h>9 |  [8 D2 q& [6 D* |
#include <string.h>- F) S$ K, e3 {! j, ]
#include <stdlib.h>! \9 P+ g+ O! s7 o4 k( L* O
#include <unistd.h>
( Y. i& I/ \! p6 s2 M#include "iot_import.h"
+ Y1 |' r) e+ ^* ~& }9 v#include "iot_export.h"
* e1 |# `/ W1 H  f/ G% E1 t( C  h5 V  _
#define IOTX_ONLINE_DTLS_SERVER_URL     "coaps://%s.iot-as-coap.cn-shanghai.aliyuncs.com:5684"2 B/ p9 O1 [& X1 x* N1 i7 F% j
#define IOTX_PRODUCT_KEY         "a1Lb1FNnDns"  ]7 f6 J" D5 W2 `' U+ Y* t5 }2 e
#define IOTX_DEVICE_NAME         "LED1_1"
: D8 }/ t4 C9 _#define IOTX_DEVICE_SECRET       "wxmHSXd0soSJPLE2oAGsjeGYrf6a3YM3"
/ m. ^) W0 r% @7 X! G8 Ichar m_coap_client_running = 0;
: w. B0 W, [: }6 e# Z) Tstatic void iotx_response_handler(void *arg, void *p_response)' O1 F4 g( n4 M$ }
{7 e6 K. ?4 c8 n- s1 C
    int            len       = 0;
+ {/ V8 P+ u9 J8 G, M. _2 I- V9 p    unsigned char *p_payload = NULL;. X, `% u; W( u+ w7 O
    iotx_coap_resp_code_t resp_code;$ d1 x5 [7 @; ~' ^1 s
    IOT_CoAP_GetMessageCode(p_response, &resp_code);% E5 x% M+ y6 s0 x4 P
    IOT_CoAP_GetMessagePayload(p_response, &p_payload, &len);
8 I: d* S. o  A% |    HAL_Printf("[APPL]: Message response code: 0x%x\r\n", resp_code);
+ {7 C; l5 C. E' B" d    HAL_Printf("[APPL]: Len: %d, Payload: %s, \r\n", len, p_payload);2 V0 r% ~+ t' C$ [) W; r* k
}: c/ P2 ^4 B; p3 V8 Y, Y4 g

: g4 e1 D! I8 U2 P/ t+ V( f/ j1 cint iotx_get_devinfo(iotx_deviceinfo_t *p_devinfo)( z2 ]% N1 k% x0 ^
{/ |" {$ l. i8 w' C2 {1 d" u! q
    if (NULL == p_devinfo) 9 T0 z$ k; N. r! v- ~
        {
. ]: `0 N1 C9 {+ H8 v: ~        return IOTX_ERR_INVALID_PARAM;
" x' a5 E: N" V$ m7 m    }$ P: `( z: L- k- X, A
    memset(p_devinfo, 0x00, sizeof(iotx_deviceinfo_t));* d6 [4 ]- N6 D& G# }
    HAL_GetProductKey(p_devinfo->product_key);0 z- e  Q* r& \* U* R' p  @
    HAL_GetDeviceName(p_devinfo->device_name);: c; n. ~3 i' ?& d
    HAL_GetDeviceSecret(p_devinfo->device_secret);
) n  q$ w7 l. a0 _2 d" r3 Z    HAL_GetDeviceID(p_devinfo->device_id);
4 c$ x5 Y* J. V6 W( X% @, e) \    fprintf(stderr, "*****The Product Key  : %s *****\r\n", p_devinfo->product_key);
. [- N! S9 ~4 e4 ]( A* n9 n: {- u: S" u    fprintf(stderr, "*****The Device Name  : %s *****\r\n", p_devinfo->device_name);  M9 p. [9 H3 s5 u$ r$ ]5 s
    fprintf(stderr, "*****The Device ID    : %s *****\r\n", p_devinfo->device_id);; O' ^- _+ |* y. `  }  S
    return IOTX_SUCCESS;
1 z' [6 F( n1 f, Y  e}4 P- Q" N( q2 L% J  P6 s( C) ~- R& B
( b, K0 i& }& V! {$ c! t! e% |
static void iotx_post_data_to_server(void *param)
2 |2 j% L% B* E2 B; Z+ L0 c. c4 d{
9 Y3 z& D8 G, E$ B) d        char               path[IOTX_URI_MAX_LEN + 1] = {0};; _6 u) s* d4 }* d/ t( b; C: q: k, Y
        iotx_message_t     message;
4 L+ f# y4 ?. |' {3 l5 A2 Q" v        iotx_deviceinfo_t  devinfo;
+ d; t0 Z1 o: O7 Z        const char        *payload = "{\"id\": \"1\",\"version\": \"1.0\",\"params\": {\"LightSwitch\":1},\"method\": \"thing.event.property.post\"}";
. i3 @& [4 J4 {        memset(&message, 0, sizeof(iotx_message_t));
/ \6 Q+ b( Z! G& v0 A9 ?6 @        memset(&devinfo, 0, sizeof(iotx_deviceinfo_t));
* Q" Z5 X( X! g7 S) H5 E        message.p_payload = (unsigned char *)payload;
* O: s6 w$ x2 i( F8 v. o; y        message.payload_len = strlen(payload);5 _; x: e, y$ ~0 S2 d: D
        message.resp_callback = iotx_response_handler;
, l0 [2 R: [: l" G- @" S! H9 T        message.msg_type = IOTX_MESSAGE_CON;
! b6 K9 f2 F% `0 S5 C! F3 M: z        message.content_type = IOTX_CONTENT_TYPE_JSON;
$ z& Y' _0 ]* N( q" q+ v6 W. H7 p        iotx_coap_context_t *p_ctx = (iotx_coap_context_t *)param;
/ J6 S4 j5 H0 n- [  \4 p6 @  ^        iotx_get_devinfo(&devinfo);+ I0 T& w8 u$ o' k% J  ]" p+ |
        snprintf(path, IOTX_URI_MAX_LEN, "/topic/sys/%s/%s/thing/event/property/post", (char *)devinfo.product_key, (char *)devinfo.device_name);
# T( `- q" A5 V9 ]+ O        IOT_CoAP_SendMessage(p_ctx, path, &message);/ y0 y# x' }: `/ ~( Y; v/ Y" P
}
9 O/ p8 O  Y; U; Q3 T- B3 u& H! r
void coap_client(void* parameter)
7 J0 x) X7 L. _* t  ?# u% R3 R{0 d6 t% _4 N' g: H, e* s0 d5 @6 R" q
    int                     count = 0;  V% u: N7 c8 m4 w" F, R
    iotx_coap_config_t      config;7 z1 s, ]! y% T
    iotx_deviceinfo_t       deviceinfo;' j& X' K3 N+ w9 T5 R* _9 b" f

* J) Q/ N9 `2 S6 h    /**< set device info*/
4 v; K  T2 ?# e    HAL_SetProductKey(IOTX_PRODUCT_KEY);: X, ~/ s9 O) [' r! G: E
    HAL_SetDeviceName(IOTX_DEVICE_NAME);% X; o/ O/ b8 j
    HAL_SetDeviceSecret(IOTX_DEVICE_SECRET);
/ |* o6 `3 i) a2 d# v8 ?3 i0 u3 _    /**< end*/, I+ g3 T: }3 r4 C
    IOT_OpenLog("coap");+ `- T% _, _& Q1 H
    IOT_SetLogLevel(IOT_LOG_DEBUG);, g3 o+ l# k3 x

; K' x" D! M) _* o1 x    HAL_Printf("[COAP-Client]: Enter Coap Client\r\n");
; n+ L0 M7 J2 }4 F3 s& D    m_coap_client_running = 1;
8 w8 p7 {* q4 v0 r/ M    char url[256] = {0};9 L  z3 t! [# ~- Y  A  `  b1 ^1 _
    snprintf(url, sizeof(url), IOTX_ONLINE_DTLS_SERVER_URL, IOTX_PRODUCT_KEY);. ^' @/ j5 a; P4 \
    config.p_url = url;
2 C3 ?+ R; @! i% ?7 N2 s    iotx_get_devinfo(&deviceinfo);
, J2 g. j& w) R8 r* u: Y+ m    config.p_devinfo = (iotx_device_info_t*)&deviceinfo;2 q; g$ q/ ^% \1 m0 Y9 ]
    config.wait_time_ms = 3000;
$ ]4 U9 Y4 t4 d+ ?# A* k' J6 Q9 [; z: H3 ~
    iotx_coap_context_t *p_ctx = NULL;
% C# X  e7 o+ A9 \    p_ctx = IOT_CoAP_Init(&config);
- O# S, G1 S" P, w    if (NULL != p_ctx) " `6 \/ ~3 S' l) _) p& Q" Z4 X
        {: m/ `( S' l1 X' v& ^# K
        IOT_CoAP_DeviceNameAuth(p_ctx);
2 z5 N1 u4 P+ C: y" `( V9 S' i        do + X- \/ D" \2 {- b
                {
0 }# q# b  ]3 O            count ++;
: n+ ~* r/ |; V" m' c" ~            if (count == 11)
; l4 E3 q  O, p  t* v- N) h                        {
, C' w# _& M4 \2 E1 K! x+ j                iotx_post_data_to_server((void *)p_ctx);, H5 R. d  E' z* B6 y  }4 [
                count = 1;) T6 p+ w: l, Q
            }
- X# l0 h- x! m& e: h            IOT_CoAP_Yield(p_ctx);- L1 w: c5 X3 u2 R  g
        } while (m_coap_client_running);
" M1 t  m$ j* I0 ?$ ~. ^+ _5 F. m; B9 t5 m' o; `, [# ?
        IOT_CoAP_Deinit(&p_ctx);
# g* U( T& K6 K1 l+ c  L, U* i. j    } 8 m& R" }1 Y+ ~
        else
+ a, @& [8 s2 R/ ^. A4 ^; z        {
7 _8 c, T3 I! E& G$ c        HAL_Printf("IoTx CoAP init failed\r\n");+ H; s+ }6 L# F0 _/ G
    }. |6 K/ y: _7 F$ W$ Y
% G4 W% M- g4 O0 [5 T. X' H6 M
    IOT_CloseLog();
0 o6 X9 i' a/ ^! D9 _' a}
- r$ Y* ?4 |2 B2 w; b/ t8 Q
& G# S! E8 c2 _+ cstatic int ali_coap_main(int argc, char **argv)
  K/ G2 H. U3 C, Q" o{
# h; ]# N3 S6 p! a        rt_thread_t tid;
6 A+ n+ a3 i; ?6 g" v) m
9 k# s8 B" I- |! e- Y9 N: O- ?, |    tid = rt_thread_create("ali-coap",(void (*)(void *))coap_client, NULL,6 * 1024, RT_THREAD_PRIORITY_MAX / 2 - 1, 10);
. M( t  O0 F: {    if (tid != RT_NULL)
9 V3 y( h  V% ?! r- q- s, R( H        {
# D( F9 n0 D" Q  M2 I6 G, [            rt_thread_startup(tid);
7 V3 K" ]1 @) W        }
; ~! z; @5 t" O        return 0;2 D# Z8 t3 ^! W( s4 G% X
}
6 m7 E$ f& S8 J9 m) `( x& h/ B7 |- l1 m2 v, Z9 J  G' {' I% R
#ifdef RT_USING_FINSH
# r7 o. Z, z+ i. ~- W+ r. F# h#include <finsh.h>0 O; k% s9 S$ `! {' v1 u7 |7 \. P
MSH_CMD_EXPORT_ALIAS(ali_coap_main, ali_coap_test, Example: ali_coap_test);
5 Z* u! w+ u# \#endif1 c0 J6 W, i" H- Z/ b7 p7 ]) k
哪位大神帮忙看看,谢谢0 _( c( B- m% @: V
# F: l, t! @7 V0 ]; j# e7 c. d9 ^6 Q

6 Z" x* U; j$ D) X( N2 B1 k# s- e# Y# X, r" M

7 ]* g5 f' b6 @) N% w2 p4 K  U+ x3 ]' B+ ~4 Y3 c6 Z( h) n
使用道具 举报 显示全部楼层 回复
最新评论 | 正序浏览
显示全部楼层 |楼层直达:
发表于 2019-6-14 15:28:16 | 显示全部楼层
Coap 目前连接有问题,更新阿里 iotkit 软件包的时候统一修改测试。
使用道具 举报 回复
发表于 2019-6-19 10:20:57 | 显示全部楼层
MurphyZhao 发表于 2019-6-14 15:28
- q/ z  A0 \1 E% m( l$ w6 nCoap 目前连接有问题,更新阿里 iotkit 软件包的时候统一修改测试。

- _. d9 X! l) P5 W; S; K请问,什么时候会更新修改?
使用道具 举报 回复
发表于 2019-6-20 14:29:25 | 显示全部楼层
klickauf 发表于 2019-6-19 10:20/ B, V9 e: F; e4 B* g$ @" T
请问,什么时候会更新修改?

# i+ d- }4 o& `( I4 C% ~7 O. P来加我 QQ 95530056 一起来改(我改了一部分了)
. a* ~* l/ r6 Q0 ]) {9 g6 Z3 N
; z+ |( ~9 }& u9 o) vgithub https://github.com/murphyzhao/iotkit-embedded/tree/v3.0.1-rtt-dev
使用道具 举报 回复
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Archiver|手机版|小黑屋|RT-Thread开发者社区  

© 2013-2017 上海睿赛德电子科技有限公司 沪ICP备13014002号-1

Powered by RT-Thread

快速回复 返回顶部 返回列表