123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238 |
- #include <ESP8266WiFi.h>
- #include <PubSubClient.h>
- #define SerialDebug false
- const char* ssid = "";
- const char* password = "!";
- const char* server = "";
- const int port = 1883;
- const char* mqtt_user = "";
- const char* mqtt_pwd = "";
- const char* topic = "Postkasten";
- const int BattLowValue = 2600;
- const int powerdownAfter = 20;
- byte holdPin = 0;
- byte inputPin = 3;
- bool event_is_emptying = false;
- ADC_MODE(ADC_VCC);
- int Vcc;
- byte BattLow;
- WiFiClient wifiClient;
- PubSubClient client(server, port, callback, wifiClient);
- void setup() {
- pinMode(holdPin, OUTPUT);
- digitalWrite(holdPin, HIGH);
- #if SerialDebug
-
- Serial.begin(9600, SERIAL_8N1, SERIAL_TX_ONLY, 1);
- #endif
-
- pinMode(inputPin, INPUT);
- delay(100);
- if ( digitalRead(inputPin) == HIGH ) event_is_emptying = true;
- #if SerialDebug
- delay(1000);
- Serial.print("begin...\n");
- Serial.print("inputPin/event_is_emptying=");
- Serial.print(digitalRead(inputPin));
- Serial.print("\n");
- #endif
- #if SerialDebug
- Serial.print("connecting WiFi");
- #endif
- WiFi.begin(ssid, password);
- uint8_t wifiRetryCounter = 0;
- while (WiFi.status() != WL_CONNECTED) {
- delay(500);
- #if SerialDebug
- Serial.print(".");
- #endif
- wifiRetryCounter++;
- if (wifiRetryCounter >= 60) {
- #if SerialDebug
- Serial.println();
- #endif
- break;
- }
- }
- if (WiFi.status() == WL_CONNECTED) {
- #if SerialDebug
- Serial.println("WiFi connected");
- Serial.println("IP: ");
- Serial.println(WiFi.localIP());
- #endif
- }
- else {
- #if SerialDebug
- Serial.println("ERROR: could not connect WiFi. Powering down now.");
- #endif
- ESP.deepSleep(0);
- }
-
- String clientName;
- clientName += "esp8266-";
- uint8_t mac[6];
- WiFi.macAddress(mac);
- clientName += macToStr(mac);
- clientName += "-";
- clientName += String(micros() & 0xff, 16);
- Vcc = ESP.getVcc();
- #if SerialDebug
- Serial.print("Connecting to ");
- Serial.print(server);
- Serial.print(" as ");
- Serial.println(clientName);
- Serial.print("Vcc=");
- Serial.print(Vcc);
- Serial.print("\n");
- Serial.print("Batt=");
- if ( Vcc <= BattLowValue ) Serial.print("LOW");
- else Serial.print("OK");
- Serial.print("\n");
- #endif
-
-
- if (client.connect((char*) clientName.c_str(), mqtt_user, mqtt_pwd)) {
- #if SerialDebug
- Serial.println("Connected to MQTT broker");
- Serial.print("Topic: ");
- Serial.println(topic);
- #endif
- client.loop();
- String payload = "{\"event\":\"";
- if ( !event_is_emptying ) payload += "new_post";
- else payload += "emptied";
- payload += "\",\"Vcc\":\"";
- payload += Vcc;
- payload += "\",\"Batt\":";
- if ( Vcc <= BattLowValue ) payload += "\"LOW\"";
- else payload += "\"OK\"";
- payload += "}";
- #if SerialDebug
- Serial.print("Sending payload: ");
- Serial.println(payload);
- #endif
- client.loop();
- if (client.publish(topic, (char*) payload.c_str())) {
- client.loop();
- #if SerialDebug
- Serial.println("Publish ok");
- #endif
- }
- else {
- client.loop();
- #if SerialDebug
- Serial.println("Publish failed");
- #endif
- }
- client.loop();
- client.disconnect();
- }
- else {
- #if SerialDebug
- Serial.println("MQTT connect failed");
-
- #endif
-
- }
- #if SerialDebug
- Serial.print("sent, stopping WIFI...\n");
- #endif
- WiFi.disconnect();
- WiFi.mode(WIFI_OFF);
- WiFi.forceSleepBegin();
- #if SerialDebug
- Serial.print("WIFI deactivated\n");
- #endif
-
- for (int i = 0; i <= powerdownAfter; i++) {
- delay(1000);
- }
-
- #if SerialDebug
- Serial.print("power down now\n");
- #endif
-
-
- ESP.deepSleep(0);
-
-
-
-
-
-
-
-
- }
- void loop() {
- }
- void callback(char* topic, byte* payload, unsigned int length) {
-
- }
- String macToStr(const uint8_t* mac)
- {
- String result;
- for (int i = 0; i < 6; ++i) {
- result += String(mac[i], 16);
- if (i < 5)
- result += ':';
- }
- return result;
- }
|