Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Not working on ESP8266-01 #7

Open
Sphinix1 opened this issue Feb 7, 2019 · 0 comments
Open

Not working on ESP8266-01 #7

Sphinix1 opened this issue Feb 7, 2019 · 0 comments

Comments

@Sphinix1
Copy link

Sphinix1 commented Feb 7, 2019

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);  

}
}```

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant