example code for Axeda backend using SocketModem library for Cellular or Wifi access

Dependencies:   AxedaWrapper SocketModem mbed

Committer:
mfiore
Date:
Sat Jan 04 08:01:33 2014 +0000
Revision:
2:2dd8eba7cb9c
Parent:
1:f0b38aa21aaf
update AxedaWrapper

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mfiore 0:5b04479914b6 1 #include "mbed.h"
mfiore 0:5b04479914b6 2 #include "include_me.h"
mfiore 0:5b04479914b6 3 #include "AxedaWrapper.h"
mfiore 0:5b04479914b6 4
mfiore 0:5b04479914b6 5 // set to 1 for cellular shield board
mfiore 0:5b04479914b6 6 // set to 0 for wifi shield board
mfiore 0:5b04479914b6 7 #define CELL_SHIELD 1
mfiore 0:5b04479914b6 8
mfiore 0:5b04479914b6 9 using namespace mts;
mfiore 0:5b04479914b6 10
mfiore 0:5b04479914b6 11 int main() {
mfiore 0:5b04479914b6 12 #if CELL_SHIELD
mfiore 0:5b04479914b6 13 for (int i = 30; i >= 0; i = i - 2) {
mfiore 0:5b04479914b6 14 wait(2);
mfiore 0:5b04479914b6 15 printf("Waiting %d seconds...\n\r", i);
mfiore 0:5b04479914b6 16 }
mfiore 0:5b04479914b6 17
mfiore 0:5b04479914b6 18 MTSSerialFlowControl* serial = new MTSSerialFlowControl(PTD3, PTD2, PTA12, PTC8);
mfiore 0:5b04479914b6 19 serial->baud(115200);
mfiore 0:5b04479914b6 20 Transport::setTransport(Transport::CELLULAR);
mfiore 0:5b04479914b6 21 Cellular* cell = Cellular::getInstance();
mfiore 1:f0b38aa21aaf 22 cell->init(serial, PTA4, PTC9);
mfiore 0:5b04479914b6 23
mfiore 0:5b04479914b6 24 int max_tries = 5;
mfiore 0:5b04479914b6 25 int i;
mfiore 0:5b04479914b6 26 std::string apn = "wap.cingular";
mfiore 0:5b04479914b6 27
mfiore 0:5b04479914b6 28 i = 0;
mfiore 0:5b04479914b6 29 while (i++ < max_tries) {
mfiore 0:5b04479914b6 30 if (cell->getRegistration() == Cellular::REGISTERED) {
mfiore 0:5b04479914b6 31 printf("registered with tower\n\r");
mfiore 0:5b04479914b6 32 break;
mfiore 0:5b04479914b6 33 } else if (i >= max_tries) {
mfiore 0:5b04479914b6 34 printf("failed to register with tower\n\r");
mfiore 0:5b04479914b6 35 } else {
mfiore 0:5b04479914b6 36 wait(3);
mfiore 0:5b04479914b6 37 }
mfiore 0:5b04479914b6 38 }
mfiore 0:5b04479914b6 39
mfiore 0:5b04479914b6 40 printf("signal strength: %d\n\r", cell->getSignalStrength());
mfiore 0:5b04479914b6 41
mfiore 0:5b04479914b6 42 i = 0;
mfiore 0:5b04479914b6 43 printf("setting APN to %s\n\r", apn.c_str());
mfiore 0:5b04479914b6 44 while (i++ < max_tries) {
mfiore 0:5b04479914b6 45 if (cell->setApn(apn) == SUCCESS) {
mfiore 0:5b04479914b6 46 printf("successfully set APN\n\r");
mfiore 0:5b04479914b6 47 break;
mfiore 0:5b04479914b6 48 } else if (i >= max_tries) {
mfiore 0:5b04479914b6 49 printf("failed to set APN\n\r");
mfiore 0:5b04479914b6 50 } else {
mfiore 0:5b04479914b6 51 wait(1);
mfiore 0:5b04479914b6 52 }
mfiore 0:5b04479914b6 53 }
mfiore 0:5b04479914b6 54
mfiore 0:5b04479914b6 55 i = 0;
mfiore 0:5b04479914b6 56 printf("bringing up PPP link\n\r");
mfiore 0:5b04479914b6 57 while (i++ < max_tries) {
mfiore 0:5b04479914b6 58 if (cell->connect()) {
mfiore 0:5b04479914b6 59 printf("PPP link is up\n\r");
mfiore 0:5b04479914b6 60 break;
mfiore 0:5b04479914b6 61 } else if (i >= max_tries) {
mfiore 0:5b04479914b6 62 printf("failed to bring PPP link up\n\r");
mfiore 0:5b04479914b6 63 } else {
mfiore 0:5b04479914b6 64 wait(1);
mfiore 0:5b04479914b6 65 }
mfiore 0:5b04479914b6 66 }
mfiore 0:5b04479914b6 67 #else
mfiore 0:5b04479914b6 68 for (int i = 6; i >= 0; i = i - 2) {
mfiore 0:5b04479914b6 69 wait(2);
mfiore 0:5b04479914b6 70 printf("Waiting %d seconds...\n\r", i);
mfiore 0:5b04479914b6 71 }
mfiore 0:5b04479914b6 72 MTSSerial* serial = new MTSSerial(PTD3, PTD2, 256, 256);
mfiore 0:5b04479914b6 73 serial->baud(9600);
mfiore 0:5b04479914b6 74 Transport::setTransport(Transport::WIFI);
mfiore 0:5b04479914b6 75 Wifi* wifi = Wifi::getInstance();
mfiore 0:5b04479914b6 76 printf("Init: %s\n\r", wifi->init(serial) ? "SUCCESS" : "FAILURE");
mfiore 0:5b04479914b6 77 printf("Set Network: %s\n\r", getCodeNames(wifi->setNetwork("", Wifi::WPA2, "")).c_str());
mfiore 0:5b04479914b6 78 printf("Set DHCP: %s\n\r", getCodeNames(wifi->setDeviceIP("DHCP")).c_str());
mfiore 0:5b04479914b6 79 printf("Signal Strnegth (dBm): %d\n\r", wifi->getSignalStrength());
mfiore 0:5b04479914b6 80 printf("Is Connected: %s\n\r", wifi->isConnected() ? "True" : "False");
mfiore 0:5b04479914b6 81 printf("Connect: %s\n\r", wifi->connect() ? "Success" : "Failure");
mfiore 0:5b04479914b6 82 printf("Is Connected: %s\n\r", wifi->isConnected() ? "True" : "False");
mfiore 0:5b04479914b6 83 #endif
mfiore 0:5b04479914b6 84
mfiore 0:5b04479914b6 85 std::string serial_num = "";
mfiore 0:5b04479914b6 86 AxedaWrapper* client = new AxedaWrapper(serial_num);
mfiore 0:5b04479914b6 87 //AxedaWrapper* client = new AxedaWrapper(serial_num, "mbed", "54.235.103.204");
mfiore 0:5b04479914b6 88 bool retval;
mfiore 0:5b04479914b6 89 while (true) {
mfiore 0:5b04479914b6 90 int val = rand();
mfiore 0:5b04479914b6 91 printf("sending %d\r\n", val);
mfiore 0:5b04479914b6 92 retval = client->send("test", val);
mfiore 0:5b04479914b6 93 printf("%s\n\r", retval ? "succeeded" : "failed");
mfiore 0:5b04479914b6 94 wait(5);
mfiore 0:5b04479914b6 95 }
mfiore 0:5b04479914b6 96
mfiore 0:5b04479914b6 97 return 0;
mfiore 0:5b04479914b6 98 }