Last active
September 4, 2016 18:59
-
-
Save erijpkema/ad507670999e7224a23237003f2e5225 to your computer and use it in GitHub Desktop.
This file contains 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
/* | |
* Sketch for esp8266 | |
* see https://github.com/esp8266/Arduino | |
* used on a wemos D1 | |
* When button 13 is pressed a post request is made. | |
* (Button 13 has variable 14) | |
*/ | |
#include <Arduino.h> | |
#include <ESP8266WiFi.h> | |
#include <ESP8266WiFiMulti.h> | |
#include <ESP8266HTTPClient.h> | |
#define USE_SERIAL Serial | |
ESP8266WiFiMulti WiFiMulti; | |
int pushButton = 14; | |
void setup() { | |
USE_SERIAL.begin(9600); | |
// USE_SERIAL.setDebugOutput(true); | |
USE_SERIAL.println(); | |
USE_SERIAL.println(); | |
USE_SERIAL.println(); | |
for(uint8_t t = 4; t > 0; t--) { | |
USE_SERIAL.printf("[SETUP] WAIT %d...\n", t); | |
USE_SERIAL.flush(); | |
delay(1000); | |
} | |
WiFiMulti.addAP("XXXXX", "XXXX"); | |
// Setup the Pushbutton | |
// make the pushbutton's pin an input: | |
pinMode(pushButton, OUTPUT); // Output first seems to be nessecary on wemos d1. | |
pinMode(pushButton, INPUT); | |
} | |
void post() { | |
// wait for WiFi connection | |
if((WiFiMulti.run() == WL_CONNECTED)) { | |
HTTPClient http; | |
USE_SERIAL.print("[HTTP] begin...\n"); | |
// configure traged server and url | |
//http.begin("https://api.lifx.com/v1/lights/all/effects/pulse"); | |
http.begin("http://192.168.178.26:5000/"); | |
//http.addHeader("Authorization: Bearer", "SOME-STRING"); | |
int httpCode = http.POST(""); | |
USE_SERIAL.print("[HTTP] POST...\n"); | |
// start connection and send HTTP header | |
// httpCode will be negative on error | |
if(httpCode > 0) { | |
// HTTP header has been send and Server response header has been handled | |
USE_SERIAL.printf("[HTTP] GET... code: %d\n", httpCode); | |
// file found at server | |
if(httpCode == HTTP_CODE_OK) { | |
String payload = http.getString(); | |
USE_SERIAL.println(payload); | |
} | |
} else { | |
USE_SERIAL.printf("[HTTP] POST... failed, error: %s\n", http.errorToString(httpCode).c_str()); | |
} | |
http.end(); | |
delay(10000); | |
} | |
} | |
void loop() { | |
// read the input pin: | |
int buttonState = digitalRead(pushButton); | |
// print out the state of the button: | |
Serial.println(buttonState); | |
delay(1); // delay in between reads for stability | |
if(buttonState == 1) { | |
post(); | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment