diff --git a/platformio.ini b/platformio.ini index 2856f3a..26b1275 100644 --- a/platformio.ini +++ b/platformio.ini @@ -15,3 +15,4 @@ framework = arduino lib_deps = adafruit/DHT sensor library@^1.4.4 adafruit/Adafruit Unified Sensor@^1.1.9 + knolleary/PubSubClient@^2.8 diff --git a/src/main.cpp b/src/main.cpp index 3e10da9..b876ff6 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,7 +1,7 @@ #include #include #include // Include the Wi-Fi-Multi library -#include +#include #include ESP8266WiFiMulti wifiMulti; // Create an instance of the ESP8266WiFiMulti class, called 'wifiMulti' @@ -9,14 +9,18 @@ ESP8266WiFiMulti wifiMulti; // Create an instance of the ESP8266WiFiMulti class // WiFi connect timeout per AP. Increase when connecting takes longer. const uint32_t connectTimeoutMs = 5000; -// Replace with your server's address -const char* serverAddress = "sensors.tanshu.com"; -const int serverPort = 443; +// MQTT broker's address and port +const char* mqttServer = "iot.tanshu.com"; +const int mqttPort = 1883; -// Replace with your DHT sensor pin +// MQTT broker username and password +const char* mqttUser = "esp8266"; +const char* mqttPassword = "esp8266"; + +// DHT sensor pin const int dhtPin = D4; -// Replace with your device name +// Device name const char* deviceName = "s1-gwb"; // Initialize the DHT sensor @@ -24,6 +28,7 @@ DHT dht(dhtPin, DHT22); // Function declaration void setupWiFi(); +void connectToMQTT(); void sendSensorData(float temperature, float humidity, unsigned long timestamp, const char* device); void sendBufferedData(); @@ -41,12 +46,17 @@ int bufferIndex = 0; unsigned long previousTimestamp = 0; const unsigned long interval = 30000; +WiFiClient espClient; +PubSubClient client(espClient); + void setup() { Serial.begin(9600); setupWiFi(); dht.begin(); + + client.setServer(mqttServer, mqttPort); } void loop() { @@ -86,6 +96,12 @@ void loop() { previousTimestamp = currentTimestamp; } + + if (!client.connected()) { + connectToMQTT(); + } + + client.loop(); } void setupWiFi() { @@ -110,37 +126,27 @@ void setupWiFi() { } void sendSensorData(float temperature, float humidity, unsigned long age, const char* device) { - String url = "/upload"; + char topic[50]; + snprintf(topic, sizeof(topic), "sensor/%s", device); + char payload[100]; + snprintf(payload, sizeof(payload), "{\"temperature\":%.2f,\"humidity\":%.2f,\"age\":%lu}", temperature, humidity, millis() - age); + client.publish(topic, payload); + Serial.println("Sent sensor data to MQTT broker!"); +} - WiFiClientSecure client; - client.setInsecure(); // skip verification +void connectToMQTT() { + while (!client.connected()) { + Serial.print("Connecting to MQTT broker..."); - if (!client.connect(serverAddress, serverPort)) { - Serial.println("Failed to connect to server!"); - return; - } - - // Calculate the length of the payload - String payload = "temp=" + String(temperature) + "&humidity=" + String(humidity) + "&age=" + String(millis() - age) + "&device=" + device; - int payloadLength = payload.length(); - - client.print("POST " + url + " HTTP/1.1\r\n" + - "Host: " + String(serverAddress) + "\r\n" + - "Connection: close\r\n" + - "Content-Type: application/x-www-form-urlencoded\r\n" + - "Content-Length: " + String(payloadLength) + "\r\n\r\n" + - payload); - - while (client.connected()) { - if (client.available()) { - String line = client.readStringUntil('\n'); - Serial.println(line); + if (client.connect("ESP8266Client", mqttUser, mqttPassword)) { + Serial.println("connected"); + } else { + Serial.print("failed, rc="); + Serial.print(client.state()); + Serial.println(" retrying in 5 seconds"); + delay(5000); } } - - Serial.println("Sent sensor data to server!"); - - client.stop(); } void sendBufferedData() {