A simple example that demonstrates how to upload data to the GroveStreams.com IoT analytics platform from the Discovery STM32F746 board.

Dependencies:   F7_Ethernet NetworkAPI mbed-rtos mbed

Committer:
mmills
Date:
Thu Jan 05 18:30:39 2017 +0000
Revision:
0:89abc26736a4
Revision 1

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mmills 0:89abc26736a4 1 /*
mmills 0:89abc26736a4 2 STM32F746 GroveStreams Stream Feed via Ethernet
mmills 0:89abc26736a4 3
mmills 0:89abc26736a4 4 This GroveStreams embed code is designed for an STM32 with Ethernet.
mmills 0:89abc26736a4 5 It has been tested on the STM32F46NG Discovery board.
mmills 0:89abc26736a4 6 A full "how to" guide for this sketh can be found at https://www.grovestreams.com/developers/getting_started_stm32F746_simple.html
mmills 0:89abc26736a4 7 This example updates two stream feeds, temperature and voltage, via the
mmills 0:89abc26736a4 8 GroveStreams API: https://www.grovestreams.com/developers/api.html#1
mmills 0:89abc26736a4 9
mmills 0:89abc26736a4 10 License:
mmills 0:89abc26736a4 11 Copyright 2017 GroveStreams LLC.
mmills 0:89abc26736a4 12 Licensed under the Apache License, Version 2.0 (the "License");
mmills 0:89abc26736a4 13 you may not use this file except in compliance with the License.
mmills 0:89abc26736a4 14 You may obtain a copy of the License at: http://www.apache.org/licenses/LICENSE-2.0
mmills 0:89abc26736a4 15
mmills 0:89abc26736a4 16 Unless required by applicable law or agreed to in writing, software
mmills 0:89abc26736a4 17 distributed under the License is distributed on an "AS IS" BASIS,
mmills 0:89abc26736a4 18 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
mmills 0:89abc26736a4 19 See the License for the specific language governing permissions and
mmills 0:89abc26736a4 20 limitations under the License.
mmills 0:89abc26736a4 21
mmills 0:89abc26736a4 22 GroveStreams Setup:
mmills 0:89abc26736a4 23
mmills 0:89abc26736a4 24 * Sign Up for Free User Account - https://www.grovestreams.com
mmills 0:89abc26736a4 25 * Create a GroveStreams organization
mmills 0:89abc26736a4 26 * Enter your GroveStreams secret api key under "GroveStreams Settings" in this code
mmills 0:89abc26736a4 27 * (The api key can be retrieved from within a GroveStreams organization: click the Api Keys toolbar button,
mmills 0:89abc26736a4 28 * select your Api Key, and click View Secret Key. Paste the Secret Key below)
mmills 0:89abc26736a4 29
mmills 0:89abc26736a4 30 STM32 Requirements:
mmills 0:89abc26736a4 31
mmills 0:89abc26736a4 32 * STM32 with Ethernet capabilities
mmills 0:89abc26736a4 33 * mbed IDE and compiler: https://developer.mbed.org
mmills 0:89abc26736a4 34
mmills 0:89abc26736a4 35 Network Requirements:
mmills 0:89abc26736a4 36
mmills 0:89abc26736a4 37 * Ethernet port on Router
mmills 0:89abc26736a4 38 * DHCP enabled on Router
mmills 0:89abc26736a4 39 * Unique MAC Address and IP Address for STM32
mmills 0:89abc26736a4 40
mmills 0:89abc26736a4 41 */
mmills 0:89abc26736a4 42
mmills 0:89abc26736a4 43 #if !FEATURE_LWIP
mmills 0:89abc26736a4 44 #error [NOT_SUPPORTED] LWIP not supported for this target
mmills 0:89abc26736a4 45 #endif
mmills 0:89abc26736a4 46
mmills 0:89abc26736a4 47 #include "mbed.h"
mmills 0:89abc26736a4 48 #include "EthernetInterface.h"
mmills 0:89abc26736a4 49 #include "NetworkAPI/tcp/socket.hpp"
mmills 0:89abc26736a4 50
mmills 0:89abc26736a4 51 // GroveStreams Settings
mmills 0:89abc26736a4 52 const char gsApiKey[] = "YOUR_SECRET_API_KEY_HERE"; //Change This!!!
mmills 0:89abc26736a4 53 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.
mmills 0:89abc26736a4 54
mmills 0:89abc26736a4 55 //GroveStreams Stream IDs. Stream IDs tell GroveStreams which component streams the values will be assigned to.
mmills 0:89abc26736a4 56 //Don't change these unless you edit your GroveStreams component definition and change the stream IDs to match these.
mmills 0:89abc26736a4 57 const char gsStreamId1[] = "voltage";
mmills 0:89abc26736a4 58 const char gsStreamId2[] = "temperature";
mmills 0:89abc26736a4 59
mmills 0:89abc26736a4 60 EthernetInterface eth;
mmills 0:89abc26736a4 61
mmills 0:89abc26736a4 62
mmills 0:89abc26736a4 63 AnalogIn adc_temp(ADC_TEMP);
mmills 0:89abc26736a4 64 AnalogIn adc_vref(ADC_VREF);
mmills 0:89abc26736a4 65
mmills 0:89abc26736a4 66
mmills 0:89abc26736a4 67 int main()
mmills 0:89abc26736a4 68 {
mmills 0:89abc26736a4 69 eth.init();
mmills 0:89abc26736a4 70 eth.connect();
mmills 0:89abc26736a4 71
mmills 0:89abc26736a4 72 //myIpAddress is used for X-Forwarded-For
mmills 0:89abc26736a4 73 //myMax is used for the component ID
mmills 0:89abc26736a4 74 const char* myIpAddress = eth.getIPAddress();
mmills 0:89abc26736a4 75 const char* myMac = eth.getMACAddress();
mmills 0:89abc26736a4 76
mmills 0:89abc26736a4 77 network::tcp::Socket socket;
mmills 0:89abc26736a4 78 while (true) {
mmills 0:89abc26736a4 79
mmills 0:89abc26736a4 80 //Assemble Samples
mmills 0:89abc26736a4 81 int temperature = adc_temp.read() * 100;
mmills 0:89abc26736a4 82 int voltage = adc_vref.read() * 100;
mmills 0:89abc26736a4 83 char samples[50] = {0};
mmills 0:89abc26736a4 84 sprintf(samples, "&%s=%d&%s=%d", gsStreamId1, voltage, gsStreamId2, temperature);
mmills 0:89abc26736a4 85
mmills 0:89abc26736a4 86 //You may need to increase the size of sbuffer if any other char array sizes have increased
mmills 0:89abc26736a4 87 char sbuffer[512] = {0};
mmills 0:89abc26736a4 88 char rbuffer[700];
mmills 0:89abc26736a4 89
mmills 0:89abc26736a4 90 //Assemble the HTTP PUT string (All on the URL GroveStreams Feed PUT API)
mmills 0:89abc26736a4 91 sprintf(sbuffer, "PUT /api/feed?compId=%s&compName=%s&api_key=%s%s HTTP/1.1\r\nHost:\r\nConnection:close\r\nX-Forwarded-For:%s\r\n\r\n",
mmills 0:89abc26736a4 92 myMac, gsCompName, gsApiKey, samples, myIpAddress);
mmills 0:89abc26736a4 93
mmills 0:89abc26736a4 94 //Send the Samples into your GroveStreams organization streams
mmills 0:89abc26736a4 95 socket.open();
mmills 0:89abc26736a4 96
mmills 0:89abc26736a4 97 socket.connect("grovestreams.com", 80);
mmills 0:89abc26736a4 98
mmills 0:89abc26736a4 99 socket.write(sbuffer, strlen(sbuffer));
mmills 0:89abc26736a4 100
mmills 0:89abc26736a4 101 socket.read(rbuffer, sizeof rbuffer);
mmills 0:89abc26736a4 102
mmills 0:89abc26736a4 103 socket.close();
mmills 0:89abc26736a4 104
mmills 0:89abc26736a4 105 wait(20);
mmills 0:89abc26736a4 106 }
mmills 0:89abc26736a4 107
mmills 0:89abc26736a4 108 eth.disconnect();
mmills 0:89abc26736a4 109 }
mmills 0:89abc26736a4 110