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
Diff: PubNubDemo.cpp
- Revision:
- 10:cbefe5a22319
- Parent:
- 8:ad3a3cffc336
--- a/PubNubDemo.cpp Thu Jul 03 17:45:10 2014 +0000 +++ b/PubNubDemo.cpp Wed Oct 01 10:10:42 2014 +0000 @@ -1,8 +1,9 @@ #include <cstring> #include "mbed.h" -#include "C12832.h" -#include "MMA7660.h" +//#include "C12832.h" +//#include "MMA7660.h" +#include "MMA7455.h" #include "LM75B.h" #include "picojson.h" @@ -18,7 +19,7 @@ /*! The APN of your network operator SIM, sometimes it is "internet" check your contract with the network operator. You can also try to look-up your settings in google: https://www.google.de/search?q=APN+list */ -#define APN NULL +#define APN "online.telia.se" //! Set the user name for your APN, or NULL if not needed #define USERNAME NULL //! Set the password for your APN, or NULL if not needed @@ -32,7 +33,7 @@ * keys (press Subscribe afterwards): */ const char pubkey[] = "demo"; const char subkey[] = "demo"; -const char channel[] = "hello_world2"; +const char channel[] = "mbed"; /* 2. Attach your mbed board to your computer. A folder should pop up like * if you plug in a USB memory stick. */ /* 3. Open this example in the mbed web IDE and hit the Compile button. */ @@ -47,36 +48,37 @@ * { "send_status": true } * { "lcd": "Hi there!" } * { "beep": true } - * { "rgbled": {"r": 0.5, "g": 1, "b": 0} } + * { "led": {"r": 0.5, "g": 1, "b": 0} } * Try it out! Paste these in the Message window and press the send icon. */ Serial pc(USBTX, USBRX); // tx, rx -MMA7660 MMA(SDA, SCL); -LM75B tmp(SDA, SCL); -C12832 lcd(D11, D13, D12, D7, D10); +//MMA7660 MMA(P0_27, P0_28); +MMA7455 MMA(P0_27, P0_28); +LM75B tmp(P0_27, P0_28, LM75B::ADDRESS_1); +//C12832 lcd(D11, D13, D12, D7, D10); -PwmOut led_r(D5); // RGB LED with 3 PWM outputs for dimmer control -PwmOut led_g(D9); -PwmOut led_b(D8); -PwmOut speaker(D6); // Speaker with PWM driver +PwmOut led_r(p25); // RGB LED with 3 PWM outputs for dimmer control +PwmOut led_g(p28); +PwmOut led_b(p26); +//PwmOut speaker(D6); // Speaker with PWM driver PubNub pn(pubkey, subkey); void status_msg(PubNub &pn) { /* Read sensors. */ - float m[3]; - MMA.readData(m); - float temp = tmp.read(); + int m[3]; + MMA.read(m[0], m[1], m[2]); + float temp = (float)tmp; /* Print on LCD. */ - lcd.printf("pub: mx=%.2f, my=%.2f, mz=%.2f, t=%.2f \n", m[0], m[1], m[2], temp); + pc.printf("pub: mx=%3d, my=%3d, mz=%3d, t=%.2f \n", m[0], m[1], m[2], temp); /* Prepare JSON message. */ char jsonmsg[128]; snprintf(jsonmsg, sizeof(jsonmsg), - "{\"status\":{\"mx\":%.2f,\"my\":%.2f,\"mz\":%.2f,\"temp\":%.2f}}", + "{\"status\":{\"mx\":%3d,\"my\":%3d,\"mz\":%3d,\"temp\":%.2f}}", m[0], m[1], m[2], temp); #if 0 @@ -98,7 +100,7 @@ /* Publish on PubNub. */ PubNubRes ret = pn.publish(channel, jsonmsg); if (ret != PNR_OK) - lcd.printf("puberr: %d \n", ret); + pc.printf("puberr: %d \n", ret); } void process_msg(PubNub &pn, const char *jsonmsg) @@ -109,7 +111,7 @@ picojson::value msg; std::string err = picojson::parse(msg, jsonmsg, jsonmsg + strlen(jsonmsg)); if (!err.empty()) { - lcd.printf("JSON parse: %s \n", err.c_str()); + pc.printf("JSON parse: %s \n", err.c_str()); return; } @@ -117,16 +119,18 @@ status_msg(pn); } if (msg.get("lcd").is<std::string>()) { - lcd.printf("in: %s \n", msg.get("lcd").get<std::string>().c_str()); + pc.printf("in: %s \n", msg.get("lcd").get<std::string>().c_str()); } if (msg.get("beep").is<bool>()) { - speaker = msg.get("beep").get<bool>() ? 0.5 : 0; + //speaker = msg.get("beep").get<bool>() ? 0.5 : 0; } if (msg.get("led").is<picojson::object>()) { picojson::value led = msg.get("led"); + printf("Old RGB { %.3f, %.3f, %.3f }\n", led_r.read(), led_g.read(), led_b.read()); if (led.get("r").is<double>()) led_r = 1.0 - led.get("r").get<double>(); if (led.get("g").is<double>()) led_g = 1.0 - led.get("g").get<double>(); if (led.get("b").is<double>()) led_b = 1.0 - led.get("b").get<double>(); + printf("New RGB { %.3f, %.3f, %.3f }\n", led_r.read(), led_g.read(), led_b.read()); } } @@ -135,44 +139,74 @@ /* For debugging, you may find it useful to print memory usage * stats. AvailableMemory may be flaky, but the following is nice. * It will get printed to the USB serial port interface. */ - printf("%d: ", __LINE__); __heapstats((__heapprt)fprintf, stdout); + //printf("%d: ", __LINE__); __heapstats((__heapprt)fprintf, stdout); /* Generate a 800Hz tone using PWM hardware output */ - speaker.period(1.0/800.0); // 800hz period + //speaker.period(1.0/800.0); // 800hz period led_r = led_g = led_b = 1.0; // lights out - lcd.cls(); - lcd.locate(0,0); + //lcd.cls(); + //lcd.locate(0,0); + + if (!tmp.open()) + pc.printf("Failed to open LM75 temperature sensor\n"); + +// if (!MMA.testConnection()) +// pc.printf("MMA error \n"); - if (!MMA.testConnection()) - lcd.printf("MMA error \n"); + // Initialize the accelerometer + if (!MMA.setMode(MMA7455::ModeMeasurement)) { + printf("Unable to set mode for MMA7455!\n"); + } + + // Calibrate it. It does not matter if it is on a level surface + // as this test is only interested in relative values. + if (!MMA.calibrate()) { + printf("Failed to calibrate MMA7455!\n"); + } MDMSerial mdm; //mdm.setDebug(4); // enable this for debugging issues - if (!mdm.connect(SIMPIN, APN,USERNAME,PASSWORD)) - return -1; + int i; + for (i = 1; i <= 10; i++) { + if (mdm.connect(SIMPIN, APN,USERNAME,PASSWORD)) { + printf("Connected\n"); + mdm.setDebug(0); // disable debug again + break; + } else { + printf("Attempt %2d to connect to the modem FAILED.\n", i); + wait(1); + mdm.setDebug(min(i, 4)); // to add more and more debug output + } + } + if (i > 10) { + printf("Failed to connect to the modem\n"); + mbed_die(); + } status_msg(pn); // lcd.printf("pub... "); while (1) { // lcd.printf("sub... "); - printf("%d: ", __LINE__); __heapstats((__heapprt)fprintf, stdout); + //printf("%d: ", __LINE__); __heapstats((__heapprt)fprintf, stdout); + printf(".\n"); char *reply = NULL; PubNubRes ret = pn.subscribe(channel, &reply); if (ret != PNR_OK) { - lcd.printf("suberr: %d \n", ret); + printf("suberr: %d \n", ret); wait(1.0); continue; } if (reply) { - // lcd.printf("recv(%s)\n", reply); + printf("recv(%s)\n", reply); process_msg(pn, reply); } wait(0.5); // avoid busy loop in bad situations + status_msg(pn); } mdm.disconnect();