Sample project to connect to AT&T M2X from the STM32 Nucleo + MTS WiFi shield
Dependencies: M2XStreamClient SocketModem jsonlite mbed
Fork of STM32_MTS_Wifi_Connect_M2X by
main.cpp@14:df2fe4b77d83, 2014-07-25 (annotated)
- Committer:
- joe_tijerina
- Date:
- Fri Jul 25 16:31:19 2014 +0000
- Revision:
- 14:df2fe4b77d83
- Parent:
- 13:d8957cc4c88e
- Child:
- 16:921fec88838d
Increased the Serial Rx buffer to avoid overruns and added location and data fetch functions.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
joe_tijerina | 14:df2fe4b77d83 | 1 | #include "mbed.h" |
joe_tijerina | 14:df2fe4b77d83 | 2 | #include "M2XStreamClient.h" |
joe_tijerina | 14:df2fe4b77d83 | 3 | #include "MTSSerial.h" |
joe_tijerina | 14:df2fe4b77d83 | 4 | #include "Wifi.h" |
joe_tijerina | 14:df2fe4b77d83 | 5 | #include "include_me.h" |
joe_tijerina | 14:df2fe4b77d83 | 6 | #include "math.h" |
joe_tijerina | 14:df2fe4b77d83 | 7 | |
joe_tijerina | 14:df2fe4b77d83 | 8 | using namespace mts; |
joe_tijerina | 14:df2fe4b77d83 | 9 | |
joe_tijerina | 14:df2fe4b77d83 | 10 | /* This example shows how to do a basic connectivity test to AT&T M2X Cloud |
joe_tijerina | 14:df2fe4b77d83 | 11 | * using the MTS Wifi shield board. You will need to change the network |
joe_tijerina | 14:df2fe4b77d83 | 12 | * SSID and security key. You may need to chage the security type. |
joe_tijerina | 14:df2fe4b77d83 | 13 | */ |
joe_tijerina | 14:df2fe4b77d83 | 14 | |
joe_tijerina | 14:df2fe4b77d83 | 15 | using namespace mts; |
joe_tijerina | 14:df2fe4b77d83 | 16 | |
joe_tijerina | 14:df2fe4b77d83 | 17 | const char key[] = "your_api_key"; // Replace with your M2X API key |
joe_tijerina | 14:df2fe4b77d83 | 18 | const char feed[] = "your_feed_id"; // Replace with your blueprint Feed ID |
joe_tijerina | 14:df2fe4b77d83 | 19 | const char stream[] = "your_stream_name"; // Replace with your stream name |
joe_tijerina | 14:df2fe4b77d83 | 20 | char name[] = "your_device_location_name"; // Name of current location of datasource |
joe_tijerina | 14:df2fe4b77d83 | 21 | |
joe_tijerina | 14:df2fe4b77d83 | 22 | double latitude = 30.3748076; |
joe_tijerina | 14:df2fe4b77d83 | 23 | double longitude = -97.7386896; // You can also read those values from a GPS |
joe_tijerina | 14:df2fe4b77d83 | 24 | double elevation = 400.00; |
joe_tijerina | 14:df2fe4b77d83 | 25 | |
joe_tijerina | 14:df2fe4b77d83 | 26 | DigitalOut myled(D7); |
joe_tijerina | 14:df2fe4b77d83 | 27 | AnalogIn tempSensor(A0); |
joe_tijerina | 14:df2fe4b77d83 | 28 | |
joe_tijerina | 14:df2fe4b77d83 | 29 | // Note: This callback function needs to be debugged further as it does not print |
joe_tijerina | 14:df2fe4b77d83 | 30 | // the actual fetched reports, instead both "at" and "value" pointers print the |
joe_tijerina | 14:df2fe4b77d83 | 31 | // timestamps of each report. |
joe_tijerina | 14:df2fe4b77d83 | 32 | void on_data_point_found(const char* at, const char* value, int index, void* context) { |
joe_tijerina | 14:df2fe4b77d83 | 33 | printf("Found a data point, index: %d\r\n", index); |
joe_tijerina | 14:df2fe4b77d83 | 34 | printf("At: %s Value: %s \r\n", at, value); |
joe_tijerina | 14:df2fe4b77d83 | 35 | } |
joe_tijerina | 14:df2fe4b77d83 | 36 | |
joe_tijerina | 14:df2fe4b77d83 | 37 | void on_location_found(const char* name, |
joe_tijerina | 14:df2fe4b77d83 | 38 | double latitude, |
joe_tijerina | 14:df2fe4b77d83 | 39 | double longitude, |
joe_tijerina | 14:df2fe4b77d83 | 40 | double elevation, |
joe_tijerina | 14:df2fe4b77d83 | 41 | const char* timestamp, |
joe_tijerina | 14:df2fe4b77d83 | 42 | int index, |
joe_tijerina | 14:df2fe4b77d83 | 43 | void* context) { |
joe_tijerina | 14:df2fe4b77d83 | 44 | printf("Found a location, index: %d\r\n", index); |
joe_tijerina | 14:df2fe4b77d83 | 45 | printf("Name: %s Latitude: %lf Longitude: %lf\r\n", name, latitude, longitude); |
joe_tijerina | 14:df2fe4b77d83 | 46 | printf("Elevation: %lf Timestamp: %s\r\n", elevation, timestamp); |
joe_tijerina | 14:df2fe4b77d83 | 47 | } |
joe_tijerina | 14:df2fe4b77d83 | 48 | |
joe_tijerina | 14:df2fe4b77d83 | 49 | |
joe_tijerina | 14:df2fe4b77d83 | 50 | int main() |
joe_tijerina | 14:df2fe4b77d83 | 51 | { |
joe_tijerina | 14:df2fe4b77d83 | 52 | char amb_temp[6]; |
joe_tijerina | 14:df2fe4b77d83 | 53 | int response; |
joe_tijerina | 14:df2fe4b77d83 | 54 | int a; |
joe_tijerina | 14:df2fe4b77d83 | 55 | int adc_scale = 4095; |
joe_tijerina | 14:df2fe4b77d83 | 56 | int B = 3975; |
joe_tijerina | 14:df2fe4b77d83 | 57 | float resistance; |
joe_tijerina | 14:df2fe4b77d83 | 58 | float temperature; |
joe_tijerina | 14:df2fe4b77d83 | 59 | float temperature_f; |
joe_tijerina | 14:df2fe4b77d83 | 60 | |
joe_tijerina | 14:df2fe4b77d83 | 61 | //Set the network parameters |
joe_tijerina | 14:df2fe4b77d83 | 62 | std::string ssid = "your_wifi_ssid"; |
joe_tijerina | 14:df2fe4b77d83 | 63 | std::string securityKey = "your_wifi_passphrase"; |
joe_tijerina | 14:df2fe4b77d83 | 64 | Wifi::SecurityType securityType = Wifi::WPA2; |
joe_tijerina | 14:df2fe4b77d83 | 65 | |
joe_tijerina | 14:df2fe4b77d83 | 66 | //Wait for wifi module to boot up |
joe_tijerina | 14:df2fe4b77d83 | 67 | for (int i = 10; i >= 0; i = i - 2) { |
joe_tijerina | 14:df2fe4b77d83 | 68 | wait(2); |
joe_tijerina | 14:df2fe4b77d83 | 69 | printf("Waiting %d seconds...\n\r", i); |
joe_tijerina | 14:df2fe4b77d83 | 70 | } |
joe_tijerina | 14:df2fe4b77d83 | 71 | |
joe_tijerina | 14:df2fe4b77d83 | 72 | //Setup serial interface to WiFi module |
joe_tijerina | 14:df2fe4b77d83 | 73 | MTSSerial* serial = new MTSSerial(D8, D2, 256, 4096); |
joe_tijerina | 14:df2fe4b77d83 | 74 | serial->baud(9600); |
joe_tijerina | 14:df2fe4b77d83 | 75 | |
joe_tijerina | 14:df2fe4b77d83 | 76 | Transport::setTransport(Transport::WIFI); |
joe_tijerina | 14:df2fe4b77d83 | 77 | |
joe_tijerina | 14:df2fe4b77d83 | 78 | //Setup Wifi class |
joe_tijerina | 14:df2fe4b77d83 | 79 | Wifi* wifi = Wifi::getInstance(); |
joe_tijerina | 14:df2fe4b77d83 | 80 | printf("Init: %s\n\r", wifi->init(serial) ? "SUCCESS" : "FAILURE"); |
joe_tijerina | 14:df2fe4b77d83 | 81 | |
joe_tijerina | 14:df2fe4b77d83 | 82 | //Setup and check connection |
joe_tijerina | 14:df2fe4b77d83 | 83 | printf("Set Network: %s\n\r", getCodeNames(wifi->setNetwork(ssid, securityType, securityKey)).c_str()); |
joe_tijerina | 14:df2fe4b77d83 | 84 | printf("Set DHCP: %s\n\r", getCodeNames(wifi->setDeviceIP("DHCP")).c_str()); |
joe_tijerina | 14:df2fe4b77d83 | 85 | while (! wifi->connect()) { |
joe_tijerina | 14:df2fe4b77d83 | 86 | printf("Connect: Failure\r\n"); |
joe_tijerina | 14:df2fe4b77d83 | 87 | wait(1); |
joe_tijerina | 14:df2fe4b77d83 | 88 | } |
joe_tijerina | 14:df2fe4b77d83 | 89 | printf("Connect: Success\r\n"); |
joe_tijerina | 14:df2fe4b77d83 | 90 | printf("Is Connected: %s\n\r", wifi->isConnected() ? "True" : "False"); |
joe_tijerina | 14:df2fe4b77d83 | 91 | |
joe_tijerina | 14:df2fe4b77d83 | 92 | printf("Ping Server: %s\n\r", wifi->ping("8.8.8.8") ? "Success" : "Failed"); |
joe_tijerina | 14:df2fe4b77d83 | 93 | wait(1); |
joe_tijerina | 14:df2fe4b77d83 | 94 | |
joe_tijerina | 14:df2fe4b77d83 | 95 | |
joe_tijerina | 14:df2fe4b77d83 | 96 | // Initialize the M2X client |
joe_tijerina | 14:df2fe4b77d83 | 97 | Client client; |
joe_tijerina | 14:df2fe4b77d83 | 98 | M2XStreamClient m2xClient(&client, key); |
joe_tijerina | 14:df2fe4b77d83 | 99 | |
joe_tijerina | 14:df2fe4b77d83 | 100 | // update location |
joe_tijerina | 14:df2fe4b77d83 | 101 | response = m2xClient.updateLocation(feed, name, latitude, longitude, elevation); |
joe_tijerina | 14:df2fe4b77d83 | 102 | printf("updateLocation response code: %d\r\n", response); |
joe_tijerina | 14:df2fe4b77d83 | 103 | if (response == -1) while (true) ; |
joe_tijerina | 14:df2fe4b77d83 | 104 | |
joe_tijerina | 14:df2fe4b77d83 | 105 | response = m2xClient.updateLocation(feed, name, latitude, longitude, elevation); |
joe_tijerina | 14:df2fe4b77d83 | 106 | printf("updateLocation response code: %d\r\n", response); |
joe_tijerina | 14:df2fe4b77d83 | 107 | if (response == -1) while (true) ; |
joe_tijerina | 14:df2fe4b77d83 | 108 | |
joe_tijerina | 14:df2fe4b77d83 | 109 | //while(0) |
joe_tijerina | 14:df2fe4b77d83 | 110 | for (int i = 0; i < 5; i++) |
joe_tijerina | 14:df2fe4b77d83 | 111 | { |
joe_tijerina | 14:df2fe4b77d83 | 112 | myled = 1; // LED is ON |
joe_tijerina | 14:df2fe4b77d83 | 113 | a = tempSensor.read_u16(); |
joe_tijerina | 14:df2fe4b77d83 | 114 | |
joe_tijerina | 14:df2fe4b77d83 | 115 | resistance = (float)(adc_scale-a)*10000/a; //get the resistance of the sensor; |
joe_tijerina | 14:df2fe4b77d83 | 116 | temperature = 1/(log(resistance/10000)/B+1/298.15)-273.15; //convert to temperature via datasheet |
joe_tijerina | 14:df2fe4b77d83 | 117 | temperature_f = (1.8 * temperature) + 32.0; |
joe_tijerina | 14:df2fe4b77d83 | 118 | sprintf(amb_temp, "%0.2f", temperature_f); |
joe_tijerina | 14:df2fe4b77d83 | 119 | |
joe_tijerina | 14:df2fe4b77d83 | 120 | printf("Temp Sensor Analog Reading is 0x%X = %d ", a, a); |
joe_tijerina | 14:df2fe4b77d83 | 121 | printf("Current Temperature: %f C %f F \n\r", temperature, temperature_f); |
joe_tijerina | 14:df2fe4b77d83 | 122 | |
joe_tijerina | 14:df2fe4b77d83 | 123 | response = m2xClient.post(feed, stream, amb_temp); |
joe_tijerina | 14:df2fe4b77d83 | 124 | printf("Post response code: %d\r\n", response); |
joe_tijerina | 14:df2fe4b77d83 | 125 | if (response == -1) while (true) ; |
joe_tijerina | 14:df2fe4b77d83 | 126 | |
joe_tijerina | 14:df2fe4b77d83 | 127 | myled = 0; // LED is OFF |
joe_tijerina | 14:df2fe4b77d83 | 128 | |
joe_tijerina | 14:df2fe4b77d83 | 129 | delay(5000); |
joe_tijerina | 14:df2fe4b77d83 | 130 | } |
joe_tijerina | 14:df2fe4b77d83 | 131 | |
joe_tijerina | 14:df2fe4b77d83 | 132 | |
joe_tijerina | 14:df2fe4b77d83 | 133 | // fetch location |
joe_tijerina | 14:df2fe4b77d83 | 134 | response = m2xClient.readLocation(feed, on_location_found, NULL); |
joe_tijerina | 14:df2fe4b77d83 | 135 | printf("readLocation response code: %d\r\n", response); |
joe_tijerina | 14:df2fe4b77d83 | 136 | if (response == -1) while (true) ; |
joe_tijerina | 14:df2fe4b77d83 | 137 | |
joe_tijerina | 14:df2fe4b77d83 | 138 | // fetch temperature |
joe_tijerina | 14:df2fe4b77d83 | 139 | response = m2xClient.fetchValues(feed, stream, on_data_point_found, NULL); |
joe_tijerina | 14:df2fe4b77d83 | 140 | printf("Fetch response code: %d\r\n", response); |
joe_tijerina | 14:df2fe4b77d83 | 141 | if (response == -1) while (true) ; |
joe_tijerina | 14:df2fe4b77d83 | 142 | |
joe_tijerina | 14:df2fe4b77d83 | 143 | } |
joe_tijerina | 14:df2fe4b77d83 | 144 |