1
0
mirror of https://github.com/daniviga/bite.git synced 2024-11-22 21:16:12 +01:00

Merge pull request #24 from daniviga/esp32v2

Add an ESP32 example that collects HALL effect and WiFi RSSI
This commit is contained in:
Daniele Viganò 2021-03-24 23:33:58 +01:00 committed by GitHub
commit f7e45a5531
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
13 changed files with 9209 additions and 10 deletions

1
.gitignore vendored
View File

@ -132,3 +132,4 @@ dmypy.json
## ##
production.py production.py
settings.h

View File

@ -20,23 +20,19 @@
#include <EEPROM.h> #include <EEPROM.h>
#include <Ethernet.h> #include <Ethernet.h>
#include "settings.h"
#define ERASE_FIRST 0 #define ERASE_FIRST 0
const byte mac[] = { const byte mac[] = {
0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED
}; };
const char serial[] = "abcd1234"; const char serial[] = SERIAL;
struct netConfig { struct netConfig {
IPAddress address; IPAddress address = REMOTE_IP;
unsigned int port; unsigned int port = REMOTE_PORT;
}; } config;
netConfig config = {
{192, 168, 10, 123},
80
};
void setup() { void setup() {

View File

@ -0,0 +1,23 @@
/* -*- coding: utf-8 -*-
* vim: tabstop=2 shiftwidth=2 softtabstop=2 syntax=c
*
* BITE - A Basic/IoT/Example
* Copyright (C) 2020-2021 Daniele Viganò <daniele@vigano.me>
*
* BITE is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* BITE is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#define SERIAL "uno_1"
#define REMOTE_IP {192, 168, 0, 1}
#define REMOTE_PORT 80

View File

@ -91,7 +91,10 @@ void setup(void) {
Serial.print("Connecting to: "); Serial.print("Connecting to: ");
Serial.print(config.address); Serial.print(config.address);
Serial.print(":"); Serial.print(":");
Serial.println(config.port); Serial.print(config.port);
Serial.print(" every ");
Serial.print(TELEMETRY_DELAY);
Serial.println("s");
#if USE_INTERNAL_NTP #if USE_INTERNAL_NTP
timeClient.setPoolServerIP(config.address); timeClient.setPoolServerIP(config.address);

1
esp32/libraries/ArduinoJson Symbolic link
View File

@ -0,0 +1 @@
../../arduino/libraries/ArduinoJson

1
esp32/libraries/Ethernet Symbolic link
View File

@ -0,0 +1 @@
../../arduino/libraries/Ethernet

1
esp32/libraries/NTPClient Symbolic link
View File

@ -0,0 +1 @@
../../arduino/libraries/NTPClient

View File

@ -0,0 +1 @@
../../arduino/libraries/pubsubclient

File diff suppressed because one or more lines are too long

View File

@ -0,0 +1,4 @@
requests
jupyter
pandas
ipywidgets

View File

@ -0,0 +1,75 @@
/* -*- coding: utf-8 -*-
* vim: tabstop=2 shiftwidth=2 softtabstop=2
*
* BITE - A Basic/IoT/Example
* Copyright (C) 2020-2021 Daniele Viganò <daniele@vigano.me>
*
* BITE is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* BITE is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <Preferences.h>
#include <Ethernet.h>
#include "settings.h"
#define ERASE_FIRST 1
Preferences preferences;
const char* serial = SERIAL;
const char* ssid = SECRET_SSID;
const char* password = SECRET_PASSWORD;
struct netConfig {
IPAddress address = REMOTE_IP;
unsigned int port = REMOTE_PORT;
} config;
void setup() {
Serial.begin(115200);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
preferences.begin("iot", false);
#if ERASE_FIRST
Serial.println("Erasing IoT data");
preferences.clear();
#endif
Serial.print("Writing IoT data");
preferences.putString("serial", serial);
Serial.print(".");
Serial.println(".");
preferences.putBytes("config", &config, sizeof(config));
Serial.println("Committing...");
preferences.end();
Serial.println("IoT data written!");
preferences.begin("wifi", false);
#if ERASE_FIRST
Serial.println("Erasing WiFI data");
preferences.clear();
#endif
Serial.println("Writing WiFi data");
preferences.putString("ssid", ssid);
preferences.putString("password", password);
Serial.println("Committing...");
preferences.end();
Serial.println("WiFi data written!");
}
void loop() {
/* Empty loop */
}

View File

@ -0,0 +1,25 @@
/* -*- coding: utf-8 -*-
* vim: tabstop=2 shiftwidth=2 softtabstop=2 syntax=c
*
* BITE - A Basic/IoT/Example
* Copyright (C) 2020-2021 Daniele Viganò <daniele@vigano.me>
*
* BITE is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* BITE is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#define SERIAL "<fillme>"
#define REMOTE_IP {192, 168, 0, 1}
#define REMOTE_PORT 80
#define SECRET_SSID "<fillme>"
#define SECRET_PASSWORD "<fillme>"

197
esp32/rssiHall/rssiHall.ino Normal file
View File

@ -0,0 +1,197 @@
/* -*- coding: utf-8 -*-
* vim: tabstop=2 shiftwidth=2 softtabstop=2
*
* BITE - A Basic/IoT/Example
* Copyright (C) 2020-2021 Daniele Viganò <daniele@vigano.me>
*
* BITE is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* BITE is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <Preferences.h>
#include <WiFi.h>
#include <WiFiUdp.h>
#include <PubSubClient.h>
#include <NTPClient.h>
#include <ArduinoJson.h>
#define DEBUG_TO_SERIAL 1 // debug on serial port
#define USE_MQTT 1 // use mqtt protocol instead of http post
#define USE_INTERNAL_NTP 1 // use default ntp server or the internal one
#define TELEMETRY_DELAY 10 // second between telemetry samples
// const String serverName = "sensor.server.domain";
const size_t capacity = 2 * JSON_OBJECT_SIZE(3) + JSON_OBJECT_SIZE(2) + 20;
Preferences preferences;
DynamicJsonDocument telemetry(capacity);
JsonObject payload = telemetry.createNestedObject("payload");
unsigned int counter = 0;
WiFiUDP ntpUDP;
NTPClient timeClient(ntpUDP);
bool NTPValid = false;
WiFiClient ethClient;
PubSubClient clientMQTT(ethClient);
struct netConfig {
IPAddress address;
unsigned int port;
} config;
char* serial;
const String apiURL = "/api/device/subscribe/";
const String telemetryURL = "/telemetry/";
void setup(void) {
Serial.begin(115200);
StaticJsonDocument<64> api;
preferences.begin("iot");
// Get the serial number from flash
serial = strdup(preferences.getString("serial").c_str());
// Get network configuration
size_t _len = preferences.getBytesLength("config");
char _buffer[_len];
preferences.getBytes("config", &_buffer, _len);
memcpy(&config, _buffer, _len);
preferences.end();
// Get WiFi parameters
preferences.begin("wifi");
const char* _ssid = strdup(preferences.getString("ssid").c_str());
const char* _password = strdup(preferences.getString("password").c_str());
preferences.end();
Serial.print("Starting connecting WiFi to ");
Serial.print(_ssid);
delay(10);
WiFi.begin(_ssid, _password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("\nWiFi connected");
Serial.print("IoT #");
Serial.print(serial);
Serial.print(" at address: ");
Serial.println(WiFi.localIP());
Serial.println();
Serial.print("Connecting to: ");
Serial.print(config.address.toString());
Serial.print(":");
Serial.print(config.port);
Serial.print(" every ");
Serial.print(TELEMETRY_DELAY);
Serial.println("s");
#if USE_INTERNAL_NTP
timeClient.setPoolServerIP(config.address);
#endif
timeClient.begin();
if (timeClient.update()) {
NTPValid = true;
}
#if DEBUG_TO_SERIAL
Serial.println("DEBUG: clock updated via NTP.");
#endif
api["serial"] = serial;
postData(config, apiURL, api);
telemetry["device"] = serial;
// payload["id"] = serverName;
#if USE_MQTT
clientMQTT.setServer(config.address, 1883);
#endif
}
void loop(void) {
const int postDelay = TELEMETRY_DELAY * 1000;
if (NTPValid) {
telemetry["clock"] = timeClient.getEpochTime();
} else {
telemetry["clock"] = NULL; // converted into 0
}
payload["hall"] = hallRead();
payload["wifi-rssi"] = WiFi.RSSI();
#if USE_MQTT
publishData(config, telemetry);
#else
postData(config, telemetryURL, telemetry);
#endif
if (counter == 6 * 120) { // Update clock every 6 times * 10 sec * 120 minutes = 2 hrs
timeClient.update();
counter = 0;
#if DEBUG_TO_SERIAL
Serial.println("DEBUG: clock updated via NTP.");
#endif
} else {
counter++;
}
delay(postDelay);
}
#if USE_MQTT
void publishData(const netConfig &mqtt, const DynamicJsonDocument &json) {
if (clientMQTT.connect(serial)) {
char buffer[256];
serializeJson(json, buffer);
clientMQTT.publish(serial, buffer);
#if DEBUG_TO_SERIAL
Serial.println("DEBUG: MQTT PUBLISH>>>");
serializeJsonPretty(json, Serial);
Serial.println("\n<<<");
#endif
}
}
#endif
void postData(const netConfig &postAPI, const String &URL, const DynamicJsonDocument &json) {
if (ethClient.connect(postAPI.address, postAPI.port)) {
ethClient.print("POST ");
ethClient.print(URL);
ethClient.println(" HTTP/1.1");
ethClient.print("Host: ");
ethClient.print(postAPI.address.toString());
ethClient.print(":");
ethClient.println(postAPI.port);
ethClient.println("Content-Type: application/json");
ethClient.print("Content-Length: ");
ethClient.println(measureJson(json));
ethClient.println("Connection: close");
ethClient.println();
serializeJson(json, ethClient);
ethClient.stop();
#if DEBUG_TO_SERIAL
Serial.println("DEBUG: HTTP POST>>>");
serializeJsonPretty(json, Serial);
Serial.println("\n<<<");
#endif
}
}