Skip to content

Commit

Permalink
extend trender use cases introductin ThingSpeak Connector.
Browse files Browse the repository at this point in the history
AP will be closed after 30s (TK_BASE_TIMEOUT) in case no timekeeper is initiated.
ThingSpeak mode can also be forced using dedicated webpage.
TODO : Implement states 1&2 for state machine in loop function
  • Loading branch information
cebernard22 committed May 8, 2017
1 parent 6942017 commit 2664aa0
Show file tree
Hide file tree
Showing 3 changed files with 159 additions and 10 deletions.
9 changes: 5 additions & 4 deletions firmware/Trender/Page_Admin.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,12 @@ const char PAGE_AdminMainPage[] = R"=====(
<meta name="viewport" content="width=device-width, initial-scale=1" />
<strong>Administration</strong>
<hr>
<a href="config" style="width:250px" class="btn btn--m btn--blue" >Configuration</a><br>
<!-- <a href="color" style="width:250px" class="btn btn--m btn--blue" >Color</a><br> -->
<a href="info" style="width:250px" class="btn btn--m btn--blue" >Network Information</a><br>
<a href="start" style="width:110px" class="btn btn--m btn--blue" >Start</a>
<a href="stop" style="width:110px" class="btn btn--m btn--blue" >Stop</a><br>
<a href="thingspeak" style="width:250px" class="btn btn--m btn--blue" >Set ThingSpeak Mode</a><br>
<a href="config" style="width:250px" class="btn btn--m btn--blue" >TimeKeeper Configuration</a><br>
<!-- <a href="color" style="width:250px" class="btn btn--m btn--blue" >Color</a><br> -->
<a href="start" style="width:250px" class="btn btn--m btn--blue" >Start TimeKeeper</a>
<a href="stop" style="width:250px" class="btn btn--m btn--blue" >Stop TimeKeeper</a><br>


<script>
Expand Down
88 changes: 88 additions & 0 deletions firmware/Trender/Page_ThingSpeak.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
/**
* This file is part of Trender.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* Copyright 2016 Jérôme Labidurie [email protected]
* Copyright 2016 Cédric Bernard [email protected]
* Copyright 2016 Julien Jacques [email protected]
*/

#ifndef PAGE_THINGPSEAK_H
#define PAGE_THINGPSEAK_H


//
// The HTML PAGE
//
const char PAGE_ThingSpeak[] = R"=====(
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link rel="stylesheet" href="style.css" type="text/css" />
<script src="microajax.js"></script>
<a href="/" class="btn btn--s"><</a>&nbsp;&nbsp;<strong>ThingSpeak Mode enabled, reset device to go back to Configuration Mode</strong>
<hr>
<table border="0" cellspacing="0" cellpadding="3" style="width:310px" >
<tr><td align="right">ChannelID :</td><td><span id="x_channelid"></span></td></tr>
<tr><td colspan="2" align="center"><a href="javascript:GetThingSpeakState()" class="btn btn--m btn--blue">Refresh</a></td></tr>
</table>
<script>

function GetThingSpeakState()
{
setValues("/admin/infothingspeak");
}

window.onload = function ()
{
load("style.css","css", function()
{
load("microajax.js","js", function()
{
GetThingSpeakState();
});
});
}
function load(e,t,n){if("js"==t){var a=document.createElement("script");a.src=e,a.type="text/javascript",a.async=!1,a.onload=function(){n()},document.getElementsByTagName("head")[0].appendChild(a)}else if("css"==t){var a=document.createElement("link");a.href=e,a.rel="stylesheet",a.type="text/css",a.async=!1,a.onload=function(){n()},document.getElementsByTagName("head")[0].appendChild(a)}}



</script>
)=====" ;
//
// FILL WITH INFOMATION
//

void send_thingspeak_values_html ()
{

String values ="";

values += "x_channelid|" + (String)myThingSpeakChannel + "|div\n";
server.send ( 200, "text/plain", values);
Serial.println(__FUNCTION__);

thingSpeakMode=1;




}

#endif
72 changes: 66 additions & 6 deletions firmware/Trender/Trender.ino
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,24 @@
#define VERSION "Trender v1.0"
char mySSID[13];


#define myThingSpeakChannel "264242"
unsigned char thingSpeakMode = 0;
/**
0: Accepoint mode ( configuration, timekeeper ) ,
1: Thingspeak mode, init mode. Close AP, open Wifi to connect to thingspeak server
2: wifi established, import RGB values and update neopixels accordingly
*/

ESP8266WebServer server(80);
Ticker tk, tki, tkb;
Ticker tk, tki, tkb,tkt;
/** base rate for heartbeat ticker */
#define TKB_BASE_RATE 0.10
/** base rate for main ticker */
#define TK_BASE_RATE 1
/** base rate for timeout ticker ( tkt) */
#define TK_BASE_TIMEOUT 30

volatile boolean showRainbow = false;
volatile uint32_t color = 0;

Expand Down Expand Up @@ -77,11 +89,12 @@ uint8_t currentMode = STOPPED;
#include "Page_Script_js.h"
#include "Page_Style_css.h"
#include "Page_Information.h"
#include "Page_ThingSpeak.h"
#include "favicon.h"

#define PIN D2
#define PIN_INPUT D5
#define NBPIX 3
#define NBPIX 16 // take into account several neopixels footprints: 1 pixel, 1 row of 8 pixels, 1 square of 4*4=16 pixels

// Parameter 1 = number of pixels in strip
// Parameter 2 = Arduino pin number (most are valid)
Expand Down Expand Up @@ -118,11 +131,14 @@ void setup() {
server.on ( "/", []() { Serial.println("/.html"); server.send ( 200, "text/html", PAGE_AdminMainPage ); } );
server.on ( "/admin", []() { Serial.println("admin.html"); server.send ( 200, "text/html", PAGE_AdminMainPage ); } );
server.on ( "/admin/infovalues", send_information_values_html);
server.on ( "/admin/infothingspeak", send_thingspeak_values_html);

server.on ( "/info", []() { Serial.println("info.html"); server.send ( 200, "text/html", PAGE_Information ); } );
// server.on ( "/color", processColor);
// server.on ( "/color/values", sendColorData);
server.on ( "/config", []() { processConfig(); writeConfig();});
server.on ( "/config/values", sendConfigData);
server.on ( "/thingspeak", []() { Serial.println("thingspeak.html"); server.send ( 200, "text/html", PAGE_ThingSpeak ); } );
server.on ( "/config/values", []() { Serial.println("Disable ThingSpeak timeout mode"); thingSpeakMode=0;tkt.detach();sendConfigData();});
server.on ( "/style.css", []() { Serial.println("style.css"); server.send ( 200, "text/plain", PAGE_Style_css ); } );
server.on ( "/microajax.js", []() { Serial.println("microajax.js"); server.send ( 200, "text/plain", PAGE_microajax_js ); } );
server.on ( "/start", []() { Serial.println("start"); start(); server.send ( 200, "text/html", PAGE_AdminMainPage ); } );
Expand Down Expand Up @@ -151,6 +167,7 @@ void setup() {

pinMode (PIN_INPUT, INPUT);
tki.attach(0.1, tkInput);
tkt.attach(TK_BASE_TIMEOUT, tkCheckMode);

setColor(0xff0000);
delay(300);
Expand All @@ -169,13 +186,39 @@ void setup() {
* handle http events
*/
void loop() {
dnsServer.processNextRequest();
server.handleClient();


switch (thingSpeakMode) {
case 0:
//Access point mode
dnsServer.processNextRequest();
server.handleClient();
break;
case 1:
Serial.println("Close AP and open Connection to thingspeak server: TBC");
Serial.println("ThingSpeakMode enabled");
setColor(0xff0000);
delay(300);
setColor(0x00ff00);
delay(300);
setColor(0x0000ff);
delay(300);
setColor(0);
thingSpeakMode=2;
break;
case 2:
Serial.println("Recover data: TBC");
delay(5000);
break;
default:
break;
}

}


/** ticker callback for rainbow mode
* NOT USED anymode
* NOT USED anymore
*/
void tkColor() {
uint16_t i;
Expand Down Expand Up @@ -211,6 +254,20 @@ void tkInput () {
previousState = r;
}//tkInput



/** Check if we need to stay into landing page of force Thingkspeak connector
*/
void tkCheckMode () {

Serial.println("Timeout reached, force ThingspeakMode");
thingSpeakMode=1;
tkt.detach();

}//tkCheckMode



/** set all pixel to color
* @param col the color as 0xRRGGBB
*/
Expand Down Expand Up @@ -331,6 +388,9 @@ void tkTrender(void) {
/** start main timer
*/
void start (void) {
// Disable timeout timer if any
tkt.detach();

// flash to show the 3 colors
setColor(config.colors[0]);
delay(500);
Expand Down

0 comments on commit 2664aa0

Please sign in to comment.