Created
June 9, 2017 13:08
-
-
Save a-andreyev/c1dcebde309ad541e1f11f7764d181d4 to your computer and use it in GitHub Desktop.
PERCCOM2017
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <ESP8266WiFi.h> | |
#include <WiFiClient.h> | |
#include <ESP8266WebServer.h> | |
#include <ESP8266mDNS.h> | |
#include "ESP8266WiFi.h" | |
#include "WiFiUdp.h" | |
WiFiUDP Udp; | |
unsigned int localUdpPort = 55555; | |
char replyPkt[] = "/"; | |
const char* ssid = "PERCOMM"; | |
const char* password = "strongpass123"; | |
ESP8266WebServer server(55555); | |
const int led = 13; | |
void handleRoot() { | |
digitalWrite(led, 1); | |
server.send(200, "application/ld+json", "{\n" | |
"\"@context\": \"https://raw.githubusercontent.com/semiotproject/perccom-2017/master/apidoc-temperature.json#\",\n" | |
"\"@type\": \"doc:TemperatureValue\",\n" | |
"\"value\": \"14.2\",\n" | |
"\"unitCode\": \"CEL\"\n" | |
"}"); | |
digitalWrite(led, 0); | |
} | |
void handleNotFound(){ | |
digitalWrite(led, 1); | |
String message = "File Not Found\n\n"; | |
message += "URI: "; | |
message += server.uri(); | |
message += "\nMethod: "; | |
message += (server.method() == HTTP_GET)?"GET":"POST"; | |
message += "\nArguments: "; | |
message += server.args(); | |
message += "\n"; | |
for (uint8_t i=0; i<server.args(); i++){ | |
message += " " + server.argName(i) + ": " + server.arg(i) + "\n"; | |
} | |
server.send(404, "text/plain", message); | |
digitalWrite(led, 0); | |
} | |
void setup(void){ | |
pinMode(led, OUTPUT); | |
digitalWrite(led, 0); | |
Serial.begin(115200); | |
WiFi.begin(ssid, password); | |
Serial.println(""); | |
// Wait for connection | |
while (WiFi.status() != WL_CONNECTED) { | |
delay(500); | |
Serial.print("."); | |
} | |
Serial.println(""); | |
Serial.print("Connected to "); | |
Serial.println(ssid); | |
Serial.print("IP address: "); | |
Serial.println(WiFi.localIP()); | |
if (MDNS.begin("esp8266")) { | |
Serial.println("MDNS responder started"); | |
} | |
server.on("/", handleRoot); | |
server.on("/inline", [](){ | |
server.send(200, "text/plain", "this works as well"); | |
}); | |
server.onNotFound(handleNotFound); | |
server.begin(); | |
Serial.println("HTTP server started"); | |
Udp.begin(localUdpPort); | |
Serial.println("UDP started"); | |
} | |
void loop(void){ | |
server.handleClient(); | |
int packetSize = Udp.parsePacket(); | |
if (packetSize) { | |
Udp.beginPacket(Udp.remoteIP(), | |
Udp.remotePort()); | |
Udp.write(replyPkt); | |
Udp.endPacket(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment