The M2MGO RESTful API interface supports a uni-directional communication method between the M2MGO Platform and the devices. For a bi-directional communication we advice to use the MQTT interface.
By using the M2MGO RESTful API interface the device can send any data to the M2MGO, e.g. device can send the temperature value to the M2MGO.
The M2MGO RESTful API uses the standard JSON format for data exchanging, and uses the standard methods from a RESTful web API methods for the data exchange.

Send Sensor Data

Sends sensor data to the M2MGO platform.

Endpoint: https://demo.m2mgo.com/api/device/data
Type: POST
Parameters:The parameters are given in the POST body, in JSON format

  • DeviceName:String representing the Identifier from the Device. You can choose any combination of characters with length > 3
  • DeviceType:Is the Blueprint UUID. It can be found in the Blueprints page, under UUID
  • SensorKey:Identifier from the sensor sending the data. It can be found when you edit the Blueprint, under Sensor Key
  • Value:String representing the value you want to send.

Response
  • 200 (OK):Data has been processed and is in the system.
  • 400 (Bad Request):Some of the parameters are in incorrect format or where not delivered.
  • 403 (Forbidden):There are no privileges to run this action. Normally it happens because the Device is being registered, and the maximum number of devices for this blueprint has been reached.

Examples: All this examples are ready for download in the downloads section

Web Example

C# Example

private class SaveSensorDataParameters {
	public Guid DeviceType { get; set; }
	public string DeviceName { get; set; }
	public string SensorKey { get; set; }
	public string Value { get; set; }
}
	
void Main()
{
	try{
	
		const string URL = "http://m2mgo.cloudapp.net/api/device/data";
		var parameters = new SaveSensorDataParameters(){
			DeviceType = Guid.Parse("XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX"),
			DeviceName = "Example1",
			SensorKey = "temperature",
			Value =     "25",
		};
		
		var request = (HttpWebRequest)WebRequest.Create(URL);
		request.Method = "POST";
		
		var builder = new StringBuilder();
		using (var sw = new StringWriter(builder)){        
			new Newtonsoft.Json.JsonSerializer().Serialize(sw,parameters);
			byte[] parameterInBytes = Encoding.UTF8.GetBytes(builder.ToString());
			request.ContentType = "application/json";            
			request.ContentLength = parameterInBytes.Length;
			request.GetRequestStream().Write(parameterInBytes,0,parameterInBytes.Count());
		}
		
		HttpWebResponse httpResp;
		try {
			WebResponse resp = request.GetResponse();
			httpResp = resp as HttpWebResponse;
		} catch (WebException webEx) {
			httpResp = (HttpWebResponse)webEx.Response;
		}
		httpResp.Dump();
		if (httpResp == null)
			throw new Exception("Webresponse is not a HttpWebResponse: ");
	
		string responseData = String.Empty;
		var responseStream = httpResp.GetResponseStream();
	
		if (responseStream == null)
			throw new Exception("Unexpected response null");
			
		responseData = new StreamReader(responseStream).ReadToEnd();
	
		if (httpResp.StatusCode == HttpStatusCode.Forbidden) {
			throw new Exception("Forbidden Request response!");
		}

		if (httpResp.StatusCode == HttpStatusCode.BadRequest) {
			throw new Exception("Bad Request response!");
		}

		if (httpResp.StatusCode == HttpStatusCode.OK) {
			// GREAT!
			Console.Write("Worked");
		}
		else{
			throw new Exception("Unexpected Error");
		}
	}
	catch(Exception exception)
	{
		exception.Dump();
	}
}

Send Attribute Data

Sends data for one attribute to the platform.

Endpoint: https://demo.m2mgo.com/api/device/attribute
Type: POST
Parameters: The parameters are given in the POST body, in JSON format

  • DeviceName:String representing the Identifier from the Device. You can choose any combination of characters with length > 3
  • DeviceType:Is the Blueprint UUID. It can be found in the Blueprints page, under UUID
  • AttributeName:Name from the attribute sending the data. It can be found when you edit the Blueprint, under attribute name
  • Value:String representing the value you want to send.

Response
  • 200 (OK):Data has been processed and is in the system.
  • 400 (Bad Request):Some of the parameters are in incorrect format or where not delivered.
  • 403 (Forbidden):There are no privileges to run this action. Normally it happens because the Device is being registered, and the maximum number of devices for this blueprint has been reached.

Examples: All this examples are ready for download in the downloads section

Web Example

C# Example

 
private class SaveAttributeDataParameters {
	public Guid DeviceType { get; set; }
	public string DeviceName { get; set; }
	public string AttributeName { get; set; }
	public string Value { get; set; }
}
	
void Main()
{
	try{
	
		const string URL = "http://m2mgo.cloudapp.net/api/device/attribute";
		var parameters = new SaveAttributeDataParameters(){
			DeviceType = Guid.Parse("XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX"),
			DeviceName = "Example1",
			AttributeName = "color",
			Value =     "blue",
		};
		
		var request = (HttpWebRequest)WebRequest.Create(URL);
		request.Method = "POST";
		
		var builder = new StringBuilder();
		using (var sw = new StringWriter(builder)){        
			new Newtonsoft.Json.JsonSerializer().Serialize(sw,parameters);
			byte[] parameterInBytes = Encoding.UTF8.GetBytes(builder.ToString());
			request.ContentType = "application/json";            
			request.ContentLength = parameterInBytes.Length;
			request.GetRequestStream().Write(parameterInBytes,0,parameterInBytes.Count());
		}
		
		HttpWebResponse httpResp;
		try {
			WebResponse resp = request.GetResponse();
			httpResp = resp as HttpWebResponse;
		} catch (WebException webEx) {
			httpResp = (HttpWebResponse)webEx.Response;
		}
		httpResp.Dump();
		if (httpResp == null)
			throw new Exception("Webresponse is not a HttpWebResponse: ");
	
		string responseData = String.Empty;
		var responseStream = httpResp.GetResponseStream();
	
		if (responseStream == null)
			throw new Exception("Unexpected response null");
			
		responseData = new StreamReader(responseStream).ReadToEnd();
	
		if (httpResp.StatusCode == HttpStatusCode.Forbidden) {
			throw new Exception("Forbidden Request response!");
		}
	
		if (httpResp.StatusCode == HttpStatusCode.BadRequest) {
			throw new Exception("Bad Request response!");
		}
	
		if (httpResp.StatusCode == HttpStatusCode.OK) {
			// GREAT!
			Console.Write("Worked");
		}
		else{
			throw new Exception("Unexpected Error");
		}
	}
	catch(Exception exception)
	{
		exception.Dump();
	}
}

Get Pending Commands

Get the pending commands for the selected device.

Endpoint: https://demo.m2mgo.com/api/pendingcommand/{DeviceTypeID}/{DeviceName}
Type: POST
Parameters: None
Route

  • DeviceTypeID:Is the Blueprint UUID. It can be found in the Blueprints page, under UUID
  • DeviceName:String representing the Identifier from the Device. You can choose any combination of characters with length > 3

Response
  • 200 (OK):Returns a lit of pending commands with an ID and a command String.
  • 400 (Bad Request):Some of the parameters are in incorrect format or where not delivered.
  • 403 (Forbidden):There are no privileges to run this action. Normally it happens because the device does not exist

Examples: All this examples are ready for download in the downloads section

Web Example

Removes a pending command from a device

Removes a pending command from a device. This method should be used once the command has been processed by the device.

Endpoint: https://demo.m2mgo.com/api/pendingcommand/{PendingCommandID}
Type: DELETE
Parameters: None
Route

  • PendingCommandID:The id of the Pending Command. This can be retrieben in with the Get Pending Commands method
Response
  • 200 (OK):Command is deleted
  • 400 (Bad Request):Some of the parameters are in incorrect format or where not delivered.
  • 403 (Forbidden):There are no privileges to run this action. Normally it happens because the pending command Id does not exist

Examples: All this examples are ready for download in the downloads section

Web Example