Advertisement
Guest User

Untitled

a guest
Oct 23rd, 2019
172
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.54 KB | None | 0 0
  1. #include <ESP8266WiFi.h>
  2. #include <PubSubClient.h>
  3.  
  4.  
  5.  
  6.  
  7.  
  8.  
  9.  
  10. //////////////MQTT SETTING/////////////////////////////////////////////////////
  11. const char* ssid = "ELKARL"; // Enter your WiFi name
  12. const char* password = "elkarln307"; // Enter WiFi password
  13. const char* mqttServer = "soldier.cloudmqtt.com";
  14. const int mqttPort = 16197;
  15. const char* mqttUser = "hrcifacm";
  16. const char* mqttPassword = "ixe1PUM6Ij";
  17. int monster;
  18.  
  19. float nilaisp,nilaikp,nilaiki,nilaikd,pidduty;
  20.  
  21. WiFiClient espClient;
  22. PubSubClient client(espClient);
  23. ///////////////////////////////////////////////////////////////////
  24. int titolampu1;
  25. ////////////////////////DOMAIN POMPA MIKROKONTROL//////////////////////////
  26. //inisialisasi pin
  27.  
  28.  
  29. float jarak,jarakreal;
  30. int Read = 0;
  31. float distance = 0.0;
  32. float elapsedTime, timePrev; //Variables for time control
  33.  
  34.  
  35. //////////////////////////////////////////////////
  36. void setup() {
  37.  
  38. Serial.begin(115200);
  39.  
  40.  
  41. WiFi.begin(ssid, password);
  42.  
  43. while (WiFi.status() != WL_CONNECTED) {
  44. delay(500);
  45. Serial.println("Connecting to WiFi..");
  46. }
  47. Serial.println("Connected to the WiFi network");
  48.  
  49. client.setServer(mqttServer, mqttPort);
  50. client.setCallback(callback);
  51.  
  52. while (!client.connected()) {
  53. Serial.println("Connecting to MQTT...");
  54.  
  55. if (client.connect("ESP8266Client", mqttUser, mqttPassword )) {
  56.  
  57. Serial.println("connected");
  58.  
  59. } else {
  60.  
  61. Serial.print("failed with state ");
  62. Serial.print(client.state());
  63. delay(2000);
  64.  
  65. }
  66. }
  67.  
  68. //List Topic name
  69. client.subscribe("/titolampu1");
  70.  
  71.  
  72.  
  73. ///////////////////////////////////////////////////////////inisialisasi Sensor
  74.  
  75.  
  76.  
  77.  
  78.  
  79. ////////////////////////////////////////////////////////////////
  80. }
  81.  
  82. void callback(char* topic, byte* payload, unsigned int length) {
  83.  
  84.  
  85. for (int i = 0; i < 1; i++) {
  86.  
  87. ///////////////////////////////////////////////Get Topic
  88.  
  89. if (strcmp(topic,"/titolampu1")==0) {
  90. int mytopic = atoi (topic);
  91. payload[length] = '\0';
  92. titolampu1 = atof( (const char *) payload);
  93. Serial.print("Lampu1:");
  94. Serial.print(titolampu1);
  95.  
  96. }
  97.  
  98.  
  99.  
  100. }
  101.  
  102. Serial.println();
  103. Serial.println("-----------------------");
  104.  
  105. }
  106.  
  107.  
  108.  
  109.  
  110.  
  111.  
  112.  
  113.  
  114. void loop() {
  115. client.loop();
  116.  
  117.  
  118.  
  119. }
  120.  
  121.  
  122.  
  123.  
  124. void pid()
  125. {
  126.  
  127.  
  128.  
  129. Serial.print("Lampu 1:");
  130. Serial.println(String(titolampu1).c_str());
  131. client.publish("/titolampu1", String(titolampu1).c_str(), true);
  132.  
  133.  
  134.  
  135.  
  136.  
  137.  
  138.  
  139.  
  140.  
  141.  
  142. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement