Page tree

Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

  • this method sends the sensor data to the UCW Platform
  • the payload is checked if data has been received from sensor

    Code Block
    languagecpp
    themeFadeToGrey
     if (payload.length() < 1) {
        Serial.println("No data to send!");
        return;
      }


  • connection is made to the server and payload, device ID information are transferred using REST API

    Code Block
    languagecpp
    themeFadeToGrey
    if (client.connect(server, UCW_API_PORT)) {
        Serial.println();
        Serial.println("Request:");
        Serial.println();
        Serial.println("Sending payload: " + payload);
        Serial.print("Payload length: ");
        Serial.println(payload.length());
    
        String apiUri = "POST /api/ucw/v1/data-streams/%dataStream/messages/%deviceId HTTP/1.1";
        apiUri.replace("%dataStream", UCW_API_DATA_STREAM);
    	apiUri.replace("%deviceId", UCW_API_DEVICE_ID);
    	
        Serial.print("API URI: ");
        Serial.println(apiUri);
        
        client.println(apiUri);
        client.print("Host: ");
        client.println(UCW_API_HOST);
        client.println("User-Agent: Adafruit-Feather-M0-Wifi");
        client.println("Connection: close");
        client.println("Content-Type: application/json");
        client.print("Content-Length: ");
        client.println(payload.length());
        client.print("Authorization: Bearer ");
        client.println(UCW_API_DEVICE_TOKEN);
        client.println();
        client.println(payload);
    
        Serial.println();
        Serial.println("Response:");
        Serial.println();
    
        while (client.connected()) {
          while (client.available()) {
            char c = client.read();
            Serial.write(c);
          }
        }
     // This will free the socket on the WiFi shield
        client.stop();
      }


  • reset the Wifi connection using resetWifi() method if connection to server fails

    Code Block
    languagecpp
    themeFadeToGrey
    else {
        // if you couldn't make a connection
        Serial.println("connection failed");
        resetWifi();
      }


...