Background information

The user specifies a city name, city code, or the latitude and longitude of a city. IoT Platform uses the rules engine to pass the request to Function Compute for further processing. Function Compute then queries the weather service and retrieves the weather data for the specified city. The query results are eventually sent to the specified topic.
  • In this example, one topic performs weather queries, and the other receives query results.
  • This example uses Function Compute to access third-party services in Alibaba Cloud API Marketplace.


  1. Define the function.

    Business logic:

    • Performs weather queries based on city information.
    • An API is used to query weather information.
    • The query results are sent to the specified topic of the device using the service API provided by IoT Platform.
    • Create a new project and import the following JAR files:
    • Code logic:
      public class FcToApiDemo implements PojoRequestHandler {
      private static String host = "";
      private static String path = "/weather/query";
      private static String method = "GET";
      private static String appcode = "Your appCode";
      private static String accessKey = "Your Alibaba Cloud Access Key ID";
      private static String accessSecret = "Your Alibaba Cloud Access Key Secret";
      static DefaultAcsClient client;
      static {
      IClientProfile profile = DefaultProfile.getProfile("cn-shanghai", accessKey, accessSecret);
      try {
      DefaultProfile.addEndpoint("cn-shanghai", "cn-shanghai", "Iot", "");
      } catch (ClientException e) {
      client = new DefaultAcsClient(profile); // Initialize a client object.
      public String handleRequest(CityModel input, Context context) {
      Map headers = new HashMap();
      // The format of the header is as follows (include a space in the middle): Authorization:APPCODE
      // 83359fd73fe94948385f570e3c139105
      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 {
      * Note: Download HttpUtils from the following address:
      * For more information about the dependencies, see:
      HttpResponse response = HttpUtils.doGet(host, path, method, headers, querys);
      // Send the 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 productKey of your product");
      request.setTopicFullName("The topic for receiving weather query results");
      PubResponse response = client.getAcsResponse(request);
      return response.getSuccess();
      } catch (ServerException e) {
      } catch (ClientException e) {
      return false;
      The custom model is as shown in the following picture.

    The function implements the PojoRequestHandler interface that is provided by Function Compute. Function Compute also provides the StreamRequestHandler interface. Select an interface based on your needs.

    For more information, see Java basics.

  2. Configure the function
    1. Log on to the Function Compute console.
    2. Click Create Function, select No Trigger, and configure the basic information, as shown in the following picture.
    3. Click Next, Create.
    4. After the configuration is complete, test the function to see whether the function runs properly.

      The function runs properly if the code logic is correct.

  3. Log on to the IoT Platform console, create a product and a device, and define topics.
    1. Log on to the IoT Platform console.
    2. Select Products, and click Create Product to create an IoT Platform product. In this example, the product is named weatherProduct.
    3. Select Devices, select weatherProduct, and then click Add Device to specify the device name.
    4. Select Products, and click View next to weather Product.
    5. Select Topic Categories > Custom Topics > Define Topic Category, and define the topics for performing weather queries and receiving query results, as shown in Figure 1.
      Figure 1. Define topics
    6. Select Rules > Data Forwarding, and click Create Rule to create a new JSON rule.
    7. Click Write SQL and write the SQL, as shown in Figure 2.
      Figure 2. Rules engine
    8. Click Add Operation and configure the rule to forward data to the function.
    9. On the Data Forwarding page, click Start corresponding to the rule to start the rule.
  4. Test the functionality of weather query.
    • Log on to your emulated device and subscribe to the specified topic.
    • Send a message from the console.

      You can also use cityid, cityCode, location, or IP information to query weather information.

    • The device receives the message.