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