|
| 1 | +[](https://www.gnu.org/licenses/gpl-3.0) |
| 2 | + |
| 3 | +# arduino-iot-js |
| 4 | + |
| 5 | +JS module providing interaction with Arduino Cloud |
| 6 | + |
| 7 | +## Installation |
| 8 | + |
| 9 | +```bash |
| 10 | +$ npm install git+ssh:// [email protected]/arduino/arduino-iot-js.git |
| 11 | +``` |
| 12 | + |
| 13 | +## How to use |
| 14 | +```javascript |
| 15 | +import ArduinoCloud from 'arduino-iot-js'; |
| 16 | + |
| 17 | +// connect establishes a connection with mqtt, using token as the password |
| 18 | +// Development Vernemq server host is wss.iot.oniudra.cc port 8443, token is your Hydra bearer token |
| 19 | +// options = { |
| 20 | +// host: 'BROKER_URL', // Default is wss.iot.oniudra.cc |
| 21 | +// port: BROKER_PORT, // Default is 8443 |
| 22 | +// ssl: true/false, // Default is true |
| 23 | +// env: 'dev', // Api env, default is dev (for now!) |
| 24 | +// token: 'YOUR_BEARER_TOKEN' // Required! |
| 25 | +// apiUrl: 'AUTH SERVER URL', // https://auth-dev.arduino.cc for dev |
| 26 | +// onDisconnect: message => { /* Disconnection callback */ } |
| 27 | +// } |
| 28 | +ArduinoCloud.connect(options).then(connectionId => { |
| 29 | + // Connected |
| 30 | +}); |
| 31 | + |
| 32 | +ArduinoCloud.disconnect(connectionId).then(() => { |
| 33 | + // Disconnected |
| 34 | +}); |
| 35 | + |
| 36 | +ArduinoCloud.subscribe(connectionId, topic, cb).then(topic => { |
| 37 | + // Subscribed to topic, messaged fired in the cb |
| 38 | +}); |
| 39 | + |
| 40 | +ArduinoCloud.unsubscribe(connectionId, topic).then(topic => { |
| 41 | + // Unsubscribed to topic |
| 42 | +}); |
| 43 | + |
| 44 | +ArduinoCloud.sendMessage(connectionId, topic, message).then(() => { |
| 45 | + // Message sent |
| 46 | +}); |
| 47 | + |
| 48 | +ArduinoCloud.openCloudSerialMonitor(connectionId, deviceId, cb).then(topic => { |
| 49 | + // Serial monitor messages fired to cb |
| 50 | +}); |
| 51 | + |
| 52 | +ArduinoCloud.writeCloudSerialMonitor(connectionId, deviceId, message).then(() => { |
| 53 | + // Message sent to serial monitor |
| 54 | +}); |
| 55 | + |
| 56 | +ArduinoCloud.closeCloudSerialMonitor(connectionId, deviceId).then(topic => { |
| 57 | + // Close serial monitor |
| 58 | +}); |
| 59 | + |
| 60 | +// Send a property value to a device |
| 61 | +// - value can be a string, a boolean or a number |
| 62 | +// - timestamp is a unix timestamp, not required |
| 63 | +ArduinoCloud.sendProperty(connectionId, deviceId, name, value, timestamp).then(() => { |
| 64 | + // Property value sent |
| 65 | +}); |
| 66 | + |
| 67 | +// Register a callback on a property value change |
| 68 | +// |
| 69 | +ArduinoCloud.onPropertyValue(connectionId, deviceId, propertyName, updateCb).then(() => { |
| 70 | + // updateCb(message) will be called every time a new value is available. Value can be string, number, or a boolean depending on the property type |
| 71 | +}); |
| 72 | + |
| 73 | +``` |
| 74 | + |
| 75 | +## Run tests |
| 76 | +First of all you need a valid Hydra Arduino token, you can get it from [Create Cloud Dev](https://create-dev.arduino.cc/cloud/) |
| 77 | + |
| 78 | +After you can use this token to run tests |
| 79 | + |
| 80 | +```bash |
| 81 | +$ TOKEN=YOUR_HYDRA_TOKEN_HERE npm run test |
| 82 | +``` |
0 commit comments