Thanks to
ramonverde
for the original code
I have cut it to the basics for this test.
Tested with NodeMcu Lua
ESP8266 ESP-12E
- note the pinouts listed in this sketch/*
This is the simplest
example I can find that sends data from an ESP8266 to an MQTT
broker.
(I tested it with Mosqitto on a Mac)
It could be modified easily to send analog data or data from i2c
and other devices.
/* send values of 2 GPIO pins 12 (D6) and 16 (D0) see all with mosquitto_sub -v -t '#' or mosquitto_sub -v -t '/sensor/ESP8266-10895084/data/12_D6' or mosquitto_sub -v -t '/sensor/ESP8266-10895084/data/16_D0 0' Thanks to https://github.com/esp8266-examples/ota-mqtt/blob/master/ota-mqtt/ota-mqtt.ino */ #include <ESP8266WiFi.h> //For ESP8266 #include <PubSubClient.h> //For MQTT #include <ESP8266mDNS.h> //For OTA #include <WiFiUdp.h> //For OTA #include <ArduinoOTA.h> //For OTA int ledPin1 = 13; // LED connected to digital pin 13 D7 int inPin1 = 12; // pushbutton connected to digital pin 12 D6 int val1 = 0; // variable to store the read value int ledPin2 = 5; // LED connected to digital pin 5 D1 int inPin2 = 16; // pushbutton connected to digital pin 16 D0 int val2 = 0; // variable to store the read value //WIFI configuration #define wifi_ssid "BeattyStreet" #define wifi_password "6461766964" //MQTT configuration #define mqtt_server "10.1.1.190" #define mqtt_user "esp8266" #define mqtt_password "730" String mqtt_client_id="ESP8266-"; //This text is concatenated with ChipId to get unique client_id //MQTT Topic configuration String mqtt_base_topic="/GPIO/"+mqtt_client_id+"/data"; #define D0_topic "/16_D0" #define D6_topic "/12_D6" //MQTT client WiFiClient espClient; PubSubClient mqtt_client(espClient); //Necesary to make Arduino Software autodetect OTA device WiFiServer TelnetServer(8266); void setup_wifi() { delay(10); Serial.print("Connecting to "); Serial.print(wifi_ssid); WiFi.begin(wifi_ssid, wifi_password); while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); } Serial.println("OK"); Serial.print(" IP address: "); Serial.println(WiFi.localIP()); } void setup() { //====================================================================SETUP START pinMode(ledPin1, OUTPUT); // sets the digital pin 13 D7 as output pinMode(inPin1, INPUT); // sets the digital pin 12 D6 as input pinMode(ledPin2, OUTPUT); // sets the digital pin 5 D1 as output pinMode(inPin2, INPUT); // sets the digital pin 16 D0 as input Serial.begin(115200); Serial.println("\r\nBooting..."); setup_wifi(); Serial.print("Configuring OTA device..."); TelnetServer.begin(); //Necesary to make Arduino Software autodetect OTA device ArduinoOTA.onStart([]() {Serial.println("OTA starting...");}); ArduinoOTA.onEnd([]() {Serial.println("OTA update finished!");Serial.println("Rebooting...");}); ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) {Serial.printf("OTA in progress: %u%%\r\n", (progress / (total / 100)));}); ArduinoOTA.onError([](ota_error_t error) { Serial.printf("Error[%u]: ", error); if (error == OTA_AUTH_ERROR) Serial.println("Auth Failed"); else if (error == OTA_BEGIN_ERROR) Serial.println("Begin Failed"); else if (error == OTA_CONNECT_ERROR) Serial.println("Connect Failed"); else if (error == OTA_RECEIVE_ERROR) Serial.println("Receive Failed"); else if (error == OTA_END_ERROR) Serial.println("End Failed"); }); ArduinoOTA.begin(); Serial.println("OK"); Serial.println("Configuring MQTT server..."); mqtt_client_id=mqtt_client_id+ESP.getChipId(); mqtt_base_topic="/sensor/"+mqtt_client_id+"/data"; mqtt_client.setServer(mqtt_server, 1883); Serial.printf(" Server IP: %s\r\n",mqtt_server); Serial.printf(" Username: %s\r\n",mqtt_user); Serial.println(" Cliend Id: "+mqtt_client_id); Serial.println(" MQTT configured!"); Serial.println("Setup completed! Running app..."); } //=================================================================================SETUP END void mqtt_reconnect() { // Loop until we're reconnected while (!mqtt_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("ESP8266Client")) { if (mqtt_client.connect(mqtt_client_id.c_str(), mqtt_user, mqtt_password)) { Serial.println("connected"); } else { Serial.print("failed, rc="); Serial.print(mqtt_client.state()); Serial.println(" try again in 5 seconds"); // Wait 5 seconds before retrying delay(5000); } } } long now =0; //in ms long lastMsg = 0; int min_timeout=2000; //in ms void loop() { //=======================================================================LOOPSTART ArduinoOTA.handle(); if (!mqtt_client.connected()) { mqtt_reconnect(); } mqtt_client.loop(); now = millis(); if (now - lastMsg > min_timeout) { lastMsg = now; now = millis(); val1 = digitalRead(inPin1); // read the input pin - it will be 0 or 1 digitalWrite(ledPin1, val1); // sets the LED to the button's value val2 = digitalRead(inPin2); // read the input pin - it will be 0 or 1 digitalWrite(ledPin2, val2); // sets the LED to the button's value mqtt_client.publish((mqtt_base_topic+D0_topic).c_str(), (String(val2)).c_str(), true); mqtt_client.publish((mqtt_base_topic+D6_topic).c_str(), (String(val1)).c_str(), true); } } //======================================================================================LOOPEND /* * NodeMCU pin mapping. Pin numbers written on the board itself do not correspond to ESP8266 GPIO pin numbers. D numbers on the board GPIO static const uint8_t D0 = 16; static const uint8_t D1 = 5; static const uint8_t D2 = 4; static const uint8_t D3 = 0; static const uint8_t D4 = 2; static const uint8_t D5 = 14; static const uint8_t D6 = 12; static const uint8_t D7 = 13; static const uint8_t D8 = 15; static const uint8_t D9 = 3; static const uint8_t D10 = 1; */ |