Set up for the LPC4088 and only for whatever is available on-board. As is publishes a fixed message to PubNub.
Dependencies: EthernetInterface PubNub PubNubDemo mbed-rtos mbed picojson
Fork of PubNubDemo by
Diff: PubNubDemo.cpp
- Revision:
- 2:31ece44f8322
- Parent:
- 0:e2c6c039dfbe
- Child:
- 3:7bc8d164dc2f
--- a/PubNubDemo.cpp Thu Mar 13 03:38:52 2014 +0000 +++ b/PubNubDemo.cpp Wed Dec 17 19:28:09 2014 +0000 @@ -2,9 +2,9 @@ #include "mbed.h" #include "EthernetInterface.h" -#include "C12832_lcd.h" -#include "MMA7660.h" -#include "LM75B.h" +//#include "C12832_lcd.h" +//#include "MMA7660.h" +//#include "LM75B.h" #include "picojson.h" #include "PubNub.h" @@ -15,9 +15,9 @@ /* How to get things set up: */ /* 1. Tune in at the PubNub Developer Console, with the following * keys (press Subscribe afterwards): */ -const char pubkey[] = "demo"; -const char subkey[] = "demo"; -const char channel[] = "hello_world2"; +const char pubkey[] = "pub-c-b7ad94eb-6111-466b-94c4-858cfce6757f"; +const char subkey[] = "sub-c-31ba2fd6-80b6-11e4-b601-02ee2ddab7fe"; +const char channel[] = "lpc4088"; /* 2. Attach your mbed board to your computer. A folder should pop up like * if you plug in a USB memory stick. */ /* 3. Open this example in the mbed web IDE and hit the Compile button. */ @@ -36,34 +36,23 @@ * Try it out! Paste these in the Message window and press the send icon. */ -Serial pc(USBTX, USBRX); // tx, rx -C12832_LCD lcd; // Graphics LCD -MMA7660 MMA(p28, p27); // I2C Accelerometer -LM75B tmp(p28,p27); // I2C Temperature Sensor +//Serial pc(USBTX, USBRX); // tx, rx -PwmOut led_r(p23); // RGB LED with 3 PWM outputs for dimmer control -PwmOut led_g(p24); -PwmOut led_b(p25); -PwmOut speaker(p26); // Speaker with PWM driver EthernetInterface eth; PubNub pn(pubkey, subkey); +DigitalOut myled(LED1); + void status_msg(PubNub &pn) { - /* Read sensors. */ - float m[3]; - MMA.readData(m); - float temp = tmp.read(); - /* Print on LCD. */ - lcd.printf("pub: mx=%.2f, my=%.2f, mz=%.2f, t=%.2f \n", m[0], m[1], m[2], temp); /* Prepare JSON message. */ char jsonmsg[128]; snprintf(jsonmsg, sizeof(jsonmsg), "{\"status\":{\"mx\":%.2f,\"my\":%.2f,\"mz\":%.2f,\"temp\":%.2f}}", - m[0], m[1], m[2], temp); + 11.0, 11.0, 11.0, 73.0); #if 0 /* In some rare situations, you might want to instead use picojson @@ -83,8 +72,8 @@ /* Publish on PubNub. */ PubNubRes ret = pn.publish(channel, jsonmsg); - if (ret != PNR_OK) - lcd.printf("puberr: %d \n", ret); + // if (ret != PNR_OK) +// lcd.printf("puberr: %d \n", ret); } void process_msg(PubNub &pn, const char *jsonmsg) @@ -95,7 +84,7 @@ picojson::value msg; std::string err = picojson::parse(msg, jsonmsg, jsonmsg + strlen(jsonmsg)); if (!err.empty()) { - lcd.printf("JSON parse: %s \n", err.c_str()); + // lcd.printf("JSON parse: %s \n", err.c_str()); return; } @@ -103,50 +92,62 @@ status_msg(pn); } if (msg.get("lcd").is<std::string>()) { - lcd.printf("in: %s \n", msg.get("lcd").get<std::string>().c_str()); + // lcd.printf("in: %s \n", msg.get("lcd").get<std::string>().c_str()); } if (msg.get("beep").is<bool>()) { - speaker = msg.get("beep").get<bool>() ? 0.5 : 0; + // speaker = msg.get("beep").get<bool>() ? 0.5 : 0; } if (msg.get("led").is<picojson::object>()) { picojson::value led = msg.get("led"); - if (led.get("r").is<double>()) led_r = 1.0 - led.get("r").get<double>(); - if (led.get("g").is<double>()) led_g = 1.0 - led.get("g").get<double>(); - if (led.get("b").is<double>()) led_b = 1.0 - led.get("b").get<double>(); +// if (led.get("r").is<double>()) led_r = 1.0 - led.get("r").get<double>(); +// if (led.get("g").is<double>()) led_g = 1.0 - led.get("g").get<double>(); +// if (led.get("b").is<double>()) led_b = 1.0 - led.get("b").get<double>(); } } int main() { + + char *reply = NULL; /* For debugging, you may find it useful to print memory usage * stats. AvailableMemory may be flaky, but the following is nice. * It will get printed to the USB serial port interface. */ - printf("%d: ", __LINE__); __heapstats((__heapprt)fprintf, stdout); + // printf("%d: ", __LINE__); __heapstats((__heapprt)fprintf, stdout); /* Generate a 800Hz tone using PWM hardware output */ - speaker.period(1.0/800.0); // 800hz period - led_r = led_g = led_b = 1.0; // lights out + // speaker.period(1.0/800.0); // 800hz period + // led_r = led_g = led_b = 1.0; // lights out + +// lcd.cls(); + // lcd.locate(0,0); - lcd.cls(); - lcd.locate(0,0); - - if (!MMA.testConnection()) - lcd.printf("MMA error \n"); + // if (!MMA.testConnection()) +// lcd.printf("MMA error \n"); +/* +while(1) { + myled = 1; + wait(0.2); + myled = 0; + wait(0.2); + } +*/ eth.init(); // Use DHCP eth.connect(); - status_msg(pn); +// status_msg(pn); // lcd.printf("pub... "); while (1) { // lcd.printf("sub... "); - printf("%d: ", __LINE__); __heapstats((__heapprt)fprintf, stdout); + // printf("%d: ", __LINE__); __heapstats((__heapprt)fprintf, stdout); + + status_msg(pn); - char *reply = NULL; + PubNubRes ret = pn.subscribe(channel, &reply); if (ret != PNR_OK) { - lcd.printf("suberr: %d \n", ret); + // lcd.printf("suberr: %d \n", ret); wait(1.0); continue; }