Firebase cloud function POST HTTPS Request from ESP8266 - firebase

I want to connect to an url of a cloud function but isn´t working. someone can see the error? I´m working with an ESP8266 from arduino IDE. I try sending a POST request by postman and it function perfectly, I don´t understan what is the problem with the conection, i always try to connect with out the https and only with http and again in the esp8266 didn´t wotk but in postman does works fine, I do not understant why is this problem
#include <ESP8266WiFi.h>
#include <WiFiClientSecure.h>
const char* ssid = "My wifi";
const char* password = "My Password";
const char* host = "https://us-central1-sensores4bad6.cloudfunctions.net/actualizar";
void setup() {
Serial.begin(9600);
delay(10);
Serial.println();
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}
int value = 0;
void loop() {
delay(5000);
++value;
Serial.print("connecting to ");
Serial.println(host);
WiFiClientSecure client;
const int httpPort = 443;
if (!client.connect(host, httpPort)) {
Serial.println("connection failed");
return;
}
client.print(String("POST ") + host + " HTTP/1.1\r\n" +
"Host: " + host + "\r\n" +
"Connection: close\r\n\r\n");
unsigned long timeout = millis();
while (client.available() == 0) {
if (millis() - timeout > 5000) {
Serial.println(">>> Client Timeout !");
client.stop();
return;
}
}
while (client.available()) {
String line = client.readStringUntil('\r');
Serial.print(line);
}
Serial.println();
Serial.println("closing connection");
}

Try removing the s in https. So:
const char* host = "http://us-central1-sensores4bad6.cloudfunctions.net/actualizar";
instead of
const char* host = "https://us-central1-sensores4bad6.cloudfunctions.net/actualizar";

Related

Why is esp8266 client not connected to server?

I am new to Arduino and I am not so familiar with it. I am not sure why the client cannot connect to the server and I have been looking at it for hours. Any solution to this would be a great help.
So I successfully establish a server with the code below. But...
// server.ino
#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
const char* ssid = "WifiName"; // Wifie name
const char* password = "WifiPassword"; // Wifi password
float sensor_value = 0.0;
String Website;
ESP8266WebServer server(80);
void setup(){
Serial.begin(115200);
WiFi.mode(WIFI_AP);
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while(WiFi.status()!=WL_CONNECTED){
Serial.print(".");
delay(500);
}
Serial.println("Server started at port 80.");
Serial.println("WiFi connected.");
Serial.println("IP address: ");
Serial.print("URL=http://");
Serial.println(WiFi.localIP());
server.on("/", handleIndex); //use the top root path report last sensor value
server.on("/update",handleUpdate); // use this route to update sensor value
server.begin();
}
void loop(){
server.handleClient();
}
void handleIndex(){
Website = "<html><body onload ='process()'><div id='div1'>"+ String(sensor_value) +"</div></body></html>";
server.send(200," text/html",Website);
}
void handleUpdate(){
sensor_value = server.arg("value").toFloat();
Serial.println(sensor_value);
server.send(200,"text/plain","Updated");
}
I try to connect a client to the server so it could send data to the server, but a connection between the client and the server cannot be established and I don't know why. I check the IP address and it is all correct I just don't get why the client fails to connect to the server
#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
#include <HX711.h>
#include "Wire.h"
#define DOUT D5
#define CLK D6
HX711 scale;
const char* ssid = "WifiName";
const char* password = "Wifipassowrd";
float calibration_factor = 107095;
float val_Weight;
const char* host = "192.168.0.167"; // Server host IP.
const int port = 80;
WiFiClient client;
void setup() {
Serial.begin(115200); //set baud rate
Serial.println("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED)
{
delay(500);//millisecond
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected & connect to client");
}
void loop() {
//connect to the server and send the data as URL parameter
if(client.connect(host, port)){
String url = "update?value=";
url+=String(25.0);
client.print(String("GET /") + url + "HTTP/1.1\r\n" + "Host: " + host + "\r\n" +
"Connection: close\r\n\r\n");
delay(10);
Serial.println("Response: ");
while(client.available()){
String line = client.readStringUntil('\r');
Serial.print(line);
}
}else{
Serial.println("fail");
}
}
Any help would be great thanks!!!
Check the passwords, you have a typo.
const char* password = "Wifipassowrd";

Code not connecting to MQTT local server

I am trying to connect my Arduino Uno with WiFi shield to my MQTT server. I am running it local on my Mac. It seems to be stuck in the while loop. I think it doesn't connect to the broker. Here is my code. What could be the problem with the loop? Or is there another problem?
#include <WiFi.h>
#include <MQTTClient.h>
const char ssid[] = " : /*filed in*: ";
const char pass[] = " /*filed in*/ ";
WiFiClient net;
MQTTClient client;
unsigned long lastMillis = 0;
void setup() {
Serial.begin(9600);
WiFi.begin(ssid, pass);
// Note: Local domain names (e.g. "Computer.local" on OSX) are not supported by Arduino.
// You need to set the IP address directly.
client.begin("10.0.1.5", net);
client.onMessage(messageReceived);
connect();
}
void connect() {
Serial.print("checking wifi...");
while (WiFi.status() != WL_CONNECTED) {
Serial.print(".");
delay(1000);
}
Serial.println("wifi connected");
Serial.print("\nconnecting...");
while (!client.connect("Arduino test")) {
Serial.print(".");
delay(1000);
}
Serial.println("\nconnected!");
Serial.println("connected");
client.subscribe("hello");
// client.unsubscribe("/hello");
}
void loop() {
client.loop();
if (!client.connected()) {
connect();
Serial.print("not");
}
// publish a message roughly every second.
if (millis() - lastMillis > 1000) {
lastMillis = millis();
client.publish("hello world");
}
}
void messageReceived(String &topic, String &payload) {
Serial.println("incoming: " + topic + " - " + payload);
}

Send sensor data from Nodmcu to labview

I want to send sensor data(like DHT) from NODEMCU to LABVIEW.
Is there any library or sketch to do this ?
NODEMCU is connected to network in station mode.
In labview, I have to use which library to do this?
Solved :
1. Try this library in Arduino IDE: ESP8266WiFi.h
host is your PC like 192.168.1.11
port : 550 (for example)
#include <ESP8266WiFi.h>
const char* ssid = "xxxx"; // SSID
const char* password = "xxxx"; // Password
const char* host = "xxx.xxx.xxx.xxx"; // Server IP
const int port = 550; // Server Port
const int watchdog = 5000; // Watchdog frequency
unsigned long previousMillis = millis();
void setup() {
Serial.begin(115200);
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}
void loop() {
unsigned long currentMillis = millis();
if ( currentMillis - previousMillis > watchdog ) {
previousMillis = currentMillis;
WiFiClient client;
if (!client.connect(host, port)) {
Serial.println("connection failed");
return;
}
String url = "/watchdog?command=watchdog&uptime=";
url += String(millis());
url += "&ip=";
url += WiFi.localIP().toString();
// Envoi la requete au serveur - This will send the request to the server
client.print(String("GET ") + url + " HTTP/1.1\r\n" +
"Host: " + host + "\r\n" +
"Connection: close\r\n\r\n");
unsigned long timeout = millis();
while (client.available() == 0) {
if (millis() - timeout > 5000) {
Serial.println(">>> Client Timeout !");
client.stop();
return;
}
}
// Read all the lines of the reply from server and print them to Serial
while(client.available()){
String line = client.readStringUntil('\r');
Serial.print(line);
}
}
}
2. labview : you need 2 blocks to do this :
TCP LISTEN
TCP READ

Sending GET Request with NodeMCU ESP8266

I'm trying to send a standard http GET request to my website using the NodeMCU example client code however for some reason it is not working.
If I type into my browser: snackrefill.com/GetData.php?username=aaaa&pin=bbbb&cost=cccc then it will successfully connect with the php script and save a data base entry.
The problem is that when I try to do the same using the NodeMCU module it does not work. it seems to be connecting to WiFi and the server just fine but when it sends the request nothing seems to happen.
Am I structuring my request wrong?
#include <ESP8266WiFi.h>
const char* ssid = "BELL473";
const char* password = "XXXXXXX";
const char* host = "ns8451.hostgator.com";
void setup() {
Serial.begin(115200);
delay(10);
// We start by connecting to a WiFi network
Serial.println();
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password); //works!
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}
int value = 0;
void loop() {
delay(5000);
++value;
Serial.print("connecting to ");
Serial.println(host);
// Use WiFiClient class to create TCP connections
WiFiClient client;
const int httpPort = 80;
if (!client.connect(host, httpPort)) { //works!
Serial.println("connection failed");
return;
}
// We now create a URI for the request
String url = "/GetData.php";
url += "?username=";
url += "aaaa";
url += "&pin=";
url += "bbbb";
url += "&cost=";
url += "cccc";
Serial.print("Requesting URL: ");
Serial.println(url);
// This will send the request to the server
client.print(String("GET ") + url + " HTTP/1.1\r\n" +
"Host: " + host + "\r\n" +
"Connection: close\r\n\r\n");
Serial.println();
Serial.println("closing connection");
}
The reason it was not sending was because the host was wrong, i was pointing to my website nameserver when i should have pointed it to the regular website name. I changed const char* host = "ns8451.hostgator.com"; to const char* host = "snackrefill.com"; and now it works!

400 Bad Request arduino esp8266

Hi i am trying to test a value form nodemcu board i am using arduino ide to code and have modified the WIFIClient example to send a value to my localhost using GET. it gives me bad request error. any help or suggestion is appreciated
Modified WIFIClinet
#include <ESP8266WiFi.h>
const char* ssid = "ssid";
const char* password = "pwd";
const char* host = "http://10.0.0.39/edu/arduino.php";
void setup() {
Serial.begin(115200);
delay(10);
// We start by connecting to a WiFi network
Serial.println();
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}
int value = 0;
void loop() {
delay(5000);
++value;
Serial.print("connecting to ");
Serial.println(host);
// Use WiFiClient class to create TCP connections
WiFiClient client;
const int httpPort = 80;
if (!client.connect(host, httpPort)) {
Serial.println("connection failed");
return;
}
// We now create a URI for the request
String url ="?v=we";
//
//
Serial.print("Requesting URL: ");
Serial.println(url);
// This will send the request to the server
client.print(String("GET ") + url + " HTTP/1.1\r\n" +
"Host: " + host + "\r\n" +
"Connection: close\r\n\r\n");
//
unsigned long timeout = millis();
while (client.available() == 0) {
if (millis() - timeout > 5000) {
Serial.println(">>> Client Timeout !");
client.stop();
return;
}
}
// Read all the lines of the reply from server and print them to Serial
while(client.available()){
String line = client.readStringUntil('\r');
Serial.print(line);
}
Serial.println();
Serial.println("closing connection");
}
Output from serial monitor
connecting to http://10.0.0.39/edu/arduino.php
Requesting URL: ?v=we
HTTP/1.0 400 Bad Request
Server: httpd
Date: Sat, 01 Jan 2011 22:02:19 GMT
Content-Type: text/html
Connection: close
<HTML><HEAD><TITLE>400 Bad Request</TITLE></HEAD>
<BODY BGCOLOR="#cc9999"><H4>400 Bad Request</H4>
Bad filename.
</BODY></HTML>
I hope this is helpful for people running this on local host i changed
const char* host = "http://10.0.0.39/edu/arduino.php";
to
const char* host = "10.0.0.39";
and
String url ="?v=we";
to
String url ="/edu/arduino.php?v=we";

Resources