Skip to content

Examples

Johann Forster edited this page Jun 12, 2019 · 1 revision

... with JavaScript and fetch.

create a new device

var resp = await fetch("/devices", {
    method: "POST",
    headers: {
		'Content-Type': 'application/json'
	},
    body: JSON.stringify({
        // id: "5cde6d034b9f61" // let the server choose an id
        name: "My Device 1",    // readable device name
        sensors: [{             // sensors list:
			id: "6f840f0b1",       // sensor id (hardware id)
			name: "My Sensor 1",   // readable name
        }, {
			id: "df34b9f612",
			name: "My Sensor 2",
        }],
        actuators: [{           // actuators list:
			id: "40f034",
			name: "My Actuator 1",
        }],
    })
});
// the device id will be returned
var deviceId = await resp.json();
alert(`new device.id: ${deviceId}`);

Console output will be like:

new device.id: 5cde6d034b9f610ff8373bdb

change a device name

var deviceId = "5cde6d034b9f610ff8373bdb";
await fetch(`/devices/${deviceId}/name`, {
    method: "POST",
    headers: {
		'Content-Type': 'application/json'
	},
    body: JSON.stringify("New Device Name")
});

delete a device

var deviceId = "5cde6d034b9f610ff8373bdb";
fetch(`/devices/${deviceId}`, {
    method: "DELETE",
});

list all devices

var resp = await fetch("/devices");
var devices = await resp.json();
console.log(devices);

Console output will be like: (without comments)

[{
  name: "My Device 1",
  id: "5cde6d034b9f61",
  // Sensors List
  sensors: [{
    // unique sensor id
    id: "6f840f0b1",
    // Sensor name
    name: "My Sensor 1",
    // time when last modified
    modified: "2019-06-03T12:31:34.331Z",
    // time when created
    created: "2019-06-03T12:31:34.331Z",
    // time last value received
    time: "2019-06-03T12:15:47.971Z",
    // last value
    value: null
  },{
    id: "df34b9f612",
    name: "My Sensor 2",
    modified: "0001-01-01T00:00:00Z",
    created: "2019-06-03T12:31:34.331Z",
    time: "2019-06-03T12:15:47.971Z",
    value: null
  }],
  // Actuators List
  actuators: [{
    id: "40f034",
    name: "My Actuator 1",
    modified: "2019-06-03T12:30:52.106Z",
    created: "2019-06-03T12:31:34.331Z",
    time: "2019-06-03T12:15:47.971Z",
    value: null
  }],
  modified: "2019-06-03T12:30:52.106Z",
  created: "2019-06-03T12:31:34.331Z"
}]
  • Modified-times for devices get updated when the device name changes (using POST .../name) but not if a new sensor or actuator gets added or deleted.
  • Modified-times on sensors or actuators get updated when the sensor or actuator name changes, but not if a new value gets posted.
  • Time and Value for each sensor are the same as at GET .../value

create a new sensor or actuator

var deviceId = "5cde6d034b9f610ff8373bdb";
await fetch(`/devices/${deviceId}/sensors`, {
    method: "POST",
    headers: {
		'Content-Type': 'application/json'
	},
    body: JSON.stringify({
        id: "0ff8373bd",       // sensor id (hardware id)
        name: "My Sensor 3",   // readable name
    })
});

The same goes for actuators. Just replace sensors with actuators.

change a sensor or actuator name

var sensorId = "0ff8373bd";
var deviceId = "5cde6d034b9f610ff8373bdb";
await fetch(`/devices/${deviceId}/sensors/${sensorId}/name`, {
    method: "POST",
    headers: {
		'Content-Type': 'application/json'
	},
    body: JSON.stringify("New Sensor Name")
});

list all sensors or actuators

var deviceId = "5cde6c194b9f610ff8373bda";
var resp = await fetch(`/devices/${deviceId}/sensors`);
var sensors = await resp.json();
console.log(sensors);

delete a sensor or actuator

var sensorId = "0ff8373bd";
var deviceId = "5cde6d034b9f610ff8373bdb";
fetch(`/devices/${deviceId}/sensors/${sensorId}`, {
    method: "DELETE",
});

upload a sensor or actuator value

var sensorId = "0ff8373bd";
var deviceId = "5cde6d034b9f610ff8373bdb";

var value = 42; // numeric value
// or
var value = "Temp45%23"; // string value
// or
var value = {lat: 52, long: 7}; // complex value
// or
var value = {
  time: new Date(),  // value at specific time
  value: 42          // value
};

fetch(`/devices/${deviceId}/sensors/${sensorId}/value`, {
  method: "POST",
  headers: {
    'Content-Type': 'application/json'
  },
  body: JSON.stringify(value)
});

upload multiple sensor or actuator values

var sensorId = "0ff8373bd";
var deviceId = "5cde6d034b9f610ff8373bdb";

var values = [42, 45, 47]; // values array
// or
var values = ["a", 0x0b, "cde"]; // ...
// or
var values = [
  { // values with timestamp
    time: new Date(),
    value: 42,
  }, {
    time: new Date(),
    value: 43,
  }
]

fetch(`/devices/${deviceId}/sensors/${sensorId}/values`, {
  method: "POST",
  headers: {
    'Content-Type': 'application/json'
  },
  body: JSON.stringify(values)
});

get the last sensor or actuator value

var sensorId = "0ff8373bd";
var deviceId = "5cde6d034b9f610ff8373bdb";
var resp = await fetch(`/devices/${deviceId}/sensors/${sensorId}/value`);
var value = await resp.json();
console.log(value);

get multiple sensor or actuator values

var sensorId = "0ff8373bd";
var deviceId = "5cde6d034b9f610ff8373bdb";
var resp = await fetch(`/devices/${deviceId}/sensors/${sensorId}/values`);
var values = await resp.json();
console.log(values);

add a Waziup Cloud for synchronization

fetch(`/clouds`, {
  method: "POST",
  headers: {
    'Content-Type': 'application/json'
  },
  body: JSON.stringify({
    rest: "api.waziup.io/api/v2",
    mqtt: "api.waziup.io",
    paused: true, // default false
    credentials: {
      username: "myUsername",
      token: "myPassword"
    }
  })
});
// the cloud id will be returned
var cloudId = await resp.json();
alert(`new cloud.id: ${cloudId}`);

Console output will be like:

new cloud.id: 5ce2793d4b9f612a04a7951d

list all configured clouds

var resp = await fetch("/clouds");
var clouds = await resp.json();
console.log(clouds);

Output will be like:

{
  "5ce2793d4b9f612a04a7951d": {
    id: "5ce2793d4b9f612a04a7951d",
    paused: true,
    rest: "api.waziup.io/api/v2",
    mqtt: "api.waziup.io",
    credentials: {
      username: "myUsername",
      token: "myPassword"
    },
    // bytes in queue waiting for sync with server
    queue: 136,
    // sync status code (200 = OK)
    statusCode: 200,
    // readable status text or error, if any
    statusText: "MQTT connected for persistent sync."
  }
}

statusCode can be one of:
200 OK. The synchronization is active.
0 The synchronization is paused.
Any other code: There was a problem with this synchronization.

pause & resume cloud synchronization

var cloudId = "5ce2793d4b9f612a04a7951d";
fetch(`/clouds/${cloudId}/paused`, {
  method: "POST",
  headers: {
    'Content-Type': 'application/json'
  },
  body: JSON.stringify(false) // or true
});

change cloud credentials (username and password)

var cloudId = "5ce2793d4b9f612a04a7951d";
fetch(`/clouds/${cloudId}/credentials`, {
  method: "POST",
  headers: {
    'Content-Type': 'application/json'
  },
  body: JSON.stringify({
    username: "myNewUserName",
    token: "myNewPassword",
  })
});

change the cloud url (rest or mqtt)

var cloudId = "5ce2793d4b9f612a04a7951d";
fetch(`/clouds/${cloudId}/mqtt`, {
  method: "POST",
  headers: {
    'Content-Type': 'application/json'
  },
  body: JSON.stringify("waziup.myserver.com")
});
var cloudId = "5ce2793d4b9f612a04a7951d";
fetch(`/clouds/${cloudId}/rest`, {
  method: "POST",
  headers: {
    'Content-Type': 'application/json'
  },
  body: JSON.stringify("waziup.myserver.com")
});