Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
main.cpp
00001 #include "mbed.h" 00002 #include "MQTTESP8266.h" 00003 #include "MQTTClient.h" 00004 #include "SoftSerialSendOnry.h" 00005 #include "Milkcocoa.h" 00006 #include "MClient.h" 00007 00008 // The default setting is for the Simple IoT Board(mbed LPC1114FN28) 00009 // Please change to fit the platform 00010 SoftSerialSendOnry pc(dp10); // tx 00011 DigitalOut myled(dp18); 00012 00013 /************************* WiFi Access Point *********************************/ 00014 00015 #define WLAN_SSID "...SSID..." 00016 #define WLAN_PASS "...PASS..." 00017 00018 /************************* Your Milkcocoa Setup *********************************/ 00019 00020 #define MILKCOCOA_APP_ID "...YOUR_MILKCOCOA_APP_ID..." 00021 #define MILKCOCOA_DATASTORE "esp8266" 00022 00023 /************* Milkcocoa Setup (you don't need to change this!) ******************/ 00024 00025 #define MILKCOCOA_SERVERPORT 1883 00026 00027 /************ Global State (you don't need to change this!) ******************/ 00028 00029 // Create an ESP8266 WiFiClient class to connect to the MQTT server. 00030 // The default setting is for the Simple IoT Board(mbed LPC1114FN28) 00031 // Please change to fit the platform 00032 MQTTESP8266 ipstack(dp16,dp15,dp26,WLAN_SSID,WLAN_PASS); // TX,RX,Reset,SSID,Password,Baud 00033 MClient client(&ipstack); 00034 00035 const char MQTT_SERVER[] = MILKCOCOA_APP_ID ".mlkcca.com"; 00036 const char MQTT_CLIENTID[] = __TIME__ MILKCOCOA_APP_ID; 00037 00038 Milkcocoa milkcocoa = Milkcocoa(&client, MQTT_SERVER, MILKCOCOA_SERVERPORT, MILKCOCOA_APP_ID, MQTT_CLIENTID); 00039 00040 extern void onpush(MQTT::MessageData& md); 00041 00042 int main() { 00043 // void setup() { 00044 pc.baud(9600); 00045 pc.printf("Milkcocoa mbed ver demo\n\r\n\r\n\r"); 00046 pc.printf("Connecting to %s\n\r",WLAN_SSID); 00047 milkcocoa.connect(); 00048 pc.printf("\n\rWiFi connected\n\r"); 00049 00050 pc.printf("%d\n\r",milkcocoa.on(MILKCOCOA_DATASTORE, "push", onpush)); 00051 00052 // } 00053 while(1) { 00054 // void loop() { 00055 milkcocoa.loop(); 00056 00057 DataElement elem = DataElement(); 00058 elem.setValue("v", 1); 00059 00060 milkcocoa.push(MILKCOCOA_DATASTORE, elem); 00061 wait(0.7); 00062 } 00063 } 00064 00065 void onpush(MQTT::MessageData& md) 00066 { 00067 MQTT::Message &message = md.message; 00068 DataElement de = DataElement((char*)message.payload); 00069 pc.printf("onpush\n\r"); 00070 pc.printf("%d\n\r",de.getInt("v")); 00071 }
Generated on Tue Jul 12 2022 20:37:09 by
1.7.2