Skip to content

Instantly share code, notes, and snippets.

@fxprime
Last active November 21, 2024 10:34
Show Gist options
  • Save fxprime/21b98b22e4ca53c978993b5c2b74ac24 to your computer and use it in GitHub Desktop.
Save fxprime/21b98b22e4ca53c978993b5c2b74ac24 to your computer and use it in GitHub Desktop.
/* -------------------------------------------------------------------------- */
/* ModuleMore AM2306 (Modbus) Example code */
/* -------------------------------------------------------------------------- */
/* ------------------------------- วิธีการต่อ ------------------------------- */
/**
* ESP32 ---> MAX485 ---> AM2306
* 3.3V VCC
* 5V RED
* GND GND BLACK
* - RE (ต่อกับDE)
* 4 DE
* 18 RO(RX)
* 19 DI(TX)
* A YELLOW
* B WHITE
**/
#include <Arduino.h>
#include <SoftwareSerial.h>
#include <ModbusMaster.h>
EspSoftwareSerial::UART mySerial;
#define MAX485_DE 4
ModbusMaster node;
void preTransmission() { digitalWrite(MAX485_DE, 1); }
void postTransmission() { digitalWrite(MAX485_DE, 0); }
float computeHeatIndex(float temperature, float percentHumidity, bool isFahrenheit);
void setup()
{
Serial.begin(115200);
while(!Serial);
Serial.println("Hi!");
// กำหนดขาที่่ต่อกับ DE ให้เป็น Output
pinMode(MAX485_DE, OUTPUT);
// ตั้งค่าเริ่มต้นให้เป็นโหมดรอรับสัญญาณ
digitalWrite(MAX485_DE, 0);
Serial.println("start init serial 0");
//กำหนด baudrate สำหรับสื่อสารกับ am2306 ที่ค่าเริ่มต้นคือ 9600
Serial.println("start init software serial");
mySerial.begin(9600, EspSoftwareSerial::SWSERIAL_8N1, 18, 19, false, 95, 11);
//กำหนด Address ที่ 1 โดยใช้ช่องทางสื่อสารผ่าน mySerial ที่เรากำหนดไว้
node.begin(1, mySerial);
node.preTransmission(preTransmission);
node.postTransmission(postTransmission);
Serial.println("start++");
}
void loop()
{
uint8_t result;
uint16_t data[2];
Serial.printf("Collect data on id %d\n", 1);
result = node.readHoldingRegisters(0x00, 2);
if (result == node.ku8MBSuccess)
{
float humidity = node.getResponseBuffer(0) / 10.0f;
float temperature = 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();
}else{
Serial.println(" - failed");
}
delay(50);
}
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