//11/07/2022 00:34:39:02 FanControl4.ino //Arduino Nano 33 BLE vehicle data to Android App #include const int RadPin = A0; // Radiator Input Thermister const int CndPin = A1; // Condenser Input Thermister const int AuxPin = A2; // Auxilary Input Not used at this time const int FanPin = 9; // Set Fan pin const long Period = 10000; // Recheck period long previousMillis = 0; // last time the temperature levels were checked, in ms #define BLE_SERVICE "0000FFE0-0000-1000-8000-00805F9B34FB" #define BLE_CHARACTERISTIC "0000FFE1-0000-1000-8000-00805F9B34FB" /* Temperatur Monitor This example creates a BluetoothLow Energy peripheral with the standard Temperature service and level characteristic. Pins A0, A1 and A2 are used to calculate the temperatures of 3 sensors. The data is calculated by the time set in the constant Period. The circuit: - Arduino MKR WiFi 1010, Arduino Uno WiFi Rev2 board, Arduino Nano 33 IoT, Arduino Nano 33 BLE, or Arduino Nano 33 BLE Sense board. You can use a generic Bluetooth Low Energy central app, like LightBlue (iOS and Android) or nRF Connect (Android), to interact with the services and characteristics created in this sketch. This example code is in the public domain. */ // Define the Temperature Service BLEService TempService(BLE_SERVICE); // BluetoothLE Temperature Service // Define Characteristics BLEUnsignedIntCharacteristic TempCharacteristic(BLE_CHARACTERISTIC, BLEWrite | BLENotify); void setup() { Serial.begin(9600); // initialize serial communication with PC Arduino Serial Monitor pinMode(LED_BUILTIN, OUTPUT); // initialize the built-in LED pin to indicate when a central is connected // set advertised local name and service UUID: BLE.setLocalName("VehTemp"); BLE.setAdvertisedService(TempService); // add service BLE.addService(TempService); // add the characteristic to the service TempService.addCharacteristic(TempCharacteristic); // set the initial value for the characteristic TempCharacteristic.writeValue(0); // start advertising BLE.advertise(); Serial.println("BLE VehTemp Peripheral"); Serial.println("Bluetoothdevice active, waiting for connections..."); } void loop() { // wait for a BluetoothLow Energy central BLEDevice central = BLE.central(); // if a central is connected to the peripheral: if (central) { Serial.print("Connected to central: "); // print the central's BT address: Serial.println(central.address()); // turn on the LED to indicate the connection: digitalWrite(LED_BUILTIN, HIGH); // check the temperature levels every Period (ms) // while the central is connected: while (central.connected()) { // if Period ms have passed, check the temperature levels: if ((millis() - previousMillis) > Period) { previousMillis = millis(); updateTempAndFanSpeed(); } } if (!central.connected()) { // when the central disconnects, turn off the LED: digitalWrite(LED_BUILTIN, LOW); Serial.print("Disconnected from central: "); Serial.println(central.address()); } } } void updateTempAndFanSpeed() { // Read the current voltage level on each analog input pin. // This is used here to simulate the charge level of temperatures. //int Rad = analogRead(RadPin); //int Cnd = analogRead(CndPin); //int Aux = analogRead(AuxPin); //int RadLevel = map(Rad, 0, 1023, 0, 100); //int CndLevel = map(Cnd, 0, 1023, 0, 100); //int AuxLevel = map(Aux, 0, 1023, 0, 100); int RadLevel = 10; int CndLevel = 11; int AuxLevel = 12; int FanSpeed = 13; Serial.print("Rad Level % is now: "); Serial.println(RadLevel); Serial.print("Cnd Level % is now: "); Serial.println(CndLevel); Serial.print("Aux Level % is now: "); Serial.println(AuxLevel); Serial.print("Fan Speed is now: "); Serial.println(FanSpeed); TempCharacteristic.writeValue(RadLevel); // update the Radiator level characteristic in App TempCharacteristic.writeValue(CndLevel); // update the Condenser level characteristic in App TempCharacteristic.writeValue(AuxLevel); // update the Aux level characteristic in App TempCharacteristic.writeValue(FanSpeed); // update the Fan Speed characteristic in App }