Azure / azure-iot-arduino

Azure IoT library for the Arduino
Other
167 stars 96 forks source link

'MQTT_Protocol' was not declared in this scope #108

Closed Soumojit28 closed 4 years ago

Soumojit28 commented 4 years ago

I am trying to compile a code but getting this error 'MQTT_Protocol' was not declared in this scope

`#include

include

include

include

include

include "dht_temperature_AzureIotHub_config.h"

include "DHT.h"

define DHTPIN 2 // what digital pin we're connected to

define DHTTYPE DHT11 // DHT 11

DHT dht(DHTPIN, DHTTYPE);

const char ssid = IOT_CONFIG_WIFI_SSID; const char password= IOT_CONFIG_WIFI_PASSWORD;

static bool messagePending = false; static bool messageSending = true;

static int interval = INTERVAL; static IOTHUB_CLIENT_LL_HANDLE iotHubClientHandle; static const char* connectionString = IOT_CONFIG_CONNECTION_STRING;

void setup() { // put your setup code here, to run once: Serial.begin(9600); dht.begin();

//Connect wifi initWiFi(); initTime();

//Create AureIOtHubConnectionString iotHubClientHandle = IoTHubClient_LL_CreateFromConnectionString(connectionString, MQTT_Protocol); if (iotHubClientHandle == NULL) { Serial.println("Failed on IoTHubClient_CreateFromConnectionString."); while (1); } else // added else block {
Serial.println(" IoTHubClient_CreateFromConnectionString Ceated");
}

}

static int messageCount = 1;

void loop() { // put your main code here, to run repeatedly: delay(2000);

if (!messagePending && messageSending) { char messagePayload[MESSAGE_MAX_LEN]; readMessage(messageCount, messagePayload); sendMessage(iotHubClientHandle, messagePayload); messageCount++; delay(interval); } IoTHubClient_LL_DoWork(iotHubClientHandle); delay(10);
}

void initWiFi() { Serial.print("Connecting to "); Serial.println(ssid); WiFi.begin(ssid,password); while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); }

if(WiFi.status() == WL_CONNECTED)
{
 Serial.println("");
 Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}

}

void readMessage(int messageId, char *payload) { // get humidity reading float h = dht.readHumidity(); // get temperature reading in Celsius float t = dht.readTemperature(); // get temperature reading in Fahrenheit //float f = dht.readTemperature(true); // Check if any reads failed and exit early (to try again). if (isnan(h) || isnan(t)) { Serial.println("Failed to read from DHT sensor!"); return; }

StaticJsonBuffer jsonBuffer; JsonObject &root = jsonBuffer.createObject(); root["deviceId"] = DEVICE_ID; root["messageId"] = messageId; // NAN is not the valid json, change it to NULL if (std::isnan(t)) { root["temperatureC"] = NULL; } else { root["temperatureC"] = t;

}

if (std::isnan(h))
{
    root["humidity"] = NULL;
}
else
{
    root["humidity"] = h;
}

root.printTo(payload, MESSAGE_MAX_LEN);

}

void initTime() { time_t epochTime; configTime(0, 0, "pool.ntp.org", "time.nist.gov");

while (true)
{
    epochTime = time(NULL);

    if (epochTime == 0)
    {
        Serial.println("Fetching NTP epoch time failed! Waiting 2 seconds to retry.");
        delay(2000);
    }
    else
    {
        Serial.printf("Fetched NTP epoch time is: %lu.\r\n", epochTime);
        break;
    }
}

}`

the tutorial I followed here http://learniotwithzain.com/2018/10/send-data-from-nodemcu-to-azure-iot-hub/ and the code is here https://github.com/zainuvk/AzureIoTSamples/tree/master/dht_temperature_AzureIotHub_New

here is the compile log https://hastebin.com/ewokeyober.http I am using nodemcu 1.0 board I installed all library from device manager and also followed the steps in this repo instructions

jbobotek commented 4 years ago

You need to include this as well, as listed on this line: https://github.com/Azure/azure-iot-arduino/blob/master/examples/esp8266/iothub_ll_telemetry_sample/iothub_ll_telemetry_sample.ino#L24

mielniczekm commented 4 years ago

Hi, I had the same issue. After including iothubtransportmqtt.h I came to next error: Linking .pio\build\nodemcuv2\firmware.elf c:/users/mmielnic/.platformio/packages/toolchain-xtensa/bin/../lib/gcc/xtensa-lx106-elf/4.8.2/../../../../xtensa-lx106-elf/bin/ld.exe: .pio\build\nodemcuv2\lib1b2\libAzureIoTHub_ID480.a(iothub_client_core_ll.c.o):(.text.initialize_iothub_client+0x6c): undefined reference to IoTHubClient_LL_UploadToBlob_Destroy' c:/users/mmielnic/.platformio/packages/toolchain-xtensa/bin/../lib/gcc/xtensa-lx106-elf/4.8.2/../../../../xtensa-lx106-elf/bin/ld.exe: .pio\build\nodemcuv2\lib1b2\libAzureIoTHub_ID480.a(iothub_client_core_ll.c.o):(.text.initialize_iothub_client+0x78): undefined reference toIoTHubClient_LL_UploadToBlob_Create' .....

collect2.exe: error: ld returned 1 exit status *** [.pio\build\nodemcuv2\firmware.elf] Error 1

It happens only when IoTHubClient_LL_CreateFromConnectionString is in code: static IOTHUB_CLIENT_TRANSPORT_PROVIDER protocol = MQTT_Protocol; iotHubClientHandle = IoTHubClient_LL_CreateFromConnectionString("connectionString", protocol);

but without it I cannt connect to Azure Iot hub.

This is what I'm using: PLATFORM: Espressif 8266 2.5.1 > NodeMCU 1.0 (ESP-12E Module) HARDWARE: ESP8266 80MHz, 80KB RAM, 4MB Flash PACKAGES:

Dependency Graph DHT 1.0.0 AzureIoTHub 1.3.8 AzureIoTUtility 1.3.8.1 ESP8266WiFi 1.0 ArduinoJson 6.15.2 AzureIoTProtocol_MQTT 1.3.8

Any idea what I can do with it?

priyankakrota commented 4 years ago

Where you able to resolve the above issue , I am following the same by @zainuvk https://github.com/zainuvk/AzureIoTSamples/tree/master/dht_temperature_AzureIotHub_New

Error line- iotHubClientHandle = IoTHubClient_LL_CreateFromConnectionString(connectionString, MQTT_Protocol);

Issue - dht_temperature_AzureIotHub_New:45:85: error: 'MQTT_Protocol' was not declared in this scope iotHubClientHandle = IoTHubClient_LL_CreateFromConnectionString(connectionString, MQTT_Protocol);

@mielniczekm @jbobotek @Soumojit28 Please help and also am not able to find -iothubtransportmqtt.h in manage libraries to include.

Is there code to run , using nodemcu esp8266 wifi module and send temperature data to azure iot hub?

jbobotek commented 4 years ago

@mielniczekm You need to add -DDONT_USE_UPLOADTOBLOB to your platform.local.txt as seen here.

jbobotek commented 4 years ago

@priyankakrota Note that that sample you linked is not a Microsoft sample, but one made by a private individual 2 years ago. I advise basing your work on more recently supported samples.

priyankakrota commented 4 years ago

@jbobotek Is there any reference and working sample which you would suggest to send db18620 temperature sensor data via nodemcu esp8266 ethernet wifi shield +Arduino uno to Microsoft azure iot hub.. need help please..

Soumojit28 commented 4 years ago

@priyankakrota +interested, I managed it to work by changing the platform.txt file, but I didnt worked as I expected so I moved to mqtt and nodered, if you found any newer version of the code please share.

priyankakrota commented 4 years ago

@Soumojit28 ..sure,if I find any.. where you able to post sensor data to azure iot hub ..mqtt and node red ? Let me know,if you have any working sample..