diff --git a/software/Basic_Blink/Makefile-user.mk b/software/Basic_Blink/Makefile-user.mk index 07057cf..556dbe7 100644 --- a/software/Basic_Blink/Makefile-user.mk +++ b/software/Basic_Blink/Makefile-user.mk @@ -34,6 +34,7 @@ COM_SPEED = 460800 # SPI_MODE = dio ## SPIFFS options -DISABLE_SPIFFS = 1 +# DISABLE_SPIFFS = 1 # SPIFF_FILES = files +SPIFF_SIZE = 196608 diff --git a/software/Basic_Blink/app/ads101x.cpp b/software/Basic_Blink/app/ads101x.cpp index 9b2d88a..9eb5d73 100644 --- a/software/Basic_Blink/app/ads101x.cpp +++ b/software/Basic_Blink/app/ads101x.cpp @@ -8,6 +8,8 @@ #include "ads101x.h" #include +#include + namespace rijnfel { namespace ads { diff --git a/software/Basic_Blink/app/ads101x.h b/software/Basic_Blink/app/ads101x.h index 0e2d738..f8730d8 100644 --- a/software/Basic_Blink/app/ads101x.h +++ b/software/Basic_Blink/app/ads101x.h @@ -34,7 +34,7 @@ struct ads_sample_t { enum eGainAmplifier gain; }; -class cADS101x: cADC { +class cADS101x: public cADC { public: cADS101x(uint8_t i_address = 0x48); diff --git a/software/Basic_Blink/app/application.cpp b/software/Basic_Blink/app/application.cpp index 6413cc8..e95832d 100644 --- a/software/Basic_Blink/app/application.cpp +++ b/software/Basic_Blink/app/application.cpp @@ -2,65 +2,77 @@ #include #include #include "ads101x.h" +#include "sensor_hub.h" +#include "sensor_settings.h" +#include "double_buffer.h" +#include "web_interface.h" #define LED_PIN 2 // GPIO2 using namespace rijnfel; + +void STADisconnect(String ssid, uint8_t ssid_len, uint8_t bssid[6], + uint8_t reason); +void STAGotIP(IPAddress ip, IPAddress mask, IPAddress gateway); + Timer procTimer; ads::cADS101x adc; -bool state = true; -uint16_t sampless = 1; uint8_t channel = 0; -unsigned int average[4]; -void readAdc() { - //while (adc.IsReady()) { +cSensorHub hub(5); +int test = 0; +void updateSensorHub() { + hub.Update(); +} + +void adcCallback(cDoubleBuffer & buffer) { + cWebInterface::GetInstance()->UpdateAdc(adc, buffer); adc.SetMux(static_cast(ads::eInputMux::AIN_0 + channel)); channel++; if (channel > 3) { channel = 0; } - uint16_t settings = adc.GetSettings(); - Serial.printf("Settings = %d;\n\r", settings); - - if (false) { - adc.SetMux( - static_cast(ads::eInputMux::AIN_0 + channel)); - ads::ads_sample_t sample = adc.RawSample(); - average[channel] += adc.ConvertSample(sample) / 1000; //want it in mv - channel++; - if (channel > 3) { - channel = 0; - sampless++; - if (sampless > 3300) { - for (uint8_t i = 0; i < 4; i++) { - Serial.printf("C[%d] = %d; ", i, average[i] / sampless); - average[i] = 0; - } - Serial.printf("\n\r"); - sampless = 0; - } - } - } - WDT.alive(); - //} - //Serial.printf("Not ready anymore\n\r"); -} - -void blink() { - readAdc(); } void init() { + spiffs_mount(); Serial.begin(460800); - Wire.pins(5, 4); - + Wire.pins(4, 5); Wire.begin(); //SET higher CPU freq & disable wifi sleep system_update_cpu_freq(SYS_CPU_160MHZ); - WDT.enable(false); + wifi_set_sleep_type(NONE_SLEEP_T); + //WDT.enable(false); pinMode(LED_PIN, OUTPUT); adc.SetMux(ads::eInputMux::AIN_0); adc.SetSampleSpeed(ads::eSampleSpeed::SPS_3300); - procTimer.initializeMs(500, readAdc).start(); + hub.SetAdc(&adc); + + cSensorSettings * adcSettings; + adcSettings = new cSensorSettings(&adcCallback, 250, 5); + hub.SetAdcSettings(adcSettings); + + WifiEvents.onStationDisconnect(STADisconnect); + WifiEvents.onStationGotIP(STAGotIP); +/* WifiAccessPoint.setIP(IPAddress(10, 0, 0, 1)); //TODO + WifiAccessPoint.config("Sensus", "", AUTH_OPEN, false, 3);*/ + cWebInterface::GetInstance()->Start(); + + procTimer.initializeMs(100, updateSensorHub).start(); +} + +void STADisconnect(String ssid, uint8_t ssid_len, uint8_t bssid[6], + uint8_t reason) { + if (!WifiAccessPoint.isEnabled()) { + WifiStation.disconnect(); + WifiAccessPoint.enable(true); + WifiStation.connect(); + } +} + +void STAGotIP(IPAddress ip, IPAddress mask, IPAddress gateway) { + if (WifiAccessPoint.isEnabled()) { + WifiAccessPoint.enable(false); + } + // Add commands to be executed after successfully connecting to AP and got IP from it } diff --git a/software/Basic_Blink/app/double_buffer.h b/software/Basic_Blink/app/double_buffer.h index ffa1d06..5bb0e3b 100644 --- a/software/Basic_Blink/app/double_buffer.h +++ b/software/Basic_Blink/app/double_buffer.h @@ -9,6 +9,7 @@ #define APP_DOUBLE_BUFFER_H_ #include +#include namespace rijnfel { @@ -25,7 +26,11 @@ public: } void Resize(int i_newSize) { - + m_size = i_newSize; + delete m_buf[0]; + delete m_buf[1]; + m_buf[0] = new BufferType[m_size]; + m_buf[1] = new BufferType[m_size]; } bool AddValue(BufferType i_val) { @@ -39,14 +44,22 @@ public: return false; } + BufferType * GetReadyBuffer() { + return m_buf[(m_bufferIndex == 0) ? 1 : 0]; + } + BufferType * GetBuffer(int i_index) { return m_buf[i_index]; } + int GetSize() { + return m_size; + } + virtual ~cDoubleBuffer() { - delete m_buf[0]; - delete m_buf[1]; - delete m_buf; + delete[] m_buf[0]; + delete[] m_buf[1]; + delete[] m_buf; } private: int m_size; diff --git a/software/Basic_Blink/app/sensor_hub.cpp b/software/Basic_Blink/app/sensor_hub.cpp index 9adce3d..c70b926 100644 --- a/software/Basic_Blink/app/sensor_hub.cpp +++ b/software/Basic_Blink/app/sensor_hub.cpp @@ -27,6 +27,13 @@ void cSensorHub::Update() { } } +void cSensorHub::SetAdc(ads::cADS101x* i_adc) { + if (m_adc != NULL) { + delete m_adc; + } + m_adc = i_adc; +} + void cSensorHub::SetAdcSettings( cSensorSettings * i_adcSettings) { m_adcSettings = i_adcSettings; @@ -45,3 +52,4 @@ cSensorHub::~cSensorHub() { } } /* namespace rijnfel */ + diff --git a/software/Basic_Blink/app/sensor_hub.h b/software/Basic_Blink/app/sensor_hub.h index be91fc4..14db4c6 100644 --- a/software/Basic_Blink/app/sensor_hub.h +++ b/software/Basic_Blink/app/sensor_hub.h @@ -17,6 +17,7 @@ class cSensorHub { public: //in milliseconds cSensorHub(uint32_t i_updatePeriod); + void SetAdc(ads::cADS101x * i_adc); void SetAdcSettings(cSensorSettings * i_adcSettings); void SetTempSettings(cSensorSettings * i_tempSettings); void Update(); diff --git a/software/Basic_Blink/app/sensor_settings.h b/software/Basic_Blink/app/sensor_settings.h index 5abc6e0..9387d0b 100644 --- a/software/Basic_Blink/app/sensor_settings.h +++ b/software/Basic_Blink/app/sensor_settings.h @@ -11,16 +11,19 @@ #include "double_buffer.h" namespace rijnfel { - +//timebase, in miliseconds. +//Example, period = 10 (ms) timebase = 250(ms) +//Callback will be called after 250/10 = 25 samples template class cSensorSettings { public: cSensorSettings(void (*i_callback)(cDoubleBuffer & buffer), - uint32_t i_periodTimer, uint32_t i_period) : - m_callback(i_callback), m_periodTimer(i_periodTimer), m_period( - i_period) { - m_buffer(1000 / m_period); + uint32_t i_timeBase, uint32_t i_period) : + m_callback(i_callback), m_periodTimer(0), m_period(i_period), m_buffer( + 0) { + m_buffer.Resize((int) (i_timeBase / m_period)); } + bool ShouldSample(uint32_t i_updatePeriod) { m_periodTimer += i_updatePeriod; if (m_periodTimer >= m_period) { @@ -29,6 +32,7 @@ public: } return false; } + cDoubleBuffer m_buffer; void (*m_callback)(cDoubleBuffer & buffer); private: diff --git a/software/Basic_Blink/app/web_interface.cpp b/software/Basic_Blink/app/web_interface.cpp new file mode 100644 index 0000000..f1a94ea --- /dev/null +++ b/software/Basic_Blink/app/web_interface.cpp @@ -0,0 +1,114 @@ +/* + * web_interface.cpp + * + * Created on: Aug 6, 2017 + * Author: Elmar + */ + +#include "web_interface.h" +#include + +//TODO remove this +#include "ads101x.h" + +namespace rijnfel { + +cWebInterface *cWebInterface::s_instance = 0; + +static void onIndex(HttpRequest &request, HttpResponse &response) { + cWebInterface::GetInstance()->OnIndex(request, response); +} + +static void onRefresh(HttpRequest &request, HttpResponse &response) { + cWebInterface::GetInstance()->OnRefresh(request, response); +} + +cWebInterface::cWebInterface() : + m_serverStarted(false) { + for (int i = 0; i < 4; i++) { + m_adc_value[i] = 0; + } + // TODO Auto-generated constructor stub + +} + +void onFile(HttpRequest &request, HttpResponse &response) { + String file = request.getPath(); + if (file[0] == '/') + file = file.substring(1); + + if (file[0] == '.') + response.forbidden(); + else { + response.setCache(86400, true); // It's important to use cache for better performance. + response.sendFile(file); + } +} + +void cWebInterface::Start() { + if (m_serverStarted) + return; + server.addPath("/", onIndex); + server.addPath("/state", onRefresh); + server.setDefaultHandler(onFile); + server.listen(80); +} + +void cWebInterface::Stop() { + if (!m_serverStarted) + return; + m_serverStarted = false; +} + +void cWebInterface::OnIndex(HttpRequest &request, HttpResponse &response) { + response.setCache(86400, true); // It's important to use cache for better performance. + response.sendFile("index.html"); +} + +void cWebInterface::OnRefresh(HttpRequest &request, HttpResponse &response) { + JsonObjectStream* stream = new JsonObjectStream(); + JsonObject& json = stream->getRoot(); + + json["adc_0"] = m_adc_value[0]; + json["adc_1"] = m_adc_value[1]; + json["adc_2"] = m_adc_value[2]; + json["adc_3"] = m_adc_value[3]; + + response.sendJsonObject(stream); +} + +void cWebInterface::UpdateAdc(cADC & adc, + cDoubleBuffer& adcBuffer) { + ads::ads_sample_t * buf = &adcBuffer.GetReadyBuffer()[0]; + if (buf != NULL) { + ads::ads_sample_t averageSample; + averageSample.gain = buf->gain; + averageSample.mux = buf->mux; + int64_t average = 0; + + int size = adcBuffer.GetSize(); + for (int i = 0; i < size; i++) { + average += buf[i].rawSample; + } + + if (size != 0) { + average /= size; + } + + averageSample.rawSample = static_cast(average); + int pos = averageSample.mux - ads::eInputMux::AIN_0; + m_adc_value[pos] = adc.ConvertSample(averageSample); + Serial.printf("Current mux: %d pos: %d \n\r", averageSample.mux, pos); + Serial.printf("%d %d %d %d\n\r", m_adc_value[0], m_adc_value[1], + m_adc_value[2], m_adc_value[3]); + } +} + +void cWebInterface::UpdateTemp(cDoubleBuffer& adcBuffer) { +} + +cWebInterface::~cWebInterface() { + // TODO Auto-generated destructor stub +} + +} /* namespace rijnfel */ diff --git a/software/Basic_Blink/app/web_interface.h b/software/Basic_Blink/app/web_interface.h new file mode 100644 index 0000000..341b6a8 --- /dev/null +++ b/software/Basic_Blink/app/web_interface.h @@ -0,0 +1,51 @@ +/* + * web_interface.h + * + * Created on: Aug 6, 2017 + * Author: Elmar + */ + +#ifndef APP_WEB_INTERFACE_H_ +#define APP_WEB_INTERFACE_H_ + +#include "adc.h" +#include "ads101x.h" +#include "double_buffer.h" +#include + +namespace rijnfel { + +class cWebInterface { +public: + static cWebInterface *s_instance; + static cWebInterface *GetInstance() { + if (cWebInterface::s_instance == NULL) + cWebInterface::s_instance = new cWebInterface(); + return cWebInterface::s_instance; + } +public: + cWebInterface(); + cWebInterface(cWebInterface const&) { + m_serverStarted = false; + } + cWebInterface& operator=(cWebInterface const&) { + } + void Start(); + void Stop(); + void UpdateAdc(cADC & adc, + cDoubleBuffer & adcBuffer); + void UpdateTemp(cDoubleBuffer & adcBuffer); + virtual ~cWebInterface(); +public: + void OnIndex(HttpRequest &request, HttpResponse &response); + void OnRefresh(HttpRequest &request, HttpResponse &response); +private: + bool m_serverStarted; + int16_t m_adc_value[4]; + HttpServer server; + +}; + +} /* namespace rijnfel */ + +#endif /* APP_WEB_INTERFACE_H_ */ diff --git a/software/Basic_Blink/files/bootstrap.min.css.gz b/software/Basic_Blink/files/bootstrap.min.css.gz new file mode 100644 index 0000000..80c9946 Binary files /dev/null and b/software/Basic_Blink/files/bootstrap.min.css.gz differ diff --git a/software/Basic_Blink/files/config.html b/software/Basic_Blink/files/config.html new file mode 100644 index 0000000..6a93eb7 --- /dev/null +++ b/software/Basic_Blink/files/config.html @@ -0,0 +1,69 @@ + + + + + + + + TyTherm configuration + + + + + + + + + + +
+
+ +

TyTherm

+
+ +
+

 

+
+ +
+
+
+

Network

+
+
+
+
+ + +
+
+ + +
+
+ + +
+ + +
+
+
+
+ + + +
+
+ +
+ + \ No newline at end of file diff --git a/software/Basic_Blink/files/config.js b/software/Basic_Blink/files/config.js new file mode 100644 index 0000000..2ff2d1e --- /dev/null +++ b/software/Basic_Blink/files/config.js @@ -0,0 +1,38 @@ +function get_config() { + $.getJSON('/config.json', + function(data) { + $.each(data, function(key, value){ + document.getElementById(key).value = value; + if (data.StaEnable == 1) { + document.getElementById('StaEnable').checked = true; + } + else + document.getElementById('StaEnable').checked = false; + }); + }); +} + + +function post_netcfg(event) { + event.preventDefault(); + var formData = { + 'StaSSID' : document.getElementById('StaSSID').value, + 'StaPassword' : document.getElementById('StaPassword').value, + 'StaEnable' : (document.getElementById('StaEnable').checked ? 1 : 0) + }; + $.ajax({ + type : 'POST', + url : '/config', + contentType : 'application/json; charset=utf-8', + data : JSON.stringify(formData), + dataType : 'json' + }) +} + + +$( document ).ready(function() { + get_config(); + + document.getElementById('form_netcfg').addEventListener('submit', post_netcfg); + document.getElementById('netcfg_cancel').addEventListener('click', get_config); +}); \ No newline at end of file diff --git a/software/Basic_Blink/files/index.html b/software/Basic_Blink/files/index.html new file mode 100644 index 0000000..8b9a876 --- /dev/null +++ b/software/Basic_Blink/files/index.html @@ -0,0 +1,88 @@ + + + + + + + + TyTherm Status + + + + + + + + + + +
+
+ +

TyTherm

+
+ +
+

 

+
+
+
+
+

Channel 0

+
+
+

{adc_0}

+
+
+
+
+
+
+
+
+

Channel 1

+
+
+

{adc_1}

+
+
+
+
+
+
+
+
+
+
+

Channel 2

+
+
+

{adc_2}

+
+
+
+
+
+
+
+
+

Channel 3

+
+
+

{adc_3}

+
+
+
+
+
+ +
+ + \ No newline at end of file diff --git a/software/Basic_Blink/files/index.js b/software/Basic_Blink/files/index.js new file mode 100644 index 0000000..036f4ae --- /dev/null +++ b/software/Basic_Blink/files/index.js @@ -0,0 +1,13 @@ +$( document ).ready(function() { + + (function worker() { + $.getJSON('/state', function(data) { + document.getElementById('adc_0').textContent = data.adc_0; + document.getElementById('adc_1').textContent = data.adc_1; + document.getElementById('adc_2').textContent = data.adc_2; + document.getElementById('adc_3').textContent = data.adc_3; + + setTimeout(worker, 5000); + }); + })(); +}); \ No newline at end of file diff --git a/software/Basic_Blink/files/jquery-2.1.4.min.js.gz b/software/Basic_Blink/files/jquery-2.1.4.min.js.gz new file mode 100644 index 0000000..bc77daf Binary files /dev/null and b/software/Basic_Blink/files/jquery-2.1.4.min.js.gz differ