すべてのプロダクト
Search
ドキュメントセンター

IoT Platform:CoAP を使用してデバイスを IoT Platform に接続する

最終更新日:Mar 22, 2025

このトピックでは、Constrained Application Protocol(CoAP)を使用してデバイスを IoT Platform に接続する方法について説明します。この例では、Java コードを使用します。

背景情報

CoAP は、NB-IoT デバイスなどの低電力でリソースに制約のあるデバイスに適しています。CoAP を使用してデバイスを IoT Platform に接続する方法については、「CoAP 経由でデバイスを IoT Platform に接続する」をご参照ください。

説明 中国(上海)リージョンでのみ、CoAP を使用してデバイスを IoT Platform に接続できます。

CoAP を使用してデバイスを IoT Platform に接続する場合は、必須パラメーターを指定し、デバイスの署名を生成し、その他の操作を実行する必要があります。構成プロセスを説明するために、Eclipse Californium ベースのサンプルコードを使用します。データセキュリティを確保するために、対称暗号化が使用されます。

開発環境を準備する

次の Java 開発環境を使用できます。

手順

  1. IntelliJ IDEA を開き、Maven プロジェクトを作成します。例: IotCoap-demo
  2. プロジェクトの pom.xml ファイルに次の依存関係を追加し、[Load Maven Changes] をクリックして依存関係パッケージをダウンロードします。 これにより、Californium フレームワーク、Apache commons toolkit、および Alibaba Cloud fastjson パッケージがインポートされます。
    <dependency>
      <groupId>org.eclipse.californium</groupId>
      <artifactId>californium-core</artifactId>
      <version>2.0.0-M17</version>
    </dependency>
    <dependency>
      <groupId>org.apache.commons</groupId>
      <artifactId>commons-lang3</artifactId>
      <version>3.5</version>
    </dependency>
    <dependency>
      <groupId>commons-codec</groupId>
      <artifactId>commons-codec</artifactId>
      <version>1.13</version>
    </dependency>
    <dependency>
      <groupId>com.alibaba</groupId>
      <artifactId>fastjson</artifactId>
      <version>1.2.83</version>
    </dependency>
  3. /src/main/javaIotCoap-demo プロジェクトの ディレクトリに、IotCoapClientWithAes.java などの Java クラスを作成し、コードを入力します。サンプルコード:
    import java.io.IOException;
    import java.nio.charset.StandardCharsets;
    import java.security.MessageDigest;
    import java.security.NoSuchAlgorithmException;
    import java.util.Set;
    import java.util.SortedMap;
    import java.util.TreeMap;
    
    import javax.crypto.Cipher;
    import javax.crypto.Mac;
    import javax.crypto.spec.IvParameterSpec;
    import javax.crypto.spec.SecretKeySpec;
    
    import org.apache.commons.codec.DecoderException;
    import org.apache.commons.codec.binary.Hex;
    import org.apache.commons.lang3.RandomUtils;
    import org.eclipse.californium.core.CoapClient;
    import org.eclipse.californium.core.CoapResponse;
    import org.eclipse.californium.core.Utils;
    import org.eclipse.californium.core.coap.CoAP;
    import org.eclipse.californium.core.coap.CoAP.Code;
    import org.eclipse.californium.core.coap.CoAP.Type;
    import org.eclipse.californium.core.coap.MediaTypeRegistry;
    import org.eclipse.californium.core.coap.Option;
    import org.eclipse.californium.core.coap.OptionNumberRegistry;
    import org.eclipse.californium.core.coap.OptionSet;
    import org.eclipse.californium.core.coap.Request;
    import org.eclipse.californium.elements.exception.ConnectorException;
    
    import com.alibaba.fastjson.JSONObject;
    
    /**
     * 次の Eclipse Californium ベースのコードは、CoAP を使用してデバイスを IoT Platform に接続する方法を示しています。
     * 自動アクセスの開発プロセスとパラメーターの詳細については、「CoAP 経由でデバイスを IoT Platform に接続する」トピックの「対称暗号化を使用してデバイスを接続する」セクションを参照してください。
     */
    public class IotCoapClientWithAes {
    
        // ======================== 以下のパラメーターを指定する必要があります。 ===========================
        // リージョン ID。この例では、中国(上海)を使用します。
        private static String regionId = "cn-shanghai";
        // デバイスが属するプロダクトの ProductKey。
        private static String productKey = "デバイスの ProductKey";
        // デバイスの DeviceName。
        private static String deviceName = "デバイスの DeviceName";
        // デバイスの DeviceSecret。
        private static String deviceSecret = "デバイスの DeviceSecret";
        //======================= 終了 ===========================
    
        // HMAC_ALGORITHM パラメーターを hmacsha1 または hmacmd5 に設定します。指定する値は、signmethod パラメーターの値と同じである必要があります。
        private static final String HMAC_ALGORITHM = "hmacsha1";
    
        // CoAP 経由でデバイスを IoT Platform インスタンスに接続するために使用されるエンドポイント。対称暗号化を使用する場合、エンドポイントのポート番号は 5682 です。
        private static String serverURI = "coap://" + productKey + ".coap." + regionId + ".link.aliyuncs.com:5682";
    
        // メッセージの送信先のトピック。IoT Platform コンソールで、カスタムトピックを作成し、デバイスにトピックに対するパブリッシュ権限を付与できます。
        private static String updateTopic = "/" + productKey + "/" + deviceName + "/user/update";
    
        // token オプション
        private static final int COAP2_OPTION_TOKEN = 2088;
        // seq オプション
        private static final int COAP2_OPTION_SEQ = 2089;
    
        // SHA-256 暗号化アルゴリズム。
        private static final String SHA_256 = "SHA-256";
    
        private static final int DIGITAL_16 = 16;
        private static final int DIGITAL_48 = 48;
    
        // CoAP クライアント。
        private CoapClient coapClient = new CoapClient();
    
        // トークンの有効期間は 7 日間です。トークンの有効期限が切れたら、新しいトークンを取得する必要があります。
        private String token = null;
        private String random = null;
        @SuppressWarnings("unused")
        private long seqOffset = 0;
    
        /**
         * CoAP クライアントを初期化します。
         *
         * @param productKey: デバイスが属するプロダクトの ProductKey。
         * @param deviceName: デバイスの DeviceName。
         * @param deviceSecret: デバイスの DeviceSecret。
         */
        public void connect(String productKey, String deviceName, String deviceSecret) {
            try {
                // 認証に使用されるエンドポイント。
                String uri = serverURI + "/auth";
    
                // POST メソッドのみがサポートされています。
                Request request = new Request(Code.POST, Type.CON);
    
                // オプションを指定します。
                OptionSet optionSet = new OptionSet();
                optionSet.addOption(new Option(OptionNumberRegistry.CONTENT_FORMAT, MediaTypeRegistry.APPLICATION_JSON));
                optionSet.addOption(new Option(OptionNumberRegistry.ACCEPT, MediaTypeRegistry.APPLICATION_JSON));
                request.setOptions(optionSet);
    
                // デバイスの認証に使用できるエンドポイントを指定します。
                request.setURI(uri);
    
                // 認証リクエストに必要なパラメーターを指定します。
                request.setPayload(authBody(productKey, deviceName, deviceSecret));
    
                // 認証リクエストを送信します。
                CoapResponse response = coapClient.advanced(request);
                System.out.println(Utils.prettyPrint(response));
                System.out.println();
    
                // レスポンスを解析します。
                JSONObject json = JSONObject.parseObject(response.getResponseText());
                token = json.getString("token");
                random = json.getString("random");
                seqOffset = json.getLongValue("seqOffset");
            } catch (ConnectorException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    
        /**
         * メッセージを送信します。
         *
         @param topic メッセージの送信先のトピック。
         * @param payload メッセージの内容。
         */
        public void publish(String topic, byte[] payload) {
            try {
                // トピックのエンドポイント。トピックの構文は /topic/${topic} です。
                String uri = serverURI + "/topic" + topic;
    
                // AES 暗号化アルゴリズムを使用して seq オプションを暗号化します。seq=RandomUtils.nextInt()。
                String shaKey = encod(deviceSecret + "," + random);
                byte[] keys = Hex.decodeHex(shaKey.substring(DIGITAL_16, DIGITAL_48));
                byte[] seqBytes = encrypt(String.valueOf(RandomUtils.nextInt()).getBytes(StandardCharsets.UTF_8), keys);
    
                // POST メソッドのみがサポートされています。
                Request request = new Request(CoAP.Code.POST, CoAP.Type.CON);
    
                // オプションを指定します。
                OptionSet optionSet = new OptionSet();
                optionSet.addOption(new Option(OptionNumberRegistry.CONTENT_FORMAT, MediaTypeRegistry.APPLICATION_JSON));
                optionSet.addOption(new Option(OptionNumberRegistry.ACCEPT, MediaTypeRegistry.APPLICATION_JSON));
                optionSet.addOption(new Option(COAP2_OPTION_TOKEN, token));
                optionSet.addOption(new Option(COAP2_OPTION_SEQ, seqBytes));
                request.setOptions(optionSet);
    
                // トピックのエンドポイントを指定します。
                request.setURI(uri);
    
                // payload パラメーターを指定します。
                request.setPayload(encrypt(payload, keys));
    
                // メッセージを送信します。
                CoapResponse response = coapClient.advanced(request);
                System.out.println(Utils.prettyPrint(response));
    
                // 結果を解析します。
                String result = null;
                if (response.getPayload() != null) {
                    result = new String(decrypt(response.getPayload(), keys));
                }
                System.out.println("payload: " + result);
                System.out.println();
            } catch (ConnectorException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } catch (DecoderException e) {
                e.printStackTrace();
            }
        }
    
        /**
         * 認証に必要なパラメーターを生成します。
         *
         * @param productKey: デバイスが属するプロダクトの ProductKey。
         * @param deviceName: デバイスの DeviceName。
         * @param deviceSecret: デバイスの DeviceSecret。
         * @return: 認証リクエスト。
         */
        private String authBody(String productKey, String deviceName, String deviceSecret) {
    
            // 認証リクエストを作成します。
            JSONObject body = new JSONObject();
            body.put("productKey", productKey);
            body.put("deviceName", deviceName);
            body.put("clientId", productKey + "." + deviceName);
            body.put("timestamp", String.valueOf(System.currentTimeMillis()));
            body.put("signmethod", HMAC_ALGORITHM);
            body.put("seq", DIGITAL_16);
            body.put("sign", sign(body, deviceSecret));
    
            System.out.println("----- auth body -----");
            System.out.println(body.toJSONString());
    
            return body.toJSONString();
        }
    
        /**
         * デバイスの署名を生成します。
         *
         * @param params: 署名の生成に必要なパラメーター。
         * @param deviceSecret: デバイスの DeviceSecret。
         * @return: 16 進数の署名文字列。
         */
        private String sign(JSONObject params, String deviceSecret) {
    
            // リクエストパラメーターをアルファベット順にソートします。
            Set<String> keys = getSortedKeys(params);
    
            // sign、signmethod、version、および resources パラメーターを削除します。
            keys.remove("sign");
            keys.remove("signmethod");
            keys.remove("version");
            keys.remove("resources");
    
            // 署名のプレーンテキストを取得します。
            StringBuffer content = new StringBuffer();
            for (String key : keys) {
                content.append(key);
                content.append(params.getString(key));
            }
    
            // 署名を生成します。
            String sign = encrypt(content.toString(), deviceSecret);
            System.out.println("sign content=" + content);
            System.out.println("sign result=" + sign);
    
            return sign;
        }
    
        /**
         * JSON オブジェクトのキーをアルファベット順にソートします。
         *
         * @param json: キーをソートする JSON オブジェクト。
         * @return: アルファベット順にソートされたキーのセット。
         */
        private Set<String> getSortedKeys(JSONObject json) {
            SortedMap<String, String> map = new TreeMap<String, String>();
            for (String key : json.keySet()) {
                String value = json.getString(key);
                map.put(key, value);
            }
            return map.keySet();
        }
    
        /**
         * HMAC_ALGORITHM パラメーターを使用して暗号化アルゴリズムを指定します。
         *
         * @param content: プレーンテキスト
         * @param secret: 暗号鍵
         * @return: 暗号文
         */
        private String encrypt(String content, String secret) {
            try {
                byte[] text = content.getBytes(StandardCharsets.UTF_8);
                byte[] key = secret.getBytes(StandardCharsets.UTF_8);
                SecretKeySpec secretKey = new SecretKeySpec(key, HMAC_ALGORITHM);
                Mac mac = Mac.getInstance(secretKey.getAlgorithm());
                mac.init(secretKey);
                return Hex.encodeHexString(mac.doFinal(text));
            } catch (Exception e) {
                e.printStackTrace();
                return null;
            }
        }
    
        /**
         * SHA-256
         *
         * @param str: 暗号化するメッセージ。
         */
        private String encod(String str) {
            MessageDigest messageDigest;
            String encdeStr = "";
            try {
                messageDigest = MessageDigest.getInstance(SHA_256);
                byte[] hash = messageDigest.digest(str.getBytes(StandardCharsets.UTF_8));
                encdeStr = Hex.encodeHexString(hash);
            } catch (NoSuchAlgorithmException e) {
                System.out.println(String.format("Exception@encod: str=%s;", str));
                e.printStackTrace();
                return null;
            }
            return encdeStr;
        }
    
        // AES アルゴリズムを使用してデータを暗号化および復号化します。
        private static final String IV = "543yhjy97ae7fyfg";
        private static final String TRANSFORM = "AES/CBC/PKCS5Padding";
        private static final String ALGORITHM = "AES";
    
        /**
         * key length = 16 bits
         */
        private byte[] encrypt(byte[] content, byte[] key) {
            return encrypt(content, key, IV);
        }
    
        /**
         * key length = 16 bits
         */
        private byte[] decrypt(byte[] content, byte[] key) {
            return decrypt(content, key, IV);
        }
    
        /**
         * aes 128 cbc key length = 16 bits
         */
        private byte[] encrypt(byte[] content, byte[] key, String ivContent) {
            try {
                SecretKeySpec keySpec = new SecretKeySpec(key, ALGORITHM);
                Cipher cipher = Cipher.getInstance(TRANSFORM);
                IvParameterSpec iv = new IvParameterSpec(ivContent.getBytes(StandardCharsets.UTF_8));
                cipher.init(Cipher.ENCRYPT_MODE, keySpec, iv);
                return cipher.doFinal(content);
            } catch (Exception ex) {
                System.out.println(
                        String.format("AES encrypt error, %s, %s, %s", content, Hex.encodeHex(key), ex.getMessage()));
                return null;
            }
        }
    
        /**
         * aes 128 cbc key length = 16 bits
         */
        private byte[] decrypt(byte[] content, byte[] key, String ivContent) {
            try {
                SecretKeySpec keySpec = new SecretKeySpec(key, ALGORITHM);
                Cipher cipher = Cipher.getInstance(TRANSFORM);
                IvParameterSpec iv = new IvParameterSpec(ivContent.getBytes(StandardCharsets.UTF_8));
                cipher.init(Cipher.DECRYPT_MODE, keySpec, iv);
                return cipher.doFinal(content);
            } catch (Exception ex) {
                System.out.println(String.format("AES decrypt error, %s, %s, %s", Hex.encodeHex(content),
                        Hex.encodeHex(key), ex.getMessage()));
                return null;
            }
        }
    
        public static void main(String[] args) throws InterruptedException {
            IotCoapClientWithAes client = new IotCoapClientWithAes();
            client.connect(productKey, deviceName, deviceSecret);
            client.publish(updateTopic, "hello coap".getBytes(StandardCharsets.UTF_8));
            client.publish(updateTopic, new byte[] { 0x01, 0x02, 0x03, 0x05 });
        }
    }
  4. 次のパラメーターを指定します。
    パラメーター説明
    regionIdIoT Platform が存在するリージョンの ID。

    IoT Platform コンソール の左上隅でリージョンを確認できます。

    リージョン ID の詳細については、「リージョン」をご参照ください。

    productKeyデバイスを IoT Platform コンソールに追加した後に取得できるデバイス証明書情報。

    IoT Platform コンソールの [デバイスの詳細] ページで情報を表示できます。

    deviceName
    deviceSecret
    serverURICoAP 経由でデバイスを IoT Platform に接続するために使用されるエンドポイント。詳細については、「インスタンスのエンドポイントを管理する」をご参照ください。
  5. IotCoapClientWithAes.java プログラムを実行します。
    次のサンプルコードは、実行結果を示しています。デバイスが認証されると、デバイスは IoT Platform と通信できます。
    sign content=clientIda1****RK0.devicedeviceNamedeviceproductKeya1OX****K0seq16timestamp1658909565141
    sign result=7f3b76dc21e7******fec424838d1858
    ----- auth body -----
    {"clientId":"a1OXp8sXRK0.device","signmethod":"hmacsha1","sign":"7f3b76dc21e7******fec424838d1858","productKey":"a1OX****K0","deviceName":"device","seq":16,"timestamp":"1658909565141"}
    ==[ CoAP Response ]============================================
    MID    : 37682
    Token  : 6E1F******6B91
    Type   : ACK
    Status : 2.05 - CONTENT
    Options: {}
    RTT    : 116 ms
    Payload: 85 Bytes
    ---------------------------------------------------------------
    {"random":"a25******6d44","seqOffset":1,"token":"mnx4OF0b******R000000.5ac7"}
    ===============================================================
    
    ==[ CoAP Response ]============================================
    MID    : 37683
    Token  : AC60******106E7
    Type   : ACK
    Status : 2.05 - CONTENT
    Options: {"Unknown (2090)":0x158a******6aa00}
    RTT    : 118 ms
    Payload: 0 Bytes
    ===============================================================
    payload: null
    
    ==[ CoAP Response ]============================================
    MID    : 37684
    Token  : AA9******EFCC
    Type   : ACK
    Status : 2.05 - CONTENT
    Options: {"Unknown (2090)":0x158a******f600}
    RTT    : 103 ms
    Payload: 0 Bytes
    ===============================================================
    payload: null