|
| 1 | +// esp32 lite 4mb |
| 2 | +// wemos lolin 32 |
| 3 | +// speed 460k |
| 4 | +// Partition - no OTA |
| 5 | + |
| 6 | +#include <HTTPClient.h> |
| 7 | + |
| 8 | +#include <WiFiUdp.h> |
| 9 | +#include <NTPClient.h> |
| 10 | + |
| 11 | +#include <BLEDevice.h> |
| 12 | +#include <BLEUtils.h> |
| 13 | +#include <BLEScan.h> |
| 14 | +#include <BLEAdvertisedDevice.h> |
| 15 | + |
| 16 | + |
| 17 | +#include <WiFiUdp.h> |
| 18 | + |
| 19 | +// sensors and extras |
| 20 | +#include <OneWire.h> |
| 21 | +#include <DallasTemperature.h> |
| 22 | +#define ONE_WIRE_BUS 19 |
| 23 | + |
| 24 | + |
| 25 | +OneWire oneWire(ONE_WIRE_BUS); |
| 26 | +DallasTemperature sensors(&oneWire); |
| 27 | + |
| 28 | + |
| 29 | + |
| 30 | +// --------------------------------------------------------------------- // |
| 31 | +// WiFi settings |
| 32 | +const char* ssid = "***** ***"; |
| 33 | +const char* password = "***** ***"; |
| 34 | + |
| 35 | +// api url for the influx gateway + token |
| 36 | +String apiurl = "http://your_api_url/&message="; |
| 37 | + |
| 38 | +// host name |
| 39 | +const char* esphost = "esp32-living_room"; |
| 40 | + |
| 41 | +// MAC address(es) of the BLE beacons |
| 42 | +const char* cat1Mac = "xx:xx:cc:dd:ee:ff"; |
| 43 | +const char* cat2Mac = "yy:yy:cc:dd:ff:gg"; |
| 44 | + |
| 45 | +// RSSI values in case no beacons are found |
| 46 | +// here, we are looking for two cats, so two values |
| 47 | +int cat1RSSI = -110; |
| 48 | +int cat2RSSI = -110; |
| 49 | + |
| 50 | +// BLE scan time |
| 51 | +int scanTime = 11; //BLE scanning time, In seconds |
| 52 | + |
| 53 | + |
| 54 | +// --------------------------------------------------------------------- // |
| 55 | + |
| 56 | +#include "include.h" |
| 57 | + |
| 58 | +// ------------------------------------------------------------------------------- // |
| 59 | + |
| 60 | + |
| 61 | +// class for BLE scanning |
| 62 | +BLEScan* pBLEScan; |
| 63 | + |
| 64 | +class MyAdvertisedDeviceCallbacks: public BLEAdvertisedDeviceCallbacks { |
| 65 | + |
| 66 | + void onResult(BLEAdvertisedDevice advertisedDevice) { |
| 67 | + Serial.printf("Advertised Device: %s \n", advertisedDevice.toString().c_str()); |
| 68 | + Serial.println(""); |
| 69 | + Serial.printf("Advertised Device mac %s \n", advertisedDevice.getAddress().toString().c_str()); |
| 70 | + Serial.println(""); |
| 71 | + Serial.printf("Advertised Device rssi %i \n", advertisedDevice.getRSSI()); |
| 72 | + } |
| 73 | +}; |
| 74 | + |
| 75 | + |
| 76 | +// ------------------------------------------------------------------------------- // |
| 77 | + |
| 78 | + |
| 79 | + |
| 80 | +void setup() { |
| 81 | + |
| 82 | + delay(10); |
| 83 | + unsigned long startTime = millis(); |
| 84 | + Serial.begin(115200); |
| 85 | + |
| 86 | + // turn off blinking |
| 87 | + pinMode(LED_BUILTIN, OUTPUT); |
| 88 | + digitalWrite(LED_BUILTIN, HIGH); |
| 89 | + |
| 90 | + // sensory |
| 91 | + sensors.begin(); |
| 92 | + |
| 93 | + // wifi |
| 94 | + WiFi.setHostname(esphost); |
| 95 | + |
| 96 | + int attemptsCount = 0; |
| 97 | + WiFi.begin(ssid, password); |
| 98 | + while (WiFi.status() != WL_CONNECTED) { |
| 99 | + delay(500); |
| 100 | + attemptsCount++; |
| 101 | + if (attemptsCount >= 10) { |
| 102 | + ESP.restart(); |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + |
| 107 | + // time server settings |
| 108 | + WiFiUDP ntpUDP; |
| 109 | + NTPClient timeClient(ntpUDP, "de.pool.ntp.org", 7200, 60000); |
| 110 | + timeClient.begin(); |
| 111 | + timeClient.update(); |
| 112 | + |
| 113 | + |
| 114 | + // temperature reading from ds18b20 |
| 115 | + sensors.requestTemperatures(); // Send the command to get temperatures |
| 116 | + delay(900); |
| 117 | + float temperature = sensors.getTempCByIndex(0); |
| 118 | + |
| 119 | + if ((temperature > -80) and (temperature < 80)) { |
| 120 | + Serial.println(temperatura); |
| 121 | + // send influx data |
| 122 | + sendData("temp,location=home,source=esp32-living_room,room=living_room", temperature); |
| 123 | + } |
| 124 | + else { |
| 125 | + Serial.print("doesnt work: "); |
| 126 | + Serial.println(temperature); |
| 127 | + |
| 128 | + } |
| 129 | + |
| 130 | + |
| 131 | + // skan for BLE |
| 132 | + doBLEScans(); |
| 133 | + |
| 134 | + // go to sleep |
| 135 | + int sleepTimeS = 59-timeClient.getSeconds(); |
| 136 | +// int sleepTimeS = 45; |
| 137 | + ESP.deepSleep(1e6 * sleepTimeS); |
| 138 | +} |
| 139 | + |
| 140 | + |
| 141 | + |
| 142 | +void loop() { |
| 143 | +} |
| 144 | + |
| 145 | + |
| 146 | +void doBLEScans() { |
| 147 | + Serial.println("Scanning..."); |
| 148 | + |
| 149 | + BLEDevice::init(""); |
| 150 | + pBLEScan = BLEDevice::getScan(); //create new scan |
| 151 | + pBLEScan->setAdvertisedDeviceCallbacks(new MyAdvertisedDeviceCallbacks()); |
| 152 | + pBLEScan->setActiveScan(true); //active scan uses more power, but get results faster |
| 153 | + pBLEScan->setInterval(100); |
| 154 | + pBLEScan->setWindow(99); // less or equal setInterval value |
| 155 | + |
| 156 | + // put your main code here, to run repeatedly: |
| 157 | + BLEScanResults foundDevices = pBLEScan->start(scanTime, false); |
| 158 | + Serial.print("Devices found: "); |
| 159 | + Serial.println(foundDevices.getCount()); |
| 160 | + Serial.println("Scan done!"); |
| 161 | + BLEDevice::deinit(false); // turn off ble to make wifi great again. |
| 162 | + |
| 163 | + int count = foundDevices.getCount(); |
| 164 | + if (count > 0) { |
| 165 | + |
| 166 | + for (int i = 0; i < count; i++) { |
| 167 | + BLEAdvertisedDevice d = foundDevices.getDevice(i); |
| 168 | + |
| 169 | + // ----------------------------- miko ------------------------------------ // |
| 170 | + if (d.getAddress().toString() == cat1Mac) { |
| 171 | + // jest miko |
| 172 | + cat1RSSI = d.getRSSI(); |
| 173 | + } |
| 174 | + |
| 175 | + // ----------------------------- figa ------------------------------------ // |
| 176 | + if (d.getAddress().toString() == cat2Mac) { |
| 177 | + // jest figa |
| 178 | + cat2RSSI = d.getRSSI(); |
| 179 | + } |
| 180 | + |
| 181 | + } // for each device |
| 182 | + |
| 183 | + // talk with server |
| 184 | + sendData("rssi,location=home,source=esp32-living_room,room=living_room,kto=cat1", cat1RSSI); |
| 185 | + sendData("rssi,location=home,source=esp32-living_room,room=living_room,kto=cat2", cat2RSSI); |
| 186 | + |
| 187 | + |
| 188 | + } // non-zero devices found |
| 189 | + pBLEScan->clearResults(); // delete results fromBLEScan buffer to release memory |
| 190 | + |
| 191 | + delay(100); |
| 192 | + |
| 193 | +} |
0 commit comments