An example that demonstrates uploading data from the DISCO-F746 (STM32F746) board to GroveStreams.com, an IoT analytics platform. Also demonstrations downloading commands, re-establishing dropped ethernet connections, accurate polling frequencies, and tracing to the LCD.

Dependencies:   BSP_DISCO_F746NG F7_Ethernet GroveStreams LCD_DISCO_F746NG LcdDiscoF746NgTracer MbedJSONValue NetworkAPI mbed-rtos mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /*
00002 
00003  STM32F746 GroveStreams Stream Feed via Ethernet
00004 
00005  This GroveStreams example is designed for the STM32F746.
00006  A full "how to" guide for this sketh can be found at https://www.grovestreams.com/developers/getting_started_stm32F746.html
00007  This example:
00008   * Demonstrates uploading data to GroveStreams while downloading commands.
00009   * Demonstrates the GroveStreams API: https://www.grovestreams.com/developers/api.html
00010   * Passing in a custom name for a new component
00011   * Passing in a component template ID to be used for new component definitions
00012   * Downloading a couple of commands during the sample upload and implements them.
00013   * Demonstrates an accurate way to poll and send samples periodically
00014   * Demonstrates send retries and Internet Connection Reset logic to ensure the
00015     STM32 stays connected and can regain connectivity after a network outage.
00016   * Printing verbose trace statements to the LCD (optional)
00017  The STM32 uses DHCP and DNS for a simpler network setup.
00018 
00019  License:
00020   Copyright (C) 2017 GroveStreams LLC.
00021   Licensed under the Apache License, Version 2.0 (the "License");
00022   you may not use this file except in compliance with the License.
00023   You may obtain a copy of the License at: http://www.apache.org/licenses/LICENSE-2.0
00024 
00025   Unless required by applicable law or agreed to in writing, software
00026   distributed under the License is distributed on an "AS IS" BASIS,
00027   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00028   See the License for the specific language governing permissions and
00029   limitations under the License.
00030 */
00031 
00032 #if !FEATURE_LWIP
00033 #error [NOT_SUPPORTED] LWIP not supported for this target
00034 #endif
00035 
00036 #include "mbed.h"
00037 #include "LcdDiscoF746NgTracer.h"
00038 #include "GroveStreams.h"
00039 #include "MbedJSONValue.h"
00040 
00041 // GroveStreams Settings
00042 const char gsApiKey[] = "YOUR_SECRET_API_KEY_HERE";   //Change This!!!
00043 const char gsCompName[] = "STM32F746+Discovery";  //Optionally change. Set this to give your component a name when it initially registers. Encode special chars such as spaces.
00044 
00045 const char gsCompTmplId[] = "";  //Optional. Tells GS what template to use when the feed initially arrives and a new component needs to be created.
00046 
00047 //GroveStreams Stream IDs. Stream IDs tell GroveStreams which component streams the values will be assigned to.
00048 //Don't change these unless you edit your GroveStreams component definition and change the stream IDs to match these.
00049 const char gsStreamId1[] = "voltage";
00050 const char gsStreamId2[] = "temperature";
00051 
00052 // Other Settings
00053 int updateFrequency = 20;    // Update frequency in seconds. Change this to change your sample frequency.
00054 
00055 AnalogIn adc_temp(ADC_TEMP);
00056 AnalogIn adc_vref(ADC_VREF);
00057 DigitalOut myled(LED1);
00058 
00059 int main()
00060 {
00061     //Create Lcd class for verbose tracing
00062     LcdDiscoF746NgTracer lcd;
00063     
00064     //lastSuccessfulUploadTime is used for upload frequency.
00065     time_t lastSuccessfulUploadTime = 0;  
00066 
00067     lcd.printf("Starting...");
00068 
00069     GroveStreams groveStreams(gsApiKey, &lcd);
00070 
00071     const char* myMac = groveStreams.getMACAddress();
00072 
00073     while (true) {
00074         // Update sensor data to GroveStreams
00075         time_t seconds = time(NULL);
00076 
00077         if(seconds - lastSuccessfulUploadTime > updateFrequency) {
00078             lcd.clear();
00079             
00080             lcd.printf("Getting Samples...");
00081             
00082             //Assemble the samples into URL parameters which are seperated with the "&" character
00083             // Example: &s1=6.2&s2=78.231
00084             int temperature = adc_temp.read() * 100.0f;
00085             int voltage = adc_vref.read() * 100.0f;
00086             char samples[64] = {0};
00087             sprintf(samples, "&%s=%d&%s=%d", gsStreamId1, voltage, gsStreamId2, temperature);
00088             
00089             //Append on command requests (request stream values)
00090             //This will indicate to GroveStreams to return the last value
00091             // of each request stream during the sample upload
00092             strcat(samples, "&rsid=freq&rsid=led");
00093             
00094             char resultBuffer[700]= {0};
00095 
00096             //Sending Samples (and returning current command stream values)
00097             time_t connectAttemptTime = time(NULL);
00098             int sendResult = groveStreams.send(myMac, samples, gsCompName, gsCompTmplId, resultBuffer, sizeof resultBuffer);
00099 
00100             if (sendResult == 0) {
00101                 lcd.printf("Send Successful");
00102                 lastSuccessfulUploadTime = connectAttemptTime;
00103                 
00104                 //Handle command streams
00105                 if (strlen(resultBuffer) > 0 && resultBuffer[0] == '{') {
00106                     MbedJSONValue mbedJson;
00107                     parse(mbedJson, resultBuffer);
00108                     
00109                     if (mbedJson.hasMember("freq") && mbedJson["freq"].get<int>() >= 10) {
00110                         //Change the update frequency
00111                         updateFrequency = mbedJson["freq"].get<int>();
00112                         lcd.printf("updateFrequency: %d", updateFrequency);
00113                     }
00114                     if (mbedJson.hasMember("led")) {
00115                         //Change LED
00116                         myled = mbedJson["led"].get<bool>() ? 1 : 0;
00117                         lcd.printf("LED: %s", mbedJson["led"].get<bool>() ? "On" : "Off");
00118                     }
00119                 }
00120             } 
00121         }
00122     }
00123 }
00124