/**
* An Azure IoT example for sending telemetry for 2 temperature and humidity values.
* Use these URLs to get ESP32 board installs:
* https://dl.espressif.com/dl/package_esp32_index.json
* http://arduino.esp8266.com/stable/package_esp8266com_index.json
* Base for this code is SimpleMQTT sketch from the File->Examples->ESP32 Azure IoT Arduino
* The device MAC is deviceId in the JSON event for the IOThub identify
* Libraries needed:
* Adafruit Unified Sensors 1.1.4
* Adafruit DHT sensor library 1.4.0
* Download NTPClient-master.zip from https://github.com/taranais/NTPClient
* Unzip NTPClient-master.zip to Documents->Arduino->libraries and rename the folder to NTPClient
*/
#include <WiFi.h>
#include <Esp32MQTTClient.h>
#include <Adafruit_Sensor.h>
#include <DHT.h>
#include <NTPClient.h>
// Please input the SSID and password of WiFi
const char* ssid = "<your-network-SSID>";
const char* password = "<your-wifi-network-password>";
/*Strings containing Hostname, Device Id & Device Key for 2 IOT devices defined in the Azure IOThub */
//static const char* connectionString = "<connection-string-fromAzure-IOTdevice#1>";
static const char* connectionString = "<connection-string-fromAzure-IOTdevice#2>";
#define DHTPIN 14 // Digital pin connected to the DHT sensor
// Uncomment the type of sensor in use:
//#define DHTTYPE DHT11 // DHT 11
#define DHTTYPE DHT22 // DHT 22 (AM2302)
//#define DHTTYPE DHT21 // DHT 21 (AM2301)
DHT dht(DHTPIN, DHTTYPE); // Object for DHT sensor
WiFiUDP ntpUDP;
NTPClient timeClient(ntpUDP);
static char deviceMAC[23]; // chip MAC keeper
static long int messageId = 0;
String temperature;
String humidity;
// Variables to save date and time
String formattedDate;
//String dayStamp;
//String timeStamp;
static bool hasIoTHub;
void setup() {
delay(3000);
Serial.begin(115200);
Serial.println("Starting connecting WiFi.");
delay(10);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println(" WiFi connected");
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
uint64_t chipid = ESP.getEfuseMac(); // The chip ID is essentially its MAC address(length: 6 bytes).
uint16_t chip = (uint16_t)(chipid >> 32);
snprintf(deviceMAC, 23, "%04X%08X", chip, (uint32_t)chipid); // get chip MAC
Serial.print("Chip MAC: ");
Serial.println(deviceMAC);
dht.begin();
// make DHT22 dummy reads to ensure correct readings later on
dht.readTemperature();
dht.readHumidity();
if (!Esp32MQTTClient_Init((const uint8_t*)connectionString))
{
hasIoTHub = false;
Serial.println("Initializing IoT hub failed.");
}
else hasIoTHub = true;
}
void loop() {
if (hasIoTHub)
{
char buff[256];
String Json_mqtt_msg = String("{\"deviceId\":\"") + deviceMAC + String("\",\"deviceNTP\":\"");
while(!timeClient.update()) {
timeClient.forceUpdate();
}
// The formattedDate comes with the following format:
// 2018-05-28T16:00:13Z
// We need to extract date and time
formattedDate = timeClient.getFormattedDate();
Json_mqtt_msg += formattedDate; // add NTP datetime
/*
// Extract date
int splitT = formattedDate.indexOf("T");
dayStamp = formattedDate.substring(0, splitT);
// Extract time
timeStamp = formattedDate.substring(splitT+1, formattedDate.length()-1);
*/
float humidity = dht.readHumidity(); // Read temperature as Celsius (the default)
float temperature = dht.readTemperature();
// For Fahrenheit reading: float f = dht.readTemperature(true);
// If any reads from dht1 failed , set value to -99.0
if (isnan(humidity)) humidity = -99.0;
if (isnan(temperature)) temperature = -99.0;
// Json_mqtt_msg += String(",\"date\":\"") + dayStamp + String("\",\"time\":\"") + timeStamp;
Json_mqtt_msg += String("\",\"temperature\":") + String((int)temperature) + String(",\"humidity\":") + String((int)humidity)+ String("}");
Serial.println(String("Sending: ") + Json_mqtt_msg);
Json_mqtt_msg.toCharArray(buff, (Json_mqtt_msg.length()+1));
if (Esp32MQTTClient_SendEvent(buff)) { }
else { Serial.println("Failure..."); }
}
else Serial.println("Waiting for MQTT connection");
delay(5000);
}