Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <ModbusMaster.h>
- ModbusMaster node;
- static uint8_t pzemSlaveAddr = 0x0C;
- float voltage, current, power, energy, freq, pf;
- #define pzemSerial Serial2
- #define TINY_GSM_MODEM_SIM800
- #define SerialMon Serial
- #define SerialAT Serial1
- #if !defined(TINY_GSM_RX_BUFFER)
- #define TINY_GSM_RX_BUFFER 650
- #endif
- #define TINY_GSM_DEBUG SerialMon
- #define GSM_PIN ""
- // Your GPRS credentials, if any
- const char apn[] = "Internet";
- const char gprsUser[] = "";
- const char gprsPass[] = "";
- // Server details
- const char _server[] = "vsh.pp.ua";
- const char resource[] = "/TinyGSM/logo.txt";
- const int port = 80;
- #include <TinyGsmClient.h>
- #include <ArduinoHttpClient.h>
- #ifdef DUMP_AT_COMMANDS
- #include <StreamDebugger.h>
- StreamDebugger debugger(SerialAT, SerialMon);
- TinyGsm modem(debugger);
- #else
- TinyGsm modem(SerialAT);
- #endif
- TinyGsmClient client(modem);
- HttpClient http(client, _server, port);
- #define RXD2 26
- #define TXD2 25
- #define RXD1 17
- #define TXD1 16
- #define PKEY 14
- #define TEMP_CPT_SEL_BIT ((uint16_t)0x01)
- #define TEMP_CPT_ENABLE_BIT ((uint16_t)0x01 << 1)
- #define MEASURE_MODE_BIT ((uint16_t)0x01 << 2)
- #define MEASURE_TRIG_BIT ((uint16_t)0x01 << 3)
- bool state = true;
- volatile uint16_t cr = 0;
- void setup() {
- //pzemSerial.begin(9600);
- Serial.begin(115200);
- Serial2.begin(19200, SERIAL_8N1, RXD1, TXD1);
- SerialAT.begin(9600, SERIAL_8N1, RXD2, TXD2);
- SerialMon.println("Initializing modem...");
- modem.restart();
- // modem.init();
- String modemInfo = modem.getModemInfo();
- SerialMon.print("Modem Info: ");
- SerialMon.println(modemInfo);
- node.begin(pzemSlaveAddr, Serial2);
- cr |= MEASURE_MODE_BIT;//Set bit2 , Set to trigger mode
- cr &= ~(uint16_t)TEMP_CPT_SEL_BIT;//Select internal temperature compensation
- cr &= ~(uint16_t)TEMP_CPT_ENABLE_BIT;//enable temperature compensation
- Serial.print("cr: ");
- Serial.println(cr);
- node.writeSingleCoil(cr,state);
- delay(500);
- }
- void loop() {
- uint8_t result;
- Serial.println("tulis");
- cr |= MEASURE_TRIG_BIT;
- result = node.writeSingleCoil(cr, state);
- state = !state;
- delay(500);
- //Serial.println("baca");
- result = node.readHoldingRegisters(0, 9); //read the 9 registers of the PZEM-014 / 016
- if (result == node.ku8MBSuccess)
- {
- //Serial.println("sukses modbus");
- Serial.println(node.getResponseBuffer(0));
- Serial.println(node.getResponseBuffer(1));
- Serial.println(node.getResponseBuffer(2));
- Serial.println(node.getResponseBuffer(3));
- Serial.println(node.getResponseBuffer(4));
- Serial.println(node.getResponseBuffer(5));
- Serial.println(node.getResponseBuffer(6));
- Serial.println("--------------------------------");
- } else {
- Serial.println("Failed to read modbus");
- }
- delay(2000);
- }
- void print_data() {
- Serial.println();
- }
Add Comment
Please, Sign In to add comment