Advertisement
vitareinforce

ultrasonic_sensor

Dec 20th, 2024
13
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.00 KB | None | 0 0
  1. #include <ESP8266WiFi.h>
  2. #include <PubSubClient.h>
  3.  
  4. // Update these with values suitable for your network.
  5.  
  6. const char* ssid = "LSKKWorks";
  7. const char* password = "1234567890";
  8. const char* mqtt_server = "192.168.7.114";
  9. int mqtt_port = 1883;
  10. const char* mqtt_user = "camera";
  11. const char* mqtt_pass = "camera";
  12. const char* mqtt_topic = "sensor/1";
  13.  
  14. const char* CL = "b2da3fb1-7b7f-43f5-bbae-21f3e0c6a7b7_MEGABOT_Sensor_01";//nama alat
  15. String guid = "b2da3fb1-7b7f-43f5-bbae-21f3e0c6a7b7";
  16. int loop_count = 0 ;
  17. const int input1 = D1;
  18. const int input2 = D2;
  19. const int input3 = D3;
  20. const int input4 = D4;
  21. const int input6 = D5;
  22. const int input5 = D6;
  23. const int input7 = D7;
  24.  
  25.  
  26. byte mac[6];
  27. String MACAddress;
  28. uint8_t MAC_array[6];
  29. char MAC_char[18];
  30.  
  31. String button_status[8];
  32.  
  33. WiFiClient espClient;
  34. PubSubClient client(espClient);
  35. unsigned long lastMsg = 0;
  36. #define MSG_BUFFER_SIZE (50)
  37. char msg[MSG_BUFFER_SIZE];
  38. int value = 0;
  39.  
  40. String mac2String(byte ar[]) {
  41. String s;
  42. for (byte i = 0; i < 6; ++i)
  43. {
  44. char buf[3];
  45. sprintf(buf, "%2X", ar[i]);
  46. s += buf;
  47. if (i < 5) s += ':';
  48. }
  49. return s;
  50. }
  51.  
  52. void printMACAddress() {
  53. WiFi.macAddress(mac);
  54. MACAddress = mac2String(mac);
  55. Serial.println(MACAddress);
  56. }
  57.  
  58. void setup_wifi() {
  59.  
  60. delay(10);
  61. // We start by connecting to a WiFi network
  62. Serial.println();
  63. Serial.print("Connecting to ");
  64. Serial.println(ssid);
  65.  
  66. WiFi.mode(WIFI_STA);
  67. WiFi.begin(ssid, password);
  68.  
  69. while (WiFi.status() != WL_CONNECTED) {
  70. delay(500);
  71. Serial.print(".");
  72. }
  73.  
  74. randomSeed(micros());
  75.  
  76. Serial.println("");
  77. Serial.println("WiFi connected");
  78. Serial.println("IP address: ");
  79. Serial.println(WiFi.localIP());
  80.  
  81. WiFi.macAddress(MAC_array);
  82. for (int i = 0; i < sizeof(MAC_array) - 1; ++i) {
  83. sprintf(MAC_char, "%s%02x:", MAC_char, MAC_array[i]);
  84. }
  85. sprintf(MAC_char, "%s%02x", MAC_char, MAC_array[sizeof(MAC_array) - 1]);
  86. }
  87.  
  88. void callback(char* topic, byte* payload, unsigned int length) {
  89. Serial.print("Message arrived [");
  90. Serial.print(topic);
  91. Serial.print("] ");
  92. for (int i = 0; i < length; i++) {
  93. Serial.print((char)payload[i]);
  94. }
  95. Serial.println();
  96.  
  97. // Switch on the LED if an 1 was received as first character
  98. if ((char)payload[0] == '1') {
  99. digitalWrite(BUILTIN_LED, LOW); // Turn the LED on (Note that LOW is the voltage level
  100. // but actually the LED is on; this is because
  101. // it is active low on the ESP-01)
  102. } else {
  103. digitalWrite(BUILTIN_LED, HIGH); // Turn the LED off by making the voltage HIGH
  104. }
  105.  
  106. }
  107.  
  108. void reconnect() {
  109. printMACAddress();
  110. // Loop until we're reconnected
  111. while (!client.connected()) {
  112. Serial.print("Attempting MQTT connection...");
  113. // Create a random client ID
  114. String clientId = "ESP8266Client-";
  115. clientId += String(random(0xffff), HEX);
  116. // Attempt to connect
  117. if (client.connect(CL, mqtt_user, mqtt_pass)) {
  118. Serial.println("connected");
  119. // Once connected, publish an announcement...
  120. // client.publish("outTopic", "hello world");
  121. // ... and resubscribe
  122. // client.subscribe("inTopic");
  123. } else {
  124. Serial.print("failed, rc=");
  125. Serial.print(client.state());
  126. Serial.println(" try again in 5 seconds");
  127. // Wait 5 seconds before retrying
  128. delay(5000);
  129. }
  130. }
  131. }
  132.  
  133. void setup() {
  134. pinMode(BUILTIN_LED, OUTPUT); // Initialize the BUILTIN_LED pin as an output
  135. Serial.begin(115200);
  136.  
  137. pinMode(input1, INPUT_PULLUP);
  138. pinMode(input2, INPUT_PULLUP);
  139. pinMode(input3, INPUT_PULLUP);
  140. pinMode(input4, INPUT_PULLUP);
  141. pinMode(input5, INPUT_PULLUP);
  142. pinMode(input6, INPUT_PULLUP);
  143. pinMode(input7, INPUT_PULLUP);
  144.  
  145. setup_wifi();
  146. client.setServer(mqtt_server, mqtt_port);
  147. client.setCallback(callback);
  148. }
  149.  
  150. void loop() {
  151.  
  152. String pubmsg = "";
  153.  
  154. if (!client.connected()) {
  155. reconnect();
  156. }
  157. client.loop();
  158.  
  159. delay(1000);
  160.  
  161. int instate1 = digitalRead(input1);
  162. if(instate1==LOW) button_status[0]="1";
  163. else button_status[0] ="0";
  164.  
  165. int instate2 = digitalRead(input2);
  166. if(instate2==LOW) button_status[1]="1";
  167. else button_status[1] ="0";
  168.  
  169. int instate3 = digitalRead(input3);
  170. if(instate3==LOW) button_status[2]="1";
  171. else button_status[2] ="0";
  172.  
  173. int instate4 = digitalRead(input4);
  174. if(instate4==LOW) button_status[3]="1";
  175. else button_status[3] ="0";
  176.  
  177. int instate5 = digitalRead(input5);
  178. if(instate5==LOW) button_status[4]="1";
  179. else button_status[4] ="0";
  180.  
  181. int instate6 = digitalRead(input6);
  182. if(instate6==LOW) button_status[5]="1";
  183. else button_status[5] ="0";
  184.  
  185. int instate7 = digitalRead(input7);
  186. if(instate7==LOW) button_status[6]="1";
  187. else button_status[6] ="0";
  188.  
  189. pubmsg = String(mqtt_topic) + "#" + button_status[0] + button_status[1] + button_status[2] + button_status[3] + button_status[4] + button_status[5]+ button_status[6];
  190.  
  191. Serial.print("Publish message: ");
  192. Serial.println(pubmsg);
  193. client.publish(mqtt_topic, pubmsg.c_str());
  194.  
  195. }
  196.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement