Post topics, source code that relate to the Arduino Platform

User avatar
By DKRT86
#42706 Helle everyone,

right now I am trying to send a push message to my windows phone.

I am using the pushalot (http://www.pushalot.com) service to send messages to my smartphone.
You can send a simple POST request to the homepage and it forwards your information to your smartphone. The necessary information included in the request is shown below (also described here http://www.pushalot.com/api):
AuthorizationToken: XXXXXXXXXXXXXXXXXXXXXX
Body: "this is an example message"

I wrote a really simple html-file with the following code:
Code: Select all<html>
<body>
 <FORM action="https://pushalot.com/api/sendmessage" method="POST">
    <P>
    <LABEL for="firstname">Data To Send:</LABEL>
    <INPUT type="text" name="AuthorizationToken" value="XXXXXXXXXXXXXXXXXXXXXX" readonly><BR>
    <INPUT type="text" name="Body"><BR>
    <INPUT type="submit" value="Submit">
    </P>
 </FORM>
</body>
</html>


This works perfectly. With the ESP8266 I wasn't successful until now. Here is the Code:

Code: Select all#include <ESP8266WiFi.h>
#include <WiFiClient.h>

// data Accesspoint
const char* ssid = "ESP";
const char* password = "";

// remote site information
const char http_site[] = "pushalot.com";
const int http_port = 80;

// network
char ssids[] = "SSID";     // ssid
char passwords[17] = "Password"; // pass

IPAddress ip(192,168,178,38);
IPAddress gateway(192,168,178,1);
IPAddress subnet(255,255,255,0);

// global variables
WiFiClient client;

void WiFiconnect() {
  Serial.begin(9600);

  Serial.println();
  Serial.println("Starting WiFi...");

  WiFi.mode(WIFI_STA); //STA
  WiFi.begin(ssids, passwords);

  if (WiFi.waitForConnectResult() == WL_CONNECTED) {
    WiFi.config(ip,gateway,subnet);
    Serial.println("WiFi successfully connected...");
    Serial.print("Connected to ");
    Serial.println(ssids);
    Serial.print("IP address: ");
    Serial.println(WiFi.localIP());
  }
  else {
   // WiFi Setup
  Serial.println("Stationmode failed!");
  Serial.println("Starting Accesspoint...");
  WiFi.mode(WIFI_AP);
  WiFi.softAP(ssid,password);
  WiFi.softAPConfig(ip, gateway, subnet);
 
 // establish connection
  Serial.printf("accesspoint with the name ");
  Serial.println(ssid);
  Serial.print("IP-Address: ");
  Serial.println(WiFi.softAPIP());
  Serial.print("Mac-Address: "); // show mac address
  Serial.println(WiFi.softAPmacAddress());
  }
}

bool getPage() {
 
  // connect with remote server
  if ( !client.connect(http_site, http_port) ) {
    return false;
  }
 
  // HTTP POST request
  client.println("POST /api/sendmessage HTTP/1.0");
  client.print("Host: ");
  client.println(http_site);
  client.print("Content-Type: ");
  client.println("application/x-www-form-urlencoded");
  client.print("AuthorizationToken: ");
  client.println("XXXXXXXXXXXXXXXXXXXXXX");
  client.print("Body: ");
  client.println("this is an example message");
  client.println("Connection: close");
  client.println();
 
  return true;
}

void setup()
{
WiFiconnect();

if ( !getPage() ) {
    Serial.println("GET request failed");
    }
    else {
    Serial.println("GET request successful");
    }
}
void loop()
{
}


It seems that I can connect to the website but the post request doesn't work. Can someone of you help me out and tell me what is wrong with my code.

Thank you very much in advance for every hint.

Greetings Daniel
User avatar
By torntrousers
#42728 I don't think your HTTP message has the correct format. The "this is an example message" bit should be sent after the headers separated by CR LF and doesn't need the "Body: " prefix, also there should be a Content-Length header.

Its much easier to use HTTPClient. An example of doing a POST with HTTPClient is here.
User avatar
By Kalix
#54368 Just in case you might still want to know. I managed to get it working with the code below.
Trick is to us HTTPClient and begin is with https and the fingerprint of the ssl certificate used on the website.

Code: Select all  HTTPClient http;
  http.begin("https://pushalot.com/api/sendmessage", "3f 07 e9 45 86 b8 7c 85 76 0e 18 d0 67 a6 08 00 e9 54 7a da");
  http.addHeader("Content-Type", "application/json");
  String postMessage = String("{'AuthorizationToken' : 'yourapistring here', 'Title' : 'Test', 'Body' : 'Test'}");
  int httpCode = http.POST(postMessage);
  Serial.print("http result:");
  Serial.println(httpCode);
  http.writeToStream(&Serial);
  http.end();