Created
March 1, 2020 11:50
-
-
Save ryuzakyl/569b3b993d836344aaecdd21f6f22c7d to your computer and use it in GitHub Desktop.
ESP32-based HTTP daemon sending temperature value to a server from an LM35 sensor every 1s
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <WiFi.h> | |
#include <HTTPClient.h> | |
#define WIFI_SSID "<ssid>" | |
#define WIFI_PASSWORD "<password>" | |
// --------------------------------------------------------------- | |
// LM35 temperature sensor is connected to GPIO 33 (Analog ADC1_5) | |
const int lm35InputPin = 33; | |
const int esp32BitsPrecision = 10; | |
const float esp32MaxVolts = 1.5; | |
// --------------------------------------------------------------- | |
float analog_to_volts(float value, float max_volts, int n_bits) { | |
// compute analog resolution | |
float resolution = pow(2, n_bits) - 1; | |
// transform value in [0, 2^n_bits] interval, to [0, max_volts] interval | |
float volts = (value / resolution) * max_volts; | |
return volts; | |
} | |
float volts_to_celcius(float volts) { | |
// for LM35: divide by 10 mV (i.e., multiply by 100) | |
return 100.0 * volts; | |
} | |
float analog_to_celcius(float value) { | |
// converts analog value to volts | |
float volts = analog_to_volts(value, esp32MaxVolts, esp32BitsPrecision); | |
// converts volts value to celcius | |
float celcius = volts_to_celcius(volts); | |
return celcius; | |
} | |
float celcius_to_farenheit(float celcius) { | |
float fahrenheit; | |
/* celsius to fahrenheit conversion formula */ | |
fahrenheit = (celcius * 9 / 5) + 32; | |
return fahrenheit; | |
} | |
float get_lm35_temperature() { | |
// read analog value from input GPIO | |
int analog_value = analogRead(lm35InputPin); | |
// convert analog input to temperature (in celcius) | |
float t_celcius = analog_to_celcius(analog_value); | |
return t_celcius; | |
} | |
// --------------------------------------------------------------- | |
// this sets the ground pin to LOW and the input voltage pin to high | |
void setup() { | |
// using 9600 baud rate | |
Serial.begin(9600); | |
delay(1000); | |
WiFi.begin(WIFI_SSID, WIFI_PASSWORD); | |
while (WiFi.status() != WL_CONNECTED) { | |
delay(1000); | |
Serial.println("Connecting to WiFi.."); | |
} | |
Serial.println("Connected to the WiFi network"); | |
} | |
// --------------------------------------------------------------- | |
// main loop | |
void loop() { | |
// check the current connection status | |
if ((WiFi.status() == WL_CONNECTED)) | |
{ | |
// get current LM35 temperature | |
float t_celcius = get_lm35_temperature(); | |
float t_farenheit = celcius_to_farenheit(t_celcius); | |
// formatting the json content to be sent | |
char post_data[50]; | |
sprintf( | |
post_data, | |
"celcius=%.2f&farenheit=%.2f", | |
t_celcius, t_farenheit | |
); | |
HTTPClient http; | |
// set POST URL | |
http.begin("http://192.168.1.135:5000/api/1.0/temp"); | |
// specify content-type header | |
http.addHeader("Content-Type", "application/x-www-form-urlencoded"); | |
// send the request | |
int httpCode = http.POST(post_data); | |
// check for the returning code | |
if (httpCode > 0) { | |
// get the response payload | |
String payload = http.getString(); | |
Serial.println(httpCode); | |
Serial.println(payload); | |
} else { | |
Serial.println("Error on HTTP request"); | |
} | |
// close connection and free resources | |
http.end(); | |
} | |
delay(1000); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment