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 PubNub

Committer:
marin8703
Date:
Wed Dec 17 19:35:39 2014 +0000
Revision:
3:7bc8d164dc2f
Parent:
2:31ece44f8322
Set up for the LPC4088 and uses only what is available on-board, no LCD, no accel., no temperature.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
pasky 0:e2c6c039dfbe 1 #include <cstring>
pasky 0:e2c6c039dfbe 2
pasky 0:e2c6c039dfbe 3 #include "mbed.h"
pasky 0:e2c6c039dfbe 4 #include "EthernetInterface.h"
pasky 0:e2c6c039dfbe 5
pasky 0:e2c6c039dfbe 6 #include "picojson.h"
pasky 0:e2c6c039dfbe 7 #include "PubNub.h"
pasky 0:e2c6c039dfbe 8
pasky 0:e2c6c039dfbe 9
pasky 0:e2c6c039dfbe 10 /* Demo of PubNub + the mbed application board. */
pasky 0:e2c6c039dfbe 11
pasky 0:e2c6c039dfbe 12 /* How to get things set up: */
pasky 0:e2c6c039dfbe 13 /* 1. Tune in at the PubNub Developer Console, with the following
pasky 0:e2c6c039dfbe 14 * keys (press Subscribe afterwards): */
marin8703 3:7bc8d164dc2f 15 const char pubkey[] = "demo";
marin8703 3:7bc8d164dc2f 16 const char subkey[] = "demo";
marin8703 2:31ece44f8322 17 const char channel[] = "lpc4088";
pasky 0:e2c6c039dfbe 18 /* 2. Attach your mbed board to your computer. A folder should pop up like
pasky 0:e2c6c039dfbe 19 * if you plug in a USB memory stick. */
pasky 0:e2c6c039dfbe 20 /* 3. Open this example in the mbed web IDE and hit the Compile button. */
pasky 0:e2c6c039dfbe 21 /* 4. A download popup with a .bin file will appear; save it in the USB
pasky 0:e2c6c039dfbe 22 * mbed folder. */
pasky 0:e2c6c039dfbe 23 /* 5. Press reset button on the mbed to start things up. */
pasky 0:e2c6c039dfbe 24
pasky 0:e2c6c039dfbe 25 /* You will see the board publish a "status" message that shows its
pasky 0:e2c6c039dfbe 26 * current temperature and physical tilt. The board's LCD should
pasky 0:e2c6c039dfbe 27 * print some progress messages regarding that. */
pasky 0:e2c6c039dfbe 28 /* You can make the board do things too, by sending messages like:
pasky 0:e2c6c039dfbe 29 * { "send_status": true }
pasky 0:e2c6c039dfbe 30 * { "lcd": "Hi there!" }
pasky 0:e2c6c039dfbe 31 * { "beep": true }
pasky 0:e2c6c039dfbe 32 * { "rgbled": {"r": 0.5, "g": 1, "b": 0} }
pasky 0:e2c6c039dfbe 33 * Try it out! Paste these in the Message window and press the send icon.
pasky 0:e2c6c039dfbe 34 */
pasky 0:e2c6c039dfbe 35
marin8703 2:31ece44f8322 36 //Serial pc(USBTX, USBRX); // tx, rx
pasky 0:e2c6c039dfbe 37
pasky 0:e2c6c039dfbe 38
pasky 0:e2c6c039dfbe 39 EthernetInterface eth;
pasky 0:e2c6c039dfbe 40 PubNub pn(pubkey, subkey);
pasky 0:e2c6c039dfbe 41
marin8703 2:31ece44f8322 42 DigitalOut myled(LED1);
marin8703 2:31ece44f8322 43
pasky 0:e2c6c039dfbe 44 void status_msg(PubNub &pn)
pasky 0:e2c6c039dfbe 45 {
pasky 0:e2c6c039dfbe 46
pasky 0:e2c6c039dfbe 47
pasky 0:e2c6c039dfbe 48 /* Prepare JSON message. */
pasky 0:e2c6c039dfbe 49 char jsonmsg[128];
pasky 0:e2c6c039dfbe 50 snprintf(jsonmsg, sizeof(jsonmsg),
pasky 0:e2c6c039dfbe 51 "{\"status\":{\"mx\":%.2f,\"my\":%.2f,\"mz\":%.2f,\"temp\":%.2f}}",
marin8703 2:31ece44f8322 52 11.0, 11.0, 11.0, 73.0);
pasky 0:e2c6c039dfbe 53
pasky 0:e2c6c039dfbe 54 #if 0
pasky 0:e2c6c039dfbe 55 /* In some rare situations, you might want to instead use picojson
pasky 0:e2c6c039dfbe 56 * to construct JSON messages, even though it takes a lot of memory: */
pasky 0:e2c6c039dfbe 57
pasky 0:e2c6c039dfbe 58 printf("%d: ", __LINE__); __heapstats((__heapprt)fprintf, stdout);
pasky 0:e2c6c039dfbe 59 picojson::value msg(picojson::object_type, false);
pasky 0:e2c6c039dfbe 60 picojson::object &msgo = msg.get<picojson::object>();
pasky 0:e2c6c039dfbe 61 msgo["status"] = picojson::value(picojson::object_type, false);
pasky 0:e2c6c039dfbe 62 picojson::object &status = msgo["status"].get<picojson::object>();
pasky 0:e2c6c039dfbe 63 status["mx"] = picojson::value(double(mx));
pasky 0:e2c6c039dfbe 64 status["my"] = picojson::value(double(my));
pasky 0:e2c6c039dfbe 65 status["temp"] = picojson::value(temp);
pasky 0:e2c6c039dfbe 66 strcpy(jsonmsg, msg.serialize().c_str());
pasky 0:e2c6c039dfbe 67 printf("%d: ", __LINE__); __heapstats((__heapprt)fprintf, stdout);
pasky 0:e2c6c039dfbe 68 #endif
pasky 0:e2c6c039dfbe 69
pasky 0:e2c6c039dfbe 70 /* Publish on PubNub. */
pasky 0:e2c6c039dfbe 71 PubNubRes ret = pn.publish(channel, jsonmsg);
marin8703 2:31ece44f8322 72 // if (ret != PNR_OK)
marin8703 2:31ece44f8322 73 // lcd.printf("puberr: %d \n", ret);
pasky 0:e2c6c039dfbe 74 }
pasky 0:e2c6c039dfbe 75
pasky 0:e2c6c039dfbe 76 void process_msg(PubNub &pn, const char *jsonmsg)
pasky 0:e2c6c039dfbe 77 {
pasky 0:e2c6c039dfbe 78 /* Use the picojson parser since we want to deal with complex messages.
pasky 0:e2c6c039dfbe 79 * If you are short on memory, you can find an example for parsing simple
pasky 0:e2c6c039dfbe 80 * JSON messages in the PubNub::subscribe() API docs. */
pasky 0:e2c6c039dfbe 81 picojson::value msg;
pasky 0:e2c6c039dfbe 82 std::string err = picojson::parse(msg, jsonmsg, jsonmsg + strlen(jsonmsg));
pasky 0:e2c6c039dfbe 83 if (!err.empty()) {
marin8703 2:31ece44f8322 84 // lcd.printf("JSON parse: %s \n", err.c_str());
pasky 0:e2c6c039dfbe 85 return;
pasky 0:e2c6c039dfbe 86 }
pasky 0:e2c6c039dfbe 87
pasky 0:e2c6c039dfbe 88 if (msg.get("send_status").get<bool>()) {
pasky 0:e2c6c039dfbe 89 status_msg(pn);
pasky 0:e2c6c039dfbe 90 }
pasky 0:e2c6c039dfbe 91 if (msg.get("lcd").is<std::string>()) {
marin8703 2:31ece44f8322 92 // lcd.printf("in: %s \n", msg.get("lcd").get<std::string>().c_str());
pasky 0:e2c6c039dfbe 93 }
pasky 0:e2c6c039dfbe 94 if (msg.get("beep").is<bool>()) {
marin8703 2:31ece44f8322 95 // speaker = msg.get("beep").get<bool>() ? 0.5 : 0;
pasky 0:e2c6c039dfbe 96 }
pasky 0:e2c6c039dfbe 97 if (msg.get("led").is<picojson::object>()) {
pasky 0:e2c6c039dfbe 98 picojson::value led = msg.get("led");
marin8703 2:31ece44f8322 99 // if (led.get("r").is<double>()) led_r = 1.0 - led.get("r").get<double>();
marin8703 2:31ece44f8322 100 // if (led.get("g").is<double>()) led_g = 1.0 - led.get("g").get<double>();
marin8703 2:31ece44f8322 101 // if (led.get("b").is<double>()) led_b = 1.0 - led.get("b").get<double>();
pasky 0:e2c6c039dfbe 102 }
pasky 0:e2c6c039dfbe 103 }
pasky 0:e2c6c039dfbe 104
pasky 0:e2c6c039dfbe 105 int main()
pasky 0:e2c6c039dfbe 106 {
marin8703 2:31ece44f8322 107
marin8703 2:31ece44f8322 108 char *reply = NULL;
pasky 0:e2c6c039dfbe 109 /* For debugging, you may find it useful to print memory usage
pasky 0:e2c6c039dfbe 110 * stats. AvailableMemory may be flaky, but the following is nice.
pasky 0:e2c6c039dfbe 111 * It will get printed to the USB serial port interface. */
marin8703 2:31ece44f8322 112 // printf("%d: ", __LINE__); __heapstats((__heapprt)fprintf, stdout);
pasky 0:e2c6c039dfbe 113
pasky 0:e2c6c039dfbe 114 /* Generate a 800Hz tone using PWM hardware output */
marin8703 2:31ece44f8322 115 // speaker.period(1.0/800.0); // 800hz period
marin8703 2:31ece44f8322 116 // led_r = led_g = led_b = 1.0; // lights out
marin8703 2:31ece44f8322 117
marin8703 2:31ece44f8322 118 // lcd.cls();
marin8703 2:31ece44f8322 119 // lcd.locate(0,0);
pasky 0:e2c6c039dfbe 120
marin8703 2:31ece44f8322 121 // if (!MMA.testConnection())
marin8703 2:31ece44f8322 122 // lcd.printf("MMA error \n");
marin8703 2:31ece44f8322 123 /*
marin8703 2:31ece44f8322 124 while(1) {
marin8703 2:31ece44f8322 125 myled = 1;
marin8703 2:31ece44f8322 126 wait(0.2);
marin8703 2:31ece44f8322 127 myled = 0;
marin8703 2:31ece44f8322 128 wait(0.2);
marin8703 2:31ece44f8322 129 }
marin8703 2:31ece44f8322 130 */
pasky 0:e2c6c039dfbe 131
pasky 0:e2c6c039dfbe 132 eth.init(); // Use DHCP
pasky 0:e2c6c039dfbe 133 eth.connect();
pasky 0:e2c6c039dfbe 134
marin8703 2:31ece44f8322 135 // status_msg(pn);
pasky 0:e2c6c039dfbe 136 // lcd.printf("pub... ");
pasky 0:e2c6c039dfbe 137
pasky 0:e2c6c039dfbe 138 while (1) {
pasky 0:e2c6c039dfbe 139 // lcd.printf("sub... ");
marin8703 2:31ece44f8322 140 // printf("%d: ", __LINE__); __heapstats((__heapprt)fprintf, stdout);
marin8703 2:31ece44f8322 141
marin8703 2:31ece44f8322 142 status_msg(pn);
pasky 0:e2c6c039dfbe 143
marin8703 2:31ece44f8322 144
pasky 0:e2c6c039dfbe 145 PubNubRes ret = pn.subscribe(channel, &reply);
pasky 0:e2c6c039dfbe 146 if (ret != PNR_OK) {
marin8703 2:31ece44f8322 147 // lcd.printf("suberr: %d \n", ret);
pasky 0:e2c6c039dfbe 148 wait(1.0);
pasky 0:e2c6c039dfbe 149 continue;
pasky 0:e2c6c039dfbe 150 }
pasky 0:e2c6c039dfbe 151
pasky 0:e2c6c039dfbe 152 if (reply) {
pasky 0:e2c6c039dfbe 153 // lcd.printf("recv(%s)\n", reply);
pasky 0:e2c6c039dfbe 154 process_msg(pn, reply);
pasky 0:e2c6c039dfbe 155 }
pasky 0:e2c6c039dfbe 156
pasky 0:e2c6c039dfbe 157 wait(0.5); // avoid busy loop in bad situations
pasky 0:e2c6c039dfbe 158 }
pasky 0:e2c6c039dfbe 159 }