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
PubNubDemo.cpp@2:31ece44f8322, 2014-12-17 (annotated)
- Committer:
- marin8703
- Date:
- Wed Dec 17 19:28:09 2014 +0000
- Revision:
- 2:31ece44f8322
- Parent:
- 0:e2c6c039dfbe
- Child:
- 3:7bc8d164dc2f
Set up to run on the LPC4088 only with whatever peripherals and options there are on board.
Who changed what in which revision?
User | Revision | Line number | New 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" |
marin8703 | 2:31ece44f8322 | 5 | //#include "C12832_lcd.h" |
marin8703 | 2:31ece44f8322 | 6 | //#include "MMA7660.h" |
marin8703 | 2:31ece44f8322 | 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): */ |
marin8703 | 2:31ece44f8322 | 18 | const char pubkey[] = "pub-c-b7ad94eb-6111-466b-94c4-858cfce6757f"; |
marin8703 | 2:31ece44f8322 | 19 | const char subkey[] = "sub-c-31ba2fd6-80b6-11e4-b601-02ee2ddab7fe"; |
marin8703 | 2:31ece44f8322 | 20 | const char channel[] = "lpc4088"; |
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 | |
marin8703 | 2:31ece44f8322 | 39 | //Serial pc(USBTX, USBRX); // tx, rx |
pasky | 0:e2c6c039dfbe | 40 | |
pasky | 0:e2c6c039dfbe | 41 | |
pasky | 0:e2c6c039dfbe | 42 | EthernetInterface eth; |
pasky | 0:e2c6c039dfbe | 43 | PubNub pn(pubkey, subkey); |
pasky | 0:e2c6c039dfbe | 44 | |
marin8703 | 2:31ece44f8322 | 45 | DigitalOut myled(LED1); |
marin8703 | 2:31ece44f8322 | 46 | |
pasky | 0:e2c6c039dfbe | 47 | void status_msg(PubNub &pn) |
pasky | 0:e2c6c039dfbe | 48 | { |
pasky | 0:e2c6c039dfbe | 49 | |
pasky | 0:e2c6c039dfbe | 50 | |
pasky | 0:e2c6c039dfbe | 51 | /* Prepare JSON message. */ |
pasky | 0:e2c6c039dfbe | 52 | char jsonmsg[128]; |
pasky | 0:e2c6c039dfbe | 53 | snprintf(jsonmsg, sizeof(jsonmsg), |
pasky | 0:e2c6c039dfbe | 54 | "{\"status\":{\"mx\":%.2f,\"my\":%.2f,\"mz\":%.2f,\"temp\":%.2f}}", |
marin8703 | 2:31ece44f8322 | 55 | 11.0, 11.0, 11.0, 73.0); |
pasky | 0:e2c6c039dfbe | 56 | |
pasky | 0:e2c6c039dfbe | 57 | #if 0 |
pasky | 0:e2c6c039dfbe | 58 | /* In some rare situations, you might want to instead use picojson |
pasky | 0:e2c6c039dfbe | 59 | * to construct JSON messages, even though it takes a lot of memory: */ |
pasky | 0:e2c6c039dfbe | 60 | |
pasky | 0:e2c6c039dfbe | 61 | printf("%d: ", __LINE__); __heapstats((__heapprt)fprintf, stdout); |
pasky | 0:e2c6c039dfbe | 62 | picojson::value msg(picojson::object_type, false); |
pasky | 0:e2c6c039dfbe | 63 | picojson::object &msgo = msg.get<picojson::object>(); |
pasky | 0:e2c6c039dfbe | 64 | msgo["status"] = picojson::value(picojson::object_type, false); |
pasky | 0:e2c6c039dfbe | 65 | picojson::object &status = msgo["status"].get<picojson::object>(); |
pasky | 0:e2c6c039dfbe | 66 | status["mx"] = picojson::value(double(mx)); |
pasky | 0:e2c6c039dfbe | 67 | status["my"] = picojson::value(double(my)); |
pasky | 0:e2c6c039dfbe | 68 | status["temp"] = picojson::value(temp); |
pasky | 0:e2c6c039dfbe | 69 | strcpy(jsonmsg, msg.serialize().c_str()); |
pasky | 0:e2c6c039dfbe | 70 | printf("%d: ", __LINE__); __heapstats((__heapprt)fprintf, stdout); |
pasky | 0:e2c6c039dfbe | 71 | #endif |
pasky | 0:e2c6c039dfbe | 72 | |
pasky | 0:e2c6c039dfbe | 73 | /* Publish on PubNub. */ |
pasky | 0:e2c6c039dfbe | 74 | PubNubRes ret = pn.publish(channel, jsonmsg); |
marin8703 | 2:31ece44f8322 | 75 | // if (ret != PNR_OK) |
marin8703 | 2:31ece44f8322 | 76 | // lcd.printf("puberr: %d \n", ret); |
pasky | 0:e2c6c039dfbe | 77 | } |
pasky | 0:e2c6c039dfbe | 78 | |
pasky | 0:e2c6c039dfbe | 79 | void process_msg(PubNub &pn, const char *jsonmsg) |
pasky | 0:e2c6c039dfbe | 80 | { |
pasky | 0:e2c6c039dfbe | 81 | /* Use the picojson parser since we want to deal with complex messages. |
pasky | 0:e2c6c039dfbe | 82 | * If you are short on memory, you can find an example for parsing simple |
pasky | 0:e2c6c039dfbe | 83 | * JSON messages in the PubNub::subscribe() API docs. */ |
pasky | 0:e2c6c039dfbe | 84 | picojson::value msg; |
pasky | 0:e2c6c039dfbe | 85 | std::string err = picojson::parse(msg, jsonmsg, jsonmsg + strlen(jsonmsg)); |
pasky | 0:e2c6c039dfbe | 86 | if (!err.empty()) { |
marin8703 | 2:31ece44f8322 | 87 | // lcd.printf("JSON parse: %s \n", err.c_str()); |
pasky | 0:e2c6c039dfbe | 88 | return; |
pasky | 0:e2c6c039dfbe | 89 | } |
pasky | 0:e2c6c039dfbe | 90 | |
pasky | 0:e2c6c039dfbe | 91 | if (msg.get("send_status").get<bool>()) { |
pasky | 0:e2c6c039dfbe | 92 | status_msg(pn); |
pasky | 0:e2c6c039dfbe | 93 | } |
pasky | 0:e2c6c039dfbe | 94 | if (msg.get("lcd").is<std::string>()) { |
marin8703 | 2:31ece44f8322 | 95 | // lcd.printf("in: %s \n", msg.get("lcd").get<std::string>().c_str()); |
pasky | 0:e2c6c039dfbe | 96 | } |
pasky | 0:e2c6c039dfbe | 97 | if (msg.get("beep").is<bool>()) { |
marin8703 | 2:31ece44f8322 | 98 | // speaker = msg.get("beep").get<bool>() ? 0.5 : 0; |
pasky | 0:e2c6c039dfbe | 99 | } |
pasky | 0:e2c6c039dfbe | 100 | if (msg.get("led").is<picojson::object>()) { |
pasky | 0:e2c6c039dfbe | 101 | picojson::value led = msg.get("led"); |
marin8703 | 2:31ece44f8322 | 102 | // if (led.get("r").is<double>()) led_r = 1.0 - led.get("r").get<double>(); |
marin8703 | 2:31ece44f8322 | 103 | // if (led.get("g").is<double>()) led_g = 1.0 - led.get("g").get<double>(); |
marin8703 | 2:31ece44f8322 | 104 | // if (led.get("b").is<double>()) led_b = 1.0 - led.get("b").get<double>(); |
pasky | 0:e2c6c039dfbe | 105 | } |
pasky | 0:e2c6c039dfbe | 106 | } |
pasky | 0:e2c6c039dfbe | 107 | |
pasky | 0:e2c6c039dfbe | 108 | int main() |
pasky | 0:e2c6c039dfbe | 109 | { |
marin8703 | 2:31ece44f8322 | 110 | |
marin8703 | 2:31ece44f8322 | 111 | char *reply = NULL; |
pasky | 0:e2c6c039dfbe | 112 | /* For debugging, you may find it useful to print memory usage |
pasky | 0:e2c6c039dfbe | 113 | * stats. AvailableMemory may be flaky, but the following is nice. |
pasky | 0:e2c6c039dfbe | 114 | * It will get printed to the USB serial port interface. */ |
marin8703 | 2:31ece44f8322 | 115 | // printf("%d: ", __LINE__); __heapstats((__heapprt)fprintf, stdout); |
pasky | 0:e2c6c039dfbe | 116 | |
pasky | 0:e2c6c039dfbe | 117 | /* Generate a 800Hz tone using PWM hardware output */ |
marin8703 | 2:31ece44f8322 | 118 | // speaker.period(1.0/800.0); // 800hz period |
marin8703 | 2:31ece44f8322 | 119 | // led_r = led_g = led_b = 1.0; // lights out |
marin8703 | 2:31ece44f8322 | 120 | |
marin8703 | 2:31ece44f8322 | 121 | // lcd.cls(); |
marin8703 | 2:31ece44f8322 | 122 | // lcd.locate(0,0); |
pasky | 0:e2c6c039dfbe | 123 | |
marin8703 | 2:31ece44f8322 | 124 | // if (!MMA.testConnection()) |
marin8703 | 2:31ece44f8322 | 125 | // lcd.printf("MMA error \n"); |
marin8703 | 2:31ece44f8322 | 126 | /* |
marin8703 | 2:31ece44f8322 | 127 | while(1) { |
marin8703 | 2:31ece44f8322 | 128 | myled = 1; |
marin8703 | 2:31ece44f8322 | 129 | wait(0.2); |
marin8703 | 2:31ece44f8322 | 130 | myled = 0; |
marin8703 | 2:31ece44f8322 | 131 | wait(0.2); |
marin8703 | 2:31ece44f8322 | 132 | } |
marin8703 | 2:31ece44f8322 | 133 | */ |
pasky | 0:e2c6c039dfbe | 134 | |
pasky | 0:e2c6c039dfbe | 135 | eth.init(); // Use DHCP |
pasky | 0:e2c6c039dfbe | 136 | eth.connect(); |
pasky | 0:e2c6c039dfbe | 137 | |
marin8703 | 2:31ece44f8322 | 138 | // status_msg(pn); |
pasky | 0:e2c6c039dfbe | 139 | // lcd.printf("pub... "); |
pasky | 0:e2c6c039dfbe | 140 | |
pasky | 0:e2c6c039dfbe | 141 | while (1) { |
pasky | 0:e2c6c039dfbe | 142 | // lcd.printf("sub... "); |
marin8703 | 2:31ece44f8322 | 143 | // printf("%d: ", __LINE__); __heapstats((__heapprt)fprintf, stdout); |
marin8703 | 2:31ece44f8322 | 144 | |
marin8703 | 2:31ece44f8322 | 145 | status_msg(pn); |
pasky | 0:e2c6c039dfbe | 146 | |
marin8703 | 2:31ece44f8322 | 147 | |
pasky | 0:e2c6c039dfbe | 148 | PubNubRes ret = pn.subscribe(channel, &reply); |
pasky | 0:e2c6c039dfbe | 149 | if (ret != PNR_OK) { |
marin8703 | 2:31ece44f8322 | 150 | // lcd.printf("suberr: %d \n", ret); |
pasky | 0:e2c6c039dfbe | 151 | wait(1.0); |
pasky | 0:e2c6c039dfbe | 152 | continue; |
pasky | 0:e2c6c039dfbe | 153 | } |
pasky | 0:e2c6c039dfbe | 154 | |
pasky | 0:e2c6c039dfbe | 155 | if (reply) { |
pasky | 0:e2c6c039dfbe | 156 | // lcd.printf("recv(%s)\n", reply); |
pasky | 0:e2c6c039dfbe | 157 | process_msg(pn, reply); |
pasky | 0:e2c6c039dfbe | 158 | } |
pasky | 0:e2c6c039dfbe | 159 | |
pasky | 0:e2c6c039dfbe | 160 | wait(0.5); // avoid busy loop in bad situations |
pasky | 0:e2c6c039dfbe | 161 | } |
pasky | 0:e2c6c039dfbe | 162 | } |