Created
November 1, 2017 15:50
-
-
Save cransom/5b0633e3e45030d7aa74954e94bd838b to your computer and use it in GitHub Desktop.
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
#include <ESP8266WiFi.h> | |
#include <ESP8266httpUpdate.h> | |
#include <PubSubClient.h> | |
// OTA Firmware Version | |
const char* VERSION = "Remote_Control_Switch_Module_MQTT.bin_v0.000"; | |
// gpio info | |
const int myPins[] = { D1, D2, D3, D4 }; | |
const int numPins = sizeof(myPins) / 4; | |
volatile bool pinState[numPins]; | |
volatile unsigned long lastInterruptOnPin[numPins]; | |
const int debounce_time = 200; //200 millisecond debounce | |
#define wifi_ssid "IOTSSID" | |
#define wifi_password "IOTSSIDPW" | |
#define mqtt_server "MQTTSERVER" | |
#define mqtt_port 1883 | |
//#define mqtt_user "" //if applicable. Change code where client connects to server | |
//#define mqtt_password "" //if applicable | |
#define topic_root "control/" | |
String client_id; | |
String topic; | |
//WIFI and MQTT | |
WiFiClient espClient; | |
PubSubClient client(espClient); | |
void setup() { | |
Serial.begin(115200); | |
Serial.print("Remote Control "); | |
Serial.print(VERSION); | |
Serial.println(); | |
//Setup WIFI and MQTT | |
WiFi.mode(WIFI_STA); | |
setup_wifi(); | |
update_firmware(); | |
// unique client ID | |
client_id = String(ESP.getChipId()); | |
//setup mqtt | |
client.setServer(mqtt_server, mqtt_port); | |
//client.connect(); | |
reconnect(); | |
topic = String(topic_root) + String(client_id); | |
client.publish(topic.c_str(), "Startup."); | |
//uint32_t topic_id = ESP.getChipId(); | |
//topic_base = topic_root + topic_id; | |
// setup gpio and interrupts | |
for ( int i = 0; i < numPins; i++ ) { | |
Serial.print("Setting up pin "); | |
Serial.print(i); | |
Serial.print(","); | |
Serial.print(myPins[i]); | |
Serial.println(); | |
pinMode(myPins[i], INPUT_PULLUP); | |
pinState[i] = 0; | |
} | |
attachInterrupt(digitalPinToInterrupt(myPins[0]), interrupt0, FALLING); | |
attachInterrupt(digitalPinToInterrupt(myPins[1]), interrupt1, FALLING); | |
attachInterrupt(digitalPinToInterrupt(myPins[2]), interrupt2, FALLING); | |
attachInterrupt(digitalPinToInterrupt(myPins[3]), interrupt3, FALLING); | |
Serial.println("Setup Complete."); | |
} | |
void interrupt0() { | |
if ( millis() - lastInterruptOnPin[0] > debounce_time ) { | |
pinState[0] = true; | |
} | |
lastInterruptOnPin[0] = millis(); | |
} | |
void interrupt1() { | |
if ( millis() - lastInterruptOnPin[1] > debounce_time ) { | |
pinState[1] = true; | |
} | |
lastInterruptOnPin[1] = millis(); | |
} | |
void interrupt2() { | |
if ( millis() - lastInterruptOnPin[2] > debounce_time ) { | |
pinState[2] = true; | |
} | |
lastInterruptOnPin[2] = millis(); | |
} | |
void interrupt3() { | |
if ( millis() - lastInterruptOnPin[3] > debounce_time ) { | |
pinState[3] = true; | |
} | |
lastInterruptOnPin[3] = millis(); | |
} | |
void setup_wifi() { | |
delay(10); | |
// We start by connecting to a WiFi network | |
Serial.println(); | |
Serial.print("Connecting to "); | |
Serial.println(wifi_ssid); | |
WiFi.begin(wifi_ssid, wifi_password); | |
while (WiFi.status() != WL_CONNECTED) { | |
delay(500); | |
Serial.print("."); | |
} | |
Serial.println(""); | |
Serial.println("WiFi connected"); | |
Serial.println("IP address: "); | |
Serial.println(WiFi.localIP()); | |
} | |
void update_firmware() { | |
const char* server = "FIRMWAREWWWSERVER"; | |
const unsigned long port = 80; | |
const char* path = "/esp8266/update.php"; | |
Serial.println(F("OTA firmware update.")); | |
Serial.print(F("[current load]: ")); | |
Serial.println(VERSION); | |
t_httpUpdate_return ret = ESPhttpUpdate.update(server, port, path, VERSION); | |
switch (ret) { | |
case HTTP_UPDATE_FAILED: | |
Serial.println(F("[update] Update failed.")); | |
break; | |
case HTTP_UPDATE_NO_UPDATES: | |
Serial.println(F("[update] Update no Update.")); | |
break; | |
case HTTP_UPDATE_OK: | |
Serial.println(F("[update] Update ok.")); | |
break; | |
} | |
} | |
void publishValue( int id, bool state) { | |
String pub_topic = topic + "/" + String(id); | |
client.publish(pub_topic.c_str(), String(state).c_str()); | |
} | |
void reconnect() { | |
// Loop until we're reconnected | |
while (!client.connected()) { | |
Serial.print("Attempting MQTT connection..."); | |
// Attempt to connect | |
// If you do not want to use a username and password, change next line to | |
if (client.connect(client_id.c_str())) { | |
//if (client.connect("ESP8266Client", mqtt_user, mqtt_password)) { | |
Serial.println("connected"); | |
} else { | |
Serial.print("failed, rc="); | |
Serial.print(client.state()); | |
Serial.println(" try again in 5 seconds"); | |
// Wait 5 seconds before retrying | |
delay(5000); | |
} | |
delay(1000); | |
} | |
} | |
void loop() { | |
if (!client.connected()) { | |
reconnect(); | |
} | |
for ( int i = 0; i < numPins; i++ ) { | |
if ( pinState[i] > 0 ) { | |
Serial.print("pin "); | |
Serial.print(i); | |
Serial.print(" triggered, value "); | |
Serial.println(pinState[i]); | |
//Do Stuff | |
publishValue(i, pinState[i]); | |
pinState[i] = false; | |
} | |
} | |
client.loop(); | |
yield(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment