Created
May 30, 2023 00:58
-
-
Save almcarvalho/629e2ddbf2d0b82b8fb1bcee496c88e1 to your computer and use it in GitHub Desktop.
Código fonte do esp8266
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 <Arduino.h> | |
#include <ESP8266WiFi.h> | |
#include <ESP8266HTTPClient.h> | |
#include <WiFiClientSecureBearSSL.h> | |
// Replace with your network credentials | |
const char* ssid = "teste"; | |
const char* password = "teste123"; | |
const int led1 = 2; | |
const int led2 = 13; | |
const int rele = 5; | |
void setup() { | |
pinMode(led1, OUTPUT); | |
pinMode(led2, OUTPUT); | |
pinMode(rele, OUTPUT); | |
Serial.begin(115200); | |
//Serial.setDebugOutput(true); | |
Serial.println(); | |
Serial.println(); | |
Serial.println(); | |
//Connect to Wi-Fi | |
WiFi.mode(WIFI_STA); | |
WiFi.begin(ssid, password); | |
Serial.print("Connecting to WiFi .."); | |
while (WiFi.status() != WL_CONNECTED) { | |
Serial.print('.'); | |
digitalWrite(led1, LOW); | |
delay(1000); | |
} | |
} | |
void loop() { | |
// wait for WiFi connection | |
if ((WiFi.status() == WL_CONNECTED)) { | |
std::unique_ptr<BearSSL::WiFiClientSecure>client(new BearSSL::WiFiClientSecure); | |
// Ignore SSL certificate validation | |
client->setInsecure(); | |
//create an HTTPClient instance | |
HTTPClient https; | |
//Initializing an HTTPS communication using the secure client | |
Serial.print("[HTTPS] begin...\n"); | |
if (https.begin(*client, "https://fit2sell.herokuapp.com/rafael")) { // HTTPS | |
Serial.print("[HTTPS] GET...\n"); | |
// start connection and send HTTP header | |
int httpCode = https.GET(); | |
// httpCode will be negative on error | |
if (httpCode > 0) { | |
// HTTP header has been send and Server response header has been handled | |
Serial.printf("[HTTPS] GET... code: %d\n", httpCode); | |
// file found at server | |
if (httpCode == HTTP_CODE_OK || httpCode == HTTP_CODE_MOVED_PERMANENTLY) { | |
String payload = https.getString(); | |
String valor = payload.substring(4,5); | |
Serial.println(payload); | |
Serial.println(valor); | |
if(valor == "2"){ | |
Serial.println("dois pulsos"); | |
digitalWrite(rele, HIGH); | |
delay(20); | |
digitalWrite(rele, LOW); | |
} | |
} | |
digitalWrite(led1, HIGH); | |
} else { | |
Serial.printf("[HTTPS] GET... failed, error: %s\n", https.errorToString(httpCode).c_str()); | |
} | |
https.end(); | |
} else { | |
Serial.printf("[HTTPS] Unable to connect\n"); | |
digitalWrite(led1, LOW); | |
} | |
} | |
Serial.println(); | |
Serial.println("Waiting 2min before the next round..."); | |
delay(1000); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment