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:
embeddedartists
Date:
Wed Oct 01 10:15:04 2014 +0000
Revision:
14:556fbe994972
Parent:
13:f38d25eef8b6
Updated

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