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
00001 #include "mbed.h" 00002 #include "M2XStreamClient.h" 00003 #include "MTSSerial.h" 00004 #include "Wifi.h" 00005 #include "include_me.h" 00006 #include "math.h" 00007 00008 using namespace mts; 00009 00010 /* This example shows how to do a basic connectivity test to AT&T M2X Cloud 00011 * using the MTS Wifi shield board. You will need to change the network 00012 * SSID and security key. You may need to chage the security type. 00013 */ 00014 00015 using namespace mts; 00016 00017 const char key[] = "your_api_key"; // Replace with your M2X API key 00018 const char feed[] = "your_feed_id"; // Replace with your blueprint Feed ID 00019 const char stream[] = "your_stream_name"; // Replace with your stream name 00020 char name[] = "your_device_location_name"; // Name of current location of datasource 00021 00022 double latitude = 30.3748076; 00023 double longitude = -97.7386896; // You can also read those values from a GPS 00024 double elevation = 400.00; 00025 00026 DigitalOut myled(D7); 00027 AnalogIn tempSensor(A0); 00028 00029 // Note: This callback function needs to be debugged further as it does not print 00030 // the actual fetched reports, instead both "at" and "value" pointers print the 00031 // timestamps of each report. 00032 void on_data_point_found(const char* at, const char* value, int index, void* context) { 00033 printf("Found a data point, index: %d\r\n", index); 00034 printf("At: %s Value: %s \r\n", at, value); 00035 } 00036 00037 void on_location_found(const char* name, 00038 double latitude, 00039 double longitude, 00040 double elevation, 00041 const char* timestamp, 00042 int index, 00043 void* context) { 00044 printf("Found a location, index: %d\r\n", index); 00045 printf("Name: %s Latitude: %lf Longitude: %lf\r\n", name, latitude, longitude); 00046 printf("Elevation: %lf Timestamp: %s\r\n", elevation, timestamp); 00047 } 00048 00049 00050 int main() 00051 { 00052 char amb_temp[6]; 00053 int response; 00054 int a; 00055 int adc_scale = 4095; 00056 int B = 3975; 00057 float resistance; 00058 float temperature; 00059 float temperature_f; 00060 00061 //Set the network parameters 00062 std::string ssid = "your_wifi_ssid"; 00063 std::string securityKey = "your_wifi_passphrase"; 00064 Wifi::SecurityType securityType = Wifi::WPA2; 00065 00066 //Wait for wifi module to boot up 00067 for (int i = 10; i >= 0; i = i - 2) { 00068 wait(2); 00069 printf("Waiting %d seconds...\n\r", i); 00070 } 00071 00072 //Setup serial interface to WiFi module 00073 MTSSerial* serial = new MTSSerial(D8, D2, 256, 4096); 00074 serial->baud(9600); 00075 00076 Transport::setTransport(Transport::WIFI); 00077 00078 //Setup Wifi class 00079 Wifi* wifi = Wifi::getInstance(); 00080 printf("Init: %s\n\r", wifi->init(serial) ? "SUCCESS" : "FAILURE"); 00081 00082 //Setup and check connection 00083 printf("Set Network: %s\n\r", getCodeNames(wifi->setNetwork(ssid, securityType, securityKey)).c_str()); 00084 printf("Set DHCP: %s\n\r", getCodeNames(wifi->setDeviceIP("DHCP")).c_str()); 00085 while (! wifi->connect()) { 00086 printf("Connect: Failure\r\n"); 00087 wait(1); 00088 } 00089 printf("Connect: Success\r\n"); 00090 printf("Is Connected: %s\n\r", wifi->isConnected() ? "True" : "False"); 00091 00092 printf("Ping Server: %s\n\r", wifi->ping("8.8.8.8") ? "Success" : "Failed"); 00093 wait(1); 00094 00095 00096 // Initialize the M2X client 00097 Client client; 00098 M2XStreamClient m2xClient(&client, key); 00099 00100 // update location 00101 response = m2xClient.updateLocation(feed, name, latitude, longitude, elevation); 00102 printf("updateLocation response code: %d\r\n", response); 00103 if (response == -1) while (true) ; 00104 00105 //for (int i = 0; i < 5; i++) 00106 while(1) 00107 { 00108 myled = 1; // LED is ON 00109 a = tempSensor.read_u16(); 00110 00111 resistance = (float)(adc_scale-a)*10000/a; //get the resistance of the sensor; 00112 temperature = 1/(log(resistance/10000)/B+1/298.15)-273.15; //convert to temperature via datasheet 00113 temperature_f = (1.8 * temperature) + 32.0; 00114 sprintf(amb_temp, "%0.2f", temperature_f); 00115 00116 printf("Temp Sensor Analog Reading is 0x%X = %d ", a, a); 00117 printf("Current Temperature: %f C %f F \n\r", temperature, temperature_f); 00118 00119 response = m2xClient.post(feed, stream, amb_temp); 00120 printf("Post response code: %d\r\n", response); 00121 if (response == -1) while (true) ; 00122 00123 myled = 0; // LED is OFF 00124 00125 delay(5000); 00126 } 00127 00128 00129 // fetch location 00130 response = m2xClient.readLocation(feed, on_location_found, NULL); 00131 printf("readLocation response code: %d\r\n", response); 00132 if (response == -1) while (true) ; 00133 00134 // fetch temperature 00135 response = m2xClient.fetchValues(feed, stream, on_data_point_found, NULL); 00136 printf("Fetch response code: %d\r\n", response); 00137 if (response == -1) while (true) ; 00138 00139 } 00140
Generated on Mon Jul 18 2022 17:38:44 by 1.7.2