From 82ae2e0dfb756edd52551d561b22e9c81d576c90 Mon Sep 17 00:00:00 2001 From: Krzysztof Date: Tue, 31 May 2016 20:52:35 +0200 Subject: [PATCH] Included logging to Emoncms.org --- EspAdafruit_NeoPixel/Emoncms.h | 8 +++ EspAdafruit_NeoPixel/Emoncms.ino | 68 +++++++++++++++++++ EspAdafruit_NeoPixel/EspAdafruit_NeoPixel.ino | 18 ++--- EspFastLED/Emoncms.h | 8 +++ EspFastLED/Emoncms.ino | 68 +++++++++++++++++++ EspFastLED/EspFastLED.ino | 18 ++--- EspNeoPixelBus/Emoncms.h | 8 +++ EspNeoPixelBus/Emoncms.ino | 68 +++++++++++++++++++ EspNeoPixelBus/EspNeoPixelBus.ino | 18 ++--- readme.md | 7 +- 10 files changed, 263 insertions(+), 26 deletions(-) create mode 100644 EspAdafruit_NeoPixel/Emoncms.h create mode 100644 EspAdafruit_NeoPixel/Emoncms.ino create mode 100644 EspFastLED/Emoncms.h create mode 100644 EspFastLED/Emoncms.ino create mode 100644 EspNeoPixelBus/Emoncms.h create mode 100644 EspNeoPixelBus/Emoncms.ino diff --git a/EspAdafruit_NeoPixel/Emoncms.h b/EspAdafruit_NeoPixel/Emoncms.h new file mode 100644 index 0000000..18254df --- /dev/null +++ b/EspAdafruit_NeoPixel/Emoncms.h @@ -0,0 +1,8 @@ +#ifndef _EMONCMS_H_ +#define _EMONCMS_H_ + +#include + +void logToEmoncms(void); + +#endif //_EMONCMS_H_ diff --git a/EspAdafruit_NeoPixel/Emoncms.ino b/EspAdafruit_NeoPixel/Emoncms.ino new file mode 100644 index 0000000..e1a563c --- /dev/null +++ b/EspAdafruit_NeoPixel/Emoncms.ino @@ -0,0 +1,68 @@ +#include "Emoncms.h" + +WiFiClient client; + +#define UPDATE_PERIOD 60000 +#define TIMEOUT_PERIOD 1000 +const char* dataHost = "emoncms.org"; +const char* apiKey = "9a3e3c9cf65c70a597097b065dcb24e3"; +long dataLogTimer; + +#ifdef ESP8266 +extern "C" { +#include "user_interface.h" +} +#endif + + +void logToEmoncms(void) +{ + if (millis() < dataLogTimer + UPDATE_PERIOD) + { + return; + } + dataLogTimer = millis(); + + Serial.print("DATA LOG > "); + Serial.print(dataHost); + Serial.print(" > "); + + if (!client.connect(dataHost, 80)) + { + Serial.print("Connection failure!"); + return; + } + + Serial.print("Sending data > "); + String message; + message += "GET http://" + (String) dataHost; + message += "/input/post.json?json={"; + message += "EspAdafruit_NeoPixel_UpTime:" + String (dataLogTimer / 60000) + ","; + message += "EspAdafruit_NeoPixel_HeapSize:" + String (system_get_free_heap_size()); + message += "}&apikey=" + (String) apiKey + "\n"; + message += "Connection: close\n\n"; + client.print(message); + + while (client.available() == 0) + { + if (millis() > dataLogTimer + TIMEOUT_PERIOD) + { + Serial.println("Reply timeout!"); + client.stop(); + return; + } + } + + while (client.available()) + { + Serial.write(client.read()); + } + Serial.print(" > Done"); + client.stop(); + + // show time to log the data on Emoncms + Serial.print(" in "); + Serial.print(millis()-dataLogTimer); + Serial.println("ms"); +} + diff --git a/EspAdafruit_NeoPixel/EspAdafruit_NeoPixel.ino b/EspAdafruit_NeoPixel/EspAdafruit_NeoPixel.ino index 78700bb..62c8ce1 100644 --- a/EspAdafruit_NeoPixel/EspAdafruit_NeoPixel.ino +++ b/EspAdafruit_NeoPixel/EspAdafruit_NeoPixel.ino @@ -1,22 +1,24 @@ #include "WebInterface.h" #include "Fire.h" +#include "Emoncms.h" void setup() { - Serial.begin(115200); - Serial.println(); - Serial.println("Fire2012_Adafruit"); + Serial.begin(115200); + Serial.println(); + Serial.println("Fire2012_Adafruit"); - setupWiFi(); - setupWebServer(); + setupWiFi(); + setupWebServer(); - setupFire(); + setupFire(); } void loop() { - server.handleClient(); - keepFireAlive(); + server.handleClient(); + logToEmoncms(); + keepFireAlive(); } diff --git a/EspFastLED/Emoncms.h b/EspFastLED/Emoncms.h new file mode 100644 index 0000000..18254df --- /dev/null +++ b/EspFastLED/Emoncms.h @@ -0,0 +1,8 @@ +#ifndef _EMONCMS_H_ +#define _EMONCMS_H_ + +#include + +void logToEmoncms(void); + +#endif //_EMONCMS_H_ diff --git a/EspFastLED/Emoncms.ino b/EspFastLED/Emoncms.ino new file mode 100644 index 0000000..16d8978 --- /dev/null +++ b/EspFastLED/Emoncms.ino @@ -0,0 +1,68 @@ +#include "Emoncms.h" + +WiFiClient client; + +#define UPDATE_PERIOD 60000 +#define TIMEOUT_PERIOD 1000 +const char* dataHost = "emoncms.org"; +const char* apiKey = "be71f01adf17bfa1a85118923c0140b4"; +long dataLogTimer; + +#ifdef ESP8266 +extern "C" { +#include "user_interface.h" +} +#endif + + +void logToEmoncms(void) +{ + if (millis() < dataLogTimer + UPDATE_PERIOD) + { + return; + } + dataLogTimer = millis(); + + Serial.print("DATA LOG > "); + Serial.print(dataHost); + Serial.print(" > "); + + if (!client.connect(dataHost, 80)) + { + Serial.print("Connection failure!"); + return; + } + + Serial.print("Sending data > "); + String message; + message += "GET http://" + (String) dataHost; + message += "/input/post.json?json={"; + message += "EspFastLED_UpTime:" + String (dataLogTimer / 60000) + ","; + message += "EspFastLED_HeapSize:" + String (system_get_free_heap_size()); + message += "}&apikey=" + (String) apiKey + "\n"; + message += "Connection: close\n\n"; + client.print(message); + + while (client.available() == 0) + { + if (millis() > dataLogTimer + TIMEOUT_PERIOD) + { + Serial.println("Reply timeout!"); + client.stop(); + return; + } + } + + while (client.available()) + { + Serial.write(client.read()); + } + Serial.print(" > Done"); + client.stop(); + + // show time to log the data on Emoncms + Serial.print(" in "); + Serial.print(millis()-dataLogTimer); + Serial.println("ms"); +} + diff --git a/EspFastLED/EspFastLED.ino b/EspFastLED/EspFastLED.ino index a716a43..3b98b59 100644 --- a/EspFastLED/EspFastLED.ino +++ b/EspFastLED/EspFastLED.ino @@ -1,22 +1,24 @@ #include "WebInterface.h" #include "Fire.h" +#include "Emoncms.h" void setup() { - Serial.begin(115200); - Serial.println(); - Serial.println("Fire2012_FastLED"); + Serial.begin(115200); + Serial.println(); + Serial.println("Fire2012_FastLED"); - setupWiFi(); - setupWebServer(); + setupWiFi(); + setupWebServer(); - setupFire(); + setupFire(); } void loop() { - server.handleClient(); - keepFireAlive(); + server.handleClient(); + logToEmoncms(); + keepFireAlive(); } diff --git a/EspNeoPixelBus/Emoncms.h b/EspNeoPixelBus/Emoncms.h new file mode 100644 index 0000000..18254df --- /dev/null +++ b/EspNeoPixelBus/Emoncms.h @@ -0,0 +1,8 @@ +#ifndef _EMONCMS_H_ +#define _EMONCMS_H_ + +#include + +void logToEmoncms(void); + +#endif //_EMONCMS_H_ diff --git a/EspNeoPixelBus/Emoncms.ino b/EspNeoPixelBus/Emoncms.ino new file mode 100644 index 0000000..57eb058 --- /dev/null +++ b/EspNeoPixelBus/Emoncms.ino @@ -0,0 +1,68 @@ +#include "Emoncms.h" + +WiFiClient client; + +#define UPDATE_PERIOD 60000 +#define TIMEOUT_PERIOD 1000 +const char* dataHost = "emoncms.org"; +const char* apiKey = "be71f01adf17bfa1a85118923c0140b4"; +long dataLogTimer; + +#ifdef ESP8266 +extern "C" { +#include "user_interface.h" +} +#endif + + +void logToEmoncms(void) +{ + if (millis() < dataLogTimer + UPDATE_PERIOD) + { + return; + } + dataLogTimer = millis(); + + Serial.print("DATA LOG > "); + Serial.print(dataHost); + Serial.print(" > "); + + if (!client.connect(dataHost, 80)) + { + Serial.print("Connection failure!"); + return; + } + + Serial.print("Sending data > "); + String message; + message += "GET http://" + (String) dataHost; + message += "/input/post.json?json={"; + message += "EspNeoPixelBus_UpTime:" + String (dataLogTimer / 60000) + ","; + message += "EspNeoPixelBus_HeapSize:" + String (system_get_free_heap_size()); + message += "}&apikey=" + (String) apiKey + "\n"; + message += "Connection: close\n\n"; + client.print(message); + + while (client.available() == 0) + { + if (millis() > dataLogTimer + TIMEOUT_PERIOD) + { + Serial.println("Reply timeout!"); + client.stop(); + return; + } + } + + while (client.available()) + { + Serial.write(client.read()); + } + Serial.print(" > Done"); + client.stop(); + + // show time to log the data on Emoncms + Serial.print(" in "); + Serial.print(millis()-dataLogTimer); + Serial.println("ms"); +} + diff --git a/EspNeoPixelBus/EspNeoPixelBus.ino b/EspNeoPixelBus/EspNeoPixelBus.ino index 1e0a4b7..6368bce 100644 --- a/EspNeoPixelBus/EspNeoPixelBus.ino +++ b/EspNeoPixelBus/EspNeoPixelBus.ino @@ -1,22 +1,24 @@ #include "WebInterface.h" #include "Fire.h" +#include "Emoncms.h" void setup() { - Serial.begin(115200); - Serial.println(); - Serial.println("Fire2012_NeoPixelBus"); + Serial.begin(115200); + Serial.println(); + Serial.println("Fire2012_NeoPixelBus"); - setupWiFi(); - setupWebServer(); + setupWiFi(); + setupWebServer(); - setupFire(); + setupFire(); } void loop() { - server.handleClient(); - keepFireAlive(); + server.handleClient(); + logToEmoncms(); + keepFireAlive(); } diff --git a/readme.md b/readme.md index 009cdd4..9fadabe 100644 --- a/readme.md +++ b/readme.md @@ -72,7 +72,7 @@ To do the testing I have collected the following hardware: - **ESP8266 module** - NodeMCU 1.0 (ESP-12E Module). I have selected this one to make my prototyping more convenient. It has USB / serial interface and power supply on board, so you do not need to provide them separately. You can use any other [typical ESP8266 modules](https://github.com/esp8266/Arduino/blob/master/doc/boards.md#table-of-contents) as well. One single pin GPIO3 (TXD0) is used for driving WS2812B and make sure it is broken out in your module. - **Pixel strip power supply** - Adjustable DC-DC power supply CN4015-3.1 together with AC-DC 12V power supply. You should be able to go away with a "wall wart" 5V DC power supply assuming it will match power consumption of your LED strip. To avoid potential issues with 3.3V logic of ESP8266, use an adjustable power supply and lover output voltage below 5V. For more details please check excellent guide ["Powering NeoPixels"](https://learn.adafruit.com/adafruit-neopixel-uberguide/power) by Adafruit. - **1000uF/16V capacitor** to improve quality of power supply to the pixel strip - see ["Best Practices"](https://learn.adafruit.com/adafruit-neopixel-uberguide/basic-connections) by Adafruit. -- **470Ω resistor** to separate ESP8266 and the pixel strip. For more details please check ["Best Practices"](https://learn.adafruit.com/adafruit-neopixel-uberguide/basic-connections) by Adafruit. +- **470? resistor** to separate ESP8266 and the pixel strip. For more details please check ["Best Practices"](https://learn.adafruit.com/adafruit-neopixel-uberguide/basic-connections) by Adafruit. - **Bread board and cables** - see also ["Basic Connections"](https://learn.adafruit.com/adafruit-neopixel-uberguide/basic-connections) by Adafruit. ![Connection of NeoPixels to ESP8266 - schematic](pictures/EspNeoPixelConnection.png) @@ -107,7 +107,9 @@ Application prepared for ESP8266 consists of the following files: 1. [Fire.ino](EspFastLED/Fire.ino) together with [Fire.h](EspFastLED/Fire.h) - fire simulation code that primarily contains original Fire2012 application. 2. [WebInterface.ino](EspFastLED/WebInterface.ino) together with [WebInterface.h](EspFastLED/WebInterface.h) - web interface shown on the [picture above](#fire2012) and Wi-Fi set up routines. -3. [EspFastLED.ino](EspFastLED/EspFastLED.ino) - a simple main sketch that integrates the above *Fire* and *WebInterface*. +3. [Emoncms.ino](EspFastLED/Emoncms.ino) together with [Emoncms.h](EspFastLED/Emoncms.h) - routines to post data on [Emoncms.org](Emoncms.org) to monitor application up time, heap size and other parametrs if required. You need to set up an account on Emoncms.org to use it. This is quick and simple - please refer to my [OnlineHumidifier](https://github.com/krzychb/OnlineHumidifier/tree/master/6-Chart#emoncms) repository that explains how to do it. +4. [EspFastLED.ino](EspFastLED/EspFastLED.ino) - a simple main sketch that integrates the above *Fire*, *WebInterface* and *Emoncms*. + EspFire2012 application has been tested with three previously described libraries for NeoPixel control. Respective application versions are available for your review in the following folders: @@ -129,6 +131,7 @@ In ``` loop() ``` the following two functions are repeatedly called: ``` server.handleClient(); // respond to commands from web browser + logToEmoncms(); // Log aplication performance data to Emoncms.org keepFireAlive(); // simulate Fire2012 ``` The main sketch file names have been changed to distinguish tested libraries.