ワークショップ用のサンプルプログラムです。

Dependencies:   DHT Milkcocoa mbed

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 #include "DHT.h"
00008 
00009 
00010 // The default setting is for the Simple IoT Board(mbed LPC1114FN28)
00011 // Please change to fit the platform
00012 SoftSerialSendOnry pc(dp10); // tx
00013 DigitalOut myled(dp18);
00014 DHT sensor(dp13, DHT11);
00015 
00016 /************************* WiFi Access Point *********************************/
00017 
00018 //#define WLAN_SSID       "...SSID..."
00019 //#define WLAN_PASS       "...PASS..."
00020 #define WLAN_SSID       "wx01-dda3ad"
00021 #define WLAN_PASS       "0db20294cb0d3"
00022 
00023 /************************* Your Milkcocoa Setup *********************************/
00024 
00025 //#define MILKCOCOA_APP_ID      "...YOUR_MILKCOCOA_APP_ID..."
00026 #define MILKCOCOA_APP_ID      "teaidsirehz"
00027 #define MILKCOCOA_DATASTORE   "esp8266"
00028 
00029 /************* Milkcocoa Setup (you don't need to change this!) ******************/
00030 
00031 #define MILKCOCOA_SERVERPORT  1883
00032 
00033 /************ Global State (you don't need to change this!) ******************/
00034 
00035 // Create an ESP8266 WiFiClient class to connect to the MQTT server.
00036 // The default setting is for the Simple IoT Board(mbed LPC1114FN28)
00037 // Please change to fit the platform
00038 MQTTESP8266 ipstack(dp16,dp15,dp26,WLAN_SSID,WLAN_PASS); // TX,RX,Reset,SSID,Password,Baud
00039 MClient client(&ipstack);
00040 
00041 const char MQTT_SERVER[]  = MILKCOCOA_APP_ID ".mlkcca.com";
00042 const char MQTT_CLIENTID[] = __TIME__ MILKCOCOA_APP_ID;
00043 
00044 Milkcocoa milkcocoa = Milkcocoa(&client, MQTT_SERVER, MILKCOCOA_SERVERPORT, MILKCOCOA_APP_ID, MQTT_CLIENTID);
00045 
00046 extern void onpush(MQTT::MessageData& md);
00047 
00048 int main() {
00049 // void setup() {
00050     int error = 0;
00051     double h = 0.0f, c = 0.0f;
00052     
00053     pc.baud(9600);
00054     pc.printf("Milkcocoa mbed ver demo\n\r\n\r\n\r");
00055     pc.printf("Connecting to %s\n\r",WLAN_SSID);
00056     milkcocoa.connect();
00057     pc.printf("\n\rWiFi connected\n\r");
00058     
00059     pc.printf("%d\n\r",milkcocoa.on(MILKCOCOA_DATASTORE, "push", onpush));
00060     
00061 // }
00062     while(1) {
00063 // void loop() {
00064         milkcocoa.loop();
00065         
00066         error = sensor.readData();
00067         if (0 == error) {
00068             c   = sensor.ReadTemperature(CELCIUS);
00069             h   = sensor.ReadHumidity();
00070             
00071             DataElement elem = DataElement();
00072             elem.setValue("temp", c);
00073             elem.setValue("hmt", h);
00074             milkcocoa.push(MILKCOCOA_DATASTORE, elem);
00075         }
00076         
00077         wait(7.0);
00078     }
00079 }
00080 
00081 void onpush(MQTT::MessageData& md)
00082 {
00083     MQTT::Message &message = md.message;
00084     DataElement de = DataElement((char*)message.payload);
00085     pc.printf("onpush\n\r");
00086     pc.printf("%d\n\r",de.getInt("v"));
00087 }