Junichi Katsu
/
MilkcocoaSampleESP8266
データの保存、更新、取得ができるWebサービス「milkcocoa」に接続し、データのプッシュ、送信、取得ができるライブラリを使ったサンプルです。ESP8266版 https://mlkcca.com/
main.cpp@4:d40cc9301d1e, 2016-02-29 (annotated)
- Committer:
- jksoft
- Date:
- Mon Feb 29 09:19:56 2016 +0000
- Revision:
- 4:d40cc9301d1e
- Parent:
- 3:9913fc5bb423
????????????????; Milkcocoa????????
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 | 4:d40cc9301d1e | 11 | DigitalOut 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 | 0:82d5445a9461 | 21 | #define MILKCOCOA_DATASTORE "esp8266" |
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 | // void loop() { |
jksoft | 0:82d5445a9461 | 55 | milkcocoa.loop(); |
jksoft | 0:82d5445a9461 | 56 | |
jksoft | 0:82d5445a9461 | 57 | DataElement elem = DataElement(); |
jksoft | 0:82d5445a9461 | 58 | elem.setValue("v", 1); |
jksoft | 0:82d5445a9461 | 59 | |
jksoft | 0:82d5445a9461 | 60 | milkcocoa.push(MILKCOCOA_DATASTORE, elem); |
jksoft | 1:48214b6d7059 | 61 | wait(7.0); |
jksoft | 0:82d5445a9461 | 62 | } |
jksoft | 0:82d5445a9461 | 63 | } |
jksoft | 0:82d5445a9461 | 64 | |
jksoft | 0:82d5445a9461 | 65 | void onpush(MQTT::MessageData& md) |
jksoft | 0:82d5445a9461 | 66 | { |
jksoft | 0:82d5445a9461 | 67 | MQTT::Message &message = md.message; |
jksoft | 0:82d5445a9461 | 68 | DataElement de = DataElement((char*)message.payload); |
jksoft | 0:82d5445a9461 | 69 | pc.printf("onpush\n\r"); |
jksoft | 0:82d5445a9461 | 70 | pc.printf("%d\n\r",de.getInt("v")); |
jksoft | 0:82d5445a9461 | 71 | } |