├── .DS_Store ├── .gitattributes └── ESP32-MQTT.ino /.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/techiesms/ESP32-MQTT/0be4a40f0f28c6883e419f45889c204a526775aa/.DS_Store -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | # Auto detect text files and perform LF normalization 2 | * text=auto 3 | -------------------------------------------------------------------------------- /ESP32-MQTT.ino: -------------------------------------------------------------------------------- 1 | //This code is edited by Sachin Soni(techiesms) for a project tutorital on 2 | //Controlling Appliances and monitoring sensor's data over Internet using Ubidots MQTT server 3 | //The video is uploaded on youtube whose link is :- https://youtu.be/LvzCeBce2mU 4 | 5 | /**************************************** 6 | * Include Libraries 7 | ****************************************/ 8 | #include 9 | #include 10 | 11 | #define WIFISSID "SSID" // Put your WifiSSID here 12 | #define PASSWORD "PASS" // Put your wifi password here 13 | #define TOKEN "TOKEN" // Put your Ubidots' TOKEN 14 | #define MQTT_CLIENT_NAME "RandomName" // MQTT client Name, please enter your own 8-12 alphanumeric character ASCII string; 15 | //it should be a random and unique ascii string and different from all other devices 16 | 17 | /**************************************** 18 | * Define Constants 19 | ****************************************/ 20 | #define VARIABLE_LABEL "hall-sensor" // Assing the variable label 21 | #define VARIABLE_LABEL_SUBSCRIBE "relay" // Assing the variable label 22 | #define DEVICE_LABEL "esp32" // Assig the device label 23 | 24 | #define relay 26 // Set the GPIO26 as RELAY 25 | 26 | char mqttBroker[] = "things.ubidots.com"; 27 | char payload[100]; 28 | char topic[150]; 29 | char topicSubscribe[100]; 30 | // Space to store values to send 31 | char str_sensor[10]; 32 | 33 | /**************************************** 34 | * Auxiliar Functions 35 | ****************************************/ 36 | WiFiClient ubidots; 37 | PubSubClient client(ubidots); 38 | 39 | 40 | 41 | void reconnect() { 42 | // Loop until we're reconnected 43 | while (!client.connected()) { 44 | Serial.println("Attempting MQTT connection..."); 45 | 46 | // Attemp to connect 47 | if (client.connect(MQTT_CLIENT_NAME, TOKEN, "")) { 48 | Serial.println("Connected"); 49 | client.subscribe(topicSubscribe); 50 | } else { 51 | Serial.print("Failed, rc="); 52 | Serial.print(client.state()); 53 | Serial.println(" try again in 2 seconds"); 54 | // Wait 2 seconds before retrying 55 | delay(2000); 56 | } 57 | } 58 | } 59 | void callback(char* topic, byte* payload, unsigned int length) { 60 | char p[length + 1]; 61 | memcpy(p, payload, length); 62 | p[length] = NULL; 63 | String message(p); 64 | if (message == "0") { 65 | digitalWrite(relay, LOW); 66 | } else { 67 | digitalWrite(relay, HIGH); 68 | } 69 | 70 | Serial.write(payload, length); 71 | Serial.println(); 72 | } 73 | 74 | /**************************************** 75 | * Main Functions 76 | ****************************************/ 77 | void setup() { 78 | Serial.begin(115200); 79 | WiFi.begin(WIFISSID, PASSWORD); 80 | // Assign the pin as INPUT 81 | pinMode(relay, OUTPUT); 82 | 83 | Serial.println(); 84 | Serial.print("Wait for WiFi..."); 85 | 86 | while (WiFi.status() != WL_CONNECTED) { 87 | Serial.print("."); 88 | delay(500); 89 | } 90 | 91 | Serial.println(""); 92 | Serial.println("WiFi Connected"); 93 | Serial.println("IP address: "); 94 | Serial.println(WiFi.localIP()); 95 | client.setServer(mqttBroker, 1883); 96 | client.setCallback(callback); 97 | 98 | sprintf(topicSubscribe, "/v1.6/devices/%s/%s/lv", DEVICE_LABEL, VARIABLE_LABEL_SUBSCRIBE); 99 | 100 | client.subscribe(topicSubscribe); 101 | } 102 | 103 | void loop() { 104 | if (!client.connected()) { 105 | client.subscribe(topicSubscribe); 106 | reconnect(); 107 | } 108 | 109 | sprintf(topic, "%s%s", "/v1.6/devices/", DEVICE_LABEL); 110 | sprintf(payload, "%s", ""); // Cleans the payload 111 | sprintf(payload, "{\"%s\":", VARIABLE_LABEL); // Adds the variable label 112 | 113 | float sensor = hallRead(); 114 | Serial.print("Value of Sensor is:- ");Serial.println(sensor); 115 | 116 | /* 4 is mininum width, 2 is precision; float value is copied onto str_sensor*/ 117 | dtostrf(sensor, 4, 2, str_sensor); 118 | 119 | sprintf(payload, "%s {\"value\": %s}}", payload, str_sensor); // Adds the value 120 | Serial.println("Publishing data to Ubidots Cloud"); 121 | client.publish(topic, payload); 122 | client.loop(); 123 | delay(1000); 124 | } 125 | --------------------------------------------------------------------------------