Demo app to read data from temperature sensor connected to STM-F411RE and send values to AT&T M2X Data Service via MTS shield.
Dependencies: M2XStreamClient SocketModem jsonlite mbed
Fork of STM32_MTS_Wifi_Connect_M2X by
Diff: main.cpp
- Revision:
- 13:d8957cc4c88e
- Parent:
- 12:676ba8e7bea2
- Child:
- 14:df2fe4b77d83
--- a/main.cpp Fri Jul 18 18:53:18 2014 +0000 +++ b/main.cpp Mon Jul 21 20:43:56 2014 +0000 @@ -6,7 +6,6 @@ #include "math.h" - using namespace mts; /* This example shows how to do a basic connectivity test using @@ -16,15 +15,14 @@ using namespace mts; -const char key[] = "6f1f72905ab5bede6ff804d58c2e834d"; // Replace with your M2X user account master key -const char feed[] = "119e6e4c262087ea3c5129576a18ee52"; // Replace with your blueprint feed ID -const char stream[] = "temperature"; // Replace with your stream name -const int thresholdvalue = 10; +const char key[] = ""; // Replace with your M2X user account master key +const char feed[] = ""; // Replace with your blueprint feed ID +const char stream[] = ""; // Replace with your stream name +char name[] = ""; // Name of current location of datasource -char name[] = "<location name>"; // Name of current location of datasource -double latitude = 33.007872; -double longitude = -96.751614; // You can also read those values from a GPS -double elevation = 697.00; +double latitude = 30.3748076; +double longitude = -97.7386896; // You can also read those values from a GPS +double elevation = 400.00; void on_data_point_found(const char* at, const char* value, int index, void* context) { printf("Found a data point, index: %d\r\n", index); @@ -43,28 +41,27 @@ printf("Elevation: %lf Timestamp: %s\r\n", elevation, timestamp); } + + int main() { char amb_temp[6]; - double temp; + int response; int a; - int sensorValue; - int adc_scale = 4095; - int B = 3975; + int adc_scale = 4095; // Scale Factor for the STM32F411's 12-bit ADC + int B = 3975; // B value from temp sensor datasheet float resistance; float temperature; float temperature_f; - float test; - float Rsensor; //Resistance of sensor in K DigitalOut myled(D7); AnalogIn analogRead(A0); - AnalogIn analogLightSensorRead(A1); + //AnalogIn analogLightSensorRead(A1); //Set the network parameters - std::string ssid = "Belkin_STM"; - std::string securityKey = "stmicroatx"; + std::string ssid = "<your_wifi_ssid>"; + std::string securityKey = "<your_wifi_passphrase>"; Wifi::SecurityType securityType = Wifi::WPA2; //Wait for wifi module to boot up @@ -95,59 +92,46 @@ printf("Ping Server: %s\n\r", wifi->ping("8.8.8.8") ? "Success" : "Failed"); wait(1); + // Initialize the M2X client Client client; M2XStreamClient m2xClient(&client, key); + + // update location + response = m2xClient.updateLocation(feed, name, latitude, longitude, elevation); + printf("updateLocation response code: %d\r\n", response); + if (response == -1) while (true) ; + // read location + response = m2xClient.readLocation(feed, on_location_found, NULL); + printf("readLocation response code: %d\r\n", response); + if (response == -1) while (true) ; + while(1) { + myled = 1; // LED is ON a = analogRead.read_u16(); - resistance = (float)(adc_scale-a)*10000/a; //get the resistance of the sensor; - temperature = 1/(log(resistance/10000)/B+1/298.15)-273.15;//convert to temperature via datasheet ; + temperature = 1/(log(resistance/10000)/B+1/298.15)-273.15; //convert to temperature via datasheet ; temperature_f = (1.8 * temperature) + 32.0; + sprintf(amb_temp, "%0.2f", temperature_f); + printf("Temp Sensor Analog Reading is 0x%X = %d ", a, a); - printf("Current Temperature: %f degC %f degF \n\r", temperature, temperature_f); - - sensorValue = analogLightSensorRead.read_u16(); - Rsensor=(float)(adc_scale-sensorValue)*10/sensorValue; - - if(Rsensor > thresholdvalue) - { - myled = 1; // LED is ON - } - else - { - myled = 0; // LED is OFF - } - - printf("Light Sensor Analog Reading is 0x%X = %d ", sensorValue, sensorValue); - printf("The sensor resistance is %f \n\n\r", Rsensor); - - sprintf(amb_temp, "%0.2f", temperature_f); //write dummy temp value in amb_temp + printf("Current Temperature: %f C %f F \n\r", temperature, temperature_f); - int response = m2xClient.post(feed, stream, amb_temp); + response = m2xClient.post(feed, stream, amb_temp); printf("Post response code: %d\r\n", response); if (response == -1) while (true) ; - - delay(60000); - } - - // Read temperature data and post to M2X stream - temp = 78.6; - while(1) - { - sprintf(amb_temp, "%0.2f", temp); //write dummy temp value in amb_temp - - int response = m2xClient.post(feed, stream, amb_temp); - printf("Post response code: %d\r\n", response); - if (response == -1) while (true) ; - - temp = temp + 0.1; /* increment the temp by 0.5 deg */ - // wait 60 secs and then loop - delay(60000); - } + // read temperature + //response = m2xClient.fetchValues(feed, stream, on_data_point_found, NULL); + //printf("Fetch response code: %d\r\n", response); + //if (response == -1) while (true) ; + + myled = 0; // LED is OFF + //delay(60000); + delay(5000); + } }