Skip to content

Instantly share code, notes, and snippets.

@hinzundcode
Created December 18, 2015 21:42
Show Gist options
  • Save hinzundcode/5f6e23cf0c02652f7d92 to your computer and use it in GitHub Desktop.
Save hinzundcode/5f6e23cf0c02652f7d92 to your computer and use it in GitHub Desktop.
#include <ESP8266WiFi.h>
const char* ssid = "...";
const char* password = "...";
const IPAddress ip(10, 0, 0, 14);
const IPAddress gateway(10, 0, 0, 1);
const IPAddress subnet(255, 255, 0, 0);
const char* host = "10.0.1.1";
const int port = 8889;
void setup() {
Serial.begin(115200);
delay(10);
//WiFi.config(ip, gateway, gateway, subnet);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.print("Connected. IP Address: ");
Serial.println(WiFi.localIP());
Serial.println(WiFi.gatewayIP());
Serial.println(WiFi.subnetMask());
delay(1000);
request("PUT", "/devices/me/attributes", "{\"temperature\":{\"temperature\":4}}");
}
void loop() {
// put your main code here, to run repeatedly:
}
void request(String method, String url, String body) {
WiFiClient client;
if (!client.connect(host, post)) {
Serial.print("connection failed");
return;
}
client.print(method+" "+url+" HTTP/1.1\r\n" +
"Host: "+host+"\r\n"+
"Accept: application/json\r\n"+
"Content-Type: application/json\r\n"+
"Connection: close\r\n\r\n"+body);
delay(10);
while (client.available()) {
String line = client.readStringUntil('\n');
Serial.print(line);
}
Serial.println();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment