Skip to content
This repository was archived by the owner on Jan 6, 2023. It is now read-only.

Commit 007626b

Browse files
committed
Moves NTP configuration to header
1 parent 6472ade commit 007626b

File tree

3 files changed

+49
-43
lines changed

3 files changed

+49
-43
lines changed
Lines changed: 38 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,38 @@
1-
/******************************************************************************
2-
* Copyright 2018 Google
3-
* Licensed under the Apache License, Version 2.0 (the "License");
4-
* you may not use this file except in compliance with the License.
5-
* You may obtain a copy of the License at
6-
*
7-
* http://www.apache.org/licenses/LICENSE-2.0
8-
*
9-
* Unless required by applicable law or agreed to in writing, software
10-
* distributed under the License is distributed on an "AS IS" BASIS,
11-
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12-
* See the License for the specific language governing permissions and
13-
* limitations under the License.
14-
*****************************************************************************/
15-
#include "esp32-mqtt.h"
16-
17-
#ifndef LED_BUILTIN
18-
#define LED_BUILTIN 13
19-
#endif
20-
21-
void setup() {
22-
// put your setup code here, to run once:
23-
Serial.begin(115200);
24-
pinMode(LED_BUILTIN, OUTPUT);
25-
setupCloudIoT();
26-
}
27-
28-
unsigned long lastMillis = 0;
29-
void loop() {
30-
mqttClient->loop();
31-
delay(10); // <- fixes some issues with WiFi stability
32-
33-
if (!mqttClient->connected()) {
34-
connect();
35-
}
36-
37-
// publish a message roughly every second.
38-
if (millis() - lastMillis > 1000) {
39-
lastMillis = millis();
40-
publishTelemetry(getDefaultSensor());
41-
}
42-
}
1+
/******************************************************************************
2+
* Copyright 2018 Google
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS,
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License.
14+
*****************************************************************************/
15+
#include "esp32-mqtt.h"
16+
17+
void setup() {
18+
// put your setup code here, to run once:
19+
Serial.begin(115200);
20+
pinMode(LED_BUILTIN, OUTPUT);
21+
setupCloudIoT();
22+
}
23+
24+
unsigned long lastMillis = 0;
25+
void loop() {
26+
mqttClient->loop();
27+
delay(10); // <- fixes some issues with WiFi stability
28+
29+
if (!mqttClient->connected()) {
30+
connect();
31+
}
32+
33+
// publish a message roughly every second.
34+
if (millis() - lastMillis > 1000) {
35+
lastMillis = millis();
36+
publishTelemetry(getDefaultSensor());
37+
}
38+
}

examples/Esp32-lwmqtt/ciotc_config.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,14 @@ const char *location = "us-central1";
2424
const char *registry_id = "my-registry";
2525
const char *device_id = "my-esp32-device";
2626

27+
// Configuration for NTP
28+
const char* ntp_primary = "pool.ntp.org";
29+
const char* ntp_secondary = "time.nist.gov";
30+
31+
#ifndef LED_BUILTIN
32+
#define LED_BUILTIN 13
33+
#endif
34+
2735
// To get the private key run (where private-key.pem is the ec private key
2836
// used to create the certificate uploaded to google cloud iot):
2937
// openssl ec -in <private-key.pem> -noout -text

examples/Esp32-lwmqtt/esp32-mqtt.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,11 @@
1515
// This file contains static methods for API requests using Wifi / MQTT
1616
#ifndef __ESP32_MQTT_H__
1717
#define __ESP32_MQTT_H__
18+
#include <String.h>
1819
#include <WiFi.h>
1920
#include <WiFiClientSecure.h>
2021

22+
#include <rBase64.h>
2123
#include <MQTT.h>
2224

2325
#include <CloudIoTCore.h>
@@ -60,7 +62,7 @@ void setupWifi() {
6062
delay(100);
6163
}
6264

63-
configTime(0, 0, "pool.ntp.org", "time.nist.gov");
65+
configTime(0, 0, ntp_primary, ntp_secondary);
6466
Serial.println("Waiting on time sync...");
6567
while (time(nullptr) < 1510644967) {
6668
delay(10);

0 commit comments

Comments
 (0)