Re: Den långa vägen till postlådan
Postat: 14 april 2017, 08:03:29
				
				Sure. Koden är inget konstigt alls, bara lite logik. wifi_mqtt_common.h bifogar jag inte, det är i princip bara en wifi+pubsubclient implementation. Codelibrary.h innehåller bara en delay() metod.
			Kod: Markera allt
// Burn settings
// Generic ESP8266 Module // DIO 40mhz // Serial // 80mhz // 4m (1m spiffs) // disabled // none // ck // 115200
#define MAJORVERSION 1
#define MINORVERSION 2
#include <wifi_mqtt_common.h>
#include <CodeLibrary.h>
String DEVICE_NAME = "mailbox";
const char* topicConnected = "/device/mailbox/connected";
const char* topicLid1Opened = "/device/mailbox/lid1";
const char* topicLid2Opened = "/device/mailbox/lid2";
const char* topicLid1NotClosed = "/device/mailbox/lid1_notclosed";
const char* topicLid2NotClosed = "/device/mailbox/lid2_notclosed";
const int pinLid1 = 12;
const int pinLid2 = 13;
bool lid1Open = false;
bool lid2Open = false;
const int pinControlEnable = 14;
WifiMqtt wifiMqtt;
CodeLibrary codeLibrary;
void setup() {
  //Serial.begin(115200);
  codeLibrary.wait(100);
  Serial.println("\n\nSetup. Version " + String(MAJORVERSION) + "." + String(MINORVERSION) + ". sdk " + String(ESP.getSdkVersion())); 
  pinMode(pinLid1, INPUT);
  pinMode(pinLid2, INPUT);
  pinMode(pinControlEnable, OUTPUT);
  // Turn *ON* ENABLE pin
  digitalWrite(pinControlEnable, HIGH);
  wifiMqtt.initSettings("iotMailbox", "mqMailbox", IPAddress(192, 168, 0, 197), IPAddress(192, 168, 0, 1));
  wifiMqtt.SetDisconnectAfterSent(true);
  wifiMqtt.setDebugOn(true);
  Serial.println("Setup() done.");
  Serial.println("Checking if needed to send notification.");
  checkIfNeedToSendNotification();
  // Check for firmware update
  wifiMqtt.CheckForFirmwareUpdate(MAJORVERSION, MINORVERSION); 
  if (lid1Open || lid2Open) {
    waitForLidsToClose();
  }
  Serial.println("Sleeping.");
  // Turn *OFF* ENABLE pin
  digitalWrite(pinControlEnable, LOW);
}
// -------------------------------------------------
void checkIfNeedToSendNotification() {
  lid1Open = (HIGH == digitalRead(pinLid1));
  lid2Open = (HIGH == digitalRead(pinLid2));
  if (lid1Open || lid2Open) {
    Serial.println(lid1Open ? "lid1 open" : "lid2 open");
    String jsonResult = "{\"lidOpen\":\"" + String(lid1Open ? "lid1" : "lid2") + "\",";
    jsonResult += "\"ver\":" + String(MAJORVERSION) + "." + String(MINORVERSION) + ",";
    jsonResult += "\"sdk\":\"" + String(ESP.getSdkVersion()) + "\","; 
    // Analog value is 0-1024, 4xAA = 4.8 volt (but is ~5.1 fully charged)
    double batteryVoltage = analogRead(0) * 5.1 / 1024;
    jsonResult += "\"vcc\":" + String(batteryVoltage, 2);
    jsonResult += "}";
    wifiMqtt.sendData(lid1Open ? topicLid1Opened : topicLid2Opened, jsonResult);
  }
}
// -------------------------------------------------
void waitForLidsToClose() {
  // Don't continue until the lids are closed again (or time out)
  unsigned long startMillis = millis();
  int timeoutCount = 0;
  bool lid1Open = (HIGH == digitalRead(pinLid1));
  bool lid2Open = (HIGH == digitalRead(pinLid2));
  while (lid1Open || lid2Open || (millis() - startMillis < 5000)) {
    timeoutCount++;
    // Every 30 seconds (sleep 10ms)
    if (timeoutCount % 3000 == 0) {
      if (lid1Open) {
        wifiMqtt.sendData(topicLid1NotClosed, String(timeoutCount));
      }
      if (lid2Open) {
        wifiMqtt.sendData(topicLid2NotClosed, String(timeoutCount));
      }
      // Abort
      if (timeoutCount > 9000) {
        return;
      }
    }
    codeLibrary.wait(10);
    lid1Open = (HIGH == digitalRead(pinLid1));
    lid2Open = (HIGH == digitalRead(pinLid2));
	  // If lids still are open, reset timer so we still wait x seconds
    if (lid1Open || lid2Open) {
      Serial.println("Resetting timer.");
      startMillis = millis();
    }
  }
}
// -------------------------------------------------
void loop() {
  // Do nothing, all is done in Setup()
}
 
  Ds
 Ds