0% found this document useful (0 votes)
4 views4 pages

Message

SFSAGAD

Uploaded by

felipe.fmantunes
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views4 pages

Message

SFSAGAD

Uploaded by

felipe.fmantunes
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 4

// ############# LIBRARIES ############### //

#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
#define SENSOR D3

// ############# VARIABLES ############### //

const char* SSID = "Rossoni"; // rede wifi


const char* PASSWORD = "10501050"; // senha da rede wifi
float id;
IPAddress IP;
HTTPClient http;
WiFiClient wifiClient;

String BASE_URL = "http://34.238.145.139:3000/sensors";

// ############# VARIABLES DO SENSOR ############### //

unsigned long contador = 0;


const float FATOR_CALIBRACAO = 5.5;
long currentMillis = 0;
long previousMillis = 0;
int interval = 1000;
boolean ledState = LOW;
float calibrationFactor = 5.5;
unsigned long pulseCount=0;
unsigned long pulse1Sec = 1;
float Vazao;
unsigned int Vazao;
unsigned long Volume;
const int INTERRUPCAO_SENSOR = 1;

// ############# PROTOTYPES ############### //

void initSerial();
void initWiFi();
void httpRequest(String path);
void enviadados();
void criaIP();
// ############### OBJECTS ################# //

// ############## SETUP ################# //

void setup() {
Serial.begin(115200);
initSerial();
initWiFi();
criaid();
criaIP();
pinMode(SENSOR, INPUT_PULLUP);
pulseCount = 0;
flowRate = 0.0;
flowMilliLitres = 0;
totalMilliLitres = 0;
previousMillis = 0;
attachInterrupt(digitalPinToInterrupt(SENSOR), pulseCounter,FALLING);
enviadados();
}
void ICACHE_RAM_ATTR pulseCounter()
{
pulseCount++;
}

// ############### LOOP ################# //

void loop() {

Serial.println("[GET] /sensors - sending request...");


Serial.println("");

httpRequest("sensors");
vazao();

Serial.println("");
delay(1000);

// ############# HTTP REQUEST ################ //

void httpRequest(String path)


{
String payload = makeRequest(path);

if (!payload) {
return;
}

Serial.println("##[RESULT]## ==> " + payload);

String makeRequest(String path)


{
http.begin(wifiClient,BASE_URL);
int httpCode = http.GET();

if (httpCode < 0) {
Serial.println("request error - " + httpCode);
return "";

if (httpCode != HTTP_CODE_OK) {
return "";
}

String response = http.getString();


http.end();

return response;
}

// ###################################### //
// implementacao dos prototypes

void initSerial() {
Serial.begin(115200);
}

void initWiFi() {
delay(10);
Serial.println("Conectando-se em: " + String(SSID));

WiFi.begin(SSID, PASSWORD);
while (WiFi.status() != WL_CONNECTED) {
delay(100);
Serial.print(".");
}
Serial.println();
Serial.print("Conectado na Rede " + String(SSID) + " | IP => ");
Serial.println(WiFi.localIP());
}
void enviadados(){

criaid();
criaIP();
sensor();
http.begin(wifiClient,"http://34.238.145.139:3000/sensors");
http.addHeader("content-type", "application/x-www-form-urlencoded");
String body = "id="+ String(id) +
"/nlocal=cozinha"+"/nVazao=10"+"/nVolume=5"+"/nIP=" + WiFi.localIP().toString();

int httpCode = http.POST(body);

}
void sensor(){

void vazao(){
currentMillis = millis();
if (currentMillis - previousMillis > interval) {
//detachInterrupt(SENSOR);
pulse1Sec = pulseCount;
Serial.print(pulse1Sec);
Vazao = ((1000.0 / (millis() - previousMillis)) * pulse1Sec) /
calibrationFactor;
previousMillis = millis();
Volume = (Vazao / 60);
totalMilliLitres +=Volume;
// Print the flow rate for this second in litres / minute
Serial.print(pulseCount);
Serial.print("Flow rate: ");
Serial.print(int(Vazao)); // Print the integer part of the variable
Serial.print("L/min");
Serial.print("\t"); // Print tab space
// Print the cumulative total of litres flowed since starting
Serial.print("Output Liquid Quantity: ");
Serial.print(Volume);
Serial.print("L / ");
}
}

}
void criaid(){
id = ESP.getFlashChipId();
/*
Serial.print("Chip Real Size: ");
Serial.println(ESP.getFlashChipRealSize());

Serial.print("Chip Size: ");


Serial.println(ESP.getFlashChipSize());

Serial.print("Chip Speed: ");


Serial.println(ESP.getFlashChipSpeed());

Serial.print("Chip Mode: ");


Serial.println(ESP.getFlashChipMode());
*/
}
void criaIP(){
IP=WiFi.localIP();
}

You might also like