Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <ESP8266WiFi.h>
- #include <PubSubClient.h>
- #define SOUND_VELOCITY 0.034
- long duration;
- float distanceCm;
- // Update these with values suitable for your network.
- const char* ssid = "LSKKWorks";
- const char* password = "1234567890";
- const char* mqtt_server = "192.168.7.2";
- int mqtt_port = 1883;
- const char* mqtt_user = "camera";
- const char* mqtt_pass = "camera";
- const char* mqtt_topic = "sensor/1";
- const char* CL = "b2da3fb1-7b7f-43f5-bbae-21f3e0c6a7b7_MEGABOT_Sensor_01";//nama alat
- String guid = "b2da3fb1-7b7f-43f5-bbae-21f3e0c6a7b7";
- int loop_count = 0 ;
- const int input1 = D1;
- const int input2 = D2;
- byte mac[6];
- String MACAddress;
- uint8_t MAC_array[6];
- char MAC_char[18];
- String button_status[8];
- WiFiClient espClient;
- PubSubClient client(espClient);
- unsigned long lastMsg = 0;
- #define MSG_BUFFER_SIZE (50)
- char msg[MSG_BUFFER_SIZE];
- int value = 0;
- String mac2String(byte ar[]) {
- String s;
- for (byte i = 0; i < 6; ++i)
- {
- char buf[3];
- sprintf(buf, "%2X", ar[i]);
- s += buf;
- if (i < 5) s += ':';
- }
- return s;
- }
- void printMACAddress() {
- WiFi.macAddress(mac);
- MACAddress = mac2String(mac);
- Serial.println(MACAddress);
- }
- 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(500);
- Serial.print(".");
- }
- randomSeed(micros());
- Serial.println("");
- Serial.println("WiFi connected");
- Serial.println("IP address: ");
- Serial.println(WiFi.localIP());
- WiFi.macAddress(MAC_array);
- for (int i = 0; i < sizeof(MAC_array) - 1; ++i) {
- sprintf(MAC_char, "%s%02x:", MAC_char, MAC_array[i]);
- }
- sprintf(MAC_char, "%s%02x", MAC_char, MAC_array[sizeof(MAC_array) - 1]);
- }
- 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.println();
- // Switch on the LED if an 1 was received as first character
- if ((char)payload[0] == '1') {
- digitalWrite(BUILTIN_LED, LOW); // Turn the LED on (Note that LOW is the voltage level
- // but actually the LED is on; this is because
- // it is active low on the ESP-01)
- } else {
- digitalWrite(BUILTIN_LED, HIGH); // Turn the LED off by making the voltage HIGH
- }
- }
- void reconnect() {
- printMACAddress();
- // Loop until we're reconnected
- while (!client.connected()) {
- Serial.print("Attempting MQTT connection...");
- // Create a random client ID
- String clientId = "ESP8266Client-";
- clientId += String(random(0xffff), HEX);
- // Attempt to connect
- if (client.connect(CL, mqtt_user, mqtt_pass)) {
- Serial.println("connected");
- } else {
- Serial.print("failed, rc=");
- Serial.print(client.state());
- Serial.println(" try again in 5 seconds");
- // Wait 5 seconds before retrying
- delay(5000);
- }
- }
- }
- void setup() {
- pinMode(BUILTIN_LED, OUTPUT); // Initialize the BUILTIN_LED pin as an output
- Serial.begin(115200);
- pinMode(input1, OUTPUT);
- pinMode(input2, INPUT);
- setup_wifi();
- client.setServer(mqtt_server, mqtt_port);
- client.setCallback(callback);
- }
- void loop() {
- String pubmsg = "";
- if (!client.connected()) {
- reconnect();
- }
- client.loop();
- delay(1000);
- digitalWrite(input1, LOW);
- delayMicroseconds(2);
- digitalWrite(input1, HIGH);
- delayMicroseconds(2);
- digitalWrite(input1, LOW);
- duration = pulseIn(input2, HIGH);
- distanceCm = duration * SOUND_VELOCITY/2;
- pubmsg = guid + "#" + String(distanceCm);
- Serial.print("Publish message: ");
- Serial.println(pubmsg);
- client.publish(mqtt_topic, pubmsg.c_str());
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement