-
-
Save j7nn7k/df0b6a76973374596bbe3f399afde781 to your computer and use it in GitHub Desktop.
#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; | |
} |
Jannik,
I wounder if you could help me, I tried to load your code onto Arduino IDE and I'm getting this error code? Any ideas/pointers please?
Arduino: 1.8.8 (Windows 7), Board: "NodeMCU 0.9 (ESP-12 Module), 80 MHz, Flash, Enabled, 4M (no SPIFFS), v2 Lower Memory, Disabled, None, Only Sketch, 115200"Build options changed, rebuilding all
In file included from C:\Users\Sugden-brook\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.5.0-beta2\libraries\ESP8266WiFi\src/WiFiClient.h:25:0,from C:\Users\Sugden-brook\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.5.0-beta2\libraries\ESP8266WiFi\src/ESP8266WiFi.h:39, from C:\Users\Sugden-brook\Documents\Arduino\libraries\AzureIoTUtility\src\adapters\sslClient_arduino.cpp:9:
c:\users\sugden-brook\appdata\local\arduino15\packages\esp8266\tools\xtensa-lx106-elf-gcc\2.5.0-3-20ed2b9\xtensa-lx106-elf\include\c++\4.8.2\bits\random.tcc: In member function 'void std::poisson_distribution<_IntType>::param_type::_M_initialize()':
C:\Users\Sugden-brook\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.5.0-beta2\cores\esp8266/Arduino.h:137:22: error: expected unqualified-id before '(' token
#define round(x) ((x)>=0?(long)((x)+0.5):(long)((x)-0.5))
^
c:\users\sugden-brook\appdata\local\arduino15\packages\esp8266\tools\xtensa-lx106-elf-gcc\2.5.0-3-20ed2b9\xtensa-lx106-elf\include\c++\4.8.2\bits\random.tcc: In member function 'void std::binomial_distribution<_IntType>::param_type::_M_initialize()':
C:\Users\Sugden-brook\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.5.0-beta2\cores\esp8266/Arduino.h:137:22: error: expected unqualified-id before '(' token
#define round(x) ((x)>=0?(long)((x)+0.5):(long)((x)-0.5))
^
C:\Users\Sugden-brook\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.5.0-beta2\cores\esp8266/Arduino.h:137:22: error: expected unqualified-id before '(' token
#define round(x) ((x)>=0?(long)((x)+0.5):(long)((x)-0.5))
^
exit status 1
Error compiling for board NodeMCU 0.9 (ESP-12 Module).This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.
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.
Jannik,
I wounder if you could help me, I tried to load your code onto Arduino IDE and I'm getting this error code? Any ideas/pointers please?
Arduino: 1.8.8 (Windows 7), Board: "NodeMCU 0.9 (ESP-12 Module), 80 MHz, Flash, Enabled, 4M (no SPIFFS), v2 Lower Memory, Disabled, None, Only Sketch, 115200"
Build options changed, rebuilding all
In file included from C:\Users\Sugden-brook\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.5.0-beta2\libraries\ESP8266WiFi\src/WiFiClient.h:25:0,
c:\users\sugden-brook\appdata\local\arduino15\packages\esp8266\tools\xtensa-lx106-elf-gcc\2.5.0-3-20ed2b9\xtensa-lx106-elf\include\c++\4.8.2\bits\random.tcc: In member function 'void std::poisson_distribution<_IntType>::param_type::_M_initialize()':
C:\Users\Sugden-brook\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.5.0-beta2\cores\esp8266/Arduino.h:137:22: error: expected unqualified-id before '(' token
#define round(x) ((x)>=0?(long)((x)+0.5):(long)((x)-0.5))
c:\users\sugden-brook\appdata\local\arduino15\packages\esp8266\tools\xtensa-lx106-elf-gcc\2.5.0-3-20ed2b9\xtensa-lx106-elf\include\c++\4.8.2\bits\random.tcc: In member function 'void std::binomial_distribution<_IntType>::param_type::_M_initialize()':
C:\Users\Sugden-brook\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.5.0-beta2\cores\esp8266/Arduino.h:137:22: error: expected unqualified-id before '(' token
#define round(x) ((x)>=0?(long)((x)+0.5):(long)((x)-0.5))
C:\Users\Sugden-brook\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.5.0-beta2\cores\esp8266/Arduino.h:137:22: error: expected unqualified-id before '(' token
#define round(x) ((x)>=0?(long)((x)+0.5):(long)((x)-0.5))
exit status 1
Error compiling for board NodeMCU 0.9 (ESP-12 Module).
This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.