Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <SoftwareSerial.h>
- #define RE 7
- #define DE 6
- const uint32_t TIMEOUT = 500UL;
- //const byte code[]= {0x01, 0x03, 0x00, 0x1e, 0x00, 0x03, 0x65, 0xCD};
- //const byte nitro[] = {0x01, 0x03, 0x00, 0x1e, 0x00, 0x01, 0xe4, 0x0c};
- //const byte phos[] = {0x01, 0x03, 0x00, 0x1f, 0x00, 0x01, 0xb5, 0xcc};
- //const byte pota[] = {0x01, 0x03, 0x00, 0x20, 0x00, 0x01, 0x85, 0xc0};
- //const byte code[]= {0x01, 0x03, 0x00, 0x00, 0x00, 0x04, 0x44, 0x09};
- const byte moist[] = {0x01, 0x03, 0x08, 0x02, 0x92, 0x57, 0xB6};
- const byte temp[] = {0x01, 0x03, 0x08, 0xFF, 0x9B, 0x57, 0xB6};
- const byte EC[] = {0x01, 0x03, 0x08, 0x03, 0xE8, 0x57, 0xB6};
- const byte PH[] = {0x01, 0x03, 0x08, 0x00, 0x38, 0x57, 0xB6};
- byte values[11];
- SoftwareSerial mod(8, 9); // Rx pin, Tx pin
- void setup() {
- Serial.begin(9600);
- mod.begin(9600);
- pinMode(RE, OUTPUT);
- pinMode(DE, OUTPUT);
- delay(500);
- }
- void loop() {
- byte val1, val2, val3, val4;
- Serial.println("Moisture: ");
- val1 = moisture();
- Serial.println(val1);
- Serial.println(" %");
- delay(1000);
- Serial.println("Temperature: ");
- val2 = temperature();
- Serial.println(val2);
- Serial.println(" *C");
- delay(1000);
- Serial.println("Conductivity: ");
- val3 = conductivity();
- delay(1000);
- Serial.println(val3);
- Serial.println(" us/cm");
- Serial.println("Ph: ");
- val4 = ph();
- delay(1000);
- Serial.println(val4);
- Serial.println(" ph");
- delay(5000);
- }
- byte moisture() {
- uint32_t startTime = 0;
- uint8_t byteCount = 0;
- digitalWrite(DE, HIGH);
- digitalWrite(RE, HIGH);
- delay(10);
- mod.write(moist, sizeof(moist));
- mod.flush();
- digitalWrite(DE, LOW);
- digitalWrite(RE, LOW);
- startTime = millis();
- while ( millis() - startTime <= TIMEOUT ) {
- if (mod.available() && byteCount<sizeof(values) ) {
- values[byteCount++] = mod.read();
- printHexByte(values[byteCount-1]);
- }
- }
- Serial.println();
- return values[4];
- }
- byte temperature() {
- uint32_t startTime = 0;
- uint8_t byteCount = 0;
- digitalWrite(DE, HIGH);
- digitalWrite(RE, HIGH);
- delay(10);
- mod.write(temp, sizeof(temp));
- mod.flush();
- digitalWrite(DE, LOW);
- digitalWrite(RE, LOW);
- startTime = millis();
- while ( millis() - startTime <= TIMEOUT ) {
- if (mod.available() && byteCount<sizeof(values) ) {
- values[byteCount++] = mod.read();
- printHexByte(values[byteCount-1]);
- }
- }
- Serial.println();
- return values[4];
- }
- byte conductivity() {
- uint32_t startTime = 0;
- uint8_t byteCount = 0;
- digitalWrite(DE, HIGH);
- digitalWrite(RE, HIGH);
- delay(10);
- mod.write(EC, sizeof(EC));
- mod.flush();
- digitalWrite(DE, LOW);
- digitalWrite(RE, LOW);
- startTime = millis();
- while ( millis() - startTime <= TIMEOUT ) {
- if (mod.available() && byteCount<sizeof(values) ) {
- values[byteCount++] = mod.read();
- printHexByte(values[byteCount-1]);
- }
- }
- Serial.println();
- return values[4];
- }
- byte ph() {
- uint32_t startTime = 0;
- uint8_t byteCount = 0;
- digitalWrite(DE, HIGH);
- digitalWrite(RE, HIGH);
- delay(10);
- mod.write(PH, sizeof(PH));
- mod.flush();
- digitalWrite(DE, LOW);
- digitalWrite(RE, LOW);
- startTime = millis();
- while ( millis() - startTime <= TIMEOUT ) {
- if (mod.available() && byteCount<sizeof(values) ) {
- values[byteCount++] = mod.read();
- printHexByte(values[byteCount-1]);
- }
- }
- Serial.println();
- return values[4];
- }
- void printHexByte(byte b)
- {
- Serial.print((b >> 4) & 0xF, HEX);
- Serial.print(b & 0xF, HEX);
- Serial.print(' ');
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement