Hi,
Is it possible to send a http get request to a web site using basic authentication with blynk librarires or esp8266 libraries?
My hardware is arduino mega 2566 with esp8266.
My current code is below:
İ want to add http get request to a web page and send result to dashboard using virtual pins. My web page requires basic authentication.
#include <Arduino.h>
#include <BlynkSimpleShieldEsp8266.h>
#include <ESP8266_Lib.h>
#include <TFT.h>
#include <TimeLib.h>
#include <WidgetRTC.h>
#pragma region dallas // dallas setup
#include <OneWire.h>
#include <DallasTemperature.h>
#define ONE_WIRE_BUS 3 // dallas pin on mega
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
uint8_t mySensor[12][8] = {
// dallas sensors
{0x28, 0x38, 0x6C, 0x49, 0xF6, 0xEF, 0x3C, 0x3D}, // asic1
{0x28, 0xBE, 0x55, 0x49, 0xF6, 0x44, 0x3C, 0xDF}, // asic2
{0x28, 0x65, 0x97, 0x48, 0xF6, 0x65, 0x3C, 0xC8}, // asic3
{0x28, 0x54, 0x4C, 0x49, 0xF6, 0xC3, 0x3C, 0xF7}, // asic4
{0x28, 0x52, 0x11, 0x48, 0xF6, 0xF0, 0x3C, 0xCB}, // onboard room
{0x28, 0xC8, 0x47, 0x48, 0xF6, 0x88, 0x3C, 0x7C}, // oilin
{0x28, 0x99, 0xC9, 0x49, 0xF6, 0x00, 0x3C, 0xDA}, // oilout
{0x28, 0x4E, 0x7A, 0x80, 0xE3, 0xE1, 0x3C, 0xFE}, //1 waterin
{0x28, 0xA9, 0x03, 0x80, 0xE3, 0xE1, 0x3C, 0xCC}, //2 waterout
{0x28, 0x3D, 0x8B, 0x80, 0xE3, 0xE1, 0x3C, 0x54}, //3 pump1
{0x28, 0x3C, 0x23, 0x80, 0xE3, 0xE1, 0x3C, 0x6E}, //4 pump2
{0x28, 0x40, 0xD2, 0x80, 0xE3, 0xE1, 0x3C, 0xE0} //5 outside
};
// {0x28, 0x81, 0xD9, 0x48, 0xF6, 0x5A, 0x3C, 0x43}, // arızalı
// {0x28, 0x5A, 0x3E, 0x49, 0xF6, 0xE2, 0x3C, 0x00} // arızalı
#pragma endregion dallas
#pragma region blynkSetup
int blynkLastSend = 0;
time_t sytemStartTime;
char ssid[] = "xxxxx";
char pass[] = "xxxxxxxx";
#define EspSerial Serial
#define ESP8266_BAUD 115200
//#define ESP8266_BAUD 9600
ESP8266 wifi(&EspSerial);
HttpClient client = HttpClient();
/*
#define BLYNK_TEMPLATE_ID "xxxxxxxxxxx"
#define BLYNK_DEVICE_NAME "xxxxx"
#define BLYNK_AUTH_TOKEN "xxxxxxxxxxxx"
*/
#define BLYNK_TEMPLATE_ID "xxxxxxx"
#define BLYNK_TEMPLATE_NAME "Tester"
#define BLYNK_AUTH_TOKEN "xxxxxxxxxxx"
char auth[] = BLYNK_AUTH_TOKEN;
BlynkTimer myTimer;
WidgetRTC rtc;
#pragma endregion blynkSetup
const int wifiLedPin = 4;
const int switchPins[] = {31, 32, 33, 34, 35, 36, 37}; // define push button inputs
const int relayPins[] = {21, 22, 23, 24, 25, 26, 27, 28}; // output pins where 4 relays will be connected
int toggleState[] = {0, 0, 0, 0, 0, 0, 0}; // status of each buttons
int wifiFlag = 10;
String formatDateTime(int x)
{
if (x < 10)
return "0" + String(x);
else
return String(x);
}
String getCurrenctTime()
{
return formatDateTime(month()) + "" + formatDateTime(day()) + " " + formatDateTime(hour()) + ":" + formatDateTime(minute()) + ":" + formatDateTime(second());
}
String getStartTime()
{
return formatDateTime(month(sytemStartTime)) + "" + formatDateTime(day(sytemStartTime)) + " " + formatDateTime(hour(sytemStartTime)) + ":" + formatDateTime(minute(sytemStartTime)) + ":" + formatDateTime(second(sytemStartTime));
}
String getElapsedTime()
{
unsigned long currentMillis = millis();
unsigned long seconds = currentMillis / 1000;
unsigned long minutes = seconds / 60;
unsigned long hours = minutes / 60;
unsigned long days = hours / 24;
currentMillis %= 1000;
seconds %= 60;
minutes %= 60;
hours %= 24;
return formatDateTime(days) + "d" + formatDateTime(hours) + "h" + formatDateTime(minutes) + "m";
}
void setTimes()
{
tft.setTextColor(ST7735_GREEN, ST7735_BLACK);
tft.setCursor(timesPixelLocX, timesPixelLocY[0]);
tft.print(getCurrenctTime());
tft.setCursor(timesPixelLocX, timesPixelLocY[1]);
tft.print(getStartTime());
tft.setCursor(timesPixelLocX, timesPixelLocY[2]);
tft.print(getElapsedTime());
blynkLastSend = blynkLastSend - 1;
if (blynkLastSend < 0)
{
Blynk.virtualWrite(20, "Times sent:" + getCurrenctTime());
Blynk.virtualWrite(21, getCurrenctTime());
Blynk.virtualWrite(22, getStartTime());
Blynk.virtualWrite(23, getElapsedTime());
blynkLastSend = 60;
}
}
void printAddress(DeviceAddress deviceAddress)
{
for (uint8_t i = 0; i < 8; i++)
{
Serial.print("0x");
if (deviceAddress[i] < 0x10)
Serial.print("0");
Serial.print(deviceAddress[i], HEX);
if (i < 7)
Serial.print(", ");
}
Serial.println("");
}
void sendSensors()
{
int myTemp = 0;
sensors.requestTemperatures();
for (int i = 0; i < 10 ;i++)
{
myTemp = sensors.getTempC(mySensor[i]);
if (myTemp < 0) myTemp = 0;
Blynk.virtualWrite(i + 1, String(myTemp));
tft.setTextColor(ST7735_GREEN, ST7735_BLACK);
tft.setCursor(tempsPixelLocX[i], tempsPixelLocY[i]);
tft.print(String(myTemp));
}
Blynk.virtualWrite(20, "Sensors sent:" + getCurrenctTime());
}
void relayOnOff(int relay)
{
// Serial.println("-------------relayOnOff:" + String(relay));
if (relay == 6)
{
if (toggleState[6] == 1)
{
digitalWrite(relayPins[6], LOW);
digitalWrite(relayPins[7], LOW);
toggleState[6] = 0;
tft.fillCircle(switchPixelLocX, switchPixelLocY[6], 5, RelayOn);
}
else
{
digitalWrite(relayPins[6], HIGH);
digitalWrite(relayPins[7], HIGH);
toggleState[6] = 1;
tft.fillCircle(switchPixelLocX, switchPixelLocY[6], 5, RelayOff);
}
}
else
{
if (toggleState[relay] == 1)
{
digitalWrite(relayPins[relay], LOW); // turn on relay 1
toggleState[relay] = 0;
tft.fillCircle(switchPixelLocX, switchPixelLocY[relay], 5, RelayOn);
}
else
{
digitalWrite(relayPins[relay], HIGH); // turn off relay 1
toggleState[relay] = 1;
tft.fillCircle(switchPixelLocX, switchPixelLocY[relay], 5, RelayOff);
}
}
// delay(100);
}
void with_internet()
{
if (wifiFlag == 0)
{
for (int i = 0; i < int(sizeof(switchPins) / sizeof(int)); i++)
{
if (digitalRead(switchPins[i]) == LOW)
{
relayOnOff(i);
Serial.println("-------------with_internet(---------------");
// Serial.println("-------------toggle---------------");
Blynk.virtualWrite(11 + i, toggleState[i]);
delay(1000);
}
}
}
}
void without_internet()
{
if (wifiFlag != 0)
{
// Manual Switch Control
for (int i = 0; i < int(sizeof(switchPins) / sizeof(int)); i++)
{
// Serial.println("digitalRead(switchPins[i]:" + digitalRead(switchPins[i]));
if (digitalRead(switchPins[i]) == LOW)
{
Serial.println("-------------without_internet(---------------");
relayOnOff(i);
// delay(100);
}
}
}
}
void initializeRelays()
{
for (int i = 0; i < int(sizeof(switchPins) / sizeof(int)); i++)
{
pinMode(switchPins[i], INPUT_PULLUP);
}
for (int i = 0; i < int(sizeof(relayPins) / sizeof(int)); i++)
{
pinMode(relayPins[i], OUTPUT);
if (i == 7)
{
digitalWrite(relayPins[i], LOW);
}
else
{
digitalWrite(relayPins[i], LOW); // initial relay status to be on
Blynk.virtualWrite(11 + i, LOW);
}
}
for (int i = 0; i < int(sizeof(switchPixelLocY) / sizeof(int)); i++)
{
tft.fillCircle(switchPixelLocX, switchPixelLocY[i], 5, RelayOn);
}
}
void setupBlynk()
{
// Set ESP8266 baud rate
EspSerial.begin(ESP8266_BAUD);
delay(100);
// Blynk.begin(auth, wifi, ssid, pass);
Blynk.begin(auth, wifi, ssid, pass, "ny3.blynk.cloud", 80);
while (Blynk.connect() == false)
{
// Wait until connected
}
// Blynk.virtualWrite(20, "Connected");
Serial.println("Connected");
}
void setupDallas()
{ // dallas initialize
// lynk.virtualWrite(20, "Myaddr");
// String Myaddr2="";
// Serial.print("Locating devices...");
sensors.begin(); // Start up the library
// Serial.print("Found ");
// deviceCount = sensors.getDeviceCount();
// Serial.print("device count:");
// Serial.println(deviceCount, DEC);
// tft.setCursor(94, 0);
// tft.setTextColor(ST7735_BLUE, ST7735_BLACK);
// tft.print("(" + String(deviceCount) + ")");
// Serial.println("Locating devices...");
// Serial.print("Found ");
// deviceCount = sensors.getDeviceCount();
// Serial.print(deviceCount, DEC);
// Serial.println(" devices.");
// Serial.println("");
// Serial.println("Printing addresses...");
// Serial.println(Myaddr2);
// Blynk.virtualWrite(20, Myaddr2);
}
BLYNK_CONNECTED()
{
// Request the latest state from the server
Blynk.syncVirtual(V11);
Blynk.syncVirtual(V12);
Blynk.syncVirtual(V13);
Blynk.syncVirtual(V14);
Blynk.syncVirtual(V15);
Blynk.syncVirtual(V16);
Blynk.syncVirtual(V17);
rtc.begin();
sytemStartTime = now();
}
void printWifiStatus(bool x)
{
tft.setTextColor(ST7735_BLUE, ST7735_WHITE);
tft.setCursor(153, 0);
if (x == true)
tft.print("+");
else
tft.print("-");
}
#pragma region blynkWrite // set blinkwrite procedures
// ka3 kontaktörler
BLYNK_WRITE(V11)
{
digitalWrite(relayPins[0], param.asInt());
toggleState[0] = param.asInt();
if (param.asInt() == 0)
tft.fillCircle(switchPixelLocX, switchPixelLocY[0], 5, RelayOn);
else
tft.fillCircle(switchPixelLocX, switchPixelLocY[0], 5, RelayOff);
}
BLYNK_WRITE(V12)
{
digitalWrite(relayPins[1], param.asInt());
toggleState[1] = param.asInt();
if (param.asInt() == 0)
tft.fillCircle(switchPixelLocX, switchPixelLocY[1], 5, RelayOn);
else
tft.fillCircle(switchPixelLocX, switchPixelLocY[1], 5, RelayOff);
}
BLYNK_WRITE(V13)
{
digitalWrite(relayPins[2], param.asInt());
toggleState[2] = param.asInt();
if (param.asInt() == 0)
tft.fillCircle(switchPixelLocX, switchPixelLocY[2], 5, RelayOn);
else
tft.fillCircle(switchPixelLocX, switchPixelLocY[2], 5, RelayOff);
}
BLYNK_WRITE(V14)
{
digitalWrite(relayPins[3], param.asInt());
toggleState[3] = param.asInt();
if (param.asInt() == 0)
tft.fillCircle(switchPixelLocX, switchPixelLocY[3], 5, RelayOn);
else
tft.fillCircle(switchPixelLocX, switchPixelLocY[3], 5, RelayOff);
}
// pompa röle
BLYNK_WRITE(V15)
{
digitalWrite(relayPins[4], param.asInt());
toggleState[4] = param.asInt();
if (param.asInt() == 0)
tft.fillCircle(switchPixelLocX, switchPixelLocY[4], 5, RelayOn);
else
tft.fillCircle(switchPixelLocX, switchPixelLocY[4], 5, RelayOff);
}
// fan röle
BLYNK_WRITE(V16)
{
digitalWrite(relayPins[5], param.asInt());
toggleState[5] = param.asInt();
if (param.asInt() == 0)
tft.fillCircle(switchPixelLocX, switchPixelLocY[5], 5, RelayOn);
else
tft.fillCircle(switchPixelLocX, switchPixelLocY[5], 5, RelayOff);
}
// chiller kontaktör
BLYNK_WRITE(V17)
{
if (param.asInt() == 0)
{
digitalWrite(relayPins[6], 0);
digitalWrite(relayPins[7], 0);
toggleState[6] = 0;
tft.fillCircle(switchPixelLocX, switchPixelLocY[6], 5, RelayOn);
}
else
{
digitalWrite(relayPins[6], 1);
digitalWrite(relayPins[7], 1);
toggleState[6] = 1;
tft.fillCircle(switchPixelLocX, switchPixelLocY[6], 5, RelayOff);
}
}
#pragma endregion blynkWrite
void checkBlynkStatus()
{ // called every 3 seconds by SimpleTimer
bool isconnected = Blynk.connected();
printWifiStatus(isconnected);
if (isconnected == false)
{
wifiFlag = 1;
digitalWrite(wifiLedPin, LOW); // Turn off WiFi LED
Blynk.connect();
}
if (isconnected == true)
{
wifiFlag = 0;
digitalWrite(wifiLedPin, HIGH); // Turn on WiFi LED
}
}
void setup(void)
{
// Debug console
Serial.begin(115200);
initializeTFT();
pinMode(wifiLedPin, OUTPUT);
setupDallas();
initializeRelays();
Serial.println("Begin setup Blynk");
setupBlynk();
Serial.println("End setup Blynk");
Serial.println(wifi.getLocalIP());
setSyncInterval(10 * 60);
myTimer.setInterval(5000L, checkBlynkStatus);
myTimer.setInterval(1000L, with_internet);
myTimer.setInterval(500L, without_internet);
myTimer.setInterval(10000L, sendSensors);
myTimer.setInterval(1000L, setTimes);
}
void loop(void)
{
if (Blynk.connected())
{
// Serial.println("WiFi Connected");
printWifiStatus(true);
Blynk.run();
}
else
{
printWifiStatus(false);
// Serial.println("WiFi Not Connected");
}
myTimer.run(); // Initiates SimpleTimer
}