mikroavr

URM14_RS485

Feb 16th, 2022 (edited)
312
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <ModbusMaster.h>
  2. ModbusMaster node;
  3. static uint8_t pzemSlaveAddr = 0x0C;
  4.  
  5. float voltage,  current, power, energy, freq, pf;
  6. #define pzemSerial Serial2
  7.  
  8. #define TINY_GSM_MODEM_SIM800
  9. #define SerialMon Serial
  10. #define SerialAT Serial1
  11.  
  12. #if !defined(TINY_GSM_RX_BUFFER)
  13. #define TINY_GSM_RX_BUFFER 650
  14. #endif
  15. #define TINY_GSM_DEBUG SerialMon
  16. #define GSM_PIN ""
  17.  
  18. // Your GPRS credentials, if any
  19. const char apn[]      = "Internet";
  20. const char gprsUser[] = "";
  21. const char gprsPass[] = "";
  22.  
  23. // Server details
  24. const char _server[]   = "vsh.pp.ua";
  25. const char resource[] = "/TinyGSM/logo.txt";
  26. const int  port       = 80;
  27.  
  28. #include <TinyGsmClient.h>
  29. #include <ArduinoHttpClient.h>
  30.  
  31. #ifdef DUMP_AT_COMMANDS
  32. #include <StreamDebugger.h>
  33. StreamDebugger debugger(SerialAT, SerialMon);
  34. TinyGsm        modem(debugger);
  35. #else
  36. TinyGsm        modem(SerialAT);
  37. #endif
  38.  
  39. TinyGsmClient client(modem);
  40. HttpClient    http(client, _server, port);
  41.  
  42. #define RXD2 26
  43. #define TXD2 25
  44.  
  45. #define RXD1 17
  46. #define TXD1 16
  47.  
  48. #define PKEY 14
  49.  
  50. #define   TEMP_CPT_SEL_BIT          ((uint16_t)0x01)
  51. #define   TEMP_CPT_ENABLE_BIT       ((uint16_t)0x01 << 1)
  52. #define   MEASURE_MODE_BIT          ((uint16_t)0x01 << 2)
  53. #define   MEASURE_TRIG_BIT          ((uint16_t)0x01 << 3)
  54.  
  55. bool state =  true;
  56. volatile uint16_t cr = 0;
  57.  
  58. void setup() {
  59.  
  60.   //pzemSerial.begin(9600);
  61.   Serial.begin(115200);
  62.   Serial2.begin(19200, SERIAL_8N1, RXD1, TXD1);
  63.  
  64.   SerialAT.begin(9600, SERIAL_8N1, RXD2, TXD2);
  65.  
  66.   SerialMon.println("Initializing modem...");
  67.   modem.restart();
  68.   // modem.init();
  69.  
  70.   String modemInfo = modem.getModemInfo();
  71.   SerialMon.print("Modem Info: ");
  72.   SerialMon.println(modemInfo);
  73.  
  74.   node.begin(pzemSlaveAddr, Serial2);
  75.   cr |= MEASURE_MODE_BIT;//Set bit2 , Set to trigger mode
  76.   cr &= ~(uint16_t)TEMP_CPT_SEL_BIT;//Select internal temperature compensation
  77.   cr &= ~(uint16_t)TEMP_CPT_ENABLE_BIT;//enable temperature compensation
  78.  
  79.   Serial.print("cr: ");
  80.   Serial.println(cr);
  81.   node.writeSingleCoil(cr,state);
  82.   delay(500);
  83. }
  84. void loop() {
  85.  
  86.   uint8_t result;
  87.   Serial.println("tulis");
  88.   cr |= MEASURE_TRIG_BIT;
  89.  
  90.   result = node.writeSingleCoil(cr, state);
  91.   state = !state;
  92.   delay(500);
  93.   //Serial.println("baca");
  94.   result = node.readHoldingRegisters(0, 9); //read the 9 registers of the PZEM-014 / 016
  95.   if (result == node.ku8MBSuccess)
  96.   {
  97.     //Serial.println("sukses modbus");
  98.     Serial.println(node.getResponseBuffer(0));
  99.     Serial.println(node.getResponseBuffer(1));
  100.     Serial.println(node.getResponseBuffer(2));
  101.     Serial.println(node.getResponseBuffer(3));
  102.     Serial.println(node.getResponseBuffer(4));
  103.     Serial.println(node.getResponseBuffer(5));
  104.     Serial.println(node.getResponseBuffer(6));
  105.     Serial.println("--------------------------------");
  106.  
  107.   } else {
  108.     Serial.println("Failed to read modbus");
  109.   }
  110.   delay(2000);
  111. }
  112.  
  113. void print_data() {
  114.   Serial.println();
  115. }
Add Comment
Please, Sign In to add comment