四博智联产品售后

 找回密码
 立即注册
搜索
查看: 11535|回复: 0
打印 上一主题 下一主题

ESPDuino中断

[复制链接]

253

主题

306

帖子

1831

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
1831
跳转到指定楼层
楼主
发表于 2017-3-13 14:05:15 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式

  attachInterrupt(interruptPin, changeState, CHANGE);
  1. #include <SoftwareSerial.h>
  2. #include <EmonLib.h>

  3. #define EnergySaveEnable 1
  4. #define fisicalSwitch 2
  5. #define power A0
  6. #define rele 11
  7. #define interruptPin 1 //pin 3 arduino uno
  8. #define ThresholdInterruptDelay 100
  9. const int sleepTime = 2000;
  10. const int vac = 230;

  11. SoftwareSerial esp8266(9, 10);
  12. EnergyMonitor emon1;

  13. int retries = 0;
  14. int powerValue = 0;
  15. boolean isHigh = true;
  16. long lastMs;
  17. long lastSent;

  18. void setup() {
  19.   initPins();
  20.   initSerial();
  21.   emon1.current(power, 9.3);
  22.   attachInterrupt(interruptPin, changeState, CHANGE);
  23.   lastMs = millis();
  24.   lastSent = millis();
  25. }

  26. void loop() {
  27.   delay(500);
  28.   checkFisicalButtonChanged();
  29.   checkEnergySaveMode();
  30.   if (millis() - lastSent >= sleepTime) {
  31.     computePower();
  32.     lastSent = millis();
  33.     esp8266.print(getMessage());
  34.     Serial.println("potenza aggiornata!");
  35.     if (esp8266.available() > 0) {
  36.       Serial.println(esp8266.readString());
  37.     }
  38.   }
  39. }

  40. void computePower() {
  41.   powerValue = (emon1.calcIrms(1480)) * 230 ;
  42.   if (powerValue <= 13)
  43.     powerValue = 0;
  44. }

  45. void initPins() {
  46.   pinMode(rele, OUTPUT);
  47.   pinMode(fisicalSwitch, INPUT);
  48.   digitalWrite(rele, HIGH);
  49. }

  50. void initSerial() {
  51.   esp8266.begin(115200);
  52.   Serial.begin(9600);
  53. }

  54. void changeState() {
  55.   if (isHigh && millis() - lastMs > ThresholdInterruptDelay) {
  56.     isHigh = false;
  57.     digitalWrite(rele, LOW);
  58.     lastMs = millis();
  59.   }
  60.   else if (!isHigh && millis() - lastMs > ThresholdInterruptDelay) {
  61.     isHigh = true;
  62.     digitalWrite(rele, HIGH);
  63.     lastMs = millis();
  64.   }
  65. }

  66. void checkFisicalButtonChanged() {
  67.   if (digitalRead(fisicalSwitch) == HIGH) {
  68.     if (isHigh)
  69.       sendMessageSwitch(0);
  70.     else
  71.       sendMessageSwitch(1);

  72.     changeState();
  73.   }
  74. }

  75. void checkEnergySaveMode() {
  76.   if (EnergySaveEnable == 1)
  77.     if (digitalRead(rele) == HIGH && powerValue <= 0) {
  78.       if (retries >= 20) {
  79.         if (isHigh)
  80.           sendMessageSwitch(0);
  81.         else
  82.           sendMessageSwitch(1);

  83.         changeState();
  84.         retries = 0;
  85.       }
  86.       else {
  87.         retries++;
  88.       }
  89.     }
  90. }

  91. void sendMessageSwitch(int value) {
  92.   String pwr = "powerSwitch:";
  93.   pwr += value;
  94.   pwr += ";";
  95.   esp8266.print(pwr);
  96. }

  97. String getMessage() {
  98.   String pwr = "power:";
  99.   pwr += powerValue;
  100.   pwr += ";";
  101.   return pwr;
  102. }
复制代码





回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|Archiver|手机版|小黑屋|四博智联 Inc. ( 粤ICP备15034758号-1

GMT+8, 2024-4-20 09:11 , Processed in 0.078477 second(s), 32 queries .

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

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