![](/media/cache/group/default_image.jpg.50x50_q85.jpg)
Demo code for the DeviceHubNet library
Dependencies: DeviceHubNet TMRh20 mbed
main.cpp@2:74f17b595862, 2017-04-04 (annotated)
- Committer:
- gume
- Date:
- Tue Apr 04 02:22:24 2017 +0000
- Revision:
- 2:74f17b595862
- Parent:
- 0:c3057a9bb613
Add comments
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
gume | 0:c3057a9bb613 | 1 | #include <mbed.h> |
gume | 0:c3057a9bb613 | 2 | #include "DeviceHubNet.h" |
gume | 0:c3057a9bb613 | 3 | |
gume | 0:c3057a9bb613 | 4 | Serial pc(PB_6, PB_7); |
gume | 0:c3057a9bb613 | 5 | DigitalOut led(PA_8); |
gume | 0:c3057a9bb613 | 6 | |
gume | 2:74f17b595862 | 7 | // Here is your project settings |
gume | 2:74f17b595862 | 8 | // Projecty ID, APIkey, Device ID |
gume | 2:74f17b595862 | 9 | // Get your own IDs from the DeviceHub.net site |
gume | 2:74f17b595862 | 10 | DeviceHubNet DHN(4275, "bbbb950b-ad0c-4fcd-8f0a-546e154a1c35", "40854b01-0ff4-407f-bc63-fa75f6604ec4"); |
gume | 0:c3057a9bb613 | 11 | |
gume | 2:74f17b595862 | 12 | // This one is a callback function, you can set up with the actuator |
gume | 2:74f17b595862 | 13 | // The type will tell you the type of the data as 0: digital, 1: float |
gume | 2:74f17b595862 | 14 | // According to the type, only ddata OR adata is valid. The other is 0. |
gume | 0:c3057a9bb613 | 15 | void onLightSwitchMsg(uint8_t type, uint8_t ddata, float adata) |
gume | 0:c3057a9bb613 | 16 | { |
gume | 0:c3057a9bb613 | 17 | pc.printf("Data received. Type: %d\n\r", type); |
gume | 0:c3057a9bb613 | 18 | pc.printf("Switching light: %d\n\r", ddata); |
gume | 0:c3057a9bb613 | 19 | } |
gume | 0:c3057a9bb613 | 20 | |
gume | 0:c3057a9bb613 | 21 | int main() |
gume | 0:c3057a9bb613 | 22 | { |
gume | 0:c3057a9bb613 | 23 | led = 1; |
gume | 0:c3057a9bb613 | 24 | wait_ms(2000); |
gume | 0:c3057a9bb613 | 25 | |
gume | 2:74f17b595862 | 26 | // Here you set the radio communication parameters |
gume | 2:74f17b595862 | 27 | // First the pins, which the radio modul uses |
gume | 2:74f17b595862 | 28 | // Parameters: SPI_MOSI, SPI_MISO, SPI_SCK, CS, CE |
gume | 2:74f17b595862 | 29 | |
gume | 2:74f17b595862 | 30 | //DHN.radioPinConfig(SPI_MOSI, SPI_MISO, SPI_SCK, PB_4, PB_3); |
gume | 0:c3057a9bb613 | 31 | DHN.radioPinConfig(PA_7, PA_6, PA_5, PB_3, PB_4); |
gume | 2:74f17b595862 | 32 | |
gume | 2:74f17b595862 | 33 | // Second the ID and the channel |
gume | 2:74f17b595862 | 34 | // Parameters: Unique ID (0-65535), channel (0-127) |
gume | 0:c3057a9bb613 | 35 | DHN.radioConfig(0x1234, 100); |
gume | 0:c3057a9bb613 | 36 | |
gume | 0:c3057a9bb613 | 37 | pc.printf("DeviceHubNet DEMO started.\n\r\n\r"); |
gume | 2:74f17b595862 | 38 | |
gume | 2:74f17b595862 | 39 | // Dumps the configuration |
gume | 2:74f17b595862 | 40 | // If all 0 or anything weired, the the configuration was wrong |
gume | 0:c3057a9bb613 | 41 | DHN.radioDump(); |
gume | 0:c3057a9bb613 | 42 | |
gume | 0:c3057a9bb613 | 43 | //uint16_t delay = DHN.radioPing(); |
gume | 0:c3057a9bb613 | 44 | //pc.printf("Ping delay: %d", delay); |
gume | 0:c3057a9bb613 | 45 | |
gume | 2:74f17b595862 | 46 | |
gume | 2:74f17b595862 | 47 | // Registers your sensor. Name should match with the DHN's sensor name |
gume | 2:74f17b595862 | 48 | // The returned ID can be used for data sending |
gume | 0:c3057a9bb613 | 49 | uint16_t sid = DHN.registerSensor("LightSense"); |
gume | 0:c3057a9bb613 | 50 | pc.printf("Sensor registered. %d\n\r", sid); |
gume | 2:74f17b595862 | 51 | |
gume | 2:74f17b595862 | 52 | // Registers your actuator. Name should match with the DHN's actuator name |
gume | 2:74f17b595862 | 53 | // Type of the actuator should be set here. 0: digital (0-1), 1: analog (float) |
gume | 2:74f17b595862 | 54 | // You should define a callback function for the data |
gume | 2:74f17b595862 | 55 | // Parameters: name, type, callback |
gume | 0:c3057a9bb613 | 56 | uint16_t aid = DHN.registerActuator("LightSwitch", 0, &onLightSwitchMsg); |
gume | 0:c3057a9bb613 | 57 | pc.printf("Actuator registered. %d\n\r", aid); |
gume | 0:c3057a9bb613 | 58 | |
gume | 0:c3057a9bb613 | 59 | while (1) { |
gume | 2:74f17b595862 | 60 | |
gume | 2:74f17b595862 | 61 | // Message processing. Must be called frequently in order to receiver actuator messages |
gume | 0:c3057a9bb613 | 62 | DHN.processMsgs(); |
gume | 2:74f17b595862 | 63 | |
gume | 2:74f17b595862 | 64 | // Send sensor data |
gume | 0:c3057a9bb613 | 65 | DHN.sendDigitalData(sid, 1); |
gume | 0:c3057a9bb613 | 66 | wait_ms(500); |
gume | 0:c3057a9bb613 | 67 | led = ! led; |
gume | 0:c3057a9bb613 | 68 | DHN.sendAnalogData(sid, 3.14); |
gume | 0:c3057a9bb613 | 69 | wait_ms(200); |
gume | 0:c3057a9bb613 | 70 | led = ! led; |
gume | 0:c3057a9bb613 | 71 | } |
gume | 0:c3057a9bb613 | 72 | |
gume | 0:c3057a9bb613 | 73 | } |