Created
July 28, 2024 13:33
-
-
Save EDISON-SCIENCE-CORNER/b62682b0e02f3606a36fccee063c8b21 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #define BLYNK_TEMPLATE_ID "TMPL3zt3E3vMt" | |
| #define BLYNK_TEMPLATE_NAME "temp" | |
| #define BLYNK_AUTH_TOKEN "HTWA-NwX2X-EQy2XglVjx2o40LTtWYCA" | |
| #define BLYNK_PRINT Serial | |
| #include <WiFi.h> | |
| //#include <ESP8266WiFi.h> | |
| #include <BlynkSimpleEsp32.h> | |
| char auth[] = BLYNK_AUTH_TOKEN; | |
| char ssid[] = "edison science corner"; // type your wifi name | |
| char pass[] = "eeeeeeee"; // type your wifi password | |
| BlynkTimer timer; | |
| #include <Arduino.h> | |
| #include <BLEDevice.h> | |
| #include <BLEUtils.h> | |
| #include <BLEScan.h> | |
| #include <BLEAdvertisedDevice.h> | |
| #include <BLEEddystoneURL.h> | |
| #include <BLEEddystoneTLM.h> | |
| #include <BLEBeacon.h> | |
| #define ENDIAN_CHANGE_U16(x) ((((x)&0xFF00) >> 8) + (((x)&0xFF) << 8)) | |
| float Sensor_Data; | |
| int scanTime = 5; //In seconds | |
| int percentage1; | |
| int percentage2; | |
| BLEScan *pBLEScan; | |
| class MyAdvertisedDeviceCallbacks : public BLEAdvertisedDeviceCallbacks | |
| { | |
| void onResult(BLEAdvertisedDevice advertisedDevice) | |
| { | |
| if (advertisedDevice.haveName()) | |
| { | |
| if(String(advertisedDevice.getName().c_str()) == "SENSOR 1"){ | |
| Serial.print("Device name: "); | |
| Serial.println(advertisedDevice.getName().c_str()); | |
| std::string strManufacturerData = advertisedDevice.getManufacturerData(); | |
| uint8_t cManufacturerData[100]; | |
| strManufacturerData.copy((char *)cManufacturerData, strManufacturerData.length(), 0); | |
| Serial.printf("strManufacturerData: %d ", strManufacturerData.length()); | |
| for (int i = 0; i < strManufacturerData.length(); i++) | |
| { | |
| Serial.printf("[%X]", cManufacturerData[i]); | |
| } | |
| Sensor_Data = int(cManufacturerData[2]<<8 | cManufacturerData[3]); | |
| percentage1= map(Sensor_Data,7500,200,0,100); | |
| Blynk.virtualWrite(V1, percentage1); | |
| Serial.println(); | |
| Serial.print("MOISTURE LEVEL:");Serial.print(percentage1);Serial.println("%"); | |
| Serial.println("------------------"); | |
| } | |
| if(String(advertisedDevice.getName().c_str()) == "SENSOR 2"){ | |
| Serial.print("Device name: "); | |
| Serial.println(advertisedDevice.getName().c_str()); | |
| std::string strManufacturerData = advertisedDevice.getManufacturerData(); | |
| uint8_t cManufacturerData[100]; | |
| strManufacturerData.copy((char *)cManufacturerData, strManufacturerData.length(), 0); | |
| Serial.printf("strManufacturerData: %d ", strManufacturerData.length()); | |
| for (int i = 0; i < strManufacturerData.length(); i++) | |
| { | |
| Serial.printf("[%X]", cManufacturerData[i]); | |
| } | |
| Sensor_Data = int(cManufacturerData[2]<<8 | cManufacturerData[3]); | |
| percentage2= map(Sensor_Data,50,3000,0,100); | |
| Blynk.virtualWrite(V2, percentage2); | |
| Serial.println(); | |
| Serial.print("MOISTURE LEVEL:");Serial.print(percentage2);Serial.println("%"); | |
| Serial.println("------------------"); | |
| } | |
| } | |
| } | |
| }; | |
| void setup() | |
| { | |
| Serial.begin(115200); | |
| Serial.println("Scanning..."); | |
| BLEDevice::init(""); | |
| pBLEScan = BLEDevice::getScan(); //create new scan | |
| pBLEScan->setAdvertisedDeviceCallbacks(new MyAdvertisedDeviceCallbacks()); | |
| pBLEScan->setActiveScan(true); //active scan uses more power, but get results faster | |
| pBLEScan->setInterval(100); | |
| pBLEScan->setWindow(99); // less or equal setInterval value | |
| Blynk.begin(auth, ssid, pass); | |
| } | |
| void loop() | |
| { | |
| BLEScanResults foundDevices = pBLEScan->start(scanTime, false); | |
| pBLEScan->clearResults(); // delete results fromBLEScan buffer to release memory | |
| delay(2000); | |
| Blynk.run(); | |
| timer.run(); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment