Can some one help me make a MIT app that can receive the ACS712 data from the esp32

#include <BluetoothSerial.h>
#include "ACS712.h"

#define RELAY_A_PIN 2
#define RELAY_B_PIN 27
#define RELAY_D_PIN 33
#define RELAY_E_PIN 32

ACS712 sensor(ACS712_30A, 4);
int relayStates[4] = {LOW, LOW, LOW, LOW}; // Array to store relay states

char receivedChar;
BluetoothSerial SerialBT;

enum AppState {
  WAITING_FOR_E,
  RELAY_CONTROL
};

AppState appState = WAITING_FOR_E;

void setup() {
  Serial.begin(9600);
  sensor.calibrate();
  SerialBT.begin("SMART FAN");
  Serial.println("Bluetooth initialized");

  int relayPins[] = {RELAY_A_PIN, RELAY_B_PIN, RELAY_D_PIN, RELAY_E_PIN};
  for (int i = 0; i < 4; ++i) {
    pinMode(relayPins[i], OUTPUT);
    digitalWrite(relayPins[i], relayStates[i]); // Ensure all relays are initially off
  }
}

void toggleRelay(int pin, int& state) {
  state = !state; // Toggle relay state
  digitalWrite(pin, state);
  Serial.print("Relay state toggled: ");
  Serial.println(state);
}

void loop() {
  if (SerialBT.available()) {
    receivedChar = SerialBT.read();
    Serial.print("Received char: ");
    Serial.println(receivedChar);

    switch (appState) {
      case WAITING_FOR_E:
        if (receivedChar == 'e') appState = RELAY_CONTROL;
        break;
      case RELAY_CONTROL:
        switch (receivedChar) {
          case 'v': if (relayStates[0] == LOW) controlRelay(RELAY_A_PIN, relayStates[0]); break;
          case 'w': if (relayStates[1] == LOW) controlRelay(RELAY_B_PIN, relayStates[1]); break;
          case 'x': if (relayStates[2] == LOW) controlRelay(RELAY_D_PIN, relayStates[2]); break;
          case 'y': if (relayStates[3] == LOW) controlRelay(RELAY_E_PIN, relayStates[3]); break;
          case 'E': appState = WAITING_FOR_E; turnOffAllRelays(); break;
        }
        break;
    }
  }
 sensor.calibrate();
  float U = 220;
  float I = sensor.getCurrentAC();
  float P = U * I;
  Serial.println(I);
  Serial.println(String("P = ") + P + " Watts");
  delay(1000);
}

void turnOffAllRelays() {
  int relayPins[] = {RELAY_A_PIN, RELAY_B_PIN, RELAY_D_PIN, RELAY_E_PIN};
  for (int i = 0; i < 4; ++i) {
    relayStates[i] = LOW;
    digitalWrite(relayPins[i], relayStates[i]);
  }
}

void controlRelay(int pin, int& state) {
  turnOffAllRelays();
  toggleRelay(pin, state);
}

Are you starting the app from scratch or you already have some draft project? If you are starting from scratch there's plenty of finished basic projects to rely on. If you already started the app and you are stuck somewhere provide your blocks so we can help.


i dont know where to start in getting the acs712 data

Be sure to use println() at the end of each message to send from the sending device, to signal end of message.

Only use print() in the middle of a message.

Be sure not to println() in the middle of a message, or you will break it into two short messages and mess up the item count after you split the message in AI2.

Do not rely on timing for this, which is unreliable.

In the AI2 Designer, set the Delimiter attribute of the BlueTooth Client component to 10 to recognize the End of Line character.
BlueToothClient1_Properties
Also, return data is not immediately available after sending a request,
you have to start a Clock Timer repeating and watch for its arrival in the Clock Timer event. The repeat rate of the Clock Timer should be faster than the transmission rate in the sending device, to not flood the AI2 buffers.

In your Clock Timer, you should check

  Is the BlueTooth Client still Connected?
  Is Bytes Available > 0?
     IF Bytes Available > 0 THEN
       set message var  to BT.ReceiveText(-1) 

This takes advantage of a special case in the ReceiveText block:

ReceiveText(numberOfBytes)
Receive text from the connected Bluetooth device. If numberOfBytes is less than 0, read until a delimiter byte value is received.

If you are sending multiple data values per message separated by | or comma, have your message split into a local or global variable for inspection before trying to select list items from it. Test if (length of list(split list result) >= expected list length) before doing any select list item operations, to avoid taking a long walk on a short pier. This bulletproofing is necessary in case your sending device sneaks in some commentary messages with the data values.

Some people send temperature and humidity in separate messages with distinctive prefixes like "t:" (for temperature) and "h:" (for humidity).
(That's YAML format.)

The AI2 Charts component can recognize these and graph them. See Bluetooth Client Polling Rate - #12 by ABG

To receive YAML format messages, test if the incoming message contains ':' . If true, split it at ':' into a list variable, and find the prefix in item 1 and the value in item 2.

float U = 220;
  float I = sensor.getCurrentAC();
  float P = U * I;
  Serial.print(":");
  Serial.print(I);
  Serial.print(":");
  Serial.print(String("P = ") + P + " Watts");
   Serial.println(P);
  delay(1000);

have zero knowledge about this. i tried watching youtube but didnt work

Start out by looking for a BlueTooth terminal program on the Play Store, and use that to make sure your hardware is sending messages to BlueTooth.

I am not convinced you know the difference between Serial and SerialBT.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.