Last active
October 17, 2019 00:46
-
-
Save govaniso/96cfbe67537b51416a99715a9a270838 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
//#include <Servo.h> | |
#include <ArduinoJson.h> | |
#include <ESP8266WiFi.h> | |
#include <ESP8266HTTPClient.h> | |
#include <WiFiClient.h> | |
// #define CLK D0 | |
#define BTN_BLANCO D7 //blanco | |
#define BTN_AMARILLO D6 | |
#define BTN_ROJO D5 | |
#define LED_VERDE D0 | |
#define LED_AMARILLO D1 | |
#define LED_ROJO D2 | |
const char* ssid = "ThinkPad"; //"TP-LINK_A89012"; | |
const char* password = "tu_password"; //"63004496"; | |
// "jardin/set_xy/5d896075f2a3a7fc14643def" | |
//http://172.21.70.103:8080/kumbiaphpiot/jardin/set_xy/5d896075f2a3a7fc14643def | |
String server = "http://172.21.70.103:3000/dispositivos/5d71996e87f4463f70fa302a/registrar"; | |
//Encoder myEnc(D0, D1); | |
//long oldPosition = -999; | |
void setup() { | |
Serial.begin(9600); | |
pinMode(BTN_BLANCO, INPUT); | |
pinMode(BTN_AMARILLO, INPUT); | |
pinMode(BTN_ROJO, INPUT); | |
pinMode(LED_VERDE, OUTPUT); | |
pinMode(LED_AMARILLO, OUTPUT); | |
pinMode(LED_ROJO, OUTPUT); | |
Serial.println("WiFi connected"); | |
WiFi.mode(WIFI_STA); | |
WiFi.begin(ssid, password); | |
while (WiFi.status() != WL_CONNECTED) { | |
delay(500); | |
} | |
Serial.println(""); | |
Serial.println("WiFi connected"); | |
Serial.println("IP address: "); | |
Serial.println(WiFi.localIP()); | |
} | |
void loop() { | |
if(digitalRead(BTN_BLANCO) == HIGH){ | |
digitalWrite(LED_VERDE, HIGH); | |
post(20); | |
digitalWrite(LED_VERDE, LOW); | |
} | |
if(digitalRead(BTN_AMARILLO) == HIGH){ | |
digitalWrite(LED_AMARILLO, HIGH); | |
post(25); | |
digitalWrite(LED_AMARILLO, LOW); | |
} | |
if(digitalRead(BTN_ROJO) == HIGH){ | |
digitalWrite(LED_ROJO, HIGH); | |
post(35); | |
digitalWrite(LED_ROJO, LOW); | |
} | |
//post("rojo"); | |
delay(5000); | |
post(22); | |
} | |
void post(int temperatura) { | |
HTTPClient http; | |
/* | |
// Serial.println("JOSN = "); | |
//String json = "{\"distancia\":"; | |
//json.concat(distancia); | |
//json.concat("}"); | |
{"distancia":50} | |
*/ | |
String json; | |
StaticJsonBuffer<200> jsonBuffer; | |
JsonObject& root = jsonBuffer.createObject(); | |
root["temp"] = temperatura; | |
root["dispositivo"] = "Cocina"; | |
root.printTo(json); | |
Serial.println(""); // salto de linea para http.writeToStream(&Serial); | |
http.begin(server); | |
http.addHeader("Content-Type", "application/json"); | |
http.POST(json); | |
http.writeToStream(&Serial); | |
http.end(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment