/* Originally from MQTT ESP8266 Example Basic XIAO-ESP32 MQTTS example Before starting , You should have - MQTT Broker Name - MQTT Broker Username - MQTT Borker Password Modified by Salman Faris - Added MQTTS Implimenation as per ESP32 and MQTTS. Modified by Dion Tsang - Added Colorwheel, DHT Datavisualization. */ #include #include #include "DHT.h" #include #ifdef __AVR__ #include // Required for 16 MHz Adafruit Trinket #endif #define DHTPIN D2 #define DHTTYPE DHT11 // DHT 11 #define NUMPIXELS 80 // Popular NeoPixel ring size #define PIN D1 #define BUF_SIZE (100) #define BUF_SIZE2 (100) DHT dht(DHTPIN, DHTTYPE); // Initialize DHT sensor. Adafruit_NeoPixel strip(NUMPIXELS, PIN, NEO_GRBW + NEO_KHZ800); Adafruit_NeoPixel pixels(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800); #define DELAYVAL 20 // Time (in milliseconds) to pause between pixels // Update these with values suitable for your network. const char* ssid = "AndroidAP26ab"; // WiFi Name const char* password = "tawd7050"; // WiFi Password const char* mqtt_server = "mqtt.fabcloud.org"; //MQTT Broker Name WiFiClient espClient; PubSubClient client(espClient); unsigned long lastMsg = 0; #define MSG_BUFFER_SIZE (50) char msg[MSG_BUFFER_SIZE]; int value = 0; // int BUILTIN_LED = D10 void setup_wifi() { delay(10); // We start by connecting to a WiFi network Serial.println(); Serial.print("Connecting to "); Serial.println(ssid); WiFi.mode(WIFI_STA); WiFi.begin(ssid, password); while (WiFi.status() != WL_CONNECTED) { delay(DELAYVAL); Serial.print("."); } randomSeed(micros()); Serial.println(""); Serial.println("WiFi connected"); Serial.println("IP address: "); Serial.println(WiFi.localIP()); } void callback(char* topic, byte* payload, unsigned int length) { Serial.print("Message arrived ["); Serial.print(topic); Serial.print("] "); for (int i = 0; i < length; i++) { Serial.print((char)payload[i]); } Serial.print("This is length"); Serial.print(length); Serial.println(); if (strcmp(topic, "fabacademy/myLampSwitch") == 0) { if ((char)payload[0] == '1') { pixels.clear(); // Set all pixel colors to 'On' for (int i = 0; i < NUMPIXELS; i++) { // pixels.setPixelColor(i, pixels.Color(0, 150, 0)); pixels.setPixelColor(i,0xDE3759); } pixels.show(); // Send the updated pixel colors to the hardware } else if ((char)payload[0] == '0') { pixels.clear(); // Set all pixel colors to 'Off' for (int i = 0; i < NUMPIXELS; i++) { pixels.setPixelColor(i, pixels.Color(0, 0, 0)); } pixels.show(); // Send the updated pixel colors to the hardware } } if (strcmp(topic, "fabacademy/LampColor") == 0) { // Parse the hex color code from the payload char hexColor[7] = { 0 }; // 6 characters + null terminator if (length == 7) { // strncpy(hexColor, (char*)payload, 6); for (int i=1; i > 16) & 0xFF; byte green = (color >> 8) & 0xFF; byte blue = color & 0xFF; // Set the NeoPixel color using the 32-bit integer pixels.clear(); for (int i = 1; i < NUMPIXELS; i++) { pixels.setPixelColor(i, pixels.Color(red, green, blue)); } pixels.show(); // Send the updated pixel colors to the hardware } } } void reconnect() { // Loop until we're reconnected while (!client.connected()) { Serial.print("Attempting MQTT connection..."); // Create a random client ID String clientId = "XIAO-ESP32-Client-"; clientId += String(random(0xffff), HEX); // Attempt to connect // if (client.connect(clientId.c_str())) { if (client.connect(clientId.c_str(), "fabacademy", "fabacademy")) { Serial.println("connected"); // Once connected, publish an announcement... //client.publish("fabacademy/dionTest/Temperature", "hello world"); // ... and resubscribe client.subscribe("fabacademy/myLampSwitch"); client.subscribe("fabacademy/LampColor"); } else { Serial.print("failed, rc="); Serial.print(client.state()); Serial.println(" try again in 5 seconds"); // Wait 5 seconds before retrying delay(DELAYVAL); } } } void setup() { // pinMode(PIN, OUTPUT); // Initialize the BUILTIN_LED pin as an output Serial.begin(115200); setup_wifi(); client.setServer(mqtt_server, 1883); client.setCallback(callback); dht.begin(); // END of Trinket-specific code. pixels.begin(); // INITIALIZE NeoPixel strip object (REQUIRED) } void loop() { if (!client.connected()) { reconnect(); } client.loop(); delay(DELAYVAL); // Reading temperature or humidity takes about 250 milliseconds! // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor) float h = dht.readHumidity(); // Read temperature as Celsius (the default) float t = dht.readTemperature(); // Read temperature as Fahrenheit (isFahrenheit = true) float f = dht.readTemperature(true); // Check if any reads failed and exit early (to try again). if (isnan(h) || isnan(t) || isnan(f)) { Serial.println(F("Failed to read from DHT sensor!")); return; } // unsigned long now = millis(); // if (now - lastMsg > 2000) { // lastMsg = now; // ++value; // snprintf (msg, MSG_BUFFER_SIZE, "hello world #%ld", value); // Serial.print("Publish message: "); // Serial.println(msg); // client.publish("fabacademy/dionTest", msg); // } // #define BUF_SIZE (100) // #define BUF_SIZE2 (100) char tempra[BUF_SIZE]; snprintf(tempra, BUF_SIZE, "%f", t); client.publish("fabacademy/Temprature", tempra); char humidity[BUF_SIZE2]; snprintf(humidity, BUF_SIZE2, "%f", h); client.publish("fabacademy/Humidity", humidity); }