Skip to content

Instantly share code, notes, and snippets.

@quentinchap
Last active April 8, 2020 15:22
Show Gist options
  • Select an option

  • Save quentinchap/78b4f8b942acf45280904e74ca05e877 to your computer and use it in GitHub Desktop.

Select an option

Save quentinchap/78b4f8b942acf45280904e74ca05e877 to your computer and use it in GitHub Desktop.
// Importer les bibliothèques
#include <OneWire.h>
#include <DallasTemperature.h>
#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
#include <ArduinoJson.h>
#define ONE_WIRE_BUS 4
// Initialisation de oneWire
OneWire oneWire(ONE_WIRE_BUS);
/* Initialisation du sensor permettant la
lecture de températures */
DallasTemperature sensors(&oneWire);
const char* ssid = "xxxxx";
const char* password = "xxxxx";
const char* host = "http://xxxx";
String API_HOST = "http://xxxx";
String API_PATH = "/api/xxx";
void setup() {
// Comme nous l'avons vu dans le précédent article les sept
// lignes ci-dessous permettent de configurer le port série
// et la Led intégrée.
Serial.begin(115200);
sensors.begin();
pinMode(LED_BUILTIN, OUTPUT);
delay(1000);
Serial.begin(115200);
Serial.println("");
Serial.print("Connecting");
WiFi.mode(WIFI_OFF); //Prevents reconnection issue (taking too long to connect)
delay(1000);
WiFi.mode(WIFI_STA); //This line hides the viewing of ESP as wifi hotspot
WiFi.begin(ssid, password); //Connect to your WiFi router
// Wait for connection
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
//If connection successful show IP address in serial monitor
Serial.println("");
Serial.print("Connected to ");
Serial.println(ssid);
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
}
// On retrouve la même fonction de lecteur de température
// que dans l'article précédent
float getTemperature(){
float temperature = -255;
Serial.println("Demande de temperature");
sensors.requestTemperatures();
Serial.print("La temperature est de: ");
temperature = sensors.getTempCByIndex(0);
return temperature;
}
String serialize(float temp)
{
String output;
StaticJsonBuffer<500> jsonBuffer;
// Create the root object
JsonObject& root = jsonBuffer.createObject();
root["temperature"] = String(getTemperature());
root.printTo(output);
root.prettyPrintTo(Serial);
return output;
}
void sendApi(float temp)
{
HTTPClient http;
String data = serialize(temp);
int httpCode = 0;
http.begin(API_HOST+API_PATH);
http.addHeader("Content-Type", "application/json");
httpCode = http.POST(data);
Serial.println(httpCode); //Print HTTP return code
http.end(); //Close connection
blink();
}
void blink()
{
digitalWrite(BUILTIN_LED, LOW);
delay(10);
digitalWrite(BUILTIN_LED, HIGH);
}
void loop() {
int httpCode = 0;
float temperature = getTemperature();
sendApi(temperature);
delay(300000); //Post Data at every 5 min
}
@RXCHAEUGVPI

RXCHAEUGVPI commented Apr 8, 2020

Copy link
Copy Markdown

bonjour,
j'ai deux erreurs lors de la compilation avec arduino 1.8.12

inclut < ArduinoJson.h > c'est #include <Arduino_JSON.h>

et avec la ligne : StaticJsonBuffer < 500 > jsonBuffer;

voilà le cr des erreurs
WEMOS_Temp_ESP32:74:3: error: 'StaticJsonBuffer' was not declared in this scope

WEMOS_Temp_ESP32:74:28: error: 'jsonBuffer' was not declared in this scope

WEMOS_Temp_ESP32:77:3: error: 'JsonObject' was not declared in this scope

WEMOS_Temp_ESP32:77:15: error: 'root' was not declared in this scope

quelles sont les corrections pour la deuxième erreur

cordialement

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment