Junichi Katsu
/
MilkcocoaOsSample_Eth_sensor
mbed(エンベッド)の仕組みを学び、試作体験!用のサンプル
Fork of MilkcocoaOsSample_Eth by
main.cpp@7:49a5a7e621d6, 2017-08-06 (annotated)
- Committer:
- jksoft
- Date:
- Sun Aug 06 15:49:14 2017 +0000
- Revision:
- 7:49a5a7e621d6
- Parent:
- 5:103da32c92c7
- Child:
- 8:d5c4aa73f69e
First edition
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
jksoft | 0:885b53a42035 | 1 | #include "mbed.h" |
jksoft | 0:885b53a42035 | 2 | #include "Milkcocoa.h" |
jksoft | 0:885b53a42035 | 3 | #include "EthernetInterface.h" |
jksoft | 0:885b53a42035 | 4 | |
jksoft | 0:885b53a42035 | 5 | EthernetInterface eth; |
jksoft | 0:885b53a42035 | 6 | RawSerial pc(USBTX,USBRX); |
jksoft | 7:49a5a7e621d6 | 7 | AnalogIn sensor(A1); |
jksoft | 0:885b53a42035 | 8 | |
jksoft | 0:885b53a42035 | 9 | /************************* Your Milkcocoa Setup *********************************/ |
jksoft | 0:885b53a42035 | 10 | |
jksoft | 1:1ab9d9f92764 | 11 | #define MILKCOCOA_APP_ID "...YOUR_MILKCOCOA_APP_ID..." |
jksoft | 7:49a5a7e621d6 | 12 | #define MILKCOCOA_DATASTORE "sensor" |
jksoft | 0:885b53a42035 | 13 | |
jksoft | 0:885b53a42035 | 14 | /************* Milkcocoa Setup (you don't need to change this!) ******************/ |
jksoft | 0:885b53a42035 | 15 | |
jksoft | 0:885b53a42035 | 16 | #define MILKCOCOA_SERVERPORT 1883 |
jksoft | 0:885b53a42035 | 17 | |
jksoft | 0:885b53a42035 | 18 | /************ Global State (you don't need to change this!) ******************/ |
jksoft | 0:885b53a42035 | 19 | const char MQTT_SERVER[] = MILKCOCOA_APP_ID ".mlkcca.com"; |
jksoft | 0:885b53a42035 | 20 | const char MQTT_CLIENTID[] = __TIME__ MILKCOCOA_APP_ID; |
jksoft | 0:885b53a42035 | 21 | |
jksoft | 0:885b53a42035 | 22 | extern void onpush(MQTT::MessageData& md); |
jksoft | 0:885b53a42035 | 23 | |
jksoft | 7:49a5a7e621d6 | 24 | |
jksoft | 0:885b53a42035 | 25 | int main() { |
jksoft | 7:49a5a7e621d6 | 26 | float val; |
jksoft | 0:885b53a42035 | 27 | pc.baud(9600); |
jksoft | 0:885b53a42035 | 28 | pc.printf("Milkcocoa mbed os ver demo\n\r\n\r\n\r"); |
jksoft | 0:885b53a42035 | 29 | |
jksoft | 0:885b53a42035 | 30 | int ret = eth.connect(); |
jksoft | 0:885b53a42035 | 31 | if (ret != 0) { |
jksoft | 0:885b53a42035 | 32 | printf("\r\nConnection error\r\n"); |
jksoft | 0:885b53a42035 | 33 | return -1; |
jksoft | 0:885b53a42035 | 34 | } |
jksoft | 0:885b53a42035 | 35 | pc.printf("\n\rEthernet connected\n\r"); |
jksoft | 0:885b53a42035 | 36 | |
jksoft | 0:885b53a42035 | 37 | Milkcocoa* milkcocoa = new Milkcocoa(ð, MQTT_SERVER, MILKCOCOA_SERVERPORT, MILKCOCOA_APP_ID, MQTT_CLIENTID); |
jksoft | 0:885b53a42035 | 38 | |
jksoft | 0:885b53a42035 | 39 | milkcocoa->connect(); |
jksoft | 7:49a5a7e621d6 | 40 | |
jksoft | 0:885b53a42035 | 41 | while(1) { |
jksoft | 0:885b53a42035 | 42 | DataElement elem = DataElement(); |
jksoft | 7:49a5a7e621d6 | 43 | val = sensor.read(); |
jksoft | 7:49a5a7e621d6 | 44 | pc.printf("light = %f\r\n",val); |
jksoft | 7:49a5a7e621d6 | 45 | elem.setValue("light", val); |
jksoft | 0:885b53a42035 | 46 | |
jksoft | 0:885b53a42035 | 47 | milkcocoa->push(MILKCOCOA_DATASTORE, elem); |
jksoft | 0:885b53a42035 | 48 | |
jksoft | 5:103da32c92c7 | 49 | milkcocoa->loop(); |
jksoft | 7:49a5a7e621d6 | 50 | |
jksoft | 7:49a5a7e621d6 | 51 | Thread::wait(2000); |
jksoft | 0:885b53a42035 | 52 | } |
jksoft | 0:885b53a42035 | 53 | } |
jksoft | 7:49a5a7e621d6 | 54 |