Please somebody guide me to merge these two codes for PZEM004T and DHT 22 to work on single ESP8266.
Code for PZEM
#include <ArduinoOTA.h>
#include <BlynkSimpleEsp8266.h>
#include <SimpleTimer.h>
#include <ModbusMaster.h>
#include <ESP8266WiFi.h>
#include "settingsPZEM.h"
#include <SoftwareSerial.h> // ( NODEMCU ESP8266 )
SoftwareSerial pzem(D5,D6); // (RX,TX) connect to TX,RX of PZEM for NodeMCU
//SoftwareSerial pzem(D7,D8); // (RX,TX) connect to TX,RX of PZEM
#include <ModbusMaster.h>
ModbusMaster node;
SimpleTimer timer;
//WiFi data
char ssid[] = "Put your WiFi SSID here"; //WiFi Credential
char pass[] = "Put your WiFi password here"; //WiFi Password
char server[] = "Put your Blynk local server IP address here"; //Blynk local server IP address
int port = 8080; //Blynk local port
#define USE_LOCAL_SERVER //Use local Blynk Server - comment-out if use Blynk hosted cloud service
#define AUTH "put your Blynk App Authorization code here" //PZEM-004v3 Auth code for Blynk Local Server
int timerTask1;
double U_PR, I_PR, P_PR, PPR, PR_F, PR_PF, PR_alarm;
uint8_t result; uint16_t data[6];
void setup(){
Serial.begin(115200); Serial.println("Start serial"); pzem.begin(9600); Serial.println("Start PZEM serial");
node.begin(1, pzem); Serial.println("Start PZEM"); // 1 = ID MODBUS
WiFi.mode(WIFI_STA);
#if defined(USE_LOCAL_SERVER)
WiFi.begin(ssid, pass);
Blynk.config(AUTH, server, port);
#else
Blynk.begin(AUTH, ssid, pass);
#endif
while (Blynk.connect() == false) {}
ArduinoOTA.setHostname(OTA_HOSTNAME);
ArduinoOTA.begin();
// timerTask1 = timer.setInterval(1000, updateBlynk);
}
void updateBlynk() {
Blynk.virtualWrite(vPIN_VOLTAGE, U_PR);
Blynk.virtualWrite(vPIN_CURRENT_USAGE, I_PR);
Blynk.virtualWrite(vPIN_ACTIVE_POWER, P_PR);
Blynk.virtualWrite(vPIN_ACTIVE_ENERGY, PPR);
Blynk.virtualWrite(vPIN_FREQUENCY, PR_F);
Blynk.virtualWrite(vPIN_POWER_FACTOR, PR_PF);
Blynk.virtualWrite(vPIN_OVER_POWER_ALARM, PR_alarm);
}
void loop(){
Blynk.run();
//ArduinoOTA.handle();
//timer.run();
result = node.readInputRegisters(0x0000, 10);
if (result == node.ku8MBSuccess) {
U_PR = (node.getResponseBuffer(0x00)/10.0f);
I_PR = (node.getResponseBuffer(0x01)/1000.000f);
P_PR = (node.getResponseBuffer(0x03)/10.0f);
PPR = (node.getResponseBuffer(0x05)/1000.0f);
PR_F = (node.getResponseBuffer(0x07)/10.0f);
PR_PF = (node.getResponseBuffer(0x08)/100.0f);
PR_alarm = (node.getResponseBuffer(0x09));
}
Serial.print("U_PR: "); Serial.println(U_PR); // V
Serial.print("I_PR: "); Serial.println(I_PR,3); // A
Serial.print("P_PR: "); Serial.println(P_PR); // W
Serial.print("PPR: "); Serial.println(PPR,3); // kWh
Serial.print("PR_F: "); Serial.println(PR_F); // Hz
Serial.print("PR_PF: "); Serial.println(PR_PF);
Serial.print("PR_alarm: "); Serial.println(PR_alarm,0);
Serial.println("====================================================");
updateBlynk();
delay(1000);
}
Code for DHT22
#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <DHT.h>
// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "Your-Auth-Token";
// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "Your-Wifi-Name";
char pass[] = "Your-Wifi-Password";
#define DHTPIN 0 // D3
// Uncomment whatever type you're using!
#define DHTTYPE DHT11 // DHT 11
//#define DHTTYPE DHT22 // DHT 22, AM2302, AM2321
//#define DHTTYPE DHT21 // DHT 21, AM2301
DHT dht(DHTPIN, DHTTYPE);
BlynkTimer timer;
// This function sends Arduino's up time every second to Virtual Pin (5).
// In the app, Widget's reading frequency should be set to PUSH. This means
// that you define how often to send data to Blynk App.
void sendSensor()
{
float h = dht.readHumidity();
float t = dht.readTemperature(); // or dht.readTemperature(true) for Fahrenheit
if (isnan(h) || isnan(t)) {
Serial.println("Failed to read from DHT sensor!");
return;
}
// You can send any value at any time.
// Please don't send more that 10 values per second.
Blynk.virtualWrite(V5, t);
Blynk.virtualWrite(V6, h);
}
void setup()
{
// Debug console
Serial.begin(9600);
Blynk.begin(auth, ssid, pass);
// You can also specify server:
//Blynk.begin(auth, ssid, pass, "blynk-cloud.com", 8442);
//Blynk.begin(auth, ssid, pass, IPAddress(192,168,1,100), 8442);
dht.begin();
// Setup a function to be called every second
timer.setInterval(1000L, sendSensor);
}
void loop()
{
Blynk.run();
timer.run();
}