Advertisement
mikroavr

Thinkspeak

May 21st, 2024 (edited)
909
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 = 1;
  4.  
  5. float voltage,  current, power, energy, freq, pf;
  6. #define pzemSerial Serial2
  7.  
  8.  
  9. #define TINY_GSM_MODEM_SIM800      // Modem is SIM800
  10. #include <TinyGsmClient.h>
  11.  
  12.  
  13. // Your GPRS credentials (leave empty, if not needed)
  14. const char apn[]  = "Internet";
  15. const char user[] = "";
  16. const char Pass[] = "";
  17.  
  18. // The server variable can be just a domain name or it can have a subdomain. It depends on the service you are using
  19. const char server[] = "thingspeak.com"; // domain name: example.com, maker.ifttt.com, etc
  20. const char resource[] = "https://api.thingspeak.com/update?api_key=QPH1MMAE2486DIG2";
  21. const int  port = 80;                   // server port number
  22. String apiKey = "QPH1MMAE2486DIG2";
  23.  
  24. // Set serial for AT commands (to SIM800 module)
  25. #define SerialAT Serial1
  26. TinyGsm modem(SerialAT);
  27.  
  28. // TinyGSM Client for Internet connection
  29. TinyGsmClient client(modem, 0);
  30.  
  31. #define MODEM_TX             25
  32. #define MODEM_RX             26
  33.  
  34. // Set serial for PZEM
  35. #define RXD1 16
  36. #define TXD1 17
  37. #define PKEY 14
  38.  
  39. //Untuk watchdog external
  40. #define  wdtPin  15
  41. #define  ledPin  19
  42. bool wdtState = 0;
  43. unsigned cur_time_push, old_time_push;
  44. unsigned cur_time_wdt, old_time_wdt;
  45. unsigned cur_time_rst, old_time_rst;
  46. /*
  47.   RegAddr Description                 Resolution
  48.   0x0000  Voltage value               1LSB correspond to 0.1V
  49.   0x0001  Current value low 16 bits   1LSB correspond to 0.001A
  50.   0x0002  Current value high 16 bits
  51.   0x0003  Power value low 16 bits     1LSB correspond to 0.1W
  52.   0x0004  Power value high 16 bits
  53.   0x0005  Energy value low 16 bits    1LSB correspond to 1Wh
  54.   0x0006  Energy value high 16 bits
  55.   0x0007  Frequency value             1LSB correspond to 0.1Hz
  56.   0x0008  Power factor value          1LSB correspond to 0.01
  57.   0x0009  Alarm status  0xFFFF is alarm,0x0000is not alarm
  58. */
  59.  
  60. void setup() {
  61.   pinMode(wdtPin, OUTPUT);
  62.   pinMode(ledPin, OUTPUT);
  63.   pzemSerial.begin(9600);
  64.   Serial.begin(115200);
  65.   // Set GSM module baud rate and UART pins
  66.   Serial2.begin(9600, SERIAL_8N1, RXD1, TXD1);//pzem 16
  67.   SerialAT.begin(9600, SERIAL_8N1, MODEM_RX, MODEM_TX);//gsm
  68.   node.begin(pzemSlaveAddr, pzemSerial);
  69.   Serial.println("Initializing modem...");
  70.   modem.restart();
  71.   Serial.print("Connecting to APN: ");
  72.   Serial.print(apn);
  73.   if (!modem.gprsConnect(apn, user, Pass)) {
  74.     Serial.println(" fail");
  75.   }
  76.   else {
  77.     Serial.println("OK");
  78.   }
  79. }
  80.  
  81. void loop() {
  82.   cur_time_wdt = millis();
  83.   if (cur_time_wdt - old_time_wdt > 250) {
  84.     wdtSeed();
  85.     old_time_wdt = millis();
  86.   }
  87.  
  88.   cur_time_rst = millis() / 1000;
  89.   if (cur_time_rst - old_time_rst >= 3600) {
  90.     ESP.restart();
  91.   }
  92.  
  93.   cur_time_push = millis();
  94.   if (cur_time_push - old_time_push > 1500) {
  95.     pushServer();
  96.     old_time_push = millis();
  97.   }
  98.  
  99. }
  100.  
  101. void pushServer() {
  102.   uint8_t result;
  103.   result = node.readInputRegisters(0, 9); //read the 9 registers of the PZEM-014 / 016
  104.   if (result == node.ku8MBSuccess)
  105.   {
  106.     Serial.println("sukses modbus");
  107.     voltage = node.getResponseBuffer(0) / 10.0;
  108.  
  109.     uint32_t tempdouble = 0x00000000;
  110.  
  111.     tempdouble = node.getResponseBuffer(1);       //LowByte
  112.     tempdouble |= node.getResponseBuffer(2) << 8;  //highByte
  113.     current = tempdouble / 1000.0;
  114.  
  115.     tempdouble |= node.getResponseBuffer(3);       //LowByte
  116.     tempdouble |= node.getResponseBuffer(4) << 8;  //highByte
  117.     power = tempdouble / 10.0;
  118.  
  119.     tempdouble = node.getResponseBuffer(5);       //LowByte
  120.     tempdouble |= node.getResponseBuffer(6) << 8;  //highByte
  121.     energy = tempdouble;
  122.  
  123.     tempdouble = node.getResponseBuffer(7);
  124.     freq = tempdouble / 10.0;
  125.  
  126.     tempdouble = node.getResponseBuffer(8);
  127.     pf = tempdouble / 10.0;
  128.  
  129.     //SEND TO THINKSPEAK
  130.  
  131.     Serial.print("Connecting to ");
  132.     Serial.print(server);
  133.     if (!client.connect(server, port)) {
  134.       Serial.println(" fail");
  135.     }
  136.     else {
  137.       Serial.println(" OK");
  138.  
  139.       // Making an HTTP POST request
  140.       Serial.println("Performing HTTP POST request...");
  141.       // Prepare your HTTP POST request data (Temperature in Celsius degrees)
  142.       Serial.println("Performing HTTP POST request...");
  143.       // Prepare your HTTP POST request data (Temperature in Celsius degrees)
  144.  
  145.  
  146.       String httpRequestData = "api_key=" + apiKey + "&field1=" + String(voltage) + "&field2=" + String(current) + "&field3=" + String(freq) + "&field4=" + String(power) + "&field5=" + String(energy) + "&field6=" + String(pf) + "";
  147.  
  148.       client.print(String("POST ") + resource + " HTTP/1.1\r\n");
  149.       client.print(String("Host: ") + server + "\r\n");
  150.       client.println("Connection: close");
  151.       client.println("Content-Type: application/x-www-form-urlencoded");
  152.       client.print("Content-Length: ");
  153.       client.println(httpRequestData.length());
  154.       client.println();
  155.       client.println(httpRequestData);
  156.  
  157.  
  158.       unsigned long timeout = millis();
  159.       while (client.connected() && millis() - timeout < 10000L) {
  160.         // Print available data (HTTP response from server)
  161.         while (client.available()) {
  162.           char c = client.read();
  163.           Serial.print(c);
  164.           timeout = millis();
  165.         }
  166.       }
  167.     }
  168.     Serial.println();
  169.  
  170.     // Close client and disconnect
  171.     client.stop();
  172.     //      Serial.println(F("Server disconnected"));
  173.     //      modem.gprsDisconnect();
  174.     //      Serial.println(F("GPRS disconnected"));
  175.   }
  176. }
  177.  
  178. void wdtSeed() {
  179.   wdtState = !wdtState;
  180.   digitalWrite(wdtPin, wdtState);
  181.   digitalWrite(ledPin, wdtState);
  182. }
  183.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement