hi guys
ı am using this code but ı don’t know how can ı add virtual pin this code please help me.
#include <SoftwareSerial.h>
#include <ModbusMaster.h>
SoftwareSerial mySerial(13, 15); // RX, TX
#define MAX485_DE 5
#define MAX485_RE_NEG 4
ModbusMaster node;
void preTransmission()
{
digitalWrite(MAX485_RE_NEG, 1);
digitalWrite(MAX485_DE, 1);
}
void postTransmission()
{
digitalWrite(MAX485_RE_NEG, 0);
digitalWrite(MAX485_DE, 0);
}
void setup()
{
pinMode(MAX485_RE_NEG, OUTPUT);
pinMode(MAX485_DE, OUTPUT);
// Init in receive mode
digitalWrite(MAX485_RE_NEG, 0);
digitalWrite(MAX485_DE, 0);
Serial.println("start init serial 0");
Serial.begin(9600);
while (!Serial) {
Serial.println("loop for init serial 0");
}
Serial.println("start init software serial");
mySerial.begin(9600);
while (!mySerial) {
Serial.println("loop for init software serial");
}
node.begin(1, mySerial);
node.preTransmission(preTransmission);
node.postTransmission(postTransmission);
}
void loop()
{
uint8_t result;
uint16_t data[2];
Serial.println("get data");
result = node.readInputRegisters(1, 2);
if (result == node.ku8MBSuccess)
{
Serial.print("Temp: ");
Serial.println(node.getResponseBuffer(0)/10.0f);
Serial.print("Humi: ");
Serial.println(node.getResponseBuffer(1)/10.0f);
Serial.println();
}
delay(1000);
}
PUYIOT
(อิทธิรงค์ ทองย้อย (ปุ๋ย))
2
This code is not complete. You should study more (Blynk version <= 1.0.1 can use BlynkTimer)
#include <SoftwareSerial.h>
//#define BLYNK_DEBUG
#define BLYNK_PRINT Serial
#include <BlynkSimpleEsp32.h>
BlynkTimer timer;
#include <ModbusMaster.h>
SoftwareSerial mySerial(13, 15); // RX, TX
#define MAX485_DE 5
#define MAX485_RE_NEG 4
ModbusMaster node;
void preTransmission()
{
digitalWrite(MAX485_RE_NEG, 1);
digitalWrite(MAX485_DE, 1);
}
void postTransmission()
{
digitalWrite(MAX485_RE_NEG, 0);
digitalWrite(MAX485_DE, 0);
}
void setup()
{
pinMode(MAX485_RE_NEG, OUTPUT);
pinMode(MAX485_DE, OUTPUT);
// Init in receive mode
digitalWrite(MAX485_RE_NEG, 0);
digitalWrite(MAX485_DE, 0);
Serial.println("start init serial 0");
Serial.begin(9600);
while (!Serial) {
Serial.println("loop for init serial 0");
}
Serial.println("start init software serial");
mySerial.begin(9600);
while (!mySerial) {
Serial.println("loop for init software serial");
}
node.begin(1, mySerial);
node.preTransmission(preTransmission);
node.postTransmission(postTransmission);
timer.setInterval(2000L, ReqadT_H); //Function internetcheck
}
void loop()
{
Blynk.run();
timer.run();
}
void ReqadT_H() {
uint8_t result;
uint16_t data[2];
Serial.println("get data");
result = node.readInputRegisters(1, 2);
if (result == node.ku8MBSuccess)
{
data[0] = node.getResponseBuffer(0) / 10.0f;
Serial.print("Temp: ");
Serial.println(data[0]);
Blynk.virtualWrite(V1, data[0]);
data[1] = node.getResponseBuffer(1) / 10.0f;
Serial.print("Humid: ");
Serial.println(data[1]);
Serial.println();
Blynk.virtualWrite(V2, data[1]);
// Serial.print("Temp: ");
// Serial.println(node.getResponseBuffer(0) / 10.0f);
// Serial.print("Humi: ");
// Serial.println(node.getResponseBuffer(1) / 10.0f);
// Serial.println();
}
}