91 lines
2.5 KiB
Arduino
91 lines
2.5 KiB
Arduino
|
#include <Arduino.h>
|
||
|
#include <ArduinoJson.h>
|
||
|
|
||
|
#include <ESP8266WiFi.h>
|
||
|
#include <ESP8266WiFiMulti.h>
|
||
|
|
||
|
#include <ESP8266HTTPClient.h>
|
||
|
|
||
|
#define USE_SERIAL Serial
|
||
|
|
||
|
ESP8266WiFiMulti WiFiMulti;
|
||
|
char macAddr[18];
|
||
|
|
||
|
// Generated with: https://arduinojson.org/assistant/
|
||
|
const size_t bufferSize = JSON_OBJECT_SIZE(3) + 50;
|
||
|
DynamicJsonBuffer jsonBuffer(bufferSize);
|
||
|
|
||
|
void setup() {
|
||
|
|
||
|
pinMode(D2, OUTPUT);
|
||
|
digitalWrite(D2, HIGH);
|
||
|
|
||
|
USE_SERIAL.begin(115200);
|
||
|
// 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);
|
||
|
}
|
||
|
|
||
|
WiFi.mode(WIFI_STA);
|
||
|
WiFiMulti.addAP("Protospace", "yycmakers");
|
||
|
|
||
|
byte ar[6];
|
||
|
WiFi.macAddress(ar);
|
||
|
sprintf(macAddr, "%02X%02X%02X%02X%02X%02X", ar[0], ar[1], ar[2], ar[3], ar[4], ar[5]);
|
||
|
USE_SERIAL.println(macAddr);
|
||
|
|
||
|
}
|
||
|
|
||
|
void loop() {
|
||
|
// 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://192.168.1.12/test.html", "7a 9c f4 db 40 d3 62 5a 6e 21 bc 5c cc 66 c8 3e a1 45 59 38"); //HTTPS
|
||
|
http.begin(String("http://172.17.32.164:3000/api/tool/") + macAddr); //HTTP
|
||
|
|
||
|
USE_SERIAL.print("[HTTP] GET...\n");
|
||
|
// start connection and send HTTP header
|
||
|
int httpCode = http.GET();
|
||
|
|
||
|
// 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);
|
||
|
JsonObject& root = jsonBuffer.parseObject(payload);
|
||
|
|
||
|
bool relayOn = root["relayOn"]; // true
|
||
|
bool ledOn = root["ledOn"]; // true
|
||
|
const char* date = root["date"]; // "2018-02-01"
|
||
|
|
||
|
USE_SERIAL.println(String("relayOn: ") + relayOn + String(" ledOn: ") + ledOn + String(" Date: ") + date);
|
||
|
|
||
|
digitalWrite(D2, !relayOn);
|
||
|
|
||
|
}
|
||
|
} else {
|
||
|
USE_SERIAL.printf("[HTTP] GET... failed, error: %s\n", http.errorToString(httpCode).c_str());
|
||
|
}
|
||
|
|
||
|
http.end();
|
||
|
}
|
||
|
|
||
|
delay(10000);
|
||
|
}
|
||
|
|