IoT Pengukur Debu Dengan ESP32 dan Sensor Sharp GP2Y1010

IoT Pengukur Debu Dengan ESP32 dan Sensor Sharp GP2Y1010

Spesifikasi

 

// CJMCU-8118
#include <WiFi.h>           // WiFi control for ESP32
#include <ThingsBoard.h>    // ThingsBoard SDK
#include <esp_task_wdt.h>

// WiFi access point
#define WIFI_AP_NAME        "APNAME" // ganti dengan WIFI AP 
// WiFi password
#define WIFI_PASSWORD       "123456"  // ganti dengan WIFI AP password
#define TOKEN               "zRG1HN8w0PRydh5gV7Il"

#define LED_SENSOR 23
#define ADC_INPUT 34
#define BUILTIN_LED 2
#define WDT_TIMEOUT 60

// ThingsBoard server instance.
#define THINGSBOARD_SERVER  "192.168.0.114"

#include <Arduino.h>
#include <Wire.h>

// Initialize ThingsBoard client
WiFiClient espClient;
// Initialize ThingsBoard instance
ThingsBoard tb(espClient);
// the Wifi radio's status
int status = WL_IDLE_STATUS;

// main application loop delay (ms)
int quant = 20;

// Period of sending a temperature/humidity data.
int send_delay = 2000;

// Time passed after telemetry data was sent, milliseconds.
int send_passed = 0;

char mac_str[20]; // storing MAC address string
byte mac_byte[6]; // storing MAC address bytes

int led_counter = 0; //blinking built int led

void setup() {
  pinMode(BUILTIN_LED, OUTPUT);
  pinMode(LED_SENSOR, OUTPUT);

  // put your setup code here, to run once:
  Serial.begin(115200);
  delay(1000);
  Serial.print("Source Code: ");
  Serial.println(__FILE__);

  //wait for serial connection to open (only necessary on some boards)
  while (!Serial);

  WiFi.begin(WIFI_AP_NAME, WIFI_PASSWORD);
  InitWiFi();
  WiFi.macAddress(mac_byte);
  sprintf(mac_str, "%02x%02x%02x%02x%02x%02x", mac_byte[0], mac_byte[1], mac_byte[2], mac_byte[3], mac_byte[4], mac_byte[5]);
  Serial.print("ESP board MAC address:  ");
  Serial.println(WiFi.macAddress());
  Serial.print("ESP board  IP address: ");
  Serial.println(WiFi.localIP());

  // setup WDT
  esp_task_wdt_init(WDT_TIMEOUT, true); //enable panic so ESP32 restarts
  esp_task_wdt_add(NULL); //add current thread to WDT watch

}

void loop() {
  float temperature = 25;
  float pressure = 0;
  float humidity = 60; // default humidity
  float co2 = 0;
  float tvoc = 0;

  delay(quant);
  send_passed += quant;

  // Reconnect to WiFi, if needed
  if (WiFi.status() != WL_CONNECTED) {
    reconnect();
    return;
  }

  // Reconnect to ThingsBoard, if needed
  if (!tb.connected()) {
    // Connect to the ThingsBoard
    Serial.print("Connecting to: ");
    Serial.print(THINGSBOARD_SERVER);
    Serial.print(" with token ");
    Serial.println(TOKEN);
    if (!tb.connect(THINGSBOARD_SERVER, TOKEN)) {
      Serial.println("Failed to connect");
      return;
    }
  }

  // Check if it is a time to send sensor data
  if (send_passed > send_delay) {
    int dust;
    dust = DustRead();

    Serial.print("Sending telemetry data...");
    Serial.print("Dust ");
    Serial.print(dust);
    Serial.println("");
    tb.sendTelemetryFloat("dust", dust);
    send_passed = 0;
  }

  // Process messages
  tb.loop();

  led_counter++; // LED blink at 1 Hz
  if (led_counter > 50) {
    led_counter = 0;
  }
  if (led_counter > 25) {
    digitalWrite(BUILTIN_LED , LOW);
  } else {
    digitalWrite(BUILTIN_LED , HIGH);
  }
  esp_task_wdt_reset(); // WDT reset setelah berhasil mengirim data
}

void I2C_Scan() {
  byte error, address;

  int nDevices;
  Serial.println("I2C Scanning...");
  nDevices = 0;
  for (address = 1; address < 127; address++ ) {
    Wire.beginTransmission(address);
    error = Wire.endTransmission();
    if (error == 0) {
      Serial.print("I2C device found at address 0x");
      if (address < 16) {
        Serial.print("0");
      }
      Serial.println(address, HEX);
      nDevices++;
    }
    else if (error == 4) {
      Serial.print("Unknow error at address 0x");
      if (address < 16) {
        Serial.print("0");
      }
      Serial.println(address, HEX);
    }
  }
  if (nDevices == 0) {
    Serial.println("No I2C devices found\n");
  }
  else {
    Serial.println("done\n");
  }
}

void InitWiFi()
{
  Serial.println("Connecting to AP ...");
  // attempt to connect to WiFi network

  WiFi.begin(WIFI_AP_NAME, WIFI_PASSWORD);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("Connected to AP");
}

void reconnect() {
  // Loop until we're reconnected
  status = WiFi.status();
  if ( status != WL_CONNECTED) {
    WiFi.begin(WIFI_AP_NAME, WIFI_PASSWORD);
    while (WiFi.status() != WL_CONNECTED) {
      delay(500);
      Serial.print(".");
    }
    Serial.println("Connected to AP");
  }
}

int DustRead() {
  int sensorValue;
  // read the analog in value:
  digitalWrite(LED_SENSOR, HIGH);
  delayMicroseconds(280);
  sensorValue = analogRead(ADC_INPUT); // measurement 280 us after signal start
  delayMicroseconds(40); // total signal duration 320 us
  digitalWrite(LED_SENSOR, LOW); // turn off LED

  // print the results to the Serial Monitor:
  Serial.print("sensor = ");
  Serial.println(sensorValue);
  return sensorValue;
}

Tinggalkan Balasan

Situs ini menggunakan Akismet untuk mengurangi spam. Pelajari bagaimana data komentar Anda diproses.