Last active
December 17, 2022 02:13
-
-
Save ItKindaWorks/3d60bb56584d6403019af5675b5b41da to your computer and use it in GitHub Desktop.
Small ESP8266 program that sends a message over MQTT and then goes into deep sleep until woken up again.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
dashMQTT.ino | |
Copyright (c) 2017 ItKindaWorks All right reserved. | |
github.com/ItKindaWorks | |
dashMQTT.ino is free software: you can redistribute it and/or modify | |
it under the terms of the GNU General Public License as published by | |
the Free Software Foundation, either version 3 of the License, or | |
(at your option) any later version. | |
dashMQTT.ino is distributed in the hope that it will be useful, | |
but WITHOUT ANY WARRANTY; without even the implied warranty of | |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
GNU General Public License for more details. | |
You should have received a copy of the GNU General Public License | |
along with dashMQTT.ino. If not, see <http://www.gnu.org/licenses/>. | |
*/ | |
#include "ESPHelper.h" | |
#include "Metro.h" | |
//setup macros for time | |
#define SECOND 1000L | |
#define MINUTE SECOND * 60L | |
#define HOUR MINUTE * 60L | |
//hostname for ESPHelper/OTA | |
const char* otaPassword = "OTA_PASSWORD"; | |
const char* netHostName = "YOUR-HOSTNAME"; | |
const char* postTopic = "/some/topic"; | |
const char* payload = "1"; | |
//timers for turn off | |
Metro minRun = Metro(1 * MINUTE); | |
Metro maxRun = Metro(5 * MINUTE); | |
//ESPHelper | |
netInfo homeNet = { .mqttHost = "YOUR MQTT-IP", //can be blank if not using MQTT | |
.mqttUser = "YOUR MQTT USERNAME", //can be blank | |
.mqttPass = "YOUR MQTT PASSWORD", //can be blank | |
.mqttPort = 1883, //default port for MQTT is 1883 - only chance if needed. | |
.ssid = "YOUR SSID", | |
.pass = "YOUR NETWORK PASS"}; | |
ESPHelper myESP(&homeNet); | |
//done keeps track of whether the device has finished the task | |
bool done = false; | |
void setup() { | |
//setup OTA | |
myESP.OTA_enable(); | |
myESP.OTA_setPassword(otaPassword); | |
myESP.OTA_setHostnameWithVersion(netHostName); | |
// myESP.enableHeartbeat(2); //07 / 12E ledPin | |
myESP.enableHeartbeat(1); //01 ledPin | |
myESP.begin(); | |
myESP.setCallback(callback); | |
} | |
void loop(){ | |
if(myESP.loop() == FULL_CONNECTION && !done){ | |
//publish message to MQTT | |
myESP.publish(postTopic, payload, false); | |
//mark as done so we dont constantly spam the message until sleeping | |
done = true; | |
} | |
//send the ESP to deep sleep (to be woken up by pulling rst low) | |
if((minRun.check() && done) || maxRun.check()){ | |
ESP.deepSleep(0, WAKE_RF_DEFAULT); | |
} | |
yield(); | |
} | |
void callback(char* topic, uint8_t* payload, unsigned int length) { | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment