After soldering – You have to upload following code which need to be tweaked according to need, but it has basic logic code –<\/p>\n\n\n\n
The code can set in hotspot mode if couldn’t connect to wifi and if could connect it would make a connection to mqtt server and make an announcement then shutdown.<\/p>\n\n\n
\n#define DEVICE_ID "YOUR_USERNAME"\n#define DEVICE_NAME "iot-" DEVICE_ID\n#define WIFI_SSID "mad-" DEVICE_ID\n#define WIFI_PASSWORD DEVICE_ID\n#define MQTT_SERVER "YOUR_SERVER"\n#define MQTT_USERNAME DEVICE_ID\n#define MQTT_PASSWORD "YOUR_PASSWORD"\n#define MQTT_SEND_CHANNEL "pushbutton"\n#define MQTT_RECV_CHANNEL DEVICE_NAME\n\n\n\n\n#define BUTTON_MODE 1\n#define USE_SSL 1\n#define GPIO2 2\n#define BUTTON_TIMEOUT 30000\n\n\n#include <ESP8266WiFi.h>\n#include <WiFiClient.h>\n#include <ESP8266WebServer.h>\n#include <ESP8266mDNS.h>\n#include <PubSubClient.h>\n#include <DNSServer.h>\n#include <WiFiManager.h>\n#include <ArduinoJson.h>\n#include <stdarg.h>\n\n\n#if USE_SSL\n# include <WiFiClientSecure.h>\n\/\/ WiFiClientSecure client;\n# define PORT 443\n#else\n\/\/ WiFiClient client;\n# define PORT 80\n#endif\n\n\nvoid reconnect();\nvoid sendMessage(String action, String data, char* num = "", ...);\nchar* stringToChar(String str);\n\nWiFiClient espClient;\nPubSubClient client(espClient);\nWiFiManager wifiManager;\n\n\nlong lastMsg = 0;\nchar msg[50];\nint value = 0;\nString DEVICE_FEATURES = "";\n\nvoid setup() {\n\n DEVICE_FEATURES += "pushbutton;";\n pinMode(GPIO2, OUTPUT);\n digitalWrite(GPIO2, HIGH);\n\n\n \/\/Serial.begin(9600);\n\n \/\/ wifiManager.resetSettings();\n if (!wifiManager.autoConnect(WIFI_SSID, WIFI_PASSWORD)) {\n \/\/Serial.println("failed to connect, we should reset as see if it connects");\n delay(1000);\n ESP.reset();\n delay(1000);\n }\n client.setServer(MQTT_SERVER, 1883);\n\n}\nvoid loop() {\n\n \/\/if button mode, & time since start is greater than TIMEOUT close the time.\n if (millis() > BUTTON_TIMEOUT)\n digitalWrite(GPIO2, LOW);\n \/\/if MQTT Client not connect connect it back.\n if (!client.connected())\n reconnect();\n client.loop();\n\n}\n\n\n\n\/**\n Send message to server\n example: sendMessage("hello", DEVICE_NAME,"ksks","mac",getMacAddress(),"localIP",WiFi.localIP().toString());\n k for key\n d : double, f : float, s : string, l : long,\n No char type\n*\/\nvoid sendMessage(String action, String data, char *types, ...) {\n StaticJsonBuffer<200> jsonBuffer;\n JsonObject& root = jsonBuffer.createObject();\n root["action"] = action;\n root["data"] = data;\n\n \/\/find length\n int count = 0;\n while (types[count++] != '\\0');\n --count;\n\n \/\/if not even args\n if (count % 2 != 0)\n return;\n\n \/\/for argument parsing.\n va_list arguments;\n va_start ( arguments, types );\n\n String key;\n\n \/\/even length validated already\n for (int i = 0; types[i] != '\\0'; i += 2) {\n \/\/this is intentionally done, default key as type k can be used, but developer might mistake, causing lot of debugging\n if (types[i] != 'k')\n continue;\n key = va_arg ( arguments, char * );\n\n switch (types[i + 1])\n {\n case 'd': root[key] = (va_arg(arguments, int));\n break;\n case 'l': root[key] = (va_arg(arguments, long));\n break;\n case 'f': root[key] = (va_arg(arguments, double));\n break;\n case 's': root[key] = (va_arg(arguments, char *));\n break;\n default: ;\n };\n }\n\n va_end ( arguments ); \/\/ Cleans up the list\n\n char tmp[root.measureLength() + 2];\n root.printTo(tmp, sizeof(tmp));\n client.publish(MQTT_SEND_CHANNEL, tmp);\n}\n\n\n\nvoid reconnect() {\n \/\/ Loop until we're reconnected\n while (!client.connected()) {\n \/\/Serial.print("Attempting MQTT connection...");\n \/\/ Attempt to connect\n if (client.connect(DEVICE_NAME, MQTT_USERNAME, MQTT_PASSWORD)) {\n \/\/Serial.println("connected");\n \/\/ Once connected, publish an announcement...\n sendMessage("hello", DEVICE_NAME, "ksks", "features", (DEVICE_FEATURES).c_str() , "localIP", (WiFi.localIP().toString().c_str()));\n digitalWrite(GPIO2, LOW);\n } else {\n \/\/Serial.print("failed, rc=");\n \/\/Serial.print(client.state());\n \/\/Serial.println(" try again in 5 seconds");\n \/\/ Wait 5 seconds before retrying\n delay(2000);\n }\n }\n}\n\n<\/pre><\/div>\n\n\nI hope you enjoyed the article, it wasn’t a detailed writeup and I dropped many details – but if you have any questions you can comment or drop mail – I will surely help.<\/p>\n","protected":false},"excerpt":{"rendered":"
Amazon Dash button is an incredible piece of hardware and another example of “applied engineering” in Amazon, just like Kindle. The inspiration for this project isn’t directly derived from amazon dash, I wasn’t aware of it until I started digging the internet but at end, I had to set the performance goal to Amazon dash, because … <\/p>\n
Continue reading<\/a><\/p>\n","protected":false},"author":4,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","enabled":false},"version":2}},"categories":[30,3,6,8],"tags":[35],"class_list":["post-215","post","type-post","status-publish","format-standard","hentry","category-arduino","category-linux","category-server","category-snippets","tag-esp","item-wrap"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack-related-posts":[],"jetpack_shortlink":"https:\/\/wp.me\/pciGs2-3t","jetpack_likes_enabled":true,"jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/madhurendra.com\/wp-json\/wp\/v2\/posts\/215","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/madhurendra.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/madhurendra.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/madhurendra.com\/wp-json\/wp\/v2\/users\/4"}],"replies":[{"embeddable":true,"href":"https:\/\/madhurendra.com\/wp-json\/wp\/v2\/comments?post=215"}],"version-history":[{"count":12,"href":"https:\/\/madhurendra.com\/wp-json\/wp\/v2\/posts\/215\/revisions"}],"predecessor-version":[{"id":365,"href":"https:\/\/madhurendra.com\/wp-json\/wp\/v2\/posts\/215\/revisions\/365"}],"wp:attachment":[{"href":"https:\/\/madhurendra.com\/wp-json\/wp\/v2\/media?parent=215"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/madhurendra.com\/wp-json\/wp\/v2\/categories?post=215"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/madhurendra.com\/wp-json\/wp\/v2\/tags?post=215"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}