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