the code of obtain senor data and connect to MQTT
Dependencies: C12832 EthernetInterface MMA7660 MQTT USBDevice mbed-rtos mbed
Fork of HelloMQTT by
Diff: main.cpp
- Revision:
- 20:63e59fe8d75d
- Parent:
- 19:7f7aba7a4a8b
--- a/main.cpp Thu Jul 30 13:49:18 2015 +0000 +++ b/main.cpp Mon May 09 20:46:07 2016 +0000 @@ -1,31 +1,4 @@ -/******************************************************************************* - * Copyright (c) 2014, 2015 IBM Corp. - * - * All rights reserved. This program and the accompanying materials - * are made available under the terms of the Eclipse Public License v1.0 - * and Eclipse Distribution License v1.0 which accompany this distribution. - * - * The Eclipse Public License is available at - * http://www.eclipse.org/legal/epl-v10.html - * and the Eclipse Distribution License is available at - * http://www.eclipse.org/org/documents/edl-v10.php. - * - * Contributors: - * Ian Craggs - initial API and implementation and/or initial documentation - * Ian Craggs - make sure QoS2 processing works, and add device headers - *******************************************************************************/ - - /** - This is a sample program to illustrate the use of the MQTT Client library - on the mbed platform. The Client class requires two classes which mediate - access to system interfaces for networking and timing. As long as these two - classes provide the required public programming interfaces, it does not matter - what facilities they use underneath. In this program, they use the mbed - system libraries. - - */ - - // change this to 0 to output messages to serial instead of LCD + #define USE_LCD 1 #if USE_LCD @@ -50,6 +23,11 @@ #include "MQTTEthernet.h" #include "MQTTClient.h" +#include "USBSerial.h" +#include "C12832.h" +#include "MMA7660.h" + +MMA7660 MMA(D14, D15); int arrivedcount = 0; @@ -63,17 +41,33 @@ } -int main(int argc, char* argv[]) +int main(int argc, char* argv[],float c,float ang) { + lcd.cls(); + lcd.locate(0,3); + printf("mbed application shield!"); + + + + while(1) { + wait(0.1f); + lcd.locate(0,14); + float c=MMA.y()/MMA.z(); + float ang=pow(c,(-1)); + printf("%.2f,%.2f,%.2f",MMA.x(),MMA.y(),MMA.z()); + printf("%.2f",ang); + + } + if (ang<=-1){ MQTTEthernet ipstack = MQTTEthernet(); float version = 0.5; - char* topic = "mbed-sample"; + char* topic = "door"; printf("HelloMQTT: version is %f\n", version); MQTT::Client<MQTTEthernet, Countdown> client = MQTT::Client<MQTTEthernet, Countdown>(ipstack); - char* hostname = "m2m.eclipse.org"; + char* hostname = "doughnut.kent.ac.uk"; int port = 1883; printf("Connecting to %s:%d\n", hostname, port); int rc = ipstack.connect(hostname, port); @@ -95,7 +89,7 @@ // QoS 0 char buf[100]; - sprintf(buf, "Hello World! QoS 0 message from app version %f\n", version); + sprintf(buf, "door opened %f\n", version); message.qos = MQTT::QOS0; message.retained = false; message.dup = false; @@ -105,21 +99,7 @@ while (arrivedcount < 1) client.yield(100); - // QoS 1 - sprintf(buf, "Hello World! QoS 1 message from app version %f\n", version); - message.qos = MQTT::QOS1; - message.payloadlen = strlen(buf)+1; - rc = client.publish(topic, message); - while (arrivedcount < 2) - client.yield(100); - - // QoS 2 - sprintf(buf, "Hello World! QoS 2 message from app version %f\n", version); - message.qos = MQTT::QOS2; - message.payloadlen = strlen(buf)+1; - rc = client.publish(topic, message); - while (arrivedcount < 3) - client.yield(100); + if ((rc = client.unsubscribe(topic)) != 0) printf("rc from unsubscribe was %d\n", rc); @@ -133,3 +113,4 @@ return 0; } +}