bangnaga

Mousturise Sensor Wemos D1 MQTT JSON

Feb 24th, 2020
74
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <ESP8266WiFi.h>
  2. #include <PubSubClient.h>
  3. #include <ArduinoJson.h>
  4.  
  5. #define wifi_ssid "MKI"
  6. #define wifi_password "tidaktau"
  7. #define mqtt_server "192.168.1.200"      //Broker's IP Address
  8. #define sensor_data_topic "sensor/data"
  9.  
  10. int moist_Sensor = 0; // A0
  11. int PUMP_PIN = D4;    // D2
  12. String moist;
  13. int persen;
  14.  
  15. WiFiClient espClient;
  16. PubSubClient client(espClient);
  17.  
  18. void setup() {
  19.  
  20.  
  21.  
  22.   pinMode(PUMP_PIN, OUTPUT);
  23.   Serial.begin(115200);
  24.   setup_wifi();
  25.   client.setServer(mqtt_server, 1883);
  26.   client.setCallback(callback);
  27.   delay(1000);
  28. }
  29.  
  30. void setup_wifi() {
  31.   delay(10);
  32.   Serial.println();
  33.   Serial.print("Connecting to ");
  34.   Serial.println(wifi_ssid);
  35.  
  36.   WiFi.begin(wifi_ssid, wifi_password);
  37.  
  38.   while (WiFi.status() != WL_CONNECTED) {
  39.     delay(500);
  40.     Serial.print(".");
  41.   }
  42.  
  43.   Serial.println("");
  44.   Serial.println("WiFi connected");
  45.   Serial.println("IP address: ");
  46.   Serial.println(WiFi.localIP());
  47. }
  48.  
  49. void callback(char* topic, byte* payload, unsigned int length) {
  50.   Serial.print("Message arrived [");
  51.   Serial.print(topic);
  52.   Serial.print("] ");
  53.   String pay_load="";
  54.   for (int i = 0; i < length; i++) {
  55.     pay_load+=(char)payload[i];
  56.   }
  57.   Serial.print(pay_load);
  58.   Serial.println();
  59.  
  60.   if (pay_load == "on") {
  61.     Serial.println("Turning On");
  62.     digitalWrite(PUMP_PIN, HIGH);
  63.   }
  64.   else {
  65.     Serial.println("Turning Off");
  66.     digitalWrite(PUMP_PIN, LOW);
  67.   }
  68.  
  69. }
  70.  
  71. void reconnect() {
  72.   while (!client.connected()) {
  73.     Serial.print("Attempting MQTT connection...");
  74.     // if (client.connect("ESP_PUMP")) {
  75.     if (client.connect("ESP_PUMP")) {
  76.       Serial.println("connected");
  77.       client.subscribe("sensor/reply");
  78.     } else {
  79.       Serial.print("failed, rc=");
  80.       Serial.print(client.state());
  81.       Serial.println(" try again in 5 seconds");
  82.       // Wait 5 seconds before retrying
  83.       delay(5000);
  84.     }
  85.   }
  86. }
  87.  
  88. void loop() {
  89.   if (!client.connected()) {
  90.     reconnect();
  91.   }
  92.   int m = analogRead(moist_Sensor);
  93.  
  94.   persen = map(m, 0, 1024, 100, 0);
  95.   moist=String(persen).c_str();
  96.   String sensor = moist;
  97.   StaticJsonBuffer<100> jsonBuffer;
  98.   JsonObject& root = jsonBuffer.createObject();
  99.   root["analog"] = m;
  100.   root["persen"] = persen;
  101.   //root.printTo(Serial);
  102.   //Serial.println(sensor);
  103.   client.loop();
  104.   //client.publish(sensor_data_topic, String(sensor).c_str(), true);
  105.  
  106. char JSONmessageBuffer[100];
  107.   root.printTo(JSONmessageBuffer, sizeof(JSONmessageBuffer));
  108.   Serial.println("Sending message to MQTT topic..");
  109.   Serial.println(JSONmessageBuffer);
  110.  
  111.   client.publish(sensor_data_topic, JSONmessageBuffer, true);
  112.  
  113.  
  114.   //client.publish(sensor_data_topic, String(sensor).c_str(), true);
  115.   delay(1000);
  116. }
RAW Paste Data