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