Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <ModbusMaster.h>
- ModbusMaster node;
- static uint8_t pzemSlaveAddr = 1;
- float voltage, current, power, energy, freq, pf;
- #define pzemSerial Serial2
- #define TINY_GSM_MODEM_SIM800 // Modem is SIM800
- #include <TinyGsmClient.h>
- // Your GPRS credentials (leave empty, if not needed)
- const char apn[] = "Internet";
- const char user[] = "";
- const char Pass[] = "";
- // The server variable can be just a domain name or it can have a subdomain. It depends on the service you are using
- const char server[] = "thingspeak.com"; // domain name: example.com, maker.ifttt.com, etc
- const char resource[] = "https://api.thingspeak.com/update?api_key=QPH1MMAE2486DIG2";
- const int port = 80; // server port number
- String apiKey = "QPH1MMAE2486DIG2";
- // Set serial for AT commands (to SIM800 module)
- #define SerialAT Serial1
- TinyGsm modem(SerialAT);
- // TinyGSM Client for Internet connection
- TinyGsmClient client(modem, 0);
- #define MODEM_TX 25
- #define MODEM_RX 26
- // Set serial for PZEM
- #define RXD1 16
- #define TXD1 17
- #define PKEY 14
- //Untuk watchdog external
- #define wdtPin 15
- #define ledPin 19
- bool wdtState = 0;
- unsigned cur_time_push, old_time_push;
- unsigned cur_time_wdt, old_time_wdt;
- unsigned cur_time_rst, old_time_rst;
- /*
- RegAddr Description Resolution
- 0x0000 Voltage value 1LSB correspond to 0.1V
- 0x0001 Current value low 16 bits 1LSB correspond to 0.001A
- 0x0002 Current value high 16 bits
- 0x0003 Power value low 16 bits 1LSB correspond to 0.1W
- 0x0004 Power value high 16 bits
- 0x0005 Energy value low 16 bits 1LSB correspond to 1Wh
- 0x0006 Energy value high 16 bits
- 0x0007 Frequency value 1LSB correspond to 0.1Hz
- 0x0008 Power factor value 1LSB correspond to 0.01
- 0x0009 Alarm status 0xFFFF is alarm,0x0000is not alarm
- */
- void setup() {
- pinMode(wdtPin, OUTPUT);
- pinMode(ledPin, OUTPUT);
- pzemSerial.begin(9600);
- Serial.begin(115200);
- // Set GSM module baud rate and UART pins
- Serial2.begin(9600, SERIAL_8N1, RXD1, TXD1);//pzem 16
- SerialAT.begin(9600, SERIAL_8N1, MODEM_RX, MODEM_TX);//gsm
- node.begin(pzemSlaveAddr, pzemSerial);
- Serial.println("Initializing modem...");
- modem.restart();
- Serial.print("Connecting to APN: ");
- Serial.print(apn);
- if (!modem.gprsConnect(apn, user, Pass)) {
- Serial.println(" fail");
- }
- else {
- Serial.println("OK");
- }
- }
- void loop() {
- cur_time_wdt = millis();
- if (cur_time_wdt - old_time_wdt > 250) {
- wdtSeed();
- old_time_wdt = millis();
- }
- cur_time_rst = millis() / 1000;
- if (cur_time_rst - old_time_rst >= 3600) {
- ESP.restart();
- }
- cur_time_push = millis();
- if (cur_time_push - old_time_push > 1500) {
- pushServer();
- old_time_push = millis();
- }
- }
- void pushServer() {
- uint8_t result;
- result = node.readInputRegisters(0, 9); //read the 9 registers of the PZEM-014 / 016
- if (result == node.ku8MBSuccess)
- {
- Serial.println("sukses modbus");
- voltage = node.getResponseBuffer(0) / 10.0;
- uint32_t tempdouble = 0x00000000;
- tempdouble = node.getResponseBuffer(1); //LowByte
- tempdouble |= node.getResponseBuffer(2) << 8; //highByte
- current = tempdouble / 1000.0;
- tempdouble |= node.getResponseBuffer(3); //LowByte
- tempdouble |= node.getResponseBuffer(4) << 8; //highByte
- power = tempdouble / 10.0;
- tempdouble = node.getResponseBuffer(5); //LowByte
- tempdouble |= node.getResponseBuffer(6) << 8; //highByte
- energy = tempdouble;
- tempdouble = node.getResponseBuffer(7);
- freq = tempdouble / 10.0;
- tempdouble = node.getResponseBuffer(8);
- pf = tempdouble / 10.0;
- //SEND TO THINKSPEAK
- Serial.print("Connecting to ");
- Serial.print(server);
- if (!client.connect(server, port)) {
- Serial.println(" fail");
- }
- else {
- Serial.println(" OK");
- // Making an HTTP POST request
- Serial.println("Performing HTTP POST request...");
- // Prepare your HTTP POST request data (Temperature in Celsius degrees)
- Serial.println("Performing HTTP POST request...");
- // Prepare your HTTP POST request data (Temperature in Celsius degrees)
- String httpRequestData = "api_key=" + apiKey + "&field1=" + String(voltage) + "&field2=" + String(current) + "&field3=" + String(freq) + "&field4=" + String(power) + "&field5=" + String(energy) + "&field6=" + String(pf) + "";
- client.print(String("POST ") + resource + " HTTP/1.1\r\n");
- client.print(String("Host: ") + server + "\r\n");
- client.println("Connection: close");
- client.println("Content-Type: application/x-www-form-urlencoded");
- client.print("Content-Length: ");
- client.println(httpRequestData.length());
- client.println();
- client.println(httpRequestData);
- unsigned long timeout = millis();
- while (client.connected() && millis() - timeout < 10000L) {
- // Print available data (HTTP response from server)
- while (client.available()) {
- char c = client.read();
- Serial.print(c);
- timeout = millis();
- }
- }
- }
- Serial.println();
- // Close client and disconnect
- client.stop();
- // Serial.println(F("Server disconnected"));
- // modem.gprsDisconnect();
- // Serial.println(F("GPRS disconnected"));
- }
- }
- void wdtSeed() {
- wdtState = !wdtState;
- digitalWrite(wdtPin, wdtState);
- digitalWrite(ledPin, wdtState);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement