データの保存、更新、取得ができるWebサービス「milkcocoa」に接続し、データのプッシュ、送信、取得ができるライブラリを使ったサンプルです。 3G版 https://mlkcca.com/
Dependencies: C027_Support Milkcocoa mbed-rtos mbed
main.cpp
00001 #include "mbed.h" 00002 #include "MQTTMDM.h" 00003 #include "Milkcocoa.h" 00004 #include "MClient.h" 00005 00006 // The default setting is for the u-blox C027 00007 // Please change to fit the platform 00008 Serial pc(USBTX, USBRX); 00009 DigitalOut myled(LED1); 00010 00011 /************************* Your SIM Card Setup *********************************/ 00012 //! Set your secret SIM pin here (e.g. "1234"). Check your SIM manual. 00013 #define SIMPIN "SIMPIN" 00014 /*! The APN of your network operator SIM, sometimes it is "internet" check your 00015 contract with the network operator. You can also try to look-up your settings in 00016 google: https://www.google.de/search?q=APN+list */ 00017 #define APN "APN" 00018 //! Set the user name for your APN, or NULL if not needed 00019 #define USERNAME "UserName" 00020 //! Set the password for your APN, or NULL if not needed 00021 #define PASSWORD "Password" 00022 00023 /************************* Your Milkcocoa Setup *********************************/ 00024 00025 #define MILKCOCOA_APP_ID "...YOUR_MILKCOCOA_APP_ID..." 00026 #define MILKCOCOA_DATASTORE "mbed" 00027 00028 /************* Milkcocoa Setup (you don't need to change this!) ******************/ 00029 00030 #define MILKCOCOA_SERVERPORT 1883 00031 00032 /************ Global State (you don't need to change this!) ******************/ 00033 00034 const char MQTT_SERVER[] = MILKCOCOA_APP_ID ".mlkcca.com"; 00035 const char MQTT_CLIENTID[] = __TIME__ MILKCOCOA_APP_ID; 00036 00037 extern void onpush(MQTT::MessageData& md); 00038 00039 int main() { 00040 // void setup() { 00041 pc.baud(9600); 00042 00043 MQTTMDM *ipstack = new MQTTMDM(SIMPIN, APN,USERNAME,PASSWORD); 00044 MClient *client = new MClient(ipstack); 00045 Milkcocoa *milkcocoa = new Milkcocoa(client, MQTT_SERVER, MILKCOCOA_SERVERPORT, MILKCOCOA_APP_ID, MQTT_CLIENTID); 00046 00047 pc.printf("Milkcocoa mbed ver demo\n\r\n\r\n\r"); 00048 00049 milkcocoa->connect(); 00050 pc.printf("\n\rEther connected\n\r"); 00051 00052 pc.printf("%d\n\r",milkcocoa->on(MILKCOCOA_DATASTORE, "push", onpush)); 00053 00054 // } 00055 while(1) { 00056 // void loop() { 00057 milkcocoa->loop(); 00058 00059 DataElement elem = DataElement(); 00060 elem.setValue("v", 1); 00061 00062 milkcocoa->push(MILKCOCOA_DATASTORE, elem); 00063 wait(7.0); 00064 } 00065 } 00066 00067 void onpush(MQTT::MessageData& md) 00068 { 00069 MQTT::Message &message = md.message; 00070 DataElement de = DataElement((char*)message.payload); 00071 pc.printf("onpush\n\r"); 00072 pc.printf("%d\n\r",de.getInt("v")); 00073 }
Generated on Tue Jul 19 2022 21:35:24 by 1.7.2