public static object SendCommand(string deviceId, string projectId,
string cloudRegion, string registryName, string data)
{
var cloudIot = CreateAuthorizedClient();
var devicePath = String.Format("projects/{0}/locations/{1}/registries/{2}/devices/{3}",
projectId, cloudRegion, registryName, deviceId);
// Data sent through the wire has to be base64 encoded.
SendCommandToDeviceRequest req = new SendCommandToDeviceRequest()
{
BinaryData = Convert.ToBase64String(Encoding.UTF8.GetBytes(data))
};
Console.WriteLine("Sending command to {0}\n", devicePath);
var res =
cloudIot.Projects.Locations.Registries.Devices.SendCommandToDevice(req, devicePath).Execute();
Console.WriteLine("Command response: " + res.ToString());
return 0;
}
# project_id = "Your Google Cloud project ID"
# location_id = "The Cloud region the registry is located in"
# registry_id = "The registry containing the device to send commands to"
# device_id = "The identifier of the device to send commands to"
# data = "The command, e.g. {move: forward} to send to the device"
require "google/apis/cloudiot_v1"
# Initialize the client and authenticate with the specified scope
Cloudiot = Google::Apis::CloudiotV1
iot_client = Cloudiot::CloudIotService.new
iot_client.authorization = Google::Auth.get_application_default(
"https://www.googleapis.com/auth/cloud-platform"
)
# The resource name of the location associated with the project
parent = "projects/#{project_id}/locations/#{location_id}"
resource = "#{parent}/registries/#{registry_id}/devices/#{device_id}"
command_req = Cloudiot::SendCommandToDeviceRequest.new
command_req.binary_data = data
# Set configuration for the provided device
iot_client.send_project_location_registry_device_command_to_device resource, command_req
puts "Command sent!"