Skip to content

Instantly share code, notes, and snippets.

@fxprime
Last active July 22, 2023 08:55
Show Gist options
  • Save fxprime/7e72c337d82aa0fec8f9a9a84997ad8d to your computer and use it in GitHub Desktop.
Save fxprime/7e72c337d82aa0fec8f9a9a84997ad8d to your computer and use it in GitHub Desktop.
ตัวอย่างโค้ดอ่านค่า SHT20 ด้วย ESP32 และ RS485 to TTL พร้อมให้ค่า Heat-index
/* -------------------------------------------------------------------------- */
/* ModuleMore SHT20 (Modbus) Example code */
/* -------------------------------------------------------------------------- */
/* ------------------------------- วิธีการต่อ ------------------------------- */
/**
* ESP32 ---> MAX485 ---> XY-MD02
* 3.3V 3.3V
* GND GND -
* 21 RX
* 22 TX
* 5V +
* A A
* B B
**/
#include <Arduino.h>
#include <SoftwareSerial.h>
#include <ModbusMaster.h>
#define ESP_RXD 21
#define ESP_TXD 22
#define BAUD_RATE 9600
EspSoftwareSerial::UART mySerial;
ModbusMaster node;
float computeHeatIndex(float temperature, float percentHumidity, bool isFahrenheit);
void setup()
{
delay(1000);
Serial.println("start init serial 0");
Serial.begin(9600);
Serial.println("start init software serial");
mySerial.begin(BAUD_RATE, EspSoftwareSerial::SWSERIAL_8N1, ESP_RXD, ESP_TXD, false, 95, 11);
//กำหนด Address ที่ 1 โดยใช้ช่องทางสื่อสารผ่าน mySerial ที่เรากำหนดไว้
node.begin(1, mySerial);
}
void loop()
{
uint8_t result;
uint16_t data[2];
Serial.println("Collect data");
result = node.readInputRegisters(0x0001, 2);
if (result == node.ku8MBSuccess)
{
float temperature = node.getResponseBuffer(0) / 10.0f;
float humidity = node.getResponseBuffer(1) / 10.0f;
float heatindex = computeHeatIndex(temperature, humidity, false);
Serial.print("Humidity: ");
Serial.println(humidity);
Serial.print("Temperature: ");
Serial.println(temperature);
Serial.print("Heat-index: ");
Serial.println(heatindex);
Serial.println();
}
delay(1000);
}
float convertCtoF(float c) { return c * 1.8 + 32; }
float convertFtoC(float f) { return (f - 32) * 0.55555; }
float computeHeatIndex(float temperature, float percentHumidity, bool isFahrenheit)
{
float hi;
if (!isFahrenheit)
temperature = convertCtoF(temperature);
hi = 0.5 * (temperature + 61.0 + ((temperature - 68.0) * 1.2) +
(percentHumidity * 0.094));
if (hi > 79)
{
hi = -42.379 + 2.04901523 * temperature + 10.14333127 * percentHumidity +
-0.22475541 * temperature * percentHumidity +
-0.00683783 * pow(temperature, 2) +
-0.05481717 * pow(percentHumidity, 2) +
0.00122874 * pow(temperature, 2) * percentHumidity +
0.00085282 * temperature * pow(percentHumidity, 2) +
-0.00000199 * pow(temperature, 2) * pow(percentHumidity, 2);
if ((percentHumidity < 13) && (temperature >= 80.0) &&
(temperature <= 112.0))
hi -= ((13.0 - percentHumidity) * 0.25) *
sqrt((17.0 - abs(temperature - 95.0)) * 0.05882);
else if ((percentHumidity > 85.0) && (temperature >= 80.0) &&
(temperature <= 87.0))
hi += ((percentHumidity - 85.0) * 0.1) * ((87.0 - temperature) * 0.2);
}
return isFahrenheit ? hi : convertFtoC(hi);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment