Connection refused
theWEAKVik opened this issue · 5 comments
Hi. I'm a young programer and i cant seam to get a break. I'm working on a arduino mkr wifi 1010. My friend has setup a firebase real time database, i would like to connect to it via this arduino and pull a json from it so i can format it to a usable state and use the data from the database to check if it maches a cody i imput through a 4x4 keypad. This project should be mostly done, except the logic to for checking and the actual connection becase my attempts to connect to this database are resulting in a refused connection message.
I don't know how to proceed? Can you help ?
This is the code I'm using:
#include <WiFi.h>
#include <WiFiUdp.h>
#include <Wire.h>
#include <Arduino.h>
#include <U8g2lib.h>
#include <Keypad.h>
#include "Firebase_Arduino_WiFiNINA.h"
using namespace std;
U8G2_SH1106_128X64_NONAME_F_HW_I2C u8g2(U8G2_R0, /* reset=*/ U8X8_PIN_NONE);
const byte ROWS = 4;
const byte COLS = 4;
const int MAX_LENGTH = 9;
char keys[ROWS][COLS] = {
{'1','2','3','A'},
{'4','5','6','B'},
{'7','8','9','C'},
{'*','0','#','D'}
};
byte rowPins[COLS] = {A4, A3, A2, A1};
byte colPins[ROWS] = {5, 4, 3, 2};
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
String input = "";
#define Firebase_Host "https://□□□□□□□□□□□□□□□□□.firebasedatabase.app/"
#define Firebase_Auth "EY□□□□□□□□□□□□□□□XCEwJUZh7gdwuAzwT"
#define WSsid "Vik"
#define Wpassword "ToJeMojeMeno"
String path = "/Skrinky";
String path2 = "/Objednávka";
const char* ssid = "Vik";
const char* password = "ToJeMojeMeno";
WiFiClient client;
unsigned long startTime;
class PingGoogle {
public: void ping() {
startTime = millis();
if (WiFi.begin(ssid, password)) {
if (client.connect("www.google.com", 80)) {
Serial.println("Connected to Google");
} else {
Serial.println("Failed to connect to Google");
}
client.stop();
Serial.println("Response time: " + String(millis() - startTime) + "ms");
delay(5000);
} else {
Serial.println("Failed to connect to WiFi network");
}
}
};
class PingServer {
public: void ping() {
unsigned long startTime = millis();
WiFiClient client;
if (WiFi.begin(ssid, password)) {
if (client.connect("https://bilza-8760c-default-rtdb.europe-west1.firebasedatabase.app/", 3315)) {
Serial.println("Connected to server");
client.stop();
unsigned long responseTime = millis() - startTime;
if (responseTime > 3000) {
Serial.println("Response time exceeded 3000ms");
client.stop();
return;
}
Serial.println("Response time: " + String(responseTime) + "ms");
} else {
Serial.println("Failed to connect to server");
}
} else {
Serial.println("Failed to connect to WiFi network");
}
delay(500);
}
};
PingGoogle pingGoogle;
PingServer pingServer;
void setup()
{
Serial.begin(9600);
Wire.begin();
u8g2.begin();
u8g2.setFont(u8g2_font_ncenB14_tr);
u8g2.firstPage();
u8g2.clearBuffer();
u8g2.drawStr(3,25,"Zadaj kod:");
while (!Serial) {
;
}
if (WiFi.begin(ssid, password) == WL_CONNECTED) {
Serial.println("Connected to WiFi");
Serial.print("Connected with IP: ");
Serial.println(WiFi.localIP());
Serial.println();
} else {
Serial.println("Failed to connect to WiFi");
}
delay(500);
pingGoogle.ping();
Firebase.begin(Firebase_Host, Firebase_Auth, WSsid, Wpassword);
Firebase.reconnectWiFi(true);
FirebaseData data;
if (Firebase.getJSON(data, path)) {
Serial.println("Data Retrieved: ");
Serial.println(data.jsonData());
} else {
Serial.println("Error Retrieving Data: ");
Serial.println(data.errorReason());
}
if (Firebase.getJSON(data, path2)) {
Serial.println("Data Retrieved: ");
Serial.println(data.jsonData());
} else {
Serial.println("Error Retrieving Data: ");
Serial.println(data.errorReason());
}
}
void loop() {
u8g2.setFont(u8g2_font_ncenB14_tr);
u8g2.firstPage();
u8g2.clearBuffer();
u8g2.drawStr(3,25,"Zadaj kod:");
char key = keypad.getKey();
if (key)
{
if (key == 'A' || key == 'B' || key=='C' || key=='D' || input.length() >= MAX_LENGTH) {
input = "";
u8g2.clearBuffer();
u8g2.drawStr(5,25,"Zadaj kod:");
} else {
input += key;
}
do {
u8g2.drawRFrame(0,0,127,63,7);
u8g2.drawStr(5,50,input.c_str());
Serial.println(input.c_str());
}while ( u8g2.nextPage() );
}
}
You need to update firmware and add host certificate to device.
Ithe firmwase for wifi nina was updated and i canot choose a board even tho i have the arduino mkr wifi 1010 connected.
The only certificate i have the option of uploading is the arduino.cc443 but that fails every time so i dont know what to do.
I have already read the issue #18 before contacting you via this thread. I tried every thing from that isue and it changed nothing so im sorry for casuing trouble, but i didnt find any other way to fix this other than contacting you.
The Firebase_Host
should not include https://
at the beginning and /
at the end of string or only host name.
Library does not manage this for you because of the code size for some device e.g., UNO WiFi Rev2. is limited.
Thanks. It finally works.