Last active
May 2, 2019 01:40
-
-
Save j7nn7k/df0b6a76973374596bbe3f399afde781 to your computer and use it in GitHub Desktop.
Wifi thermometer powerd by arduino compatible esp8266, DTH11 sensor and Azure IoT Hub
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <ESP8266WiFi.h> | |
#include <WiFiClientSecure.h> | |
#include <WiFiUdp.h> | |
#include <stdlib.h> | |
#include <stdio.h> | |
#include <stdint.h> | |
#include <time.h> | |
#include <sys/time.h> | |
#include <AzureIoTHub.h> | |
#include <AzureIoTProtocol_MQTT.h> | |
#include <ArduinoJson.h> | |
#include <DHT.h> | |
#define DHTTYPE DHT11 | |
#define DHTPIN 13 | |
DHT dht(DHTPIN, DHTTYPE); | |
float humidity, temperature; // Values read from sensor | |
#define IOT_CONFIG_CONNECTION_STRING "GET_THIS_FROM_AZURE_IOT_HUB" | |
#define DEVICE_ID "YOUR_DEVICES_ID" | |
#define WIFI_SSID "YOUR_WIFI_SSID" | |
#define WIFI_PW "YOUR_WIFI_PW" | |
#define MESSAGE_MAX_LEN 256 | |
static bool messagePending = false; | |
static IOTHUB_CLIENT_LL_HANDLE iotHubClientHandle; | |
// Time to sleep (in seconds): | |
const int sleepTimeSec = 10; | |
void setup() | |
{ | |
delay(1000); | |
Serial.begin(115200); | |
connect(); | |
initTime(); | |
iotHubClientHandle = IoTHubClient_LL_CreateFromConnectionString(IOT_CONFIG_CONNECTION_STRING, MQTT_Protocol); | |
if (iotHubClientHandle == NULL) | |
{ | |
Serial.println("Failed on IoTHubClient_CreateFromConnectionString."); | |
while (1); | |
} | |
} | |
void loop() | |
{ | |
if (!messagePending){ | |
readSensorData(); | |
char messagePayload[MESSAGE_MAX_LEN]; | |
composeMessage(messagePayload); | |
queueMessage(iotHubClientHandle, messagePayload); | |
Serial.println("mesage queued - not sent yet"); | |
} | |
IoTHubClient_LL_DoWork(iotHubClientHandle); | |
Serial.println("-"); | |
delay(500); | |
} | |
void connect() | |
{ | |
if (WiFi.status() == WL_CONNECTED) | |
{ | |
return; | |
} | |
// Connect to WiFi network | |
Serial.println(); | |
Serial.println(); | |
Serial.print("Connecting to "); | |
Serial.println(WIFI_SSID); | |
WiFi.begin(WIFI_SSID, WIFI_PW); | |
while (WiFi.status() != WL_CONNECTED) | |
{ | |
delay(500); | |
Serial.print("."); | |
} | |
Serial.println(""); | |
Serial.println("WiFi connected"); | |
} | |
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.print("Fetched NTP epoch time is: "); | |
Serial.println(epochTime); | |
break; | |
} | |
} | |
} | |
void readSensorData() { | |
delay(2000); | |
humidity = dht.readHumidity(); // Read humidity (percent) | |
temperature = dht.readTemperature(); // Read temperature as Celsius | |
} | |
void composeMessage(char *payload) | |
{ | |
StaticJsonBuffer<MESSAGE_MAX_LEN> jsonBuffer; | |
JsonObject &root = jsonBuffer.createObject(); | |
root["deviceId"] = DEVICE_ID; | |
// NAN is not the valid json, change it to NULL | |
if (std::isnan(temperature)) | |
{ | |
root["temperature"] = NULL; | |
} | |
else | |
{ | |
root["temperature"] = temperature; | |
} | |
if (std::isnan(humidity)) | |
{ | |
root["humidity"] = NULL; | |
} | |
else | |
{ | |
root["humidity"] = humidity; | |
} | |
root.printTo(payload, MESSAGE_MAX_LEN); | |
} | |
static void queueMessage(IOTHUB_CLIENT_LL_HANDLE iotHubClientHandle, char *buffer) | |
{ | |
IOTHUB_MESSAGE_HANDLE messageHandle = IoTHubMessage_CreateFromByteArray((const unsigned char *)buffer, strlen(buffer)); | |
if (messageHandle == NULL) | |
{ | |
Serial.println("Unable to create a new IoTHubMessage."); | |
} | |
else | |
{ | |
Serial.printf("Queueing message: %s.\r\n", buffer); | |
if (IoTHubClient_LL_SendEventAsync(iotHubClientHandle, messageHandle, onMessageSentCallback, NULL) != IOTHUB_CLIENT_OK) | |
{ | |
Serial.println("Failed to hand over the message to IoTHubClient."); | |
} | |
else | |
{ | |
messagePending = true; | |
Serial.println("IoTHubClient queued the message for delivery."); | |
} | |
IoTHubMessage_Destroy(messageHandle); | |
} | |
} | |
static void onMessageSentCallback(IOTHUB_CLIENT_CONFIRMATION_RESULT result, void *userContextCallback) | |
{ | |
if (IOTHUB_CLIENT_CONFIRMATION_OK == result) | |
{ | |
Serial.println("Message sent! Going to deep sleep!"); | |
// remember to tie the RST pin to GPIO 16 on the ESP8266 | |
// https://www.losant.com/blog/making-the-esp8266-low-powered-with-deep-sleep | |
ESP.deepSleep(sleepTimeSec * 1000000); | |
delay(5000); | |
} | |
else | |
{ | |
Serial.println("Failed to send message to Azure IoT Hub"); | |
} | |
messagePending = false; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I had this error too. 'Fixed' it by using the 2.4.0 version of the ESP8266. Select this version instead of the 2.5.0 version in the arduino board manager.