Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
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 00008 // The default setting is for the Simple IoT Board(mbed LPC1114FN28) 00009 // Please change to fit the platform 00010 SoftSerialSendOnry pc(dp10); // tx 00011 DigitalOut myled(dp18); 00012 AnalogIn thermistor(dp13); /* Temperature sensor connected to Analog Grove connector */ 00013 00014 /************************* WiFi Access Point *********************************/ 00015 00016 #define WLAN_SSID "...SSID..." 00017 #define WLAN_PASS "...PASS..." 00018 00019 /************************* Your Milkcocoa Setup *********************************/ 00020 00021 #define MILKCOCOA_APP_ID "...YOUR_MILKCOCOA_APP_ID..." 00022 #define MILKCOCOA_DATASTORE "esp8266" 00023 00024 /************* Milkcocoa Setup (you don't need to change this!) ******************/ 00025 00026 #define MILKCOCOA_SERVERPORT 1883 00027 00028 /************ Global State (you don't need to change this!) ******************/ 00029 00030 // Create an ESP8266 WiFiClient class to connect to the MQTT server. 00031 // The default setting is for the Simple IoT Board(mbed LPC1114FN28) 00032 // Please change to fit the platform 00033 MQTTESP8266 ipstack(dp16,dp15,dp26,WLAN_SSID,WLAN_PASS); // TX,RX,Reset,SSID,Password,Baud 00034 MClient client(&ipstack); 00035 00036 const char MQTT_SERVER[] = MILKCOCOA_APP_ID ".mlkcca.com"; 00037 const char MQTT_CLIENTID[] = __TIME__ MILKCOCOA_APP_ID; 00038 00039 Milkcocoa milkcocoa = Milkcocoa(&client, MQTT_SERVER, MILKCOCOA_SERVERPORT, MILKCOCOA_APP_ID, MQTT_CLIENTID); 00040 00041 extern void onpush(MQTT::MessageData& md); 00042 00043 float getTemperature() 00044 { 00045 unsigned int a, beta = 3975; 00046 float temperature, resistance; 00047 00048 a = thermistor.read_u16(); /* Read analog value */ 00049 00050 /* Calculate the resistance of the thermistor from analog votage read. */ 00051 resistance= (float) 10000.0 * ((65536.0 / a) - 1.0); 00052 00053 /* Convert the resistance to temperature using Steinhart's Hart equation */ 00054 temperature=(1/((log(resistance/10000.0)/beta) + (1.0/298.15)))-273.15; 00055 00056 return(temperature); 00057 } 00058 00059 00060 int main() { 00061 // void setup() { 00062 pc.baud(9600); 00063 pc.printf("Milkcocoa mbed ver demo\n\r\n\r\n\r"); 00064 pc.printf("Connecting to %s\n\r",WLAN_SSID); 00065 milkcocoa.connect(); 00066 pc.printf("\n\rWiFi connected\n\r"); 00067 00068 pc.printf("%d\n\r",milkcocoa.on(MILKCOCOA_DATASTORE, "push", onpush)); 00069 00070 // } 00071 while(1) { 00072 // void loop() { 00073 milkcocoa.loop(); 00074 00075 DataElement elem = DataElement(); 00076 elem.setValue("Temperature", getTemperature()); 00077 00078 milkcocoa.push(MILKCOCOA_DATASTORE, elem); 00079 wait(7.0); 00080 } 00081 } 00082 00083 void onpush(MQTT::MessageData& md) 00084 { 00085 MQTT::Message &message = md.message; 00086 DataElement de = DataElement((char*)message.payload); 00087 pc.printf("onpush\n\r"); 00088 pc.printf("%d\n\r",de.getInt("v")); 00089 }
Generated on Wed Jul 13 2022 01:59:28 by
