Hi everyone, I am trying to push notification to android phone via Pushetta/PushingBox via Wifi using Arduino UNO and ESP8266-01, by creating TCP connection.
The ESP8266-01 successfully connected to Wifi but was unable to establish connection to Pushetta not PushingBox. I tried to connect to both using their IP address and server name using TCP Test Tool for testing. PushingBox's connection never lasts more than 10 sec, while Pushetta's connection is fine. However, when it comes to using Arduino UNO, failure occurs.
I keep getting failure messages from my serial monitor as follow:
SEND: AT
OK
- connecting to Home Router SID: gloriachu
SEND: AT+CWJAP="gloriachu","0001984085235"
Wifi Loading...Wifi Loading...Wifi Loading...Wifi Loading...OK
- WiFi succesfully connected
Connecting to Pushetta
Connection failed
Connecting to Pushetta
Connection failed
Connecting to Pushetta
Hence, I doubt its a tcp connection issue. I have googled for more examples but all the references seem to be using Wificlient and client.connect(), which is the same one I am using. Hence I would be really grateful is anyone could help on solving this issue... Is it something to do with my code? Or wrong connection method applied?
Thanks!
Arduino Code:
#include <WiFi.h>
#include <WiFiClient.h>
#include <WiFiServer.h>
#include <WiFiUdp.h>
#include <SoftwareSerial.h>
/************
Pushetta_ESP8266/Pushetta_test.ino
https://github.com/Astu04/Pushetta_ESP8266/blob/master/Pushetta_test.ino
*/
#define _baudrate 9600
#define _rxpin 4
#define _txpin 5
SoftwareSerial debug( _rxpin, _txpin ); // RX, TX
//*-- IoT Information
#define SSID "gloriachu"
#define PASS "0001984085235"
// Put here your API key. It's in the dashboard
char APIKEY[] = "84e492d4faf558f7f644f26b389b5d3db0d32d99";
// and here your channel name
char CHANNEL[] = "PushNotification_1";
char serverName[] = "api.pushetta.com";
boolean lastConnected = false;
int status = WL_IDLE_STATUS;
//IPAddress ipMulti (192, 168, 1, 255);
boolean connectWiFi()
{
debug.println("AT+CWMODE=1");
Wifi_connect();
}
void Wifi_connect()
{
Serial.println("- connecting to Home Router SID: " + String(SSID));
String cmd="AT+CWJAP=\"";
cmd+=SSID;
cmd+="\",\"";
cmd+=PASS;
cmd+="\"";
sendDebug(cmd);
Loding("Wifi_connect");
}
void Loding(String state){
for (int timeout=0 ; timeout<10 ; timeout++)
{
if(debug.find("OK"))
{
Serial.println("OK");
break;
}
else if(timeout==9){
Serial.print( state );
Serial.println(" fail...\nExit2");
}
else
{
Serial.print("Wifi Loading...");
delay(500);
}
}
}
void sendDebug(String cmd)
{
Serial.print("SEND: ");
Serial.println(cmd);
debug.println(cmd);
}
//Function for sending the request to Pushetta
void sendToPushetta(char channel[], String text) {
Serial.println("- starting client");
WiFiClient client;
Serial.println("- connecting to server: " + String(serverName));
if (client.connect(serverName, 80))
{
Serial.println("- server succesfully connected");
client.print("POST /api/pushes/");
client.print(channel);
client.println("/ HTTP/1.1");
client.print("Host: ");
client.println(serverName);
client.print("Authorization: Token ");
client.println(APIKEY);
client.println("Content-Type: application/json");
client.print("Content-Length: ");
client.println(text.length() + 46);
client.println();
client.print("{ \"body\" : \"");
client.print(text);
client.println("\", \"message_type\" : \"text/plain\" }");
client.println();
}
else
{
client.stop();
Serial.println("- stopping the client");
}
}
void setup() {
Serial.begin(_baudrate);
debug.begin( _baudrate );
sendDebug("AT");
Loding("sent AT");
connectWiFi();
Serial.println("- WiFi succesfully connected");
}
void loop()
{
Serial.println("Connecting to Pushetta");
// Use WiFiClient class to create TCP connections
WiFiClient client;
const int httpPort = 80;
if (!client.connect("api.pushetta.com", httpPort)) {
Serial.println("Connection failed");
return;
}
// Sending a notification to your mobile phone
// function takes the message as a parameter
sendToPushetta(CHANNEL, "Hello from Arduino!");
delay(60000);
}