mbed(エンベッド)の仕組みを学び、試作体験!用のサンプル

Dependencies:   Milkcocoa-os

Fork of MilkcocoaOsSample_Eth by Junichi Katsu

Committer:
jksoft
Date:
Mon Aug 07 02:41:17 2017 +0000
Revision:
8:d5c4aa73f69e
Parent:
7:49a5a7e621d6
motion sensor add

Who changed what in which revision?

UserRevisionLine numberNew 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 8:d5c4aa73f69e 8 InterruptIn motion(D2);
jksoft 8:d5c4aa73f69e 9
jksoft 8:d5c4aa73f69e 10 int motion_detected = 0;
jksoft 0:885b53a42035 11
jksoft 0:885b53a42035 12 /************************* Your Milkcocoa Setup *********************************/
jksoft 0:885b53a42035 13
jksoft 1:1ab9d9f92764 14 #define MILKCOCOA_APP_ID "...YOUR_MILKCOCOA_APP_ID..."
jksoft 7:49a5a7e621d6 15 #define MILKCOCOA_DATASTORE "sensor"
jksoft 0:885b53a42035 16
jksoft 0:885b53a42035 17 /************* Milkcocoa Setup (you don't need to change this!) ******************/
jksoft 0:885b53a42035 18
jksoft 0:885b53a42035 19 #define MILKCOCOA_SERVERPORT 1883
jksoft 0:885b53a42035 20
jksoft 0:885b53a42035 21 /************ Global State (you don't need to change this!) ******************/
jksoft 0:885b53a42035 22 const char MQTT_SERVER[] = MILKCOCOA_APP_ID ".mlkcca.com";
jksoft 0:885b53a42035 23 const char MQTT_CLIENTID[] = __TIME__ MILKCOCOA_APP_ID;
jksoft 0:885b53a42035 24
jksoft 0:885b53a42035 25 extern void onpush(MQTT::MessageData& md);
jksoft 0:885b53a42035 26
jksoft 8:d5c4aa73f69e 27 void irq_handler(void)
jksoft 8:d5c4aa73f69e 28 {
jksoft 8:d5c4aa73f69e 29 motion_detected = 1;
jksoft 8:d5c4aa73f69e 30 }
jksoft 7:49a5a7e621d6 31
jksoft 0:885b53a42035 32 int main() {
jksoft 7:49a5a7e621d6 33 float val;
jksoft 8:d5c4aa73f69e 34 int motion_cnt = 0;
jksoft 8:d5c4aa73f69e 35
jksoft 0:885b53a42035 36 pc.baud(9600);
jksoft 0:885b53a42035 37 pc.printf("Milkcocoa mbed os ver demo\n\r\n\r\n\r");
jksoft 0:885b53a42035 38
jksoft 0:885b53a42035 39 int ret = eth.connect();
jksoft 0:885b53a42035 40 if (ret != 0) {
jksoft 0:885b53a42035 41 printf("\r\nConnection error\r\n");
jksoft 0:885b53a42035 42 return -1;
jksoft 0:885b53a42035 43 }
jksoft 0:885b53a42035 44 pc.printf("\n\rEthernet connected\n\r");
jksoft 0:885b53a42035 45
jksoft 0:885b53a42035 46 Milkcocoa* milkcocoa = new Milkcocoa(&eth, MQTT_SERVER, MILKCOCOA_SERVERPORT, MILKCOCOA_APP_ID, MQTT_CLIENTID);
jksoft 0:885b53a42035 47
jksoft 0:885b53a42035 48 milkcocoa->connect();
jksoft 8:d5c4aa73f69e 49
jksoft 8:d5c4aa73f69e 50 motion.rise(&irq_handler);
jksoft 7:49a5a7e621d6 51
jksoft 0:885b53a42035 52 while(1) {
jksoft 0:885b53a42035 53 DataElement elem = DataElement();
jksoft 7:49a5a7e621d6 54 val = sensor.read();
jksoft 8:d5c4aa73f69e 55
jksoft 8:d5c4aa73f69e 56 if(motion_detected == 1) {
jksoft 8:d5c4aa73f69e 57 motion_cnt++;
jksoft 8:d5c4aa73f69e 58 motion_detected = 0;
jksoft 8:d5c4aa73f69e 59 }
jksoft 8:d5c4aa73f69e 60
jksoft 7:49a5a7e621d6 61 elem.setValue("light", val);
jksoft 8:d5c4aa73f69e 62 elem.setValue("motion", motion_cnt);
jksoft 8:d5c4aa73f69e 63
jksoft 8:d5c4aa73f69e 64
jksoft 8:d5c4aa73f69e 65 pc.printf("light = %f motion = %d\r\n",val,motion_cnt);
jksoft 0:885b53a42035 66
jksoft 0:885b53a42035 67 milkcocoa->push(MILKCOCOA_DATASTORE, elem);
jksoft 0:885b53a42035 68
jksoft 5:103da32c92c7 69 milkcocoa->loop();
jksoft 7:49a5a7e621d6 70
jksoft 7:49a5a7e621d6 71 Thread::wait(2000);
jksoft 0:885b53a42035 72 }
jksoft 0:885b53a42035 73 }
jksoft 7:49a5a7e621d6 74