C++ 如何使用具有全局函数c++;(多次定义..和未定义引用失败)-谷歌云IoT Mqtt
我希望你能帮助我: 我正在使用platformIO和ESP32 Wifi模块,并希望通过mqtt实现与谷歌云的连接 首选以下体系结构: main.cpp---包括-->WifiClass.h---包括-->iot mqtt.h---包括-->CloudIoTCoreMqtt.h main.cpp包括用于Esp32(如Arduino)的带有setup()和loop()的脚本。WifiClass应该是来自Google/Apache的云bib与main.cpp脚本之间的连接。iot mqtt.h包含初始化wifi和mqtt以及数据交换和回调处理程序的方法 我最大的问题是: iot mqtt.h不包含类,只包含全局函数和变量。当我想将其包含在wifi.h中并开始编译时,我总是会得到带有多个定义的错误。 我不知道将变量标记为extern并在iot-mqtt.cpp文件中定义它们并将函数标记为static是否正确?如果这样做,就会得到一个错误,即回调函数具有未定义的引用,因为它们在CloudIoCoreMqtt中使用 有谁能帮我解决这些问题吗?我没有任何解决的办法 亲切问候,, 弗洛里安 main.cpp:C++ 如何使用具有全局函数c++;(多次定义..和未定义引用失败)-谷歌云IoT Mqtt,c++,C++,我希望你能帮助我: 我正在使用platformIO和ESP32 Wifi模块,并希望通过mqtt实现与谷歌云的连接 首选以下体系结构: main.cpp---包括-->WifiClass.h---包括-->iot mqtt.h---包括-->CloudIoTCoreMqtt.h main.cpp包括用于Esp32(如Arduino)的带有setup()和loop()的脚本。WifiClass应该是来自Google/Apache的云bib与main.cpp脚本之间的连接。iot mqtt.h包含初
#include "WifiClass.h"
WifiClass wifiObject;
void setup() {
wifiObject.begin();
}
void loop() {
wifiObject.func1();
}
WifiClass.h:
#ifndef __WIFICLASS__
#define __WIFICLASS__
#include "iot-mqtt.h"
class WifiClass {
public:
void begin();
void func1();
};
#endif
WifiClass.cpp:
#include "WifiClass.h"
//core functions
void WifiClass::begin()
{
setupCloudIoT();
}
void WifiClass::func1()
{
mqttLoop();
}
物联网mqtt.h:(并非所有相关)
/******************************************************************************
* Copyright 2018 Google
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*****************************************************************************/
// This file contains static methods for API requests using Wifi / MQTT
#ifndef __ESP32_MQTT_H__
#define __ESP32_MQTT_H__
#include <Client.h>
#include <WiFi.h>
#include <WiFiClientSecure.h>
#include <NTPClient.h>
#include <MQTT.h>
#include <CloudIoTCore.h>
#include <CloudIoTCoreMqtt.h>
#include "ciotc_config.h" // Update this file with your configuration
// Define NTP Client to get time
WiFiUDP ntpUDP;
NTPClient timeClient(ntpUDP);
///////////////////////////////
// Initialize WiFi and MQTT for this board
Client *netClient;
CloudIoTCoreDevice *device;
CloudIoTCoreMqtt *mqtt;
MQTTClient *mqttClient;
String jwt;
///////////////////////////////
// Orchestrates various methods from preceeding code.
///////////////////////////////
bool publishTelemetry(String data) {
return mqtt->publishTelemetry(data);
}
bool publishTelemetry(const char* data, int length) {
return mqtt->publishTelemetry(data, length);
}
bool publishTelemetry(String subfolder, String data) {
return mqtt->publishTelemetry(subfolder, data);
}
bool publishTelemetry(String subfolder, const char* data, int length) {
return mqtt->publishTelemetry(subfolder, data, length);
}
String getWifiStatus() {
return String(WiFi.RSSI());
}
// Callback handler for IoT Core messages
void messageReceived(String &topic, String &payload) {
Serial.println("incoming: " + topic + " - " + payload);
//payload == UNLOCK;30; => statemachine Start Grill
//publishTelemetry("");
String parameters[10];
parameters[0] = "";
String parameter = "";
int i = 0;
for(char& c : payload) {
if(c == ';') {
parameters[i] = parameter;
parameter = "";
i++;
} else {
parameter += c;
}
}
}
String getJwt() {
iat = time(nullptr);
Serial.println("Refreshing JWT");
jwt = device->createJWT(iat, jwt_exp_secs);
return jwt;
}
void connectRoutine () {
unsigned long startMillis = millis();
Serial.println("Connecting to WiFi");
while (WiFi.status() != WL_CONNECTED) {
Serial.print("Current WiFi Status: ");
Serial.println(WiFi.status());
if (WiFi.status() == WL_CONNECT_FAILED) { // WiFi.begin wasn't called yet
Serial.println("Disconnecting WiFi");
WiFi.disconnect(true);
}
else if (WiFi.status() == WL_NO_SHIELD || WiFi.status() == WL_CONNECTION_LOST) // WiFi.begin has failed (AUTH_FAIL); 5 - STA_DISCONNECTED
{
Serial.println("Connecting WiFi 2");
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
}
else if (WiFi.status() == WL_DISCONNECTED) // WiFi.begin has failed (WL_Disconnectes); 5 - STA_DISCONNECTED
{
if(millis()- startMillis > 2000){
Serial.println("Reconneting WiFi");
//WiFi.disconnect(true);
WiFi.reconnect();
startMillis = millis();
}
}
else if (WiFi.status() == WL_NO_SSID_AVAIL) // SSID not available
{
break;
}
delay(1000);
}
}
void setupWifi() {
Serial.println("Starting wifi");
WiFi.mode(WIFI_STA);
// WiFi.setSleep(false); // May help with disconnect? Seems to have been removed from WiFi
WiFi.begin(ssid, password);
connectRoutine ();
configTime(0, 0, ntp_primary, ntp_secondary);
Serial.println("Waiting on time sync...");
while(!timeClient.update()) {
timeClient.forceUpdate();
}
}
void connectWifi() {
Serial.print("checking wifi reconnect...");
connectRoutine ();
}
void connect() {
connectWifi();
mqtt->mqttConnect();
}
void setupCloudIoT() {
device = new CloudIoTCoreDevice(
project_id, location, registry_id, device_id,
private_key_str);
setupWifi();
netClient = new WiFiClientSecure();
mqttClient = new MQTTClient(512);
mqttClient->setOptions(180, true, 1000); // keepAlive, cleanSession, timeout
mqtt = new CloudIoTCoreMqtt(mqttClient, netClient, device);
mqtt->setUseLts(true);
mqtt->startMQTT();
}
//IoT Core Functions
void heartBeat() {
if (millis() - LAST_MILLIS_HEART_BEAT > 10000) {
LAST_MILLIS_HEART_BEAT = millis();
//publishTelemetry(mqttClient, "/sensors", getDefaultSensor());
Serial.println("Sending Device Information: " + getDeviceInformation());
publishTelemetry(getDeviceInformation());
}
}
void mqttLoop() {
mqtt->loop();
delay(10);
if (!mqttClient->connected()) {
connect();
}
}
#endif //__ESP32_MQTT_H__
/******************************************************************************
* Copyright 2018 Google
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*****************************************************************************/
// This file contains your configuration used to connect to Cloud IoT Core
// Wifi network details.
const char *ssid = "";
const char *password = "";
// Cloud iot details.
const char *project_id = "";
const char *location = "";
const char *registry_id = "";
const char *device_id = "";
// Configuration for NTP
const char* ntp_primary = "";
const char* ntp_secondary = "";
#ifndef LED_BUILTIN
#define LED_BUILTIN 13
#endif
// To get the private key run (where private-key.pem is the ec private key
// used to create the certificate uploaded to google cloud iot):
// openssl ec -in <private-key.pem> -noout -text
// and copy priv: part.
// The key length should be exactly the same as the key length bellow (32 pairs
// of hex digits). If it's bigger and it starts with "00:" delete the "00:". If
// it's smaller add "00:" to the start. If it's too big or too small something
// is probably wrong with your key.
const char *private_key_str ="";
const int jwt_exp_secs = ;
const char *root_cert = "";
/******************************************************************************
* Copyright 2019 Google
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*****************************************************************************/
#include "CloudIoTCoreMqtt.h"
// Forward global callback declarations
String getJwt();
void setupCert();
Client* setupNetwork(bool);
void messageReceived(String &topic, String &payload);