@@ -7,28 +7,33 @@ import {
77 createAuthorization ,
88 getAllResources ,
99} from 'src/authorizations/actions/thunks'
10+ import { getBuckets } from 'src/buckets/actions/thunks'
11+
12+ // Components
13+ import CodeSnippet from 'src/shared/components/CodeSnippet'
14+ import {
15+ Columns ,
16+ ComponentSize ,
17+ Grid ,
18+ InfluxColors ,
19+ Panel ,
20+ } from '@influxdata/clockface'
21+ import WriteDataHelperBuckets from 'src/writeData/components/WriteDataHelperBuckets'
22+ import { WriteDataDetailsContext } from 'src/writeData/components/WriteDataDetailsContext'
1023
1124// Selectors
1225import { getOrg } from 'src/organizations/selectors'
1326import { getMe } from 'src/me/selectors'
1427import { getAllTokensResources } from 'src/resources/selectors'
1528
16- // Thunks
17- import { getBuckets } from 'src/buckets/actions/thunks'
18-
19- // Helper Components
20- import { WriteDataDetailsContext } from 'src/writeData/components/WriteDataDetailsContext'
21-
2229// Utils
2330import { allAccessPermissions } from 'src/authorizations/utils/permissions'
2431import { event } from 'src/cloud/utils/reporting'
2532import { keyboardCopyTriggered , userSelection } from 'src/utils/crossPlatform'
2633
2734// Types
2835import { AppState , Authorization } from 'src/types'
29-
30- // Styles
31- import './ArduinoSteps.scss'
36+ import { getTimezoneOffset } from 'src/dashboards/utils/getTimezoneOffset'
3237
3338type OwnProps = {
3439 setTokenValue : ( tokenValue : string ) => void
@@ -47,32 +52,29 @@ export const InitializeClient: FC<OwnProps> = ({
4752 const me = useSelector ( getMe )
4853 const allPermissionTypes = useSelector ( getAllTokensResources )
4954 const dispatch = useDispatch ( )
50-
55+ const url =
56+ me . quartzMe ?. clusterHost || 'https://us-west-2-1.aws.cloud2.influxdata.com/'
5157 const currentAuth = useSelector ( ( state : AppState ) => {
5258 return state . resources . tokens . currentAuth . item
5359 } )
60+ const token = currentAuth . token
5461
5562 const sortedPermissionTypes = useMemo (
5663 ( ) => allPermissionTypes . sort ( ( a , b ) => collator . compare ( a , b ) ) ,
5764 [ allPermissionTypes ]
5865 )
66+
5967 const { bucket} = useContext ( WriteDataDetailsContext )
6068
6169 useEffect ( ( ) => {
6270 dispatch ( getBuckets ( ) )
71+ dispatch ( getAllResources ( ) )
6372 } , [ ] ) // eslint-disable-line react-hooks/exhaustive-deps
6473
6574 useEffect ( ( ) => {
6675 onSelectBucket ( bucket . name )
6776 } , [ bucket . name , onSelectBucket ] )
6877
69- useEffect ( ( ) => {
70- const fetchResources = async ( ) => {
71- await dispatch ( getAllResources ( ) )
72- }
73- fetchResources ( )
74- } , [ ] )
75-
7678 useEffect ( ( ) => {
7779 if ( sortedPermissionTypes . length && tokenValue === null ) {
7880 const authorization : Authorization = {
@@ -95,22 +97,112 @@ export const InitializeClient: FC<OwnProps> = ({
9597
9698 useEffect ( ( ) => {
9799 const fireKeyboardCopyEvent = event => {
98- if (
99- keyboardCopyTriggered ( event ) &&
100- ( userSelection ( ) . includes ( 'influx config create' ) ||
101- userSelection ( ) . includes ( 'influx bucket create' ) )
102- ) {
100+ if ( keyboardCopyTriggered ( event ) && userSelection ( ) . includes ( '#define' ) ) {
103101 logCopyCodeSnippet ( )
104102 }
105103 }
106104 document . addEventListener ( 'keydown' , fireKeyboardCopyEvent )
107105 return ( ) => document . removeEventListener ( 'keydown' , fireKeyboardCopyEvent )
108106 } , [ ] )
109107
108+ const codeSnippet = `#if defined(ESP32)
109+ #include <WiFiMulti.h>
110+ WiFiMulti wifiMulti;
111+ #define DEVICE "ESP32"
112+ #elif defined(ESP8266)
113+ #include <ESP8266WiFiMulti.h>
114+ ESP8266WiFiMulti wifiMulti;
115+ #define DEVICE "ESP8266"
116+ #endif
117+
118+ #include <InfluxDbClient.h>
119+ #include <InfluxDbCloud.h>
120+
121+ // WiFi AP SSID
122+ #define WIFI_SSID "YOUR_WIFI_SSID"
123+ // WiFi password
124+ #define WIFI_PASSWORD "YOUR_WIFI_PASSWORD"
125+
126+ #define INFLUXDB_URL "${ url } "
127+ #define INFLUXDB_TOKEN "${ token } "
128+ #define INFLUXDB_ORG "${ org . id } "
129+ #define INFLUXDB_BUCKET "${
130+ bucket . name === '<BUCKET>' ? 'YOUR_BUCKET' : bucket . name
131+ } "
132+
133+ // Time zone info
134+ #define TZ_INFO "UTC${ - getTimezoneOffset ( ) / 60 } "
135+
136+ // Declare InfluxDB client instance with preconfigured InfluxCloud certificate
137+ InfluxDBClient client(INFLUXDB_URL, INFLUXDB_ORG, INFLUXDB_BUCKET, INFLUXDB_TOKEN, InfluxDbCloud2CACert);
138+
139+ // Declare Data point
140+ Point sensor("wifi_status");
141+
142+ void setup() {
143+ Serial.begin(115200);
144+
145+ // Setup wifi
146+ WiFi.mode(WIFI_STA);
147+ wifiMulti.addAP(WIFI_SSID, WIFI_PASSWORD);
148+
149+ Serial.print("Connecting to wifi");
150+ while (wifiMulti.run() != WL_CONNECTED) {
151+ Serial.print(".");
152+ delay(100);
153+ }
154+ Serial.println();
155+
156+ // Accurate time is necessary for certificate validation and writing in batches
157+ // We use the NTP servers in your area as provided by: https://www.pool.ntp.org/zone/
158+ // Syncing progress and the time will be printed to Serial.
159+ timeSync(TZ_INFO, "pool.ntp.org", "time.nis.gov");
160+
161+
162+ // Check server connection
163+ if (client.validateConnection()) {
164+ Serial.print("Connected to InfluxDB: ");
165+ Serial.println(client.getServerUrl());
166+ } else {
167+ Serial.print("InfluxDB connection failed: ");
168+ Serial.println(client.getLastErrorMessage());
169+ }
170+ }`
171+
110172 // Events log handling
111173 const logCopyCodeSnippet = ( ) => {
112- event ( `firstMile.arduinoWizard.buckets .code.copied` )
174+ event ( `firstMile.arduinoWizard.initializeClient .code.copied` )
113175 }
114176
115- return < h1 > Initialize Client</ h1 >
177+ return (
178+ < >
179+ < h1 > Initialize Client</ h1 >
180+ < p > Select or create a bucket to initialize the arduino client with.</ p >
181+ < Panel backgroundColor = { InfluxColors . Grey15 } >
182+ < Panel . Body size = { ComponentSize . ExtraSmall } >
183+ < Grid >
184+ < Grid . Row >
185+ < Grid . Column widthSM = { Columns . Twelve } >
186+ < WriteDataHelperBuckets useSimplifiedBucketForm = { true } />
187+ </ Grid . Column >
188+ </ Grid . Row >
189+ </ Grid >
190+ </ Panel . Body >
191+ </ Panel >
192+ < p className = "small-margins" >
193+ Paste the following snippet into a blank Arduino sketch file.
194+ </ p >
195+ < CodeSnippet
196+ text = { codeSnippet }
197+ onCopy = { logCopyCodeSnippet }
198+ language = "arduino"
199+ />
200+ < p style = { { fontSize : '14px' , marginTop : '8px' , marginBottom : '48px' } } >
201+ Note: you will need to set the{ ' ' }
202+ < code className = "homepage-wizard--code-highlight" > WIFI_SSID</ code > and{ ' ' }
203+ < code className = "homepage-wizard--code-highlight" > WIFI_PASSWORD</ code > { ' ' }
204+ variables to the correct values for your wifi router.
205+ </ p >
206+ </ >
207+ )
116208}
0 commit comments