diff --git a/software/app/application.cpp b/software/app/application.cpp index 15af757..599ffc7 100644 --- a/software/app/application.cpp +++ b/software/app/application.cpp @@ -12,8 +12,7 @@ using namespace rijnfel; -void STADisconnect(String ssid, uint8_t ssid_len, uint8_t bssid[6], - uint8_t reason); +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; @@ -41,11 +40,11 @@ void AdcTest() { } adc.SetMux(static_cast(ads::eInputMux::AIN_0 + channel)); ads::ads_sample_t sample = adc.RawSample(); - Serial.printf("raw: %d converted: %d channel: %d\n\r", sample.rawSample, - adc.ConvertSample(sample), sample.mux); + Serial.printf("raw: %d converted: %d channel: %d\n\r", sample.rawSample, adc.ConvertSample(sample), sample.mux); } void updateSensorHub() { + WDT.alive(); hub.Update(); } @@ -53,19 +52,18 @@ void adcCallback(cDoubleBuffer & buffer) { channel++; if (channel > 3) { channel = 0; - cWebInterface::GetInstance()->PrintValues(); - Serial.printf("Settings: %d\n\r", adc.GetSettings()); + //cWebInterface::GetInstance()->PrintValues(); } cWebInterface::GetInstance()->UpdateAdc(adc, buffer); adc.SetMux(static_cast(ads::eInputMux::AIN_0 + channel)); } -void ready() -{ - debugf("READY!"); +void ready() { + WifiAccessPoint.config("Sensus", "", AUTH_OPEN, false, 3); + //debugf("READY!"); - // If AP is enabled: - debugf("AP. ip: %s mac: %s", WifiAccessPoint.getIP().toString().c_str(), WifiAccessPoint.getMAC().c_str()); + // If AP is enabled: + //debugf("AP. ip: %s mac: %s", WifiAccessPoint.getIP().toString().c_str(), WifiAccessPoint.getMAC().c_str()); } void init() { @@ -76,16 +74,13 @@ void init() { System.onReady(ready); - WifiAccessPoint.enable(true); - WifiAccessPoint.setIP(IPAddress(10, 0, 0, 1)); //TODO - WifiAccessPoint.config("UppSense Photometer", "Sexy", AUTH_WPA2_PSK, false, 3, 200); + WifiAccessPoint.setIP(IPAddress(10, 0, 0, 1)); + WifiAccessPoint.enable(true); - //scl, sda Wire.pins(4, 5); Wire.begin(); //SET higher CPU freq & disable wifi sleep - //WDT.enable(false); // Turn off LED for measurements pinMode(LED_PIN, OUTPUT); digitalWrite(LED_PIN, 1); @@ -96,14 +91,11 @@ void init() { hub.SetAdc(&adc); cSensorSettings * adcSettings; - adcSettings = new cSensorSettings(&adcCallback, - ADC_TIMEBASE, ADC_PERIOD); + adcSettings = new cSensorSettings(&adcCallback, ADC_TIMEBASE, ADC_PERIOD); 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()->StartServer(); procTimer.initializeMs(HUB_PERIOD, updateSensorHub).start(); @@ -113,8 +105,7 @@ void init() { //mylight.RectangleUpdate(); } -void STADisconnect(String ssid, uint8_t ssid_len, uint8_t bssid[6], - uint8_t reason) { +void STADisconnect(String ssid, uint8_t ssid_len, uint8_t bssid[6], uint8_t reason) { if (!WifiAccessPoint.isEnabled()) { WifiStation.disconnect(); WifiAccessPoint.enable(true); diff --git a/software/app/dummy_adc.cpp b/software/app/dummy_adc.cpp new file mode 100644 index 0000000..9474bb2 --- /dev/null +++ b/software/app/dummy_adc.cpp @@ -0,0 +1,36 @@ +/* + * dummy_adc.cpp + * + * Created on: Aug 15, 2017 + * Author: Elmar + */ + +#include "dummy_adc.h" + +namespace rijnfel { + +cDymmyAdc::cDymmyAdc() { + m_sampleCounter = 0; +} + +void cDymmyAdc::WaitSample(void) { +} + +bool cDymmyAdc::IsReady(void) { + return true; +} + +uint32_t cDymmyAdc::RawSample(void) { + m_sampleCounter++; + return m_sampleCounter; +} + +uint32_t cDymmyAdc::ConvertSample(uint32_t& sample) { + return sample - 1; +} + +cDymmyAdc::~cDymmyAdc() { + // TODO Auto-generated destructor stub +} + +} /* namespace rijnfel */ diff --git a/software/app/dummy_adc.h b/software/app/dummy_adc.h new file mode 100644 index 0000000..2f9c557 --- /dev/null +++ b/software/app/dummy_adc.h @@ -0,0 +1,30 @@ +/* + * dummy_adc.h + * + * Created on: Aug 15, 2017 + * Author: Elmar + */ + +#ifndef APP_DUMMY_ADC_H_ +#define APP_DUMMY_ADC_H_ + +#include "adc.h" +#include + +namespace rijnfel { + +class cDymmyAdc: public cADC { +public: + cDymmyAdc(); + virtual void WaitSample(void); + virtual bool IsReady(void); + virtual uint32_t RawSample(void); + virtual uint32_t ConvertSample(uint32_t & sample); + virtual ~cDymmyAdc(); +private: + uint32_t m_sampleCounter; +}; + +} /* namespace rijnfel */ + +#endif /* APP_DUMMY_ADC_H_ */ diff --git a/software/app/web_interface.cpp b/software/app/web_interface.cpp index 5e19dfd..959c5e6 100644 --- a/software/app/web_interface.cpp +++ b/software/app/web_interface.cpp @@ -8,7 +8,8 @@ #include "web_interface.h" #include #include - +#include +#include namespace rijnfel { cWebInterface *cWebInterface::s_instance = 0; @@ -35,7 +36,7 @@ cWebInterface::cWebInterface() : m_adc_value_average[i] = 0; } // Integer requires 8 digits, and one for the comma - m_jsonBuffer = new char[RAW_SAMPLES * 8 + RAW_SAMPLES * 2]; + m_jsonBuffer = new char[1]; if (m_jsonBuffer == NULL) { Serial.print("Not enough ram"); } @@ -56,10 +57,15 @@ void onFile(HttpRequest & i_request, HttpResponse & i_response) { } } +static void onChannel(HttpRequest & i_request, HttpResponse & i_response) { + cWebInterface::GetInstance()->OnFile(i_request, i_response); +} + void cWebInterface::StartServer() { if (m_serverStarted) return; m_server.addPath("/", onIndex); + m_server.addPath("/channel", onChannel); m_server.addPath("/state", onRefresh); m_server.addPath("/config", onConfiguration); m_server.addPath("/config.json", onConfiguration_json); @@ -91,16 +97,7 @@ void cWebInterface::OnRefresh(HttpRequest & i_request, HttpResponse & i_response json["adc_3"] = m_adc_value_average[2]; json["adc_4"] = m_adc_value_average[3]; #endif - - char * writePos = m_jsonBuffer; - for (int sample = 0; sample < m_adc_values_raw_cnt[RAW_CHANNEL]; sample++) { - const int pos = sprintf(writePos, "%d,", m_adc_values_raw[RAW_CHANNEL][sample]); - writePos = &m_jsonBuffer[pos]; - }/* - json["raw_adc"] = m_jsonBuffer; //ss.str(); - */ i_response.sendJsonObject(stream); - m_adc_values_raw_cnt[RAW_CHANNEL] = 0; } void cWebInterface::OnRawUpdate(HttpRequest& i_request, HttpResponse& i_response) { @@ -142,6 +139,24 @@ cWebInterface::~cWebInterface() { // TODO Auto-generated destructor stub } +void cWebInterface::OnFile(HttpRequest& i_request, HttpResponse& i_response) { + const String name = "Channel1"; + file_t file = fileOpen(name, eFO_CreateIfNotExist | eFO_ReadWrite); + + int size = m_adc_values_raw_cnt[RAW_CHANNEL]; + Serial.printf("Size: %d\n\r",size); + char buf[12]; + for (int i = 0; i < 4; i++) { + int len = sprintf(buf, "%d,", m_adc_values_raw[RAW_CHANNEL][i]); + fileWrite(file, buf, len); + fileFlush(file); + } + m_adc_values_raw_cnt[RAW_CHANNEL] = 0; + fileClose(file); + i_response.setCache(86400, true); // It's important to use cache for better performance. + i_response.sendFile(name); +} + void cWebInterface::ResetRawValues() { for (int channel = 0; channel < 4; channel++) { for (int sample = 0; sample < 1000; sample++) { @@ -152,7 +167,7 @@ void cWebInterface::ResetRawValues() { void cWebInterface::OnConfiguration(HttpRequest &request, HttpResponse &response) { - if (strcmp(request.getRequestMethod().c_str(), RequestMethod::POST) == 0) { + if (request.method == http_method::HTTP_POST) { //debugf("Update config"); // Update config if (request.getBody() == NULL) { @@ -207,7 +222,7 @@ void cWebInterface::OnConfiguration_json(HttpRequest &request, HttpResponse &res json["StaPassword"] = 23; json["StaEnable"] = 24; - response.sendDataStream(stream, ContentType::JSON); + response.sendJsonObject(stream); } } /* namespace rijnfel */ diff --git a/software/app/web_interface.h b/software/app/web_interface.h index 6ec5db6..893e430 100644 --- a/software/app/web_interface.h +++ b/software/app/web_interface.h @@ -103,6 +103,7 @@ public: * @param i_response */ void OnRawUpdate(HttpRequest& i_request, HttpResponse& i_response); + void OnFile(HttpRequest& i_request, HttpResponse& i_response); void OnConfiguration_json(HttpRequest &request, HttpResponse &response); void OnConfiguration(HttpRequest &request, HttpResponse &response); protected: