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