projetsdiy / esp8266-webserver-tutorials Goto Github PK
View Code? Open in Web Editor NEWHow to use ESP8266 as a Web Server
Home Page: http://www.diyprojects.io/category/esp8266/esp8266-interface-html-server-web/
License: MIT License
How to use ESP8266 as a Web Server
Home Page: http://www.diyprojects.io/category/esp8266/esp8266-interface-html-server-web/
License: MIT License
I think you have a file missing. script.js
When I tried to run the second tutorial it came up with the above error. I think it might have been what was needed to perform an action on the page. I don't know as I wasn't able to change the outputs and think that maybe it was due to the missing file.
Hi
Thank you for your effort and time in this fantastic work tutorial. This is changing the world. A lot of technologies on 4mb ¡¡¡
A lot of Thanks
Good morning
I'm learning a lot thanks to this tutorial. I would like to make a modification in the bootstrap table to add 2 functions:
My level of Jquery and bootstrap is a beginner. I have tried a number of modifications but without success:
<script>
$ .fn.editable.defaults.mode = 'inline';
.........
</script>
<th data-field = "mesure" data-editable = "true" data-align = "left" data-sortable = "true" data-formatter = "labelFormatter"> Mesure </ th>
$ ('# mesure'). editable ({
mode: 'inline',
});
Could you give me some indication of how to build this modification?
Thank you for your work ¡¡¡
Can someone help me with this, i stripped down the code to work to run on the ESp8266 -01 , It does not seem to work. I am using an sr04 ultra-sonic sensor.
the same code works on the lolin nodemcu v3
`
#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
#include <FS.h>
#include <TimeLib.h>
#include <NtpClientLib.h>
#include <ArduinoJson.h>
#define ssid "KharisHome" // WiFi SSID
#define password "De_deziner1" // WiFi password
#define trig 0
#define echo 2 // Broche du DHT / DHT Pin
#define HISTORY_FILE "/history.json"
float t = 0 ;
int sizeHist = 84 ; // Taille historique (7h x 12pts) - History size
const long intervalHist = 1000 * 60 * 5; // 5 mesures / heure - 5 measures / hours
// Création des objets / create Objects
ESP8266WebServer server ( 80 );
StaticJsonBuffer<10000> jsonBuffer; // Buffer static contenant le JSON courant - Current JSON static buffer
JsonObject& root = jsonBuffer.createObject();
JsonArray& timestamp = root.createNestedArray("timestamp");
JsonArray& hist_t = root.createNestedArray("t");
char json[10000]; // Buffer pour export du JSON - JSON export buffer
void sendMesures() {
String json = "{"t":"" + String(t) + ""}";
server.send(200, "application/json", json);
Serial.println("Send measures");
}
void sendTabMesures() {
double temp = root["t"][0]; // Récupère la plus ancienne mesure (temperature) - get oldest record (temperature)
String json = "[";
json += "{"mesure":"Température","valeur":"" + String(t) + "","unite":"°C","glyph":"glyphicon-indent-left","precedente":"" + String(temp) + ""}";
json += "]";
server.send(200, "application/json", json);
Serial.println("Send data tab");
}
void sendHistory(){
root.printTo(json, sizeof(json)); // Export du JSON dans une chaine - Export JSON object as a string
server.send(200, "application/json", json); // Envoi l'historique au client Web - Send history data to the web client
Serial.println("Send History");
}
void loadHistory(){
File file = SPIFFS.open(HISTORY_FILE, "r");
if (!file){
Serial.println("Aucun historique existe - No History Exist");
} else {
size_t size = file.size();
if ( size == 0 ) {
Serial.println("Fichier historique vide - History file empty !");
} else {
std::unique_ptr<char[]> buf (new char[size]);
file.readBytes(buf.get(), size);
JsonObject& root = jsonBuffer.parseObject(buf.get());
if (!root.success()) {
Serial.println("Impossible de lire le JSON - Impossible to read JSON file");
} else {
Serial.println("Historique charge - History loaded");
root.prettyPrintTo(Serial);
}
}
file.close();
}
}
void saveHistory(){
Serial.println("Save History");
File historyFile = SPIFFS.open(HISTORY_FILE, "w");
root.printTo(historyFile); // Exporte et enregsitre le JSON dans la zone SPIFFS - Export and save JSON object to SPIFFS area
historyFile.close();
}
void setup() {
pinMode(trig, OUTPUT);
pinMode(echo, INPUT);
NTP.onNTPSyncEvent([](NTPSyncEvent_t error) {
if (error) {
Serial.print("Time Sync error: ");
if (error == noResponse)
Serial.println("NTP server not reachable");
else if (error == invalidAddress)
Serial.println("Invalid NTP server address");
}
else {
Serial.print("Got NTP time: ");
Serial.println(NTP.getTimeDateString(NTP.getLastNTPSync()));
}
});
// Serveur NTP, decalage horaire, heure été - NTP Server, time offset, daylight
NTP.begin("pool.ntp.org", 0, true);
NTP.setInterval(60000);
delay(500);
Serial.begin ( 115200 );
WiFi.begin ( ssid, password );
int tentativeWiFi = 0;
// Attente de la connexion au réseau WiFi / Wait for connection
while ( WiFi.status() != WL_CONNECTED ) {
delay ( 500 ); Serial.print ( "." );
tentativeWiFi++;
if ( tentativeWiFi > 20 ) {
ESP.reset();
while(true)
delay(1);
}
}
// Connexion WiFi établie / WiFi connexion is OK
Serial.println ( "" );
Serial.print ( "Connected to " ); Serial.println ( ssid );
Serial.print ( "IP address: " ); Serial.println ( WiFi.localIP() );
if (!SPIFFS.begin()) {
Serial.println("SPIFFS Mount failed"); // Problème avec le stockage SPIFFS - Serious problem with SPIFFS
} else {
Serial.println("SPIFFS Mount succesfull");
loadHistory();
}
delay(50);
server.on("/tabmesures.json", sendTabMesures);
server.on("/mesures.json", sendMesures);
//server.on("/gpio", updateGpio);
server.on("/graph_temp.json", sendHistory);
server.serveStatic("/js", SPIFFS, "/js");
server.serveStatic("/css", SPIFFS, "/css");
server.serveStatic("/img", SPIFFS, "/img");
server.serveStatic("/", SPIFFS, "/index.html");
server.begin();
Serial.println ( "HTTP server started" );
Serial.print("Uptime :");
Serial.println(NTP.getUptime());
Serial.print("LastBootTime :");
Serial.println(NTP.getLastBootTime());
}
void loop() {
// put your main code here, to run repeatedly:
server.handleClient();
float duration, distance;
digitalWrite(trig, LOW);
//
delayMicroseconds(2);
digitalWrite(trig, HIGH);
delayMicroseconds(10);
digitalWrite(trig, LOW);
//
duration = pulseIn(echo, HIGH);
//
distance = duration / 58;
Serial.print(distance);
Serial.println(" cm");
delay(100);
t = duration / 58;
//t=34+t;
if ( isnan(t) ) {
//Erreur, aucune valeur valide - Error, no valid value
} else {
addPtToHist();
}
//delay(5);
}
void addPtToHist(){
//Serial.println(currentMillis - previousMillis);
long int tps = NTP.getTime();
//Serial.println(NTP.getTime());
if ( tps > 0 ) {
timestamp.add(tps);
hist_t.add(double_with_n_digits(t, 1));
//root.printTo(Serial);
if ( hist_t.size() > sizeHist ) {
//Serial.println("efface anciennes mesures");
timestamp.removeAt(0);
hist_t.removeAt(0);
;
}
//Serial.print("size hist_t ");Serial.println(hist_t.size());
// calcStat();
delay(100);
saveHistory();
//root.printTo(Serial);
}
}```
What a great tutorial!
I have looked everywhere but could not find an answer for this problem.
I have more than one day of data. The grapics starts over at the left side as there is the time 00:00
Now, how can I use the date and time for the hAxis?
If I simply add a row “datetime” I get an error “invallid type”.
Can you please help me?
Hi
I have some issues with the Tutorial5.
Data are stored in the JSON file:
{"timestamp":[1519303055,1519303075,1519303095],"t":[25.4,25.4,25.4],"h":[19.8,20.2,20.1],"pa":[1050,1050,1050],"bart":[],"barh":[]}
Graph is populated with the values and work as expected until a reboot of the ESP!
When the ESP reboots the file is read and in the loadHistory() function I see that the file is read with the root.prettyPrintTo(Serial);
BUT the graph never get the old data, program starts with a reading of the current temp/humidity and overwrites the file so I lose all the history.
Have anyone else experienced this and if so solved the read-back from file to the current buffer?
Hello,
thanks for your great tutorial. It's really good.
I have an issue with the json load.
It loads the json fine, but in another root... so, the rest of the app can't use it.
is it a mistake ?
if no, did I miss something ?
if yes, how to fix it ? ;)
Thanks !!
Francois (greetings from france)
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.