Created
October 14, 2023 06:56
-
-
Save fxprime/3b632ed022f247789a40d0df08445982 to your computer and use it in GitHub Desktop.
This file contains 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); } | |
void setup() | |
{ | |
// กำหนดขาที่่ต่อกับ DE ให้เป็น Output | |
pinMode(MAX485_DE, OUTPUT); | |
// ตั้งค่าเริ่มต้นให้เป็นโหมดรอรับสัญญาณ | |
digitalWrite(MAX485_DE, 0); | |
Serial.println("start init serial 0"); | |
Serial.begin(9600); | |
//กำหนด baudrate สำหรับสื่อสารกับ am2306 ที่ค่าเริ่มต้นคือ 9600 | |
Serial.println("start init software serial"); | |
mySerial.begin(9600); | |
//กำหนด Address ที่ 1 โดยใช้ช่องทางสื่อสารผ่าน mySerial ที่เรากำหนดไว้ | |
node.begin(1, mySerial); | |
node.preTransmission(preTransmission); | |
node.postTransmission(postTransmission); | |
} | |
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; | |
Serial.print("Humidity: "); | |
Serial.println(humidity); | |
Serial.print("Temperature: "); | |
Serial.println(temperature); | |
Serial.println(); | |
} | |
delay(2000); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
/* -------------------------------------------------------------------------- /
/ ModuleMore AM2306 (Modbus) Example code /
/ -------------------------------------------------------------------------- */
/* ------------------------------- วิธีการต่อ ------------------------------- /
/*
**/
#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 setup()
{
// กำหนดขาที่่ต่อกับ DE ให้เป็น Output
pinMode(MAX485_DE, OUTPUT);
// ตั้งค่าเริ่มต้นให้เป็นโหมดรอรับสัญญาณ
digitalWrite(MAX485_DE, 0);
Serial.println("start init serial 0");
Serial.begin(9600);
//กำหนด baudrate สำหรับสื่อสารกับ am2306 ที่ค่าเริ่มต้นคือ 9600
Serial.println("start init software serial");
mySerial.begin(9600);
//กำหนด Address ที่ 1 โดยใช้ช่องทางสื่อสารผ่าน mySerial ที่เรากำหนดไว้
node.begin(12, mySerial);
node.preTransmission(preTransmission);
node.postTransmission(postTransmission);
}
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;
Serial.print("Humidity: ");
Serial.println(humidity);
Serial.print("Temperature: ");
Serial.println(temperature);
Serial.println();
}else{
Serial.println(result);
}
delay(2000);
}
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);
}
return isFahrenheit ? hi : convertFtoC(hi);
}