Skip to content

Instantly share code, notes, and snippets.

@zenmanenergy
Last active May 30, 2017 16:52
Show Gist options
  • Save zenmanenergy/342cd1f6595932690006f3a47b27743d to your computer and use it in GitHub Desktop.
Save zenmanenergy/342cd1f6595932690006f3a47b27743d to your computer and use it in GitHub Desktop.
<!DOCTYPE HTML>
<html>
<script>
var app={};
</script>
<script src="jquery.js"></script>
<script src="robot.js"></script>
<head>
<script>
/*
* setTemperature(1);
setWaterLevel(2);
setLight(3);
setHumidity(4);
*/
function setTemperature(temperature){
console.log("temperature",temperature);
$("#temp").html(temperature + "C");
}
function setWaterLevel(waterLevel){
//console.log("Water Level",waterLevel);
$("#water").html(waterLevel +"%");
}
function setLight(lightLevel){
console.log("Light Level",lightLevel);
$("#light").html(lightLevel + " Lumens");
}
function setHumidity(humidity){
console.log("Humidity",humidity) ;
$("#humidity").html(humidity + "%");
}
$( document ).ready(function() {
onPageLoaded();
});
function onPageLoaded(){
app.robot.ip="192.168.0.167";
app.robot.connect();
}
</script>
<style>
body{
font-size:30px;
}
</style>
</head>
<body>
Temperature: <span id="temp">25C</span><BR>
Water Level: <span id="water">60%</span><BR>
Light: <span id="light">1000 Lumens</span><BR>
Humidity: <span id="humidity">60%</span><BR>
</body>
</html>
#include <ESP8266WiFi.h>
#include <ESP8266WiFiMulti.h>
#include <ESPAsyncTCP.h>
#include <ESPAsyncWebServer.h>
#include <ArduinoJson.h>
ESP8266WiFiMulti wifiMulti;
AsyncWebServer server(80);
AsyncWebSocket ws("/ws");
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
wifiMulti.addAP("757 Makerspace | Members", "MakingTheFuture");
//wifiMulti.addAP("nelsons1","mwitcitw711");
WiFiEvents();
//WiFi.disconnect();
//WiFi.softAP("stevesdevice","");
//WiFi.mode(WIFI_AP_STA);
Serial.println("");
while(wifiMulti.run() != WL_CONNECTED) {
Serial.print(".");
delay(100);
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
ws.onEvent(onWsServerEvent);
server.addHandler(&ws);
server.begin();
}
void loop() {
wifiMulti.run();
}
void onWsServerEvent(AsyncWebSocket * server, AsyncWebSocketClient * _client, AwsEventType type, void * arg, uint8_t *data, size_t len){
/**************************************************************************************
* The WS_EVT_CONNECT event fires when a client connects for the first time.
* it only fires a single time per session.
**************************************************************************************/
if(type == WS_EVT_CONNECT){
Serial.printf("ws[%s][%u] connect\n", server->url(), _client->id());
_client->ping();
/**************************************************************************************
* this is a request to the wand to tell it more about who the wand is
* ultimately we want to connect a "playerId" with a _client->id()
**************************************************************************************/
_client->text("{\"action\":\"whoareyou\"}");
return;
}
/**************************************************************************************
* The WS_EVT_DISCONNECT event fires when a client DISconnects for the first time.
* it only fires a single time per session.
**************************************************************************************/
else if(type == WS_EVT_DISCONNECT){
Serial.printf("ws[%s][%u] disconnect: %u\n", server->url(), _client->id());
}
/**************************************************************************************
* I've never seen these fire!
**************************************************************************************/
else if(type == WS_EVT_ERROR){
Serial.printf("ws[%s][%u] error(%u): %s\n", server->url(), _client->id(), *((uint16_t*)arg), (char*)data);
} else if(type == WS_EVT_PONG){
Serial.printf("ws[%s][%u] pong[%u]: %s\n", server->url(), _client->id(), len, (len)?(char*)data:"");
}
/**************************************************************************************
* This fires when new data comes from the wands. it is a json packet.
**************************************************************************************/
else if(type == WS_EVT_DATA){
AwsFrameInfo * info = (AwsFrameInfo*)arg;
String msg = "";
if(info->final && info->index == 0 && info->len == len){
/**************************************************************************************
* I don't know what this is. I think it's creating a single string of
* text that is ultimately the json string
**************************************************************************************/
if(info->opcode == WS_TEXT){
for(size_t i=0; i < info->len; i++) {
msg += (char) data[i];
}
} else {
char buff[3];
for(size_t i=0; i < info->len; i++) {
sprintf(buff, "%02x ", (uint8_t) data[i]);
msg += buff ;
}
}
if(info->opcode == WS_TEXT){
/**************************************************************************************
* To keep the client alive a "p" character (for "ping")is sent back and forth.
**************************************************************************************/
if (strcmp(msg.c_str(), "p") == 0){
_client->text("p");
return;
}
/**************************************************************************************
* parse the json string and turn it into a c++ object called "root"
**************************************************************************************/
char *jsonString=(char *)msg.c_str();
DynamicJsonBuffer jsonBuffer;
Serial.println(jsonString);
JsonObject& root = jsonBuffer.parseObject(jsonString);
if (!root.success()) {
Serial.println("parseObject() failed");
return;
} else{
Serial.println("parse success!");
}
const char* action=root["action"];
Serial.println(action);
/**************************************************************************************
* "test"
* this is used to debug items
**************************************************************************************/
if (strcmp(action, "test") == 0){
Serial.println("test");
}
}
}
}
}
WiFiEventHandler stationModeGotIP, stationModeDisconnected, softAPModeStationConnected, softAPModeStationDisconnected;
void WiFiEvents() {
stationModeGotIP = WiFi.onStationModeGotIP([](const WiFiEventStationModeGotIP& event)
{
Serial.println("[WiFi-event] WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
});
stationModeDisconnected = WiFi.onStationModeDisconnected([](const WiFiEventStationModeDisconnected& event)
{
Serial.println("[WiFi-event] WiFi lost connection");
});
softAPModeStationConnected = WiFi.onSoftAPModeStationConnected([](const WiFiEventSoftAPModeStationConnected& event)
{
Serial.println("[WiFi-event] client connected");
});
softAPModeStationDisconnected = WiFi.onSoftAPModeStationDisconnected([](const WiFiEventSoftAPModeStationDisconnected& event)
{
Serial.println("[WiFi-event] client disconnected");
});
}
#include <ESP8266WiFi.h>
#include <ESP8266WiFiMulti.h>
#include <ESPAsyncTCP.h>
#include <ESPAsyncWebServer.h>
#include <ArduinoJson.h>
ESP8266WiFiMulti wifiMulti;
AsyncWebServer server(80);
AsyncWebSocket ws("/ws");
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
//wifiMulti.addAP("757 Makerspace | Members", "MakingTheFuture");
//wifiMulti.addAP("nelsons1","mwitcitw711");
WiFiEvents();
WiFi.disconnect();
WiFi.softAP("stevesdevice","");
WiFi.mode(WIFI_AP_STA);
Serial.println("");
/* this loop will connect to the internet
*
while(wifiMulti.run() != WL_CONNECTED) {
Serial.print(".");
delay(100);
}
*/
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
ws.onEvent(onWsServerEvent);
server.addHandler(&ws);
server.begin();
}
void loop() {
wifiMulti.run();
}
void onWsServerEvent(AsyncWebSocket * server, AsyncWebSocketClient * _client, AwsEventType type, void * arg, uint8_t *data, size_t len){
/**************************************************************************************
* The WS_EVT_CONNECT event fires when a client connects for the first time.
* it only fires a single time per session.
**************************************************************************************/
if(type == WS_EVT_CONNECT){
Serial.printf("ws[%s][%u] connect\n", server->url(), _client->id());
_client->ping();
/**************************************************************************************
* this is a request to the wand to tell it more about who the wand is
* ultimately we want to connect a "playerId" with a _client->id()
**************************************************************************************/
_client->text("{\"action\":\"whoareyou\"}");
return;
}
/**************************************************************************************
* The WS_EVT_DISCONNECT event fires when a client DISconnects for the first time.
* it only fires a single time per session.
**************************************************************************************/
else if(type == WS_EVT_DISCONNECT){
Serial.printf("ws[%s][%u] disconnect: %u\n", server->url(), _client->id());
}
/**************************************************************************************
* I've never seen these fire!
**************************************************************************************/
else if(type == WS_EVT_ERROR){
Serial.printf("ws[%s][%u] error(%u): %s\n", server->url(), _client->id(), *((uint16_t*)arg), (char*)data);
} else if(type == WS_EVT_PONG){
Serial.printf("ws[%s][%u] pong[%u]: %s\n", server->url(), _client->id(), len, (len)?(char*)data:"");
}
/**************************************************************************************
* This fires when new data comes from the wands. it is a json packet.
**************************************************************************************/
else if(type == WS_EVT_DATA){
AwsFrameInfo * info = (AwsFrameInfo*)arg;
String msg = "";
if(info->final && info->index == 0 && info->len == len){
/**************************************************************************************
* I don't know what this is. I think it's creating a single string of
* text that is ultimately the json string
**************************************************************************************/
if(info->opcode == WS_TEXT){
for(size_t i=0; i < info->len; i++) {
msg += (char) data[i];
}
} else {
char buff[3];
for(size_t i=0; i < info->len; i++) {
sprintf(buff, "%02x ", (uint8_t) data[i]);
msg += buff ;
}
}
if(info->opcode == WS_TEXT){
/**************************************************************************************
* To keep the client alive a "p" character (for "ping")is sent back and forth.
**************************************************************************************/
if (strcmp(msg.c_str(), "p") == 0){
_client->text("p");
return;
}
/**************************************************************************************
* parse the json string and turn it into a c++ object called "root"
**************************************************************************************/
char *jsonString=(char *)msg.c_str();
DynamicJsonBuffer jsonBuffer;
Serial.println(jsonString);
JsonObject& root = jsonBuffer.parseObject(jsonString);
if (!root.success()) {
Serial.println("parseObject() failed");
return;
} else{
Serial.println("parse success!");
}
const char* action=root["action"];
Serial.println(action);
/**************************************************************************************
* "test"
* this is used to debug items
**************************************************************************************/
if (strcmp(action, "temperature") == 0){
Serial.println("temperature");
} else if (strcmp(action, "light") == 0){
Serial.println("light");
}
}
}
}
}
WiFiEventHandler stationModeGotIP, stationModeDisconnected, softAPModeStationConnected, softAPModeStationDisconnected;
void WiFiEvents() {
stationModeGotIP = WiFi.onStationModeGotIP([](const WiFiEventStationModeGotIP& event)
{
Serial.println("[WiFi-event] WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
});
stationModeDisconnected = WiFi.onStationModeDisconnected([](const WiFiEventStationModeDisconnected& event)
{
Serial.println("[WiFi-event] WiFi lost connection");
});
softAPModeStationConnected = WiFi.onSoftAPModeStationConnected([](const WiFiEventSoftAPModeStationConnected& event)
{
Serial.println("[WiFi-event] client connected");
});
softAPModeStationDisconnected = WiFi.onSoftAPModeStationDisconnected([](const WiFiEventSoftAPModeStationDisconnected& event)
{
Serial.println("[WiFi-event] client disconnected");
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment