@ -1,34 +1,38 @@
# include <Arduino.h>
# include <Arduino.h>
# include <ESP8266WiFi.h>
# include <ESP8266WiFi.h>
# include <WiFiClient.h>
//#include <WiFiClient.h>
# include <ESP8266WebServer.h>
# include <ESP8266WebServer.h>
# include <ESP8266mDNS.h>
//#include <ESP8266mDNS.h>
# include <html.h>
# include <html.h>
# include <Graph.h>
# include <Graph.h>
# include <Sensor.h>
# include <Sensor.h>
# include <WebSocketsServer.h>
# include <ArduinoJson.h>
const char * ssid = " NodeMCU " ;
const char * ssid = " NodeMCU " ;
ESP8266WebServer server ( 80 ) ;
ESP8266WebServer server ( 80 ) ;
Sensor accelerometer = Sensor ( ) ;
Sensor accelerometer = Sensor ( ) ;
Graph graphAcc = Graph ( 200 , 50 ) ;
Graph graphAcc = Graph ( 200 , 50 ) ;
int lastDataSend = 0 ;
WebSocketsServer webSocket = WebSocketsServer ( 81 ) ;
void handleAccelerometer ( )
void handleAccelerometer ( )
{
{
int16_t bias = 0 ;
int16_t bias = 0 ;
const int arr_size = 10000 ;
const int arr_size = 10000 ;
char * html_code = new char [ arr_size ] ;
char * html_code = new char [ arr_size ] ;
memset ( html_code , ' \0 ' , sizeof ( char ) * arr_size ) ;
memset ( html_code , ' \0 ' , sizeof ( char ) * arr_size ) ;
bias + = getHtml ( HTML_BEGIN , * html_code , arr_size , 0 ) ;
bias + = getHtml ( HTML_BEGIN , * html_code , arr_size , 0 ) ;
bias + = getHtml ( STYLE , * html_code , arr_size , bias ) ;
bias + = getHtml ( STYLE , * html_code , arr_size , bias ) ;
bias + = getHtml ( SCRIPT_BEGIN , * html_code , arr_size , bias ) ;
bias + = getHtml ( SCRIPT_BEGIN , * html_code , arr_size , bias ) ;
for ( int i = 0 ; i ! = graphAcc . graphSize - 1 ; i + + ) {
// for (int i = 0; i != graphAcc.graphSize - 1; i++)
bias + = snprintf ( ( html_code + bias ) , arr_size - bias , " context.lineTo(%i*2, %i); \n " , i , graphAcc . graphArray [ i ] ) ;
// {
}
// bias += snprintf((html_code + bias), arr_size - bias, "context.lineTo(%i*2, %i);\n", i, graphAcc.graphArray[i]);
// }
bias + = getHtml ( SCRIPT_END , * html_code , arr_size , bias ) ;
bias + = getHtml ( SCRIPT_END , * html_code , arr_size , bias ) ;
//bias += snprintf((html_code+bias), arr_size - bias, "<br />Current accelerometer: %i", acc);
//bias += snprintf((html_code+bias), arr_size - bias, "<br />Current accelerometer: %i", acc);
bias + = getHtml ( HTML_END , * html_code , arr_size , bias ) ;
bias + = getHtml ( HTML_END , * html_code , arr_size , bias ) ;
@ -38,20 +42,38 @@ void handleAccelerometer()
delete [ ] html_code ;
delete [ ] html_code ;
}
}
void cookAccelerometer ( String & in , int * in_array , int array_size ) {
String * output = & in ;
int * array = in_array ;
DynamicJsonBuffer jsonBuffer ;
JsonObject & root = jsonBuffer . createObject ( ) ;
JsonArray & data = root . createNestedArray ( " data " ) ;
for ( int i = 0 ; i ! = array_size ; i + + ) {
data . add ( * ( array + i ) ) ;
}
root . printTo ( * output ) ;
}
void updateAccelerometer ( )
void updateAccelerometer ( )
{
{
if ( ( millis ( ) - accelerometer . lastUpdate ) > ( 1000 / graphAcc . graphUpdateRate ) )
if ( ( millis ( ) - accelerometer . lastUpdate ) > ( 1000 / graphAcc . graphUpdateRate ) )
{
{
accelerometer . lastValue = map ( analogRead ( A0 ) , 512 , 768 , 200 , 0 ) ;
accelerometer . lastValue = map ( analogRead ( A0 ) , 512 , 768 , 200 , 0 ) ;
if ( graphAcc . graphCounter < graphAcc . graphSize - 1 ) {
if ( graphAcc . graphCounter < graphAcc . graphSize - 1 )
{
graphAcc . graphArray [ graphAcc . graphCounter ] = accelerometer . lastValue ;
graphAcc . graphArray [ graphAcc . graphCounter ] = accelerometer . lastValue ;
graphAcc . graphCounter + + ;
graphAcc . graphCounter + + ;
} else {
}
for ( int i = 0 ; i ! = graphAcc . graphCounter ; i + + ) {
else
{
for ( int i = 0 ; i ! = graphAcc . graphCounter ; i + + )
{
graphAcc . graphArray [ i ] = graphAcc . graphArray [ i + 1 ] ;
graphAcc . graphArray [ i ] = graphAcc . graphArray [ i + 1 ] ;
}
}
graphAcc . graphArray [ graphAcc . graphCounter ] = accelerometer . lastValue ;
graphAcc . graphArray [ graphAcc . graphCounter ] = accelerometer . lastValue ;
}
}
accelerometer . lastUpdate = millis ( ) ;
accelerometer . lastUpdate = millis ( ) ;
}
}
}
}
@ -73,28 +95,67 @@ void handleNotFound()
server . send ( 404 , " text/plain " , message ) ;
server . send ( 404 , " text/plain " , message ) ;
}
}
void sendAccelerometerData ( ) {
if ( millis ( ) - lastDataSend > 100 ) {
String word ;
cookAccelerometer ( word , graphAcc . graphArray , graphAcc . graphSize ) ;
webSocket . broadcastTXT ( word ) ;
lastDataSend = millis ( ) ;
}
}
//** WEBSOCKET TEST **//
void webSocketEvent ( uint8_t num , WStype_t type , uint8_t * payload , size_t length )
{
switch ( type )
{
case WStype_DISCONNECTED :
Serial . printf ( " [%u] Disconnected! \n " , num ) ;
break ;
case WStype_CONNECTED :
{
IPAddress ip = webSocket . remoteIP ( num ) ;
Serial . println ( " Client connected! " ) ;
Serial . printf ( " [%u] Connected from %d.%d.%d.%d url: %s \n " , num , ip [ 0 ] , ip [ 1 ] , ip [ 2 ] , ip [ 3 ] , payload ) ;
}
break ;
}
}
//** WEBSOCKET TEST END**//
void setup ( void )
void setup ( void )
{
{
Serial . begin ( 115200 ) ;
Serial . setDebugOutput ( true ) ;
Serial . println ( ) ;
pinMode ( A0 , INPUT ) ;
pinMode ( A0 , INPUT ) ;
Serial . begin ( 115200 ) ;
WiFi . softAP ( ssid ) ;
WiFi . softAP ( ssid ) ;
IPAddress myIP = WiFi . softAPIP ( ) ;
IPAddress myIP = WiFi . softAPIP ( ) ;
Serial . print ( " AP IP address: " ) ;
Serial . print ( " AP IP address: " ) ;
Serial . println ( myIP ) ;
Serial . println ( myIP ) ;
server . on ( " /accelerometer " , handleAccelerometer ) ;
server . on ( " /accelerometer " , handleAccelerometer ) ;
server . on ( " / " , [ ] ( ) {
server . on ( " / " , [ ] ( ) {
server . send ( 200 , " text/plain " , " http://192.168.4.1/accelerometer " ) ;
server . send ( 200 , " text/html " , " <a href= \" http://192.168.4.1/accelerometer \" >Main Page</a> " ) ;
} ) ;
} ) ;
server . onNotFound ( handleNotFound ) ;
server . onNotFound ( handleNotFound ) ;
server . begin ( ) ;
server . begin ( ) ;
Serial . println ( " HTTP server started " ) ;
Serial . println ( " HTTP server started " ) ;
//** WEBSOCKET TEST **//
webSocket . begin ( ) ;
webSocket . onEvent ( webSocketEvent ) ;
//** WEBSOCKET TEST END**//
}
}
void loop ( void )
void loop ( void )
{
{
updateAccelerometer ( ) ;
server . handleClient ( ) ;
server . handleClient ( ) ;
webSocket . loop ( ) ;
updateAccelerometer ( ) ;
sendAccelerometerData ( ) ;
}
}