Sample project to connect to AT&T M2X from the STM32 Nucleo + MTSAS Cellular SocketModem shield

Dependencies:   M2XStreamClient jsonlite mbed

Fork of MTSAS_Cellular_Connect_M2X_Example_F411 by Joe Tijerina

Committer:
joe_tijerina
Date:
Wed Sep 24 18:53:25 2014 +0000
Revision:
16:3458e36115a9
Parent:
15:a7512648f111
Child:
17:d9fb4ea14d2b
Updated M2XStreamClient library

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Vanger 0:47bc9ce390cc 1 #include "mbed.h"
Vanger 0:47bc9ce390cc 2 #include "mtsas.h"
joe_tijerina 15:a7512648f111 3 #include "M2XStreamClient.h"
Vanger 0:47bc9ce390cc 4
joe_tijerina 16:3458e36115a9 5 #define DEBUG
joe_tijerina 16:3458e36115a9 6
joe_tijerina 16:3458e36115a9 7 const char key[] = "06ce9a9febbfc50ffceb0d8214427767"; // Replace with your M2X API key
joe_tijerina 16:3458e36115a9 8 const char feed[] = "48c77c779f0eed1243e623190e588501"; // Replace with your blueprint Feed ID
joe_tijerina 15:a7512648f111 9 const char stream[] = "temperature"; // Replace with your stream name
joe_tijerina 15:a7512648f111 10 char name[] = "austin"; // Name of current location of datasource
joe_tijerina 15:a7512648f111 11
joe_tijerina 15:a7512648f111 12
joe_tijerina 15:a7512648f111 13 DigitalOut myled(D7);
joe_tijerina 15:a7512648f111 14 AnalogIn tempSensor(A0);
joe_tijerina 15:a7512648f111 15
joe_tijerina 15:a7512648f111 16 int main()
joe_tijerina 15:a7512648f111 17 {
joe_tijerina 15:a7512648f111 18 int ping_status = 0;
joe_tijerina 15:a7512648f111 19 char amb_temp[6];
joe_tijerina 15:a7512648f111 20 char water_sense[6];
joe_tijerina 15:a7512648f111 21 int response;
joe_tijerina 15:a7512648f111 22 int a;
joe_tijerina 15:a7512648f111 23 int adc_scale = 65535;
joe_tijerina 15:a7512648f111 24 int B = 3975;
joe_tijerina 15:a7512648f111 25 int water;
joe_tijerina 15:a7512648f111 26 float resistance;
joe_tijerina 15:a7512648f111 27 float temperature;
joe_tijerina 15:a7512648f111 28 float temperature_f;
joe_tijerina 15:a7512648f111 29
joe_tijerina 15:a7512648f111 30
joe_tijerina 15:a7512648f111 31
Vanger 0:47bc9ce390cc 32 //Modify to match your apn if you are using an HSPA radio with a SIM card
SeanNewton 14:025cccfe4215 33 const char APN[] = "epc.tmobile.com";
joe_tijerina 16:3458e36115a9 34 printf("Starting....\n");
Vanger 9:0ed53023033b 35 //Sets the log level to INFO, higher log levels produce more log output.
Vanger 9:0ed53023033b 36 //Possible levels: NONE, FATAL, ERROR, WARNING, INFO, DEBUG, TRACE
SeanNewton 14:025cccfe4215 37 MTSLog::setLogLevel(MTSLog::TRACE_LEVEL);
Vanger 0:47bc9ce390cc 38
Vanger 0:47bc9ce390cc 39 /** STMicro Nucelo F401RE
Vanger 0:47bc9ce390cc 40 * The supported jumper configurations of the MTSAS do not line up with
Vanger 0:47bc9ce390cc 41 * the pin mapping of the Nucleo F401RE. Therefore, the MTSAS serial TX
Vanger 0:47bc9ce390cc 42 * pin (JP8 Pin 2) must be manually jumped to Serial1 RX (Shield pin D2)
Vanger 0:47bc9ce390cc 43 * and the MTSAS serial RX pin (JP9 Pin 2) pin must be manually jumped to
Vanger 0:47bc9ce390cc 44 * Serial1 TX (Shield pin D8).
Vanger 0:47bc9ce390cc 45 * Uncomment the following line to use the STMicro Nuceleo F401RE
Vanger 0:47bc9ce390cc 46 */
Vanger 0:47bc9ce390cc 47 MTSSerialFlowControl* io = new MTSSerialFlowControl(D8, D2, D3, D6);
SeanNewton 14:025cccfe4215 48 printf("FlowControl...\n");
Vanger 0:47bc9ce390cc 49 /** Freescale KL46Z
Vanger 12:23c052e020a9 50 * To configure the serial pins for the Freescale KL46Z board, use MTSAS jumper
Vanger 12:23c052e020a9 51 * configuration B. Uncomment the following line to use the Freescale KL46Z board
Vanger 0:47bc9ce390cc 52 */
Vanger 0:47bc9ce390cc 53 //MTSSerialFlowControl* io = new MTSSerialFlowControl(D2, D9, D3, D6);
Vanger 0:47bc9ce390cc 54
Vanger 9:0ed53023033b 55 /** Freescale K64F
Vanger 12:23c052e020a9 56 * To configure the serial pins for the Freescale K64F board, use MTSAS jumper
Vanger 12:23c052e020a9 57 * configuration A. Uncomment the following line to use the Freescale K64F board
Vanger 0:47bc9ce390cc 58 */
Vanger 0:47bc9ce390cc 59 //MTSSerialFlowControl* io = new MTSSerialFlowControl(D1, D0, D3, D6);
Vanger 0:47bc9ce390cc 60
Vanger 9:0ed53023033b 61 //Sets the baud rate for communicating with the radio
Vanger 1:4c54ec0a3a20 62 io->baud(115200);
SeanNewton 14:025cccfe4215 63 printf("Baud...\n");
Vanger 8:95c226a1dca7 64 //Create radio object
Vanger 0:47bc9ce390cc 65 Cellular* radio = CellularFactory::create(io);
SeanNewton 14:025cccfe4215 66 printf("CellularFactory...\n");
Vanger 9:0ed53023033b 67 if (! radio) {
Vanger 9:0ed53023033b 68 logFatal("Failed to initialize radio");
Vanger 9:0ed53023033b 69 return 1;
Vanger 9:0ed53023033b 70 }
Vanger 13:699045e9cce9 71 radio->configureSignals(D4,D7,RESET);
Vanger 13:699045e9cce9 72 Transport::setTransport(radio);
SeanNewton 14:025cccfe4215 73 printf("setTransport...\n");
Vanger 8:95c226a1dca7 74 //Set radio APN
Vanger 1:4c54ec0a3a20 75 for (int i = 0; i < 10; i++) {
Vanger 1:4c54ec0a3a20 76 if (i >= 10) {
mfiore 5:46e66c649006 77 logError("Failed to set APN to %s", APN);
Vanger 1:4c54ec0a3a20 78 }
Vanger 1:4c54ec0a3a20 79 if (radio->setApn(APN) == MTS_SUCCESS) {
mfiore 5:46e66c649006 80 logInfo("Successfully set APN to %s", APN);
Vanger 1:4c54ec0a3a20 81 break;
Vanger 1:4c54ec0a3a20 82 } else {
Vanger 1:4c54ec0a3a20 83 wait(1);
Vanger 1:4c54ec0a3a20 84 }
Vanger 1:4c54ec0a3a20 85 }
Vanger 0:47bc9ce390cc 86
Vanger 0:47bc9ce390cc 87 //Establish PPP link
Vanger 1:4c54ec0a3a20 88 for (int i = 0; i < 10; i++) {
Vanger 1:4c54ec0a3a20 89 if (i >= 10) {
mfiore 5:46e66c649006 90 logError("Failed to establish PPP link");
Vanger 1:4c54ec0a3a20 91 }
Vanger 1:4c54ec0a3a20 92 if (radio->connect() == true) {
Vanger 3:f22ad66e049e 93 logInfo("Successfully established PPP link");
Vanger 1:4c54ec0a3a20 94 break;
Vanger 1:4c54ec0a3a20 95 } else {
Vanger 1:4c54ec0a3a20 96 wait(1);
Vanger 1:4c54ec0a3a20 97 }
Vanger 1:4c54ec0a3a20 98 }
Vanger 0:47bc9ce390cc 99
Vanger 1:4c54ec0a3a20 100 //Ping google.com
Vanger 1:4c54ec0a3a20 101 for (int i = 0; i < 10; i++) {
Vanger 1:4c54ec0a3a20 102 if (i >= 10) {
mfiore 5:46e66c649006 103 logError("Failed to ping www.google.com");
Vanger 1:4c54ec0a3a20 104 }
Vanger 3:f22ad66e049e 105 if (radio->ping("www.google.com") == true) {
Vanger 4:8b02a6b67f4d 106 logInfo("Successfully pinged www.google.com");
joe_tijerina 15:a7512648f111 107 ping_status = 1;
Vanger 1:4c54ec0a3a20 108 break;
Vanger 1:4c54ec0a3a20 109 } else {
Vanger 1:4c54ec0a3a20 110 wait(1);
Vanger 1:4c54ec0a3a20 111 }
Vanger 1:4c54ec0a3a20 112 }
joe_tijerina 15:a7512648f111 113
joe_tijerina 15:a7512648f111 114 // Initialize the M2X client
joe_tijerina 15:a7512648f111 115 Client client;
joe_tijerina 15:a7512648f111 116 M2XStreamClient m2xClient(&client, key);
joe_tijerina 15:a7512648f111 117
joe_tijerina 15:a7512648f111 118 if (ping_status)
joe_tijerina 15:a7512648f111 119 {
joe_tijerina 15:a7512648f111 120 while(1)
joe_tijerina 15:a7512648f111 121 {
joe_tijerina 15:a7512648f111 122 myled = 1; // LED is ON
joe_tijerina 15:a7512648f111 123 //a = tempSensor.read_u16();
joe_tijerina 15:a7512648f111 124
joe_tijerina 15:a7512648f111 125 //resistance = (float)(adc_scale-a)*10000/a; //get the resistance of the sensor;
joe_tijerina 15:a7512648f111 126 //temperature = 1/(log(resistance/10000)/B+1/298.15)-273.15; //convert to temperature via datasheet
joe_tijerina 15:a7512648f111 127 //temperature_f = (1.8 * temperature) + 32.0;
joe_tijerina 15:a7512648f111 128 //sprintf(amb_temp, "%0.2f", temperature_f);
joe_tijerina 15:a7512648f111 129
joe_tijerina 15:a7512648f111 130 //printf("Temp Sensor Analog Reading is 0x%X = %d ", a, a);
joe_tijerina 15:a7512648f111 131 //printf("Current Temperature: %f C %f F \n\r", temperature, temperature_f);
joe_tijerina 15:a7512648f111 132
joe_tijerina 15:a7512648f111 133 temperature++;
joe_tijerina 15:a7512648f111 134 printf("temperature: %f \n", temperature);
joe_tijerina 15:a7512648f111 135 response = m2xClient.put(feed, stream, temperature);
joe_tijerina 15:a7512648f111 136 printf("Post response code: %d\r\n", response);
joe_tijerina 16:3458e36115a9 137 //if (response == -1)
joe_tijerina 16:3458e36115a9 138 //{
joe_tijerina 16:3458e36115a9 139 // break;
joe_tijerina 16:3458e36115a9 140 //}
joe_tijerina 15:a7512648f111 141
joe_tijerina 15:a7512648f111 142 myled = 0; // LED is OFF
Vanger 0:47bc9ce390cc 143
joe_tijerina 15:a7512648f111 144 delay(5000);
joe_tijerina 15:a7512648f111 145
joe_tijerina 15:a7512648f111 146 }
joe_tijerina 15:a7512648f111 147 }
joe_tijerina 15:a7512648f111 148
Vanger 0:47bc9ce390cc 149 //Disconnect ppp link
Vanger 0:47bc9ce390cc 150 radio->disconnect();
Vanger 0:47bc9ce390cc 151
Vanger 3:f22ad66e049e 152 logInfo("End of example code");
Vanger 0:47bc9ce390cc 153 return 0;
Vanger 8:95c226a1dca7 154 }