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.
Dependencies: MQTT W5200Interface mbed DHT11
Fork of IBMIoTClientEthernetExample_W5500 by
Diff: main.cpp
- Revision:
- 4:0c9bdee36e2a
- Parent:
- 3:64a7d39e423b
--- a/main.cpp Thu Dec 25 11:25:49 2014 +0000
+++ b/main.cpp Fri Dec 26 07:54:23 2014 +0000
@@ -32,6 +32,16 @@
#define QUICKSTARTMODE 1
+// It shows real temperature and humidity with DHT11.
+#define DHT11_TEMPERATURE 0
+
+#if DHT11_TEMPERATURE
+#include "DHT11.h"
+
+DHT11 dht(D4);
+
+#endif
+
#if (QUICKSTARTMODE)
// Configuration values needed to connect to IBM IoT Cloud
#define ORG "quickstart" // For a registered connection, replace with your org
@@ -156,9 +166,27 @@
char* pubTopic = "iot-2/evt/status/fmt/json";
char buf[250];
+ double temp, humi;
+
+#if DHT11_TEMPERATURE
+ int s = dht.readData();
+ if (s != DHT11::OK) {
+ printf("DHT11 Error: %d\r\n", s);
+ temp = humi = 0;
+ }
+ else {
+ printf("T:%d, H:%d\r\n", dht.readTemperature(), dht.readHumidity());
+ temp = dht.readTemperature();
+ humi = dht.readHumidity();
+ }
+#else
+ // simulate environment data
+ temp = (120+(rand()%100)) / 10.0;
+ humi = (200+(rand()%150)) / 10.0;
+#endif
sprintf(buf,
- "{\"d\":{\"myName\":\"IoT mbed\",\"test_value\":%d}}",
- 12+(rand()%10));
+ "{\"d\":{\"myName\":\"IoT mbed\",\"temperature\":%.1f,\"humidity\":%.1f}}",
+ temp, humi);
message.qos = MQTT::QOS0;
message.retained = false;
@@ -238,8 +266,8 @@
// or maybe just add the proper members to do this disconnect and call attemptConnect(...)
}
- if (++count == 100)
- { // Publish a message every second
+ if (++count == 200)
+ { // Publish a message every two second
if (publish(&client, &ipstack) != 0)
{
WARN("publish error\r\n");
