Skip to content

Commit 53b47bb

Browse files
committedJan 22, 2021
initial commit
0 parents  commit 53b47bb

21 files changed

+5849
-0
lines changed
 

‎.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
temp
2+
*/.ipynb_checkpoints/

‎2-ESP32/esp32-ds18b20-living_room.ino

+193
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,193 @@
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+
}

‎2-ESP32/include.h

+69
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
void sendData(String preambula, float val){
2+
3+
char result[6]; // Buffer big enough for 7-character float
4+
//sprintf(result, "%-3.1f", val);
5+
sprintf(result, "%-3.2f", val);
6+
7+
HTTPClient http;
8+
9+
String serverPath = apiurl + String(preambula) + String("+value=") + String(result);;
10+
// Serial.print(serverPath);
11+
12+
// Your Domain name with URL path or IP address with path
13+
http.begin(serverPath.c_str());
14+
15+
16+
// Send HTTP GET request
17+
int httpResponseCode = http.GET();
18+
19+
if (httpResponseCode>0) {
20+
Serial.print("HTTP Response code: ");
21+
Serial.println(httpResponseCode);
22+
String payload = http.getString();
23+
Serial.println(payload);
24+
}
25+
else {
26+
Serial.print("Error code: ");
27+
Serial.println(httpResponseCode);
28+
Serial.printf("[HTTP] GET... failed, error: %s", http.errorToString(httpResponseCode).c_str());
29+
}
30+
// Free resources
31+
http.end();
32+
33+
}
34+
35+
36+
37+
38+
// --------------------------------------------------------------------- //
39+
40+
41+
String httpGETRequest(const char* serverName) {
42+
HTTPClient http;
43+
44+
// Your IP address with path or Domain name with URL path
45+
Serial.println(serverName);
46+
http.begin(serverName);
47+
48+
// Send HTTP POST request
49+
int httpResponseCode = http.GET();
50+
51+
String payload = "{}";
52+
53+
if (httpResponseCode>0) {
54+
Serial.print("HTTP Response code: ");
55+
Serial.println(httpResponseCode);
56+
payload = http.getString();
57+
}
58+
else {
59+
Serial.print("Error code: ");
60+
Serial.println(httpResponseCode);
61+
}
62+
// Free resources
63+
http.end();
64+
65+
return String(payload);
66+
// return String("OK!");
67+
}
68+
69+
// --------------------------------------------------------------------- //

‎3-server/3-models/20200110-MLP.pkl

23.5 KB
Binary file not shown.

‎3-server/3-models/20200118-MLP.pkl

12.3 KB
Binary file not shown.

‎3-server/cat-localizer.yml

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
name: cat-localizer
2+
channels:
3+
- conda-forge
4+
- main
5+
dependencies:
6+
- scikit-learn=0.24.0=py38h658cfdd_0
7+
- scikit-optimize=0.8.1=pyh9f0ad1d_0
8+
- scipy=1.6.0=py38hb2138dd_0
9+
- seaborn
10+
- matplotlib
11+
- pandas
12+
- numpy=1.19.5=py38h18fd61f_1

‎3-server/localize-cat.py

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#!/usr/bin/env python
2+
# -*- coding: utf-8 -*-
3+
4+
import joblib
5+
import numpy as np
6+
7+
def localizeTheCat(rssis):
8+
# labels = room ids
9+
labels = loaded_model.classes_.tolist()
10+
11+
# where is the cat - location
12+
where_is_a_cat = loaded_model.predict(rssis)[0].tolist()
13+
14+
# probabilities for each room
15+
where_is_a_cat_p = loaded_model.predict_proba(rssis)[0].tolist()
16+
17+
# prediction probability:
18+
location_p = round(where_is_a_cat_p[labels.index(where_is_a_cat)] * 100, 1)
19+
return (where_is_a_cat, location_p)
20+
21+
# -------------------------------------------------------------------- #
22+
23+
if __name__ == "__main__":
24+
# load models
25+
filename = '3-models/20200118-MLP.pkl'
26+
loaded_model = joblib.load(filename)
27+
28+
# input RSSI array, fetch from the database or file with last measurements
29+
rssis = np.array([[-70, -70, -30, -110]])
30+
31+
# let's make a prediction!
32+
where_is_a_cat, location_p = localizeTheCat(rssis)
33+
34+
# and output the results
35+
print("The cat is in the %s with p=%s%%" % (where_is_a_cat, location_p))

0 commit comments

Comments
 (0)
Please sign in to comment.