// Create the object key to match DB filed name from topic name(studio/temp1)
msg.payload["temp"] = msg.payload["studio/temp1"];
// Delete the unwanted studio/temp1
delete msg.payload["studio/temp1"];
// Update temp from string to float
msg.payload["temp"] = parseFloat(msg.payload["temp"]);
msg.payload["humd"] = msg.payload["studio/humd1"];
delete msg.payload["studio/humd1"];
msg.payload["humd"] = parseFloat(msg.payload["humd"]);
//remove unused topic
delete msg.topic;
//Create a temporary array
var data = [];
data.push(msg.payload);
//Add object to DB Tag(index field)
data.push({"location":"studio"});
//msg.payload.push({"location":"studio"});
//Copy temporary array to msg.payload
msg.payload = data;
return msg;
Format code for reading historical InfluxDB data
//Source from: https://discourse.nodered.org/t/display-2-extracts-from-influxdb-in-an-ui-chart/29665
var series = ["Temperature °C", "Humdity %"];
var labels = ["temp", "Humdity"];
var data0 = "[";
var thetime0;
for (var i=0; i < msg.payload.results[0].series[0].values.length; i++) {
thetime0 = (msg.payload.results[0].series[0].values[i][0]); // Some manipulation of the time may be required
thetime0 = Date.parse(thetime0);
data0 += '{ "x":' + thetime0 + ', "y":' + (msg.payload.results[0].series[0].values[i][1]) + '}';
if (i < (msg.payload.results[0].series[0].values.length - 1)) {
data0 += ","
} else {
data0 += "]"
}
}
var data1 = "[";
var thetime1;
for (var j=0; j < msg.payload.results[0].series[0].values.length; j++) {
thetime1 = (msg.payload.results[0].series[0].values[j][0]); // Some manipulation of the time may be required
thetime1 = Date.parse(thetime1);
data1 += '{ "x":' + thetime1 + ', "y":' + (msg.payload.results[0].series[0].values[j][2]) + '}';
if (j < (msg.payload.results[0].series[0].values.length - 1)) {
data1 += ","
} else {
data1 += "]"
}
}
var data = [data0, data1];
var jsondata = [JSON.parse(data0), JSON.parse(data1)];
msg.payload = [{"series": series, "data": jsondata, "labels": labels}];
msg.dataPoints = data;
return msg;
//---- Setup update frequency -----//
const long TEMP_updateInterval = 10000; // How long to change temp and update, 10000 = 10 sec
unsigned long TEMP_currentMillis = 0;
unsigned long TEMP_previousMillis = 0; // store last time temp update
#include "DHT.h"
#define DHTPIN 23 //ESP32 pin 23
#define DHTTYPE DHT11
DHT dht(DHTPIN, DHTTYPE); // Initialize DHT sensor
/*
SimpleMQTTClient.ino
The purpose of this exemple is to illustrate a simple handling of MQTT and Wifi connection.
Once it connects successfully to a Wifi network and a MQTT broker, it subscribe to a topic and send a message to it.
It will also send a message delayed 5 seconds later.
*/
#include "EspMQTTClient.h"
EspMQTTClient client(
"Your_WiFi_AP", // Wi-Fi AP name
"Wi-FI_Password", // Wi-Fi Password
"192.168.0.119", // MQTT Broker server ip
"", // Broker user name; Can be omitted if not needed
"", // Broker password; Can be omitted if not needed
"ESP32_Client", // Client name that uniquely identify your device
1883 // The MQTT port, default to 1883. this line can be omitted
);
void setup()
{
Serial.begin(115200);
//Start DH11 sensor
dht.begin();
// Optionnal functionnalities of EspMQTTClient :
client.enableDebuggingMessages(); // Enable debugging messages sent to serial output
//client.enableHTTPWebUpdater(); // Enable the web updater. User and password default to values of MQTTUsername and MQTTPassword. These can be overrited with enableHTTPWebUpdater("user", "password").
//client.enableLastWillMessage("TestClient/lastwill", "I am going offline"); // You can activate the retain flag by setting the third parameter to true
}
// This function is called once everything is connected (Wifi and MQTT)
// WARNING : YOU MUST IMPLEMENT IT IF YOU USE EspMQTTClient
void onConnectionEstablished()
{
// Subscribe to "mytopic/test" and display received message to Serial
client.subscribe("message/hello", [](const String &payload)
{ Serial.println(payload); });
// Subscribe to "mytopic/wildcardtest/#" and display received message to Serial
//client.subscribe("mytopic/wildcardtest/#", [](const String &topic, const String &payload)
// { Serial.println("(From wildcard) topic: " + topic + ", payload: " + payload); });
// Publish a message to "mytopic/test"
client.publish("studio/temp1", "Let's go!"); // You can activate the retain flag by setting the third parameter to true
// Execute delayed instructions
//client.executeDelayed(5 * 1000, []()
// { client.publish("mytopic/wildcardtest/test123", "This is a message sent 5 seconds later"); });
}
void loop()
{
TEMP_currentMillis = millis();
if(TEMP_currentMillis - TEMP_previousMillis >= TEMP_updateInterval){
TEMP_previousMillis = TEMP_currentMillis;
// Reading temperature or humidity takes about 250 milliseconds!
// Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
float humidity = dht.readHumidity();
float temperature = dht.readTemperature();
// Read temperature as Fahrenheit (isFahrenheit = true)
//float f = dht.readTemperature(true);
// Check if any reads failed and exit early (to try again).
if (isnan(humidity) || isnan(temperature))
{
Serial.println(F("Failed to read from DHT sensor!"));
return;
}
String tmp = "Current Temperature: ";
String hud = "Current Humidity: ";
Serial.print(tmp);
Serial.println(String(temperature));
Serial.print(hud);
Serial.println(String(humidity));
//Publish
client.publish("studio/temp1", String(temperature));
client.publish("studio/humd1", String(humidity));
// Here is how to subscribe
//client.subscribe("message/hello", [](const String &payload) { Serial.println(payload); });
}
client.loop();
}
*** 影片中忘了說明, 在 VS Code 裡, 按下 F1 到 Arduino Library Manager 裡,
搜尋 EspMQTTClient V.11.1版本, 並按下安裝 , 程式才能正確執行喔~~ ***
In order to use the newly purchased 0.96" OLED SD1306 display, I studied the u8g2 library and made this video to share with you on how to use U8g2 library with monochrome display. In the video, I also included a section on how to use the free 5000+ traditional Chinese font created by Mr. Yang. The file is free for anyone to use. Really appreciated what he did for Arduino community!
For those who do not have time to watch the entire video , here is the video timeline.
Video Timeline:
00:22 Install u8g2
01:04 Open PrintUTF8 example
02:00 Define display used
02:23 Explain display naming rules
04:13 Uncomment the line selected
04:33 Explain the code
04:42 enableUTF8Print() for symbol and Chinese
05:14 u8g2 supported font list06:23 display location setCursor(X,Y)
07:05 Upload the sketch & show characters
07:23 Can't overwrite characters!
08:08 Char overwrite solution
08:37 OledOverlayPrint() function
09:06 Use OledOverlayPrint() in code
10:08 Use Open Iconic Fonts
11:29 Display cloud symbol
11:52 \u with number to show cloud
13:02 Change fonts for different lines
14:34 Download 5000+ Chinese font
15:11 Install 5000+Chinese font
15:39 Test missing fonts again
16:42 Reminder of memory usage
17:45 End of tutorial