Last active
June 5, 2017 21:38
-
-
Save ydbondt/07670aabec36dc96cb95516bed10ae07 to your computer and use it in GitHub Desktop.
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 <NTPClient.h> | |
| #include <ESP8266WiFi.h> | |
| #include <WiFiUdp.h> | |
| #include <ESP8266HTTPClient.h> | |
| #include <ArduinoJson.h> | |
| // Wifi | |
| const char* ssid = "my-ssid"; | |
| const char* password = "xxxxxxx"; | |
| // Sensor | |
| int trigPin = 2; // Trig - D4 | |
| int echoPin = 0; // Echo - D3 | |
| long duration, cm, inches; | |
| WiFiUDP ntpUDP; | |
| NTPClient timeClient(ntpUDP, "any.time.com"); | |
| void setup() { | |
| Serial.begin(115200); | |
| setup_wifi(); | |
| while (WiFi.status() != WL_CONNECTED) { | |
| delay(500); | |
| Serial.print("."); | |
| } | |
| Serial.println("Server started"); | |
| // Print the IP address | |
| Serial.println(WiFi.localIP()); | |
| timeClient.begin(); | |
| Serial.println(timeClient.getFormattedTime()); | |
| setup_sensor(); | |
| } | |
| void loop() { | |
| if (WiFi.status() == WL_CONNECTED) { | |
| timeClient.update(); | |
| long cm = read_sensor(); | |
| HTTPClient http; | |
| http.begin("http://192.168.83.150:9200/logstash-creek/height"); | |
| http.addHeader("Content-Type", "application/json"); | |
| // JSON Builder | |
| StaticJsonBuffer<200> jsonBuffer; | |
| char buff[150]; | |
| JsonObject& root = jsonBuffer.createObject(); | |
| root["height"] = cm; | |
| root["date"] = timeClient.getEpochTime(); | |
| root.printTo(buff, sizeof(buff)); | |
| int http_status = http.POST(buff); | |
| Serial.println(buff); | |
| if (http_status != 201) { | |
| Serial.println(http_status); | |
| } | |
| String returnvalue = http.getString(); | |
| Serial.println(returnvalue); | |
| http.end(); | |
| } | |
| delay(2500); | |
| } | |
| // Sensor functions | |
| long read_sensor() | |
| { | |
| // The sensor is triggered by a HIGH pulse of 10 or more microseconds. | |
| // Give a short LOW pulse beforehand to ensure a clean HIGH pulse: | |
| digitalWrite(trigPin, LOW); | |
| delayMicroseconds(5); | |
| digitalWrite(trigPin, HIGH); | |
| delayMicroseconds(10); | |
| digitalWrite(trigPin, LOW); | |
| // Read the signal from the sensor: a HIGH pulse whose | |
| // duration is the time (in microseconds) from the sending | |
| // of the ping to the reception of its echo off of an object. | |
| pinMode(echoPin, INPUT); | |
| duration = pulseIn(echoPin, HIGH); | |
| // convert the time into a distance | |
| cm = (duration / 2) / 29.1; | |
| Serial.print("Afstand: "); | |
| Serial.print(cm); | |
| Serial.print("cm"); | |
| Serial.println(); | |
| return cm; | |
| } | |
| void setup_sensor() { | |
| //Define inputs and outputs | |
| pinMode(trigPin, OUTPUT); | |
| pinMode(echoPin, INPUT); | |
| } | |
| // Wifi | |
| void setup_wifi() { | |
| delay(10); | |
| // prepare GPIO2 | |
| //pinMode(2, OUTPUT); | |
| //digitalWrite(2, 0); | |
| // Connect to WiFi network | |
| Serial.println(); | |
| Serial.println(); | |
| Serial.print("Connecting to "); | |
| Serial.println(ssid); | |
| WiFi.begin(ssid, password); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment