求助电力监控系统 PZEM-004T联网后一直重启-Arduino中文社区 - Powered by Discuz!

Arduino中文社区

 找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 2359|回复: 6

[已解答] 求助电力监控系统 PZEM-004T联网后一直重启

[复制链接]
发表于 2020-5-15 15:48 | 显示全部楼层 |阅读模式
本帖最后由 chuanping87 于 2020-5-17 13:02 编辑

原帖在这里:https://www.arduino.cn/thread-97032-1-1.html
设备连接网络后一直重启,,app开关。有时候正常有时候失效,希望有大神帮我看出问题解决一下,谢谢[mw_shl_code=arduino,true]#define BLINKER_WIFI
#define BLINKER_ALIGENIE_OUTLET
#define BLINKER_ESP_SMARTCONFIG
#include <Blinker.h>
BlinkerButton Button1("btn-abc"); // 开关
BlinkerNumber Voltage("voltage"); // 电压
BlinkerNumber Current("current"); // 电流
BlinkerNumber Power("power"); // 功率
BlinkerNumber Energy("energy"); // 电量
BlinkerNumber Frequency("frequency"); // 频率
BlinkerNumber Pf("pf"); // 功率因数
BlinkerNumber Electriccharge("electriccharge"); // 电费
BlinkerNumber Temperature("temperature"); // 温度
BlinkerNumber Humidity("humidity"); // 湿度
char auth[] = "";//密钥
//char ssid[] = "";//wifi名
//char pswd[] = "";//wifi密码

#include <PZEM004Tv30.h>
PZEM004Tv30 pzem(D5, D6);
#define ELECTROVALENCE 0.5469 //电价

#include <U8g2lib.h>
#define SCL  D4                     //D4
#define SDA  D3                     //D3
U8G2_SSD1306_128X64_NONAME_F_SW_I2C u8g2(U8G2_R0, SCL, SDA, U8X8_PIN_NONE);

#include <DHT.h>
#define DHTPIN D2
#define DHTTYPE DHT11
DHT dht(DHTPIN, DHTTYPE);

#define LED_PIN1 D0
#define KEY_PIN1 D1

void readpzem()
{
    float humidity = dht.readHumidity();
    if ( !isnan(humidity) ) {
    Serial.print("Humidity: "); Serial.print(humidity); Serial.println("%");
    } else {
    humidity = 0;
    Serial.println("Error reading Humidity");
    }
   
    float temperature = dht.readTemperature();
    if ( !isnan(temperature) ) {
    Serial.print("Temperature: "); Serial.print(temperature); Serial.println("°C");
    } else {
    temperature = 0;
    Serial.println("Error reading Temperature");
    }
   
    float voltage = pzem.voltage();
    if( !isnan(voltage) ){
        Serial.print("Voltage: "); Serial.print(voltage); Serial.println("V");
    } else {
      voltage = 0;
        Serial.println("Error reading voltage");
    }

    float current = pzem.current();
    if( !isnan(current) ){
        Serial.print("Current: "); Serial.print(current); Serial.println("A");
    } else {
      current = 0;
        Serial.println("Error reading current");
    }

    float power = pzem.power();
    if( !isnan(power) ){
        Serial.print("Power: "); Serial.print(power); Serial.println("W");
    } else {
      power = 0;
        Serial.println("Error reading power");
    }

    float energy = pzem.energy();float electriccharge = 0;
    if( !isnan(energy) ){
      electriccharge = energy * ELECTROVALENCE;
        Serial.print("Energy: "); Serial.print(energy,3); Serial.println("kWh");
         Serial.print("electriccharge: "); Serial.println(electriccharge,3);
    } else {
      energy = 0;
        Serial.println("Error reading energy");
    }

    float frequency = pzem.frequency();
    if( !isnan(frequency) ){
        Serial.print("Frequency: "); Serial.print(frequency, 1); Serial.println("Hz");
    } else {
      frequency = 0;
        Serial.println("Error reading frequency");
    }

    float pf = pzem.pf();
    if( !isnan(pf) ){
        Serial.print("PF: "); Serial.println(pf);
    } else {
      pf = 0;
        Serial.println("Error reading power factor");
    }

    Serial.println();
    Voltage.print(voltage);
    Current.print(current);
    Power.print(power);
    Energy.print(energy);
    Frequency.print(frequency);
    Pf.print(pf);
    Electriccharge.print(electriccharge);
    Temperature.print(temperature);
    Humidity.print(humidity);
    Button1.color(digitalRead(LED_PIN1) ? "#FF0000" : "#00FF00");Button1.print(digitalRead(LED_PIN1) ? "off" : "on");
    delay(2000);

  u8g2.setFont(u8g2_font_wqy12_t_gb2312a);  
  u8g2.setFontDirection(0);
  u8g2.clearBuffer();

  u8g2.setCursor(0, 13);u8g2.print("温度");u8g2.print(temperature, 1);u8g2.print("°C");
  u8g2.setCursor(64, 13);u8g2.print("湿度");u8g2.print(humidity, 0);u8g2.print(" %");
  u8g2.setCursor(0, 26);u8g2.print("电压");u8g2.print(voltage, 1);u8g2.print("V");
  u8g2.setCursor(64, 26);u8g2.print("电流");u8g2.print(current, 2);u8g2.print("A");
  u8g2.setCursor(0, 38);u8g2.print("功率");u8g2.print(power, 1);u8g2.print(" W");
  u8g2.setCursor(64, 38);u8g2.print("电量");u8g2.print(energy, 2);u8g2.print("Wh");
  u8g2.setCursor(0, 50);u8g2.print("频率");u8g2.print(frequency, 1);u8g2.print("Hz");
  u8g2.setCursor(64, 50);u8g2.print("因数");u8g2.print(pf, 2);u8g2.print(" L");
  u8g2.setCursor(0, 62);u8g2.print("电费");u8g2.print(electriccharge, 2);u8g2.print(" $");
  u8g2.setCursor(64, 62);u8g2.print("开关");u8g2.print(digitalRead(LED_PIN1) ? "off" : "on");


  u8g2.sendBuffer();
  
}

void checkButton()
{
    if ( digitalRead(KEY_PIN1) == LOW )
    {
      Blinker.delay(50);
      if( digitalRead(KEY_PIN1) == LOW )
      {while( digitalRead(KEY_PIN1) == LOW );
       digitalWrite(LED_PIN1, !digitalRead(LED_PIN1));//输出反向
       BLINKER_LOG(__func__,":",digitalRead(LED_PIN1));//日志
       readpzem();
       //Button1.color(digitalRead(LED_PIN1) ? "#0000FF" : "#00FFFF");Button1.print(digitalRead(LED_PIN1) ? "off" : "on");
      
      }
    }
}
void button1_callback(const String & state)
{  
    BLINKER_LOG("need set power state: ", state);
    if (state == BLINKER_CMD_ON) {
        digitalWrite(LED_PIN1, LOW);
    }
    else if (state == BLINKER_CMD_OFF) {
        digitalWrite(LED_PIN1, HIGH);
    }
    readpzem();
    //Button1.color(digitalRead(LED_PIN1) ? "#00FFFF" : "#0000FF");Button1.print(digitalRead(LED_PIN1) ? "off" : "on");
}
void aligeniePowerState(const String & state)
{
    BLINKER_LOG("need set power state: ", state);
    if (state == BLINKER_CMD_ON) {
        digitalWrite(LED_PIN1, LOW);
        BlinkerAliGenie.powerState("on");
        BlinkerAliGenie.print();
    }
    else if (state == BLINKER_CMD_OFF) {
        digitalWrite(LED_PIN1, HIGH);
        BlinkerAliGenie.powerState("off");
        BlinkerAliGenie.print();
    }
    readpzem();
    //Button1.color(digitalRead(LED_PIN1) ? "#00FFFF" : "#0000FF");Button1.print(digitalRead(LED_PIN1) ? "off" : "on");   
}
void aligenieQuery(int32_t queryCode)
{
    BLINKER_LOG("AliGenie Query codes: ", queryCode);
    switch (queryCode)
    {
        case BLINKER_CMD_QUERY_ALL_NUMBER :
            BLINKER_LOG("AliGenie Query All");
            BlinkerAliGenie.powerState(digitalRead(LED_PIN1) ? "off" : "on");
            BlinkerAliGenie.print();
            break;
        case BLINKER_CMD_QUERY_POWERSTATE_NUMBER :
            BLINKER_LOG("AliGenie Query Power State");
            BlinkerAliGenie.powerState(digitalRead(LED_PIN1) ? "off" : "on");
            BlinkerAliGenie.print();
            break;
        default :
            BlinkerAliGenie.powerState(digitalRead(LED_PIN1) ? "off" : "on");
            BlinkerAliGenie.print();
            break;
    }
}
void heartbeat()
{
  BLINKER_LOG("heartbeat ");
  readpzem();
  }
void dataRead(const String & data)
{
    BLINKER_LOG("Blinker readString: ", data);
}
void setup() {
    Serial.begin(115200);
    BLINKER_DEBUG.stream(Serial);
    pinMode(LED_PIN1, OUTPUT);
    digitalWrite(LED_PIN1, LOW);
    pinMode(KEY_PIN1, INPUT_PULLUP);
    //Blinker.begin(auth, ssid, pswd);
    Blinker.begin(auth);
    Blinker.attachData(dataRead);
    Blinker.attachHeartbeat(heartbeat);
    BlinkerAliGenie.attachPowerState(aligeniePowerState);
    BlinkerAliGenie.attachQuery(aligenieQuery);
    Button1.attach(button1_callback);
    dht.begin();
    u8g2.begin();
    u8g2.enableUTF8Print();
    readpzem();
}

void loop() {
  Blinker.run();
  checkButton();
  
}[/mw_shl_code]
微信图片_20200515155225.jpg
发表于 2020-5-21 15:27 | 显示全部楼层

回帖奖励 +1 金币

问题有解决吗。
 楼主| 发表于 2020-5-21 18:48 | 显示全部楼层

还没有
发表于 2020-7-10 08:29 来自手机 | 显示全部楼层
要用硬串口,用软串口有问题
发表于 2020-7-10 09:06 | 显示全部楼层
回调中不能用delay
发表于 2020-7-10 14:47 | 显示全部楼层
奈何col 发表于 2020-7-10 09:06
回调中不能用delay

那应该用啥回调,版主大大能稍做修饰完善一下代码吗?配件搞回来都起灰了,一直处理不了

点评

这个只有自己尝试了,你的lib是否支持8266我也不清楚  详情 回复 发表于 2020-7-11 08:21
发表于 2020-7-11 08:21 | 显示全部楼层
雨梦随行 发表于 2020-7-10 14:47
那应该用啥回调,版主大大能稍做修饰完善一下代码吗?配件搞回来都起灰了,一直处理不了 ...

这个只有自己尝试了,你的lib是否支持8266我也不清楚
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|Archiver|手机版|Arduino中文社区

GMT+8, 2024-12-1 05:49 , Processed in 0.098026 second(s), 22 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表