![](/media/cache/profiles/5f14615696649541a025d3d0f8e0447f.jpg.50x50_q85.jpg)
ワークショップ用のサンプル
Fork of MilkcocoaSampleESP8266 by
main.cpp@2:d2e4e7ce7a65, 2016-02-27 (annotated)
- Committer:
- jksoft
- Date:
- Sat Feb 27 00:12:32 2016 +0000
- Revision:
- 2:d2e4e7ce7a65
- Parent:
- 1:48214b6d7059
??
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
jksoft | 0:82d5445a9461 | 1 | #include "mbed.h" |
jksoft | 0:82d5445a9461 | 2 | #include "MQTTESP8266.h" |
jksoft | 0:82d5445a9461 | 3 | #include "MQTTClient.h" |
jksoft | 0:82d5445a9461 | 4 | #include "SoftSerialSendOnry.h" |
jksoft | 0:82d5445a9461 | 5 | #include "Milkcocoa.h" |
jksoft | 0:82d5445a9461 | 6 | #include "MClient.h" |
jksoft | 0:82d5445a9461 | 7 | |
jksoft | 0:82d5445a9461 | 8 | // The default setting is for the Simple IoT Board(mbed LPC1114FN28) |
jksoft | 0:82d5445a9461 | 9 | // Please change to fit the platform |
jksoft | 0:82d5445a9461 | 10 | SoftSerialSendOnry pc(dp10); // tx |
jksoft | 2:d2e4e7ce7a65 | 11 | PwmOut myled(dp18); |
jksoft | 0:82d5445a9461 | 12 | |
jksoft | 0:82d5445a9461 | 13 | /************************* WiFi Access Point *********************************/ |
jksoft | 0:82d5445a9461 | 14 | |
jksoft | 0:82d5445a9461 | 15 | #define WLAN_SSID "...SSID..." |
jksoft | 0:82d5445a9461 | 16 | #define WLAN_PASS "...PASS..." |
jksoft | 0:82d5445a9461 | 17 | |
jksoft | 0:82d5445a9461 | 18 | /************************* Your Milkcocoa Setup *********************************/ |
jksoft | 0:82d5445a9461 | 19 | |
jksoft | 0:82d5445a9461 | 20 | #define MILKCOCOA_APP_ID "...YOUR_MILKCOCOA_APP_ID..." |
jksoft | 2:d2e4e7ce7a65 | 21 | #define MILKCOCOA_DATASTORE "LED" |
jksoft | 0:82d5445a9461 | 22 | |
jksoft | 0:82d5445a9461 | 23 | /************* Milkcocoa Setup (you don't need to change this!) ******************/ |
jksoft | 0:82d5445a9461 | 24 | |
jksoft | 0:82d5445a9461 | 25 | #define MILKCOCOA_SERVERPORT 1883 |
jksoft | 0:82d5445a9461 | 26 | |
jksoft | 0:82d5445a9461 | 27 | /************ Global State (you don't need to change this!) ******************/ |
jksoft | 0:82d5445a9461 | 28 | |
jksoft | 0:82d5445a9461 | 29 | // Create an ESP8266 WiFiClient class to connect to the MQTT server. |
jksoft | 0:82d5445a9461 | 30 | // The default setting is for the Simple IoT Board(mbed LPC1114FN28) |
jksoft | 0:82d5445a9461 | 31 | // Please change to fit the platform |
jksoft | 0:82d5445a9461 | 32 | MQTTESP8266 ipstack(dp16,dp15,dp26,WLAN_SSID,WLAN_PASS); // TX,RX,Reset,SSID,Password,Baud |
jksoft | 0:82d5445a9461 | 33 | MClient client(&ipstack); |
jksoft | 0:82d5445a9461 | 34 | |
jksoft | 0:82d5445a9461 | 35 | const char MQTT_SERVER[] = MILKCOCOA_APP_ID ".mlkcca.com"; |
jksoft | 0:82d5445a9461 | 36 | const char MQTT_CLIENTID[] = __TIME__ MILKCOCOA_APP_ID; |
jksoft | 0:82d5445a9461 | 37 | |
jksoft | 0:82d5445a9461 | 38 | Milkcocoa milkcocoa = Milkcocoa(&client, MQTT_SERVER, MILKCOCOA_SERVERPORT, MILKCOCOA_APP_ID, MQTT_CLIENTID); |
jksoft | 0:82d5445a9461 | 39 | |
jksoft | 0:82d5445a9461 | 40 | extern void onpush(MQTT::MessageData& md); |
jksoft | 0:82d5445a9461 | 41 | |
jksoft | 0:82d5445a9461 | 42 | int main() { |
jksoft | 0:82d5445a9461 | 43 | // void setup() { |
jksoft | 0:82d5445a9461 | 44 | pc.baud(9600); |
jksoft | 0:82d5445a9461 | 45 | pc.printf("Milkcocoa mbed ver demo\n\r\n\r\n\r"); |
jksoft | 0:82d5445a9461 | 46 | pc.printf("Connecting to %s\n\r",WLAN_SSID); |
jksoft | 0:82d5445a9461 | 47 | milkcocoa.connect(); |
jksoft | 0:82d5445a9461 | 48 | pc.printf("\n\rWiFi connected\n\r"); |
jksoft | 0:82d5445a9461 | 49 | |
jksoft | 0:82d5445a9461 | 50 | pc.printf("%d\n\r",milkcocoa.on(MILKCOCOA_DATASTORE, "push", onpush)); |
jksoft | 0:82d5445a9461 | 51 | |
jksoft | 0:82d5445a9461 | 52 | // } |
jksoft | 0:82d5445a9461 | 53 | while(1) { |
jksoft | 0:82d5445a9461 | 54 | milkcocoa.loop(); |
jksoft | 0:82d5445a9461 | 55 | } |
jksoft | 0:82d5445a9461 | 56 | } |
jksoft | 0:82d5445a9461 | 57 | |
jksoft | 0:82d5445a9461 | 58 | void onpush(MQTT::MessageData& md) |
jksoft | 0:82d5445a9461 | 59 | { |
jksoft | 0:82d5445a9461 | 60 | MQTT::Message &message = md.message; |
jksoft | 0:82d5445a9461 | 61 | DataElement de = DataElement((char*)message.payload); |
jksoft | 0:82d5445a9461 | 62 | pc.printf("onpush\n\r"); |
jksoft | 2:d2e4e7ce7a65 | 63 | myled = (float)de.getInt("RED") / 100.0f; |
jksoft | 0:82d5445a9461 | 64 | } |