Last active
October 22, 2023 07:10
-
-
Save fxprime/6e0657a0de50abf304af34be8377057c to your computer and use it in GitHub Desktop.
This file contains hidden or 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
/* -------------------------------------------------------------------------- */ | |
/* ModuleMore AM2306 (Modbus) Example code */ | |
/* -------------------------------------------------------------------------- */ | |
/* ------------------------------- วิธีการต่อ ------------------------------- */ | |
/** | |
* ESP8266 ---> MAX485 ---> AM2306 | |
* 5V 5V RED | |
* GND GND BLACK | |
* - RE (ต่อกับDE) | |
* D2 DE | |
* D5 RO(RX) | |
* D4 DI(TX) | |
* A YELLOW | |
* B WHITE | |
**/ | |
#include <Arduino.h> | |
#include <SoftwareSerial.h> | |
#include <ModbusMaster.h> | |
SoftwareSerial mySerial(D5, D4); // RX, TX | |
#define MAX485_DE D2 | |
ModbusMaster node; | |
void preTransmission() { digitalWrite(MAX485_DE, 1); } | |
void postTransmission() { digitalWrite(MAX485_DE, 0); } | |
float computeHeatIndex(float temperature, float percentHumidity, bool isFahrenheit); | |
void setAddress(uint16_t address) | |
{ | |
for(int i=1;i<247;i++) { | |
Serial.print("Trying to set address to "); | |
Serial.print(address); | |
Serial.print(" Using address "); | |
Serial.print(i); | |
node.begin(i, mySerial); | |
node.setTransmitBuffer(0, address); | |
uint8_t result = node.writeMultipleRegisters(0x16, 1); | |
if(node.ku8MBSuccess==result) { | |
Serial.println("========================"); | |
Serial.print("Success please note now this device address = "); | |
Serial.println(address); | |
Serial.println("Collect data in 5sec\n========================"); | |
delay(5000); | |
return; | |
} | |
Serial.println(" -failed"); | |
delay(200); | |
} | |
} | |
void setup() { | |
delay(5000); | |
// กำหนดขาที่่ต่อกับ DE ให้เป็น Output | |
pinMode(MAX485_DE, OUTPUT); | |
// ตั้งค่าเริ่มต้นให้เป็นโหมดรอรับสัญญาณ | |
digitalWrite(MAX485_DE, 0); | |
Serial.begin(115200); | |
Serial.println("start init serial 0"); | |
//กำหนด baudrate สำหรับสื่อสารกับ am2306 ที่ค่าเริ่มต้นคือ 9600 | |
Serial.println("start init software serial"); | |
mySerial.begin(9600); | |
node.preTransmission(preTransmission); | |
node.postTransmission(postTransmission); | |
node.begin(1, mySerial); | |
// setAddress(1); | |
} | |
void loop() { | |
uint8_t result; | |
uint16_t data[2]; | |
Serial.println("Collect data"); | |
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(); | |
} | |
delay(500); | |
} | |
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