Marin Assaliyski / Mbed 2 deprecated PubNubDemo_LPC4088

Dependencies:   EthernetInterface PubNub PubNubDemo mbed-rtos mbed picojson

Fork of PubNubDemo by PubNub

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers PubNubDemo.cpp Source File

PubNubDemo.cpp

00001 #include <cstring>
00002 
00003 #include "mbed.h"
00004 #include "EthernetInterface.h"
00005 
00006 #include "picojson.h"
00007 #include "PubNub.h"
00008 
00009 
00010 /* Demo of PubNub + the mbed application board. */
00011 
00012 /* How to get things set up: */
00013 /* 1. Tune in at the PubNub Developer Console, with the following
00014  * keys (press Subscribe afterwards): */
00015 const char pubkey[] = "demo";
00016 const char subkey[] = "demo";
00017 const char channel[] = "lpc4088";
00018 /* 2. Attach your mbed board to your computer. A folder should pop up like
00019  * if you plug in a USB memory stick. */
00020 /* 3. Open this example in the mbed web IDE and hit the Compile button. */
00021 /* 4. A download popup with a .bin file will appear; save it in the USB
00022  * mbed folder. */
00023 /* 5. Press reset button on the mbed to start things up. */
00024 
00025 /* You will see the board publish a "status" message that shows its
00026  * current temperature and physical tilt.  The board's LCD should
00027  * print some progress messages regarding that. */
00028 /* You can make the board do things too, by sending messages like:
00029  * { "send_status": true }
00030  * { "lcd": "Hi there!" }
00031  * { "beep": true }
00032  * { "rgbled": {"r": 0.5, "g": 1, "b": 0} }
00033  * Try it out! Paste these in the Message window and press the send icon.
00034  */
00035 
00036 //Serial pc(USBTX, USBRX); // tx, rx
00037 
00038 
00039 EthernetInterface eth;
00040 PubNub pn(pubkey, subkey);
00041 
00042 DigitalOut myled(LED1);
00043 
00044 void status_msg(PubNub &pn)
00045 {
00046 
00047 
00048     /* Prepare JSON message. */
00049     char jsonmsg[128];
00050     snprintf(jsonmsg, sizeof(jsonmsg),
00051             "{\"status\":{\"mx\":%.2f,\"my\":%.2f,\"mz\":%.2f,\"temp\":%.2f}}",
00052             11.0, 11.0, 11.0, 73.0);
00053 
00054 #if 0
00055     /* In some rare situations, you might want to instead use picojson
00056      * to construct JSON messages, even though it takes a lot of memory: */
00057 
00058     printf("%d: ", __LINE__); __heapstats((__heapprt)fprintf, stdout);
00059     picojson::value msg(picojson::object_type, false);
00060     picojson::object &msgo = msg.get<picojson::object>();
00061     msgo["status"] = picojson::value(picojson::object_type, false);
00062     picojson::object &status = msgo["status"].get<picojson::object>();
00063     status["mx"] = picojson::value(double(mx));
00064     status["my"] = picojson::value(double(my));
00065     status["temp"] = picojson::value(temp);
00066     strcpy(jsonmsg, msg.serialize().c_str());
00067     printf("%d: ", __LINE__); __heapstats((__heapprt)fprintf, stdout);
00068 #endif
00069 
00070     /* Publish on PubNub. */
00071     PubNubRes ret = pn.publish(channel, jsonmsg);
00072  //   if (ret != PNR_OK)
00073 //        lcd.printf("puberr: %d  \n", ret);
00074 }
00075 
00076 void process_msg(PubNub &pn, const char *jsonmsg)
00077 {
00078     /* Use the picojson parser since we want to deal with complex messages.
00079      * If you are short on memory, you can find an example for parsing simple
00080      * JSON messages in the PubNub::subscribe() API docs. */
00081     picojson::value msg;
00082     std::string err = picojson::parse(msg, jsonmsg, jsonmsg + strlen(jsonmsg));
00083     if (!err.empty()) {
00084  //       lcd.printf("JSON parse: %s  \n", err.c_str());
00085         return;
00086     }
00087 
00088     if (msg.get("send_status").get<bool>()) {
00089         status_msg(pn);
00090     }
00091     if (msg.get("lcd").is<std::string>()) {
00092  //       lcd.printf("in: %s  \n", msg.get("lcd").get<std::string>().c_str());
00093     }
00094     if (msg.get("beep").is<bool>()) {
00095   //      speaker = msg.get("beep").get<bool>() ? 0.5 : 0;
00096     }
00097     if (msg.get("led").is<picojson::object>()) {
00098         picojson::value led = msg.get("led");
00099 //        if (led.get("r").is<double>()) led_r = 1.0 - led.get("r").get<double>();
00100 //        if (led.get("g").is<double>()) led_g = 1.0 - led.get("g").get<double>();
00101 //        if (led.get("b").is<double>()) led_b = 1.0 - led.get("b").get<double>();
00102     }
00103 }
00104 
00105 int main()
00106 {
00107     
00108     char *reply = NULL;
00109     /* For debugging, you may find it useful to print memory usage
00110      * stats. AvailableMemory may be flaky, but the following is nice.
00111      * It will get printed to the USB serial port interface. */
00112  //   printf("%d: ", __LINE__); __heapstats((__heapprt)fprintf, stdout);
00113 
00114     /* Generate a 800Hz tone using PWM hardware output */
00115  //   speaker.period(1.0/800.0); // 800hz period
00116  //   led_r = led_g = led_b = 1.0; // lights out
00117 
00118 //    lcd.cls();
00119  //   lcd.locate(0,0);
00120 
00121  //   if (!MMA.testConnection())
00122 //        lcd.printf("MMA error  \n");
00123 /*
00124 while(1) {
00125         myled = 1;
00126         wait(0.2);
00127         myled = 0;
00128         wait(0.2);
00129     }
00130 */
00131 
00132     eth.init(); // Use DHCP
00133     eth.connect();
00134     
00135 //    status_msg(pn);
00136     // lcd.printf("pub... ");
00137 
00138     while (1) {
00139         // lcd.printf("sub... ");
00140   //      printf("%d: ", __LINE__); __heapstats((__heapprt)fprintf, stdout);
00141   
00142         status_msg(pn);
00143 
00144         
00145         PubNubRes ret = pn.subscribe(channel, &reply);
00146         if (ret != PNR_OK) {
00147    //         lcd.printf("suberr: %d  \n", ret);
00148             wait(1.0);
00149             continue;
00150         }
00151 
00152         if (reply) {
00153             // lcd.printf("recv(%s)\n", reply);
00154             process_msg(pn, reply);
00155         }
00156 
00157         wait(0.5); // avoid busy loop in bad situations
00158     }
00159 }