ワークショップ用のサンプル

Dependencies:   Milkcocoa mbed

Fork of MilkcocoaSampleESP8266 by Junichi Katsu

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

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 PwmOut 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   "LED"
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         milkcocoa.loop();
00055     }
00056 }
00057 
00058 void onpush(MQTT::MessageData& md)
00059 {
00060     MQTT::Message &message = md.message;
00061     DataElement de = DataElement((char*)message.payload);
00062     pc.printf("onpush\n\r");
00063     myled = (float)de.getInt("RED") / 100.0f;
00064 }