A device reports information such as the city name, city code, latitude and longitude of the city, and IP address to IoT Platform. IoT Platform forwards the information to Function Compute. Function Compute integrates with a weather service to send queried weather information to a specified topic.



  1. In the IoT Platform console, create a product and a device, and define a topic to send query messages and another topic to receive query results.
    1. Log on to the IoT Platform console.
    2. Choose Devices > Products. On the Products page, click Create Product and set the parameters as required to create a product. For more information, see Create a product.
    3. Choose Devices > Devices. On the Devices page, click Add Device and set the parameters as required to create a device of the product you created. For more information, see Create a device.
    4. On the Products page, find the created product and click View in the Actions column.
    5. On the Product Details page, click the Topic Categories tab. On the Topic Categories tab, click the Topic Category tab and then Edit Topic Category. In the Edit Topic Category dialog box, set the parameters to define a topic to send query messages. Repeat the preceding operations to define another topic to receive query results. The following figure shows the defined topics.
      Custom topics
  2. Use the device SDK to develop a device program.
  3. Define a function code package.
    • Create a project and import JAR packages.
    • Define the business logic of the function.

      The function implements the following business logic:

      • Queries weather information based on city information.
      • Uses the weather service that is purchased from Alibaba Cloud Marketplace.
      • Sends query results to the specified topic of the device by using the server API that is provided by IoT Platform.
      public class FcToApiDemo implements PojoRequestHandler {
          private static String host = "http://jisutqybmf.market.alicloudapi.com";
          private static String path = "/weather/query";
          private static String method = "GET";
          private static String appcode = "The code of your application";
          private static String accessKeyID = "The AccessKey ID of your Alibaba Cloud account";
          Private static String accessSecret = "The AccessKey secret of your Alibaba Cloud account";
          static DefaultAcsClient client;
          static {
              IClientProfile profile = DefaultProfile.getProfile("cn-shanghai", accessKey, accessSecret);
              try {
                  DefaultProfile.addEndpoint("cn-shanghai", "cn-shanghai", "Iot", "iot.cn-shanghai.aliyuncs.com");
              catch (ClientException e) {
              // Initialize the SDK client.
              client = new DefaultAcsClient(profile);
      public String handleRequest(CityModel input, Context context) {
          Map headers = new HashMap();
          * The headers in the request are in the format of Authorization:APPCODE 83359fd73fe94948385f570e3c13****.
          headers.put("Authorization", "APPCODE " + appcode);
          Map querys = new HashMap();
          querys.put("city", input.getCity());
          querys.put("citycode", input.getCityCode());
          querys.put("cityid", input.getCityid());
          querys.put("ip", input.getIp());
          querys.put("location", input.getLocation());
          try {
              * Notes: 
              * Download HttpUtils from the following link:
              * https://github.com/aliyun/api-gateway-demo-sign-java/blob/master/src/main/java/com/aliyun/api/gateway/demo/util/HttpUtils.java
              * For more information about the dependencies, visit the following link:
              * https://github.com/aliyun/api-gateway-demo-sign-java/blob/master/pom.xml
              HttpResponse response = HttpUtils.doGet(host, path, method, headers, querys);
              // Send weather query results to the specified topic.
              Boolean result = pub(EntityUtils.toString(response.getEntity()));
              return result.toString();
          catch (Exception e) {
          return "error";
      public static Boolean pub(String msg) {
          try {
          PubRequest request = new PubRequest();
          request.setProductKey("The key of your product");
          request.setTopicFullName("The topic that is used to send weather query results");
          PubResponse response = client.getAcsResponse(request);
          return response.getSuccess();
          catch (ServerException e) {
          catch (ClientException e) {
          return false;
      The following figure shows the custom model.
      Custom model

    The function implements the PojoRequestHandler interface that is provided by Function Compute. Function Compute also provides the StreamRequestHandler interface. You can select an interface based on your needs. For more information, see Use Java in Function Compute.

  4. Create a service and a function in the Function Compute console.
    1. Create a service. For more information, see Create a service.
    2. Create a function. For more information, see Create a function.
    3. After you create a function, you are directed to the Code tab on which you can upload the defined function code package. Then, check whether the function runs as expected.
  5. In the IoT Platform console, configure a data forwarding rule.
    1. Choose Rules > Data Forwarding. On the Data Forwarding page, click Create Rule and set the parameters as required to create a data forwarding rule.
    2. Find the rule that you created and click View in the Actions column. On the Data Forwarding Rule page, click Write SQL to write SQL statements for processing messages. The following figure shows the Write SQL dialog box.
      Write SQL dialog box
    3. In the Data Forwarding section, click Add Operation and set the parameters as required to configure a forwarding operation. This way, data can be forwarded to Function Compute for processing. For more information, see Forward data to Function Compute.
    4. On the Data Forwarding page, find the rule that you want to enable and click Start in the Actions column to enable the rule.
    After the rule is enabled, IoT Platform forwards relevant data to Function Compute based on the rule.
  6. Verify the feature of weather query.
    1. Log on to your simulated device and subscribe to the specified topic.
      Subscribe to a topic
    2. Log on to the IoT Platform console.Go to the Device Details page and click the Topic List tab. On the Topic List tab, find the topic that is used to publish weather query messages and click Publish in the Actions column. In the Publish dialog box, enter test information to simulate a query.
      Note You can use the city, cityid, cityCode, location, or ip field that is defined in SQL statements to query weather information.
      Publish dialog box
    3. Check whether the device receives weather information.
      Weather information