Example showing the ublox Cellular GPS/GNSS module with the online PubNub service on an LPC4088 Experiment Base Board

Dependencies:   C027_Support C12832 EALib LM75B MMA7660 PubNub mbed-rtos mbed picojson

Committer:
pasky
Date:
Sun Mar 02 01:51:30 2014 +0000
Revision:
0:e2c6c039dfbe
Child:
2:5f22df5ec656
Initial revision - reference the current PubNub library, include a working demo that connects PubNub with features of the mbed application board

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