diff --git a/docs/IMG_3853.jpg b/docs/IMG_3853.jpg new file mode 100644 index 0000000..0dbe254 Binary files /dev/null and b/docs/IMG_3853.jpg differ diff --git a/docs/IMG_3854.jpg b/docs/IMG_3854.jpg new file mode 100644 index 0000000..4b7d6c8 Binary files /dev/null and b/docs/IMG_3854.jpg differ diff --git a/docs/IMG_3855.jpg b/docs/IMG_3855.jpg new file mode 100644 index 0000000..fdceeae Binary files /dev/null and b/docs/IMG_3855.jpg differ diff --git a/docs/modbus01.png b/docs/modbus01.png new file mode 100644 index 0000000..1b278a0 Binary files /dev/null and b/docs/modbus01.png differ diff --git a/sketch/sketch.ino b/sketch/sketch.ino index 177849c..f8f4892 100644 --- a/sketch/sketch.ino +++ b/sketch/sketch.ino @@ -4,6 +4,15 @@ #include +#define RX_PIN 7 +#define TX_PIN 6 +#define RE_PIN 5 +#define DE_PIN 4 +#define LED_RED 3 +#define LED_GREEN 2 +#define LED_BLUE 45 + + /* OTAA para*/ uint8_t devEui[] = { 0x22, 0x32, 0x33, 0x00, 0x00, 0x88, 0x88, 0x02 }; @@ -62,6 +71,7 @@ uint8_t appPort = 2; uint8_t confirmedNbTrials = 4; RS485Class* pRS485_1; +ModbusRTUClientClass* pModbusClient; /* Prepares the payload of the frame */ static void prepareTxFrame( uint8_t port ) @@ -73,11 +83,24 @@ static void prepareTxFrame( uint8_t port ) *for example, if use REGION_CN470, *the max value for different DR can be found in MaxPayloadOfDatarateCN470 refer to DataratesCN470 and BandwidthsCN470 in "RegionCN470.h". */ - appDataSize = 4; - appData[0] = 0x10; - appData[1] = 0x21; - appData[2] = 0x32; - appData[3] = 0x43; + appDataSize = 8; + + Serial.println("modbus operation"); + + long v1 = pModbusClient->inputRegisterRead(7, 0x01); + Serial.print("v1: "); + Serial.println(v1); + memcpy(appData, &v1, 4); + + long v2 = pModbusClient->inputRegisterRead(7, 0x02); + Serial.print("v2: "); + Serial.println(v2); + memcpy(appData+4, &v2, 4); + + //appData[0] = 0x10; + //appData[1] = 0x21; + //appData[2] = 0x32; + //appData[3] = 0x43; } //if true, next uplink will add MOTE_MAC_DEVICE_TIME_REQ @@ -85,10 +108,18 @@ static void prepareTxFrame( uint8_t port ) void setup() { Serial.begin(115200); - Serial1.begin(9600, SERIAL_8N1, 7, 6); - pRS485_1 = new RS485Class(Serial1, 6, 5, 4); - ModbusRTUClient.begin(*pRS485_1, 9600, SERIAL_8N1); + Serial1.begin(9600, SERIAL_8N1, RX_PIN, TX_PIN); + pRS485_1 = new RS485Class(Serial1, TX_PIN, RE_PIN, DE_PIN); + pModbusClient = new ModbusRTUClientClass(*pRS485_1); + pModbusClient->begin(9600, SERIAL_8N1); + pinMode(LED_BLUE, OUTPUT); + digitalWrite(LED_BLUE, LOW); + pinMode(LED_RED, OUTPUT); + digitalWrite(LED_RED, LOW); + pinMode(LED_GREEN, OUTPUT); + digitalWrite(LED_GREEN, LOW); + Mcu.begin(); deviceState = DEVICE_STATE_INIT; } @@ -97,9 +128,12 @@ void loop() { #if 1 + digitalWrite(LED_GREEN, HIGH); + switch( deviceState ) { case DEVICE_STATE_INIT: + digitalWrite(LED_GREEN, LOW); { #if(LORAWAN_DEVEUI_AUTO) LoRaWAN.generateDeveuiByChipID(); @@ -113,12 +147,8 @@ void loop() break; } case DEVICE_STATE_SEND: + digitalWrite(LED_BLUE, HIGH); { - Serial.println("rs485 writing"); - pRS485_1->beginTransmission(); - pRS485_1->write(0xaa); - pRS485_1->endTransmission(); - Serial.println("sending"); prepareTxFrame( appPort ); LoRaWAN.send(); @@ -126,6 +156,7 @@ void loop() break; } case DEVICE_STATE_CYCLE: + digitalWrite(LED_BLUE, LOW); { // Schedule next packet transmission txDutyCycleTime = appTxDutyCycle + randr( -APP_TX_DUTYCYCLE_RND, APP_TX_DUTYCYCLE_RND );