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

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 "C12832.h"
00005 #include "MMA7660.h"
00006 #include "LM75B.h"
00007 
00008 #include "picojson.h"
00009 #include "PubNub.h"
00010 
00011 //------------------------------------------------------------------------------------
00012 // You need to configure these cellular modem / SIM parameters.
00013 // These parameters are ignored for LISA-C200 variants and can be left NULL.
00014 //------------------------------------------------------------------------------------
00015 #include "MDM.h"
00016 //! Set your secret SIM pin here (e.g. "1234"). Check your SIM manual.
00017 #define SIMPIN      NULL
00018 /*! The APN of your network operator SIM, sometimes it is "internet" check your 
00019     contract with the network operator. You can also try to look-up your settings in 
00020     google: https://www.google.de/search?q=APN+list */
00021 #define APN         NULL
00022 //! Set the user name for your APN, or NULL if not needed
00023 #define USERNAME    NULL
00024 //! Set the password for your APN, or NULL if not needed
00025 #define PASSWORD    NULL 
00026 //------------------------------------------------------------------------------------
00027 
00028 /* Demo of PubNub + the mbed application board. */
00029 
00030 /* How to get things set up: */
00031 /* 1. Tune in at the PubNub Developer Console, with the following
00032  * keys (press Subscribe afterwards): */
00033 const char pubkey[] = "demo";
00034 const char subkey[] = "demo";
00035 const char channel[] = "hello_world2";
00036 /* 2. Attach your mbed board to your computer. A folder should pop up like
00037  * if you plug in a USB memory stick. */
00038 /* 3. Open this example in the mbed web IDE and hit the Compile button. */
00039 /* 4. A download popup with a .bin file will appear; save it in the USB
00040  * mbed folder. */
00041 /* 5. Press reset button on the mbed to start things up. */
00042 
00043 /* You will see the board publish a "status" message that shows its
00044  * current temperature and physical tilt.  The board's LCD should
00045  * print some progress messages regarding that. */
00046 /* You can make the board do things too, by sending messages like:
00047  * { "send_status": true }
00048  * { "lcd": "Hi there!" }
00049  * { "beep": true }
00050  * { "rgbled": {"r": 0.5, "g": 1, "b": 0} }
00051  * Try it out! Paste these in the Message window and press the send icon.
00052  */
00053 
00054 Serial pc(USBTX, USBRX); // tx, rx
00055 MMA7660 MMA(D14/*SDA*/,D15/*SCL*/);
00056 LM75B tmp(D14/*SDA*/,D15/*SCL*/);
00057 C12832 lcd(D11, D13, D12, D7, D10);
00058 
00059 PwmOut led_r(D5); // RGB LED with 3 PWM outputs for dimmer control
00060 PwmOut led_g(D9);
00061 PwmOut led_b(D8);
00062 PwmOut speaker(D6); // Speaker with PWM driver
00063 
00064 PubNub pn(pubkey, subkey);
00065 
00066 void status_msg(PubNub &pn)
00067 {
00068     /* Read sensors. */
00069     float m[3];
00070     MMA.readData(m);
00071     float temp = tmp.read();
00072 
00073     /* Print on LCD. */
00074     lcd.printf("pub: mx=%.2f, my=%.2f, mz=%.2f, t=%.2f  \n", m[0], m[1], m[2], temp);
00075 
00076     /* Prepare JSON message. */
00077     char jsonmsg[128];
00078     snprintf(jsonmsg, sizeof(jsonmsg),
00079             "{\"status\":{\"mx\":%.2f,\"my\":%.2f,\"mz\":%.2f,\"temp\":%.2f}}",
00080             m[0], m[1], m[2], temp);
00081 
00082 #if 0
00083     /* In some rare situations, you might want to instead use picojson
00084      * to construct JSON messages, even though it takes a lot of memory: */
00085 
00086     printf("%d: ", __LINE__); __heapstats((__heapprt)fprintf, stdout);
00087     picojson::value msg(picojson::object_type, false);
00088     picojson::object &msgo = msg.get<picojson::object>();
00089     msgo["status"] = picojson::value(picojson::object_type, false);
00090     picojson::object &status = msgo["status"].get<picojson::object>();
00091     status["mx"] = picojson::value(double(mx));
00092     status["my"] = picojson::value(double(my));
00093     status["temp"] = picojson::value(temp);
00094     strcpy(jsonmsg, msg.serialize().c_str());
00095     printf("%d: ", __LINE__); __heapstats((__heapprt)fprintf, stdout);
00096 #endif
00097 
00098     /* Publish on PubNub. */
00099     PubNubRes ret = pn.publish(channel, jsonmsg);
00100     if (ret != PNR_OK)
00101         lcd.printf("puberr: %d  \n", ret);
00102 }
00103 
00104 void process_msg(PubNub &pn, const char *jsonmsg)
00105 {
00106     /* Use the picojson parser since we want to deal with complex messages.
00107      * If you are short on memory, you can find an example for parsing simple
00108      * JSON messages in the PubNub::subscribe() API docs. */
00109     picojson::value msg;
00110     std::string err = picojson::parse(msg, jsonmsg, jsonmsg + strlen(jsonmsg));
00111     if (!err.empty()) {
00112         lcd.printf("JSON parse: %s  \n", err.c_str());
00113         return;
00114     }
00115 
00116     if (msg.get("send_status").get<bool>()) {
00117         status_msg(pn);
00118     }
00119     if (msg.get("lcd").is<std::string>()) {
00120         lcd.printf("in: %s  \n", msg.get("lcd").get<std::string>().c_str());
00121     }
00122     if (msg.get("beep").is<bool>()) {
00123         speaker = msg.get("beep").get<bool>() ? 0.5 : 0;
00124     }
00125     if (msg.get("led").is<picojson::object>()) {
00126         picojson::value led = msg.get("led");
00127         if (led.get("r").is<double>()) led_r = 1.0 - led.get("r").get<double>();
00128         if (led.get("g").is<double>()) led_g = 1.0 - led.get("g").get<double>();
00129         if (led.get("b").is<double>()) led_b = 1.0 - led.get("b").get<double>();
00130     }
00131 }
00132 
00133 int main()
00134 {
00135     /* For debugging, you may find it useful to print memory usage
00136      * stats. AvailableMemory may be flaky, but the following is nice.
00137      * It will get printed to the USB serial port interface. */
00138     printf("%d: ", __LINE__); __heapstats((__heapprt)fprintf, stdout);
00139 
00140     /* Generate a 800Hz tone using PWM hardware output */
00141     speaker.period(1.0/800.0); // 800hz period
00142     led_r = led_g = led_b = 1.0; // lights out
00143 
00144     lcd.cls();
00145     lcd.locate(0,0);
00146 
00147     if (!MMA.testConnection())
00148         lcd.printf("MMA error  \n");
00149 
00150     MDMSerial mdm;
00151     //mdm.setDebug(4); // enable this for debugging issues 
00152     if (!mdm.connect(SIMPIN, APN,USERNAME,PASSWORD))
00153         return -1;
00154     
00155     status_msg(pn);
00156     // lcd.printf("pub... ");
00157 
00158     while (1) {
00159         // lcd.printf("sub... ");
00160         printf("%d: ", __LINE__); __heapstats((__heapprt)fprintf, stdout);
00161 
00162         char *reply = NULL;
00163         PubNubRes ret = pn.subscribe(channel, &reply);
00164         if (ret != PNR_OK) {
00165             lcd.printf("suberr: %d  \n", ret);
00166             wait(1.0);
00167             continue;
00168         }
00169 
00170         if (reply) {
00171             // lcd.printf("recv(%s)\n", reply);
00172             process_msg(pn, reply);
00173         }
00174 
00175         wait(0.5); // avoid busy loop in bad situations
00176     }
00177 
00178     mdm.disconnect();
00179     mdm.powerOff();
00180 }