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:
Thu Sep 25 19:10:38 2014 +0000
Revision:
17:d9fb4ea14d2b
Parent:
16:3458e36115a9
Changed the test_sensor input and pwm out signals and updated the M2X post logic.

Who changed what in which revision?

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