0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

wemosでalexaデバイス その5

Posted at

概要

wemos d1で、alexaデバイスを作ってみる。
練習問題、やってみた。

練習問題

upnpのm-searchを実装せよ。

サンプルコード

#include <ESP8266WiFi.h>
#include <WiFiUDP.h>

const char * ssid = "****";
const char * password = "****";
static WiFiUDP udp;

void setup() {
	Serial.begin(115200);
	delay(100);
	boolean state = true;
	int i = 0;
	WiFi.mode(WIFI_STA);
	WiFi.begin(ssid, password);
	Serial.println("");
	Serial.println("Connecting to WiFi");
	Serial.print("Connecting ...");
	while (WiFi.status() != WL_CONNECTED)
	{
		delay(500);
		Serial.print(".");
		if (i > 10)
		{
			state = false;
			break;
		}
		i++;
	}
	if (state)
	{
		Serial.println("");
		Serial.print("Connected to ");
		Serial.println(ssid);
		Serial.print("IP address: ");
		Serial.println(WiFi.localIP());
	}
	else
	{
		Serial.println("");
		Serial.println("Connection failed.");
	}
	udp.begin(1900);
	Serial.println("Sending M-SEARCH multicast");
	udp.beginPacketMulticast(IPAddress(239, 255, 255, 250), 1900, WiFi.localIP());
	udp.write("M-SEARCH * HTTP/1.1\r\n"
		"HOST: 239.255.255.250:1900\r\n"
		"MX: 5\r\n"
		"MAN: \"ssdp:discover\"\r\n"
		"ST: upnp:rootdevice\r\n\r\n\r\n");
	udp.endPacket();
}
void loop() {
	IPAddress sonosIP;
	unsigned long lastSearch = millis();
	while ((millis() - lastSearch) < 5000)
	{
		int packetSize = udp.parsePacket();
		if (packetSize)
		{
			char packetBuffer[255];
			Serial.print("Received packet of size ");
			Serial.println(packetSize);
			Serial.print("From ");
			sonosIP = udp.remoteIP();
			Serial.print(sonosIP);
			Serial.print(", port ");
			Serial.println(udp.remotePort());
			int len = udp.read(packetBuffer, 255);
			if (len > 0)
			{
				packetBuffer[len] = 0;
			}
			Serial.println("Contents:");
			Serial.println(packetBuffer);
		}
		delay(50);
		Serial.print(".");
	}
	Serial.println("ok");
}







以上。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?