How to do SOAP Request

@Vognsen Just found this… may save you alot of work… https://github.com/phhe/sparkSo

Here is another i just quickly ported

#include "application.h"

byte sonosip[] = { 192, 168, 1, 8 };
 
int debug = 1;
 
#define SONOS_PAUSE "<s:Envelope xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\" s:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\"><s:Body><u:Pause xmlns:u=\"urn:schemas-upnp-org:service:AVTransport:1\"><InstanceID>0</InstanceID></u:Pause></s:Body></s:Envelope>"
#define SONOS_PLAY  "<s:Envelope xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\" s:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\"><s:Body><u:Play xmlns:u=\"urn:schemas-upnp-org:service:AVTransport:1\"><InstanceID>0</InstanceID><Speed>1</Speed></u:Play></s:Body></s:Envelope>"
 
#define PLAY 1
#define PAUSE 0
 
const int buttonPin = D6;     // the number of the pushbutton pin
const int ledPin =  D7;      // the number of the LED pin
 
int state = LOW;      // the current state of the output pin
int reading;           // the current reading from the input pin
int previous = LOW;    // the previous reading from the input pin
 
long last = 0;         // the last time the output pin was toggled
long debounce = 200;   // the debounce time, increase if the output flickers
 
TCPClient client;
 
void setup()
{
  pinMode(buttonPin, INPUT_PULLUP);
  pinMode(ledPin, OUTPUT);
  if (debug) {
    Serial.begin(9600);
    while(!Serial.available()); // wait here for user to press ENTER in Serial Terminal
  }
}
 
void loop()
{
 
  reading = digitalRead(buttonPin);
 
  // if the input just went from LOW and HIGH and we've waited long enough
  // to ignore any noise on the circuit, toggle the output pin and remember
  // the time
  if (reading == HIGH && previous == LOW && millis() - last > debounce) {
    if (state == HIGH) {
      state = LOW;
      if (debug) {
        Serial.println("play");
      }
      sonos(PLAY);
    }
    else {
      state = HIGH;
      if (debug) {
        Serial.println("pause");
      }
      sonos(PAUSE);
    }
 
    last = millis();
  }
 
  digitalWrite(ledPin, state);
 
  previous = reading;
}
 
void out(const char *s)
{
  client.println(s);
  if (debug) {
    Serial.println(s);
  }
}
 
void sonos(int cmd)
{
  char buf[512];
 
  if (client.connect(sonosip, 1400)) {
    if (debug) {
      Serial.println("connected");
    }
 
    out("POST /MediaRenderer/AVTransport/Control HTTP/1.1");
    out("Connection: close");
    sprintf(buf, "Host: %d.%d.%d.%d:1400", sonosip[0], sonosip[1], sonosip[2], sonosip[3]);
    out(buf);
 
    sprintf(buf, "Content-Length: %d", (cmd == PLAY) ? strlen(SONOS_PLAY) : strlen(SONOS_PAUSE));
    out(buf);
 
    out("Content-Type: text/xml; charset=\"utf-8\"");
 
    sprintf(buf, "Soapaction: \"urn:schemas-upnp-org:service:AVTransport:1#%s\"", (cmd == PLAY) ? "Play" : "Pause");
    out(buf);
 
    out("");
    strcpy(buf, (cmd == PLAY) ? SONOS_PLAY : SONOS_PAUSE);
    out(buf);
    
    unsigned int count = 0;
    unsigned long lastTime = millis();
    while( client.available()==0 && millis()-lastTime<2000) { //2 second timeout for reply
    }  //do nothing
    lastTime = millis();
    while( client.available() && millis()-lastTime<500 ) {  //500 milliseconds timeout after last data
      char c = client.read();
      if (debug) {
        Serial.print(c);
      }
      lastTime = millis();
      count++;
    }
    client.flush();  //for safety

    //client.flush();
    delay(400);
    Serial.println();
    Serial.print("Done, Total bytes returned: ");
    Serial.println(count);
    
  } else {
    if (debug) {
      Serial.println("connection failed");
    }
  }
  client.stop();
}
3 Likes