Worked on firmware

This commit is contained in:
Martin Bauer
2019-08-22 21:33:36 +02:00
parent 9889d41805
commit 6a724b284f
24 changed files with 422 additions and 209 deletions

10
src/ConfigHardware.h Normal file
View File

@@ -0,0 +1,10 @@
#include <cstdint>
// HX711 load cell
const int CONFIG_SCALE_DOUT_PIN = D2;
const int CONFIG_SCALE_SCK_PIN = D3;
const uint8_t CONFIG_TARE_AVG_COUNT = 50; // number of measurements in tare-phase (to find 0 )
const int CONFIG_VALUE_DIVIDER = 128; // uint32 measurements are divided by this factor, before stored in uint16_t
const uint32_t CONFIG_SESSION_CHUNK_SIZE = 1024*8 - 3 * sizeof(uint32_t);

5
src/ConfigWifi.h Normal file
View File

@@ -0,0 +1,5 @@
const char *CONFIG_WIFI_SSID = "WLAN";
const char *CONFIG_WIFI_PASSWORD = "Bau3rWLAN";
const char* CONFIG_HOSTNAME = "smartcords";

9
src/config.h Normal file
View File

@@ -0,0 +1,9 @@
// Measurement parameters
const int DELAY = 100; // interval in ms between measurements
const int SESSION_SIZE = 1024*8; // how many data points are added to the session
const byte MEASUREMENT_AVG_COUNT = 1; // averages over this many consecutive AD-converter reads
const byte TARE_AVG_COUNT = 50; // number of measurements in tare-phase (to find 0 )
const int DIVIDER = 128; // uint32 measurements are divided by this factor, before stored in uint16_t

123
src/firmware_main.cpp Normal file
View File

@@ -0,0 +1,123 @@
// Arduino & ESP headers
#include <Arduino.h>
#include <ESP8266WiFi.h>
#include <ESPAsyncTCP.h>
#include <ESPAsyncWebServer.h>
#include <WiFiUdp.h> // for NTP
#include <NTPClient.h> // for NTP
// Own libs
#include "Dtypes.h"
#include "MockScale.h"
#include "MeasurementSession.h"
#include "SpiffsStorage.h"
// Configuration
#include "ConfigWifi.h"
#include "ConfigHardware.h"
AsyncWebServer server(80);
WiFiUDP ntpUDP;
NTPClient timeClient(ntpUDP, "pool.ntp.org");
typedef MeasurementSession<uint16_t, SpiffsStorageReader, SpiffsStorageWriter, CONFIG_SESSION_CHUNK_SIZE> Session_T;
template<typename Session_T>
class SessionManager
{
public:
void begin();
void startMeasurements();
void stopMeasurements();
void iteration();
private:
MockScale scale;
Session_T session;
};
SessionManager<Session_T> sessionManager;
void onNotFound(AsyncWebServerRequest *request) {
request->send(404, "text/plain", "Not found");
}
template<typename Session_T>
void httpSetup(SessionManager<Session_T> * sessionManager)
{
server.on("/api/session/start", HTTP_POST, [sessionManager](AsyncWebServerRequest * req) {
req->send(200, "text/plain", F("OK"));
sessionManager->startMeasurements();
});
server.on("/api/session/stop", HTTP_POST, [sessionManager](AsyncWebServerRequest * req) {
req->send(200, "text/plain", F("OK"));
sessionManager->stopMeasurements();
});
server.on("/api/session/data", HTTP_GET, [sessionManager](AsyncWebServerRequest * req) {
//TODO
});
server.on("/", HTTP_GET, [](AsyncWebServerRequest *request){
request->send(200, "text/plain", "Hello, world");
});
// Send a GET request to <IP>/get?message=<message>
server.on("/get", HTTP_GET, [] (AsyncWebServerRequest *request) {
String message;
if (request->hasParam(PARAM_MESSAGE)) {
message = request->getParam(PARAM_MESSAGE)->value();
} else {
message = "No message sent";
}
request->send(200, "text/plain", "Hello, GET: " + message);
});
// Send a POST request to <IP>/post with a form field message set to <message>
server.on("/post", HTTP_POST, [](AsyncWebServerRequest *request){
String message;
if (request->hasParam(PARAM_MESSAGE, true)) {
message = request->getParam(PARAM_MESSAGE, true)->value();
} else {
message = "No message sent";
}
request->send(200, "text/plain", "Hello, POST: " + message);
});
server.onNotFound(onNotFound);
server.begin();
}
void setup()
{
// Serial
Serial.begin(115200);
while(!Serial) {}
// WiFi
WiFi.mode(WIFI_STA);
WiFi.begin(CONFIG_WIFI_SSID, CONFIG_WIFI_PASSWORD);
Serial.print(F("\n\n"));
Serial.println(F("Waiting for WIFI connection..."));
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
}
Serial.print(F("Connected to WiFi. IP:"));
Serial.println(WiFi.localIP());
// NTP
timeClient.begin();
timeClient.update();
session.init( timeClient.getEpochTime() );
// Scale
scale.begin(CONFIG_SCALE_DOUT_PIN, CONFIG_SCALE_SCK_PIN);
scale.tare( CONFIG_TARE_AVG_COUNT );
// HTTP & Websocket server
httpSetup();
}
void loop() {
}