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 AT&T Developer Summit Hackathon 2016

Committer:
jb8414
Date:
Fri Sep 05 02:28:21 2014 +0000
Revision:
17:e54be812a3b3
Parent:
16:921fec88838d
Port for STM411 board

Who changed what in which revision?

UserRevisionLine numberNew 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 "include_me.h"
joe_tijerina 14:df2fe4b77d83 4 #include "math.h"
joe_tijerina 14:df2fe4b77d83 5
jb8414 17:e54be812a3b3 6 // set to 1 for cellular shield board
jb8414 17:e54be812a3b3 7 // set to 0 for wifi shield board
jb8414 17:e54be812a3b3 8 #define CELL_SHIELD 1
jb8414 17:e54be812a3b3 9
joe_tijerina 14:df2fe4b77d83 10 using namespace mts;
joe_tijerina 14:df2fe4b77d83 11
joe_tijerina 14:df2fe4b77d83 12 /* This example shows how to do a basic connectivity test to AT&T M2X Cloud
joe_tijerina 14:df2fe4b77d83 13 * using the MTS Wifi shield board. You will need to change the network
joe_tijerina 14:df2fe4b77d83 14 * SSID and security key. You may need to chage the security type.
joe_tijerina 14:df2fe4b77d83 15 */
joe_tijerina 14:df2fe4b77d83 16
joe_tijerina 14:df2fe4b77d83 17 using namespace mts;
joe_tijerina 14:df2fe4b77d83 18
jb8414 17:e54be812a3b3 19 const char key[] = "<key>"; // Replace with your M2X API key
jb8414 17:e54be812a3b3 20 const char feed[] = "<feed>"; // Replace with your blueprint Feed ID
jb8414 17:e54be812a3b3 21 const char stream[] = "<stream>"; // Replace with your stream name
jb8414 17:e54be812a3b3 22 char name[] = "<location>"; // Name of current location of datasource
joe_tijerina 14:df2fe4b77d83 23
joe_tijerina 14:df2fe4b77d83 24 double latitude = 30.3748076;
joe_tijerina 14:df2fe4b77d83 25 double longitude = -97.7386896; // You can also read those values from a GPS
joe_tijerina 14:df2fe4b77d83 26 double elevation = 400.00;
joe_tijerina 14:df2fe4b77d83 27
joe_tijerina 14:df2fe4b77d83 28 DigitalOut myled(D7);
joe_tijerina 14:df2fe4b77d83 29 AnalogIn tempSensor(A0);
joe_tijerina 14:df2fe4b77d83 30
joe_tijerina 14:df2fe4b77d83 31 // Note: This callback function needs to be debugged further as it does not print
joe_tijerina 14:df2fe4b77d83 32 // the actual fetched reports, instead both "at" and "value" pointers print the
joe_tijerina 14:df2fe4b77d83 33 // timestamps of each report.
joe_tijerina 14:df2fe4b77d83 34 void on_data_point_found(const char* at, const char* value, int index, void* context) {
joe_tijerina 14:df2fe4b77d83 35 printf("Found a data point, index: %d\r\n", index);
joe_tijerina 14:df2fe4b77d83 36 printf("At: %s Value: %s \r\n", at, value);
joe_tijerina 14:df2fe4b77d83 37 }
joe_tijerina 14:df2fe4b77d83 38
joe_tijerina 14:df2fe4b77d83 39 void on_location_found(const char* name,
joe_tijerina 14:df2fe4b77d83 40 double latitude,
joe_tijerina 14:df2fe4b77d83 41 double longitude,
joe_tijerina 14:df2fe4b77d83 42 double elevation,
joe_tijerina 14:df2fe4b77d83 43 const char* timestamp,
joe_tijerina 14:df2fe4b77d83 44 int index,
joe_tijerina 14:df2fe4b77d83 45 void* context) {
joe_tijerina 14:df2fe4b77d83 46 printf("Found a location, index: %d\r\n", index);
joe_tijerina 14:df2fe4b77d83 47 printf("Name: %s Latitude: %lf Longitude: %lf\r\n", name, latitude, longitude);
joe_tijerina 14:df2fe4b77d83 48 printf("Elevation: %lf Timestamp: %s\r\n", elevation, timestamp);
joe_tijerina 14:df2fe4b77d83 49 }
joe_tijerina 14:df2fe4b77d83 50
joe_tijerina 14:df2fe4b77d83 51
joe_tijerina 14:df2fe4b77d83 52 int main()
joe_tijerina 14:df2fe4b77d83 53 {
joe_tijerina 14:df2fe4b77d83 54 char amb_temp[6];
joe_tijerina 14:df2fe4b77d83 55 int response;
joe_tijerina 14:df2fe4b77d83 56 int a;
joe_tijerina 14:df2fe4b77d83 57 int adc_scale = 4095;
joe_tijerina 14:df2fe4b77d83 58 int B = 3975;
joe_tijerina 14:df2fe4b77d83 59 float resistance;
joe_tijerina 14:df2fe4b77d83 60 float temperature;
joe_tijerina 14:df2fe4b77d83 61 float temperature_f;
joe_tijerina 14:df2fe4b77d83 62
joe_tijerina 14:df2fe4b77d83 63 //Set the network parameters
jb8414 17:e54be812a3b3 64 std::string ssid = "<ssid>";
jb8414 17:e54be812a3b3 65 std::string securityKey = "<password>";
joe_tijerina 14:df2fe4b77d83 66 Wifi::SecurityType securityType = Wifi::WPA2;
joe_tijerina 14:df2fe4b77d83 67
jb8414 17:e54be812a3b3 68 printf("starting\n\r");
jb8414 17:e54be812a3b3 69
jb8414 17:e54be812a3b3 70 #if CELL_SHIELD
jb8414 17:e54be812a3b3 71 MTSSerialFlowControl* serial = new MTSSerialFlowControl(PA_9, PA_10, PA_1, PA_0);
jb8414 17:e54be812a3b3 72 serial->baud(115200);
jb8414 17:e54be812a3b3 73 printf("serialflow ok\n\r");
jb8414 17:e54be812a3b3 74 Transport::setTransport(Transport::CELLULAR);
jb8414 17:e54be812a3b3 75 Cellular* cell = Cellular::getInstance();
jb8414 17:e54be812a3b3 76 cell->init(serial, PB_5, PA_8); //DCD and DTR pins for STM411
jb8414 17:e54be812a3b3 77
jb8414 17:e54be812a3b3 78 int max_tries = 5;
jb8414 17:e54be812a3b3 79 int i;
jb8414 17:e54be812a3b3 80 std::string apn = "wap.cingular"; // set to the appropriate APN (i.e. "m2m.com.attz" for M2X SIMs, wap.cingular)
jb8414 17:e54be812a3b3 81
jb8414 17:e54be812a3b3 82 i = 0;
jb8414 17:e54be812a3b3 83 while (i++ < max_tries) {
jb8414 17:e54be812a3b3 84 if (cell->getRegistration() == Cellular::REGISTERED) {
jb8414 17:e54be812a3b3 85 printf("registered with tower\n\r");
jb8414 17:e54be812a3b3 86 break;
jb8414 17:e54be812a3b3 87 } else if (i >= max_tries) {
jb8414 17:e54be812a3b3 88 printf("failed to register with tower\n\r");
jb8414 17:e54be812a3b3 89 } else {
jb8414 17:e54be812a3b3 90 wait(3);
jb8414 17:e54be812a3b3 91 }
jb8414 17:e54be812a3b3 92 }
jb8414 17:e54be812a3b3 93
jb8414 17:e54be812a3b3 94 printf("signal strength: %d\n\r", cell->getSignalStrength());
jb8414 17:e54be812a3b3 95
jb8414 17:e54be812a3b3 96 i = 0;
jb8414 17:e54be812a3b3 97 printf("setting APN to %s\n\r", apn.c_str());
jb8414 17:e54be812a3b3 98 while (i++ < max_tries) {
jb8414 17:e54be812a3b3 99 if (cell->setApn(apn) == SUCCESS) {
jb8414 17:e54be812a3b3 100 printf("successfully set APN\n\r");
jb8414 17:e54be812a3b3 101 break;
jb8414 17:e54be812a3b3 102 } else if (i >= max_tries) {
jb8414 17:e54be812a3b3 103 printf("failed to set APN\n\r");
jb8414 17:e54be812a3b3 104 } else {
jb8414 17:e54be812a3b3 105 wait(1);
jb8414 17:e54be812a3b3 106 }
jb8414 17:e54be812a3b3 107 }
jb8414 17:e54be812a3b3 108
jb8414 17:e54be812a3b3 109 i = 0;
jb8414 17:e54be812a3b3 110 printf("bringing up PPP link\n\r");
jb8414 17:e54be812a3b3 111 while (i++ < max_tries) {
jb8414 17:e54be812a3b3 112 if (cell->connect()) {
jb8414 17:e54be812a3b3 113 printf("PPP link is up\n\r");
jb8414 17:e54be812a3b3 114 break;
jb8414 17:e54be812a3b3 115 } else if (i >= max_tries) {
jb8414 17:e54be812a3b3 116 printf("failed to bring PPP link up\n\r");
jb8414 17:e54be812a3b3 117 } else {
jb8414 17:e54be812a3b3 118 wait(1);
jb8414 17:e54be812a3b3 119 }
jb8414 17:e54be812a3b3 120 }
jb8414 17:e54be812a3b3 121 #else
jb8414 17:e54be812a3b3 122 // WiFi shield
jb8414 17:e54be812a3b3 123
joe_tijerina 14:df2fe4b77d83 124 //Wait for wifi module to boot up
joe_tijerina 14:df2fe4b77d83 125 for (int i = 10; i >= 0; i = i - 2) {
joe_tijerina 14:df2fe4b77d83 126 wait(2);
joe_tijerina 14:df2fe4b77d83 127 printf("Waiting %d seconds...\n\r", i);
joe_tijerina 14:df2fe4b77d83 128 }
joe_tijerina 14:df2fe4b77d83 129
joe_tijerina 14:df2fe4b77d83 130 //Setup serial interface to WiFi module
jb8414 17:e54be812a3b3 131 MTSSerial* serial = new MTSSerial(PA_9, PA_10, 256, 4096);
joe_tijerina 14:df2fe4b77d83 132 serial->baud(9600);
joe_tijerina 14:df2fe4b77d83 133
joe_tijerina 14:df2fe4b77d83 134 Transport::setTransport(Transport::WIFI);
joe_tijerina 14:df2fe4b77d83 135
joe_tijerina 14:df2fe4b77d83 136 //Setup Wifi class
joe_tijerina 14:df2fe4b77d83 137 Wifi* wifi = Wifi::getInstance();
joe_tijerina 14:df2fe4b77d83 138 printf("Init: %s\n\r", wifi->init(serial) ? "SUCCESS" : "FAILURE");
joe_tijerina 14:df2fe4b77d83 139
joe_tijerina 14:df2fe4b77d83 140 //Setup and check connection
joe_tijerina 14:df2fe4b77d83 141 printf("Set Network: %s\n\r", getCodeNames(wifi->setNetwork(ssid, securityType, securityKey)).c_str());
joe_tijerina 14:df2fe4b77d83 142 printf("Set DHCP: %s\n\r", getCodeNames(wifi->setDeviceIP("DHCP")).c_str());
joe_tijerina 14:df2fe4b77d83 143 while (! wifi->connect()) {
joe_tijerina 14:df2fe4b77d83 144 printf("Connect: Failure\r\n");
joe_tijerina 14:df2fe4b77d83 145 wait(1);
joe_tijerina 14:df2fe4b77d83 146 }
joe_tijerina 14:df2fe4b77d83 147 printf("Connect: Success\r\n");
joe_tijerina 14:df2fe4b77d83 148 printf("Is Connected: %s\n\r", wifi->isConnected() ? "True" : "False");
jb8414 17:e54be812a3b3 149
jb8414 17:e54be812a3b3 150
joe_tijerina 14:df2fe4b77d83 151
joe_tijerina 14:df2fe4b77d83 152 printf("Ping Server: %s\n\r", wifi->ping("8.8.8.8") ? "Success" : "Failed");
joe_tijerina 14:df2fe4b77d83 153 wait(1);
jb8414 17:e54be812a3b3 154 #endif
joe_tijerina 14:df2fe4b77d83 155
joe_tijerina 14:df2fe4b77d83 156 // Initialize the M2X client
joe_tijerina 14:df2fe4b77d83 157 Client client;
joe_tijerina 14:df2fe4b77d83 158 M2XStreamClient m2xClient(&client, key);
joe_tijerina 14:df2fe4b77d83 159
joe_tijerina 14:df2fe4b77d83 160 // update location
joe_tijerina 14:df2fe4b77d83 161 response = m2xClient.updateLocation(feed, name, latitude, longitude, elevation);
joe_tijerina 14:df2fe4b77d83 162 printf("updateLocation response code: %d\r\n", response);
joe_tijerina 14:df2fe4b77d83 163 if (response == -1) while (true) ;
joe_tijerina 14:df2fe4b77d83 164
joe_tijerina 16:921fec88838d 165 //for (int i = 0; i < 5; i++)
joe_tijerina 16:921fec88838d 166 while(1)
joe_tijerina 14:df2fe4b77d83 167 {
joe_tijerina 14:df2fe4b77d83 168 myled = 1; // LED is ON
joe_tijerina 14:df2fe4b77d83 169 a = tempSensor.read_u16();
joe_tijerina 14:df2fe4b77d83 170
joe_tijerina 14:df2fe4b77d83 171 resistance = (float)(adc_scale-a)*10000/a; //get the resistance of the sensor;
joe_tijerina 14:df2fe4b77d83 172 temperature = 1/(log(resistance/10000)/B+1/298.15)-273.15; //convert to temperature via datasheet
joe_tijerina 14:df2fe4b77d83 173 temperature_f = (1.8 * temperature) + 32.0;
joe_tijerina 14:df2fe4b77d83 174 sprintf(amb_temp, "%0.2f", temperature_f);
joe_tijerina 14:df2fe4b77d83 175
joe_tijerina 14:df2fe4b77d83 176 printf("Temp Sensor Analog Reading is 0x%X = %d ", a, a);
joe_tijerina 14:df2fe4b77d83 177 printf("Current Temperature: %f C %f F \n\r", temperature, temperature_f);
joe_tijerina 14:df2fe4b77d83 178
joe_tijerina 14:df2fe4b77d83 179 response = m2xClient.post(feed, stream, amb_temp);
joe_tijerina 14:df2fe4b77d83 180 printf("Post response code: %d\r\n", response);
joe_tijerina 14:df2fe4b77d83 181 if (response == -1) while (true) ;
joe_tijerina 14:df2fe4b77d83 182
joe_tijerina 14:df2fe4b77d83 183 myled = 0; // LED is OFF
joe_tijerina 14:df2fe4b77d83 184
joe_tijerina 14:df2fe4b77d83 185 delay(5000);
joe_tijerina 14:df2fe4b77d83 186 }
joe_tijerina 14:df2fe4b77d83 187
joe_tijerina 14:df2fe4b77d83 188
joe_tijerina 14:df2fe4b77d83 189 // fetch location
joe_tijerina 14:df2fe4b77d83 190 response = m2xClient.readLocation(feed, on_location_found, NULL);
joe_tijerina 14:df2fe4b77d83 191 printf("readLocation response code: %d\r\n", response);
joe_tijerina 14:df2fe4b77d83 192 if (response == -1) while (true) ;
joe_tijerina 14:df2fe4b77d83 193
joe_tijerina 14:df2fe4b77d83 194 // fetch temperature
joe_tijerina 14:df2fe4b77d83 195 response = m2xClient.fetchValues(feed, stream, on_data_point_found, NULL);
joe_tijerina 14:df2fe4b77d83 196 printf("Fetch response code: %d\r\n", response);
joe_tijerina 14:df2fe4b77d83 197 if (response == -1) while (true) ;
joe_tijerina 14:df2fe4b77d83 198
joe_tijerina 14:df2fe4b77d83 199 }
joe_tijerina 14:df2fe4b77d83 200