データの保存、更新、取得ができるWebサービス「milkcocoa」に接続し、データのプッシュ、送信、取得ができるライブラリを使ったサンプルです。 3G版 https://mlkcca.com/
Dependencies: C027_Support Milkcocoa mbed-rtos mbed
main.cpp@2:aa77e6a555db, 2016-02-29 (annotated)
- Committer:
- jksoft
- Date:
- Mon Feb 29 09:32:45 2016 +0000
- Revision:
- 2:aa77e6a555db
- Parent:
- 0:c36d07c6cd0f
????????????????
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
jksoft | 0:c36d07c6cd0f | 1 | #include "mbed.h" |
jksoft | 0:c36d07c6cd0f | 2 | #include "MQTTMDM.h" |
jksoft | 0:c36d07c6cd0f | 3 | #include "Milkcocoa.h" |
jksoft | 0:c36d07c6cd0f | 4 | #include "MClient.h" |
jksoft | 0:c36d07c6cd0f | 5 | |
jksoft | 0:c36d07c6cd0f | 6 | // The default setting is for the u-blox C027 |
jksoft | 0:c36d07c6cd0f | 7 | // Please change to fit the platform |
jksoft | 0:c36d07c6cd0f | 8 | Serial pc(USBTX, USBRX); |
jksoft | 0:c36d07c6cd0f | 9 | DigitalOut myled(LED1); |
jksoft | 0:c36d07c6cd0f | 10 | |
jksoft | 0:c36d07c6cd0f | 11 | /************************* Your SIM Card Setup *********************************/ |
jksoft | 0:c36d07c6cd0f | 12 | //! Set your secret SIM pin here (e.g. "1234"). Check your SIM manual. |
jksoft | 0:c36d07c6cd0f | 13 | #define SIMPIN "SIMPIN" |
jksoft | 0:c36d07c6cd0f | 14 | /*! The APN of your network operator SIM, sometimes it is "internet" check your |
jksoft | 0:c36d07c6cd0f | 15 | contract with the network operator. You can also try to look-up your settings in |
jksoft | 0:c36d07c6cd0f | 16 | google: https://www.google.de/search?q=APN+list */ |
jksoft | 0:c36d07c6cd0f | 17 | #define APN "APN" |
jksoft | 0:c36d07c6cd0f | 18 | //! Set the user name for your APN, or NULL if not needed |
jksoft | 0:c36d07c6cd0f | 19 | #define USERNAME "UserName" |
jksoft | 0:c36d07c6cd0f | 20 | //! Set the password for your APN, or NULL if not needed |
jksoft | 0:c36d07c6cd0f | 21 | #define PASSWORD "Password" |
jksoft | 0:c36d07c6cd0f | 22 | |
jksoft | 0:c36d07c6cd0f | 23 | /************************* Your Milkcocoa Setup *********************************/ |
jksoft | 0:c36d07c6cd0f | 24 | |
jksoft | 0:c36d07c6cd0f | 25 | #define MILKCOCOA_APP_ID "...YOUR_MILKCOCOA_APP_ID..." |
jksoft | 2:aa77e6a555db | 26 | #define MILKCOCOA_DATASTORE "mbed" |
jksoft | 0:c36d07c6cd0f | 27 | |
jksoft | 0:c36d07c6cd0f | 28 | /************* Milkcocoa Setup (you don't need to change this!) ******************/ |
jksoft | 0:c36d07c6cd0f | 29 | |
jksoft | 0:c36d07c6cd0f | 30 | #define MILKCOCOA_SERVERPORT 1883 |
jksoft | 0:c36d07c6cd0f | 31 | |
jksoft | 0:c36d07c6cd0f | 32 | /************ Global State (you don't need to change this!) ******************/ |
jksoft | 0:c36d07c6cd0f | 33 | |
jksoft | 0:c36d07c6cd0f | 34 | const char MQTT_SERVER[] = MILKCOCOA_APP_ID ".mlkcca.com"; |
jksoft | 0:c36d07c6cd0f | 35 | const char MQTT_CLIENTID[] = __TIME__ MILKCOCOA_APP_ID; |
jksoft | 0:c36d07c6cd0f | 36 | |
jksoft | 0:c36d07c6cd0f | 37 | extern void onpush(MQTT::MessageData& md); |
jksoft | 0:c36d07c6cd0f | 38 | |
jksoft | 0:c36d07c6cd0f | 39 | int main() { |
jksoft | 0:c36d07c6cd0f | 40 | // void setup() { |
jksoft | 0:c36d07c6cd0f | 41 | pc.baud(9600); |
jksoft | 0:c36d07c6cd0f | 42 | |
jksoft | 0:c36d07c6cd0f | 43 | MQTTMDM *ipstack = new MQTTMDM(SIMPIN, APN,USERNAME,PASSWORD); |
jksoft | 0:c36d07c6cd0f | 44 | MClient *client = new MClient(ipstack); |
jksoft | 0:c36d07c6cd0f | 45 | Milkcocoa *milkcocoa = new Milkcocoa(client, MQTT_SERVER, MILKCOCOA_SERVERPORT, MILKCOCOA_APP_ID, MQTT_CLIENTID); |
jksoft | 0:c36d07c6cd0f | 46 | |
jksoft | 0:c36d07c6cd0f | 47 | pc.printf("Milkcocoa mbed ver demo\n\r\n\r\n\r"); |
jksoft | 0:c36d07c6cd0f | 48 | |
jksoft | 0:c36d07c6cd0f | 49 | milkcocoa->connect(); |
jksoft | 0:c36d07c6cd0f | 50 | pc.printf("\n\rEther connected\n\r"); |
jksoft | 0:c36d07c6cd0f | 51 | |
jksoft | 0:c36d07c6cd0f | 52 | pc.printf("%d\n\r",milkcocoa->on(MILKCOCOA_DATASTORE, "push", onpush)); |
jksoft | 0:c36d07c6cd0f | 53 | |
jksoft | 0:c36d07c6cd0f | 54 | // } |
jksoft | 0:c36d07c6cd0f | 55 | while(1) { |
jksoft | 0:c36d07c6cd0f | 56 | // void loop() { |
jksoft | 0:c36d07c6cd0f | 57 | milkcocoa->loop(); |
jksoft | 0:c36d07c6cd0f | 58 | |
jksoft | 0:c36d07c6cd0f | 59 | DataElement elem = DataElement(); |
jksoft | 0:c36d07c6cd0f | 60 | elem.setValue("v", 1); |
jksoft | 0:c36d07c6cd0f | 61 | |
jksoft | 0:c36d07c6cd0f | 62 | milkcocoa->push(MILKCOCOA_DATASTORE, elem); |
jksoft | 0:c36d07c6cd0f | 63 | wait(7.0); |
jksoft | 0:c36d07c6cd0f | 64 | } |
jksoft | 0:c36d07c6cd0f | 65 | } |
jksoft | 0:c36d07c6cd0f | 66 | |
jksoft | 0:c36d07c6cd0f | 67 | void onpush(MQTT::MessageData& md) |
jksoft | 0:c36d07c6cd0f | 68 | { |
jksoft | 0:c36d07c6cd0f | 69 | MQTT::Message &message = md.message; |
jksoft | 0:c36d07c6cd0f | 70 | DataElement de = DataElement((char*)message.payload); |
jksoft | 0:c36d07c6cd0f | 71 | pc.printf("onpush\n\r"); |
jksoft | 0:c36d07c6cd0f | 72 | pc.printf("%d\n\r",de.getInt("v")); |
jksoft | 0:c36d07c6cd0f | 73 | } |