Created
September 16, 2018 12:45
-
-
Save timtheguy/69715386c0235d57b2a95a6add8e52a6 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 <ESP8266HTTPClient.h> | |
#include <ESP8266WiFi.h> | |
#include <ArduinoJson.h> | |
#include <Adafruit_MPL3115A2.h> | |
Adafruit_MPL3115A2 baro = Adafruit_MPL3115A2(); | |
const int pinAdc = A0; | |
void setup() { | |
Serial.begin(115200); //Serial connection | |
WiFi.begin("Test_001", "password"); //WiFi connection | |
while (WiFi.status() != WL_CONNECTED) { //Wait for the WiFI connection completion | |
delay(500); | |
Serial.println("Waiting for connection"); | |
} | |
} | |
void loop() { | |
if (WiFi.status() == WL_CONNECTED) { //Check WiFi connection status | |
StaticJsonBuffer<300> JSONbuffer; //Declaring static JSON buffer | |
JsonObject& JSONencoder = JSONbuffer.createObject(); | |
JSONencoder["homeid"] = "1111"; | |
if (! baro.begin()) { // Handle barometric sensor first | |
Serial.println("Couldnt find sensor"); | |
return; | |
} | |
// * GET AUDIO VALUES * | |
long sum = 0; // Values read by mic | |
for(int i=0; i<32; i++) { | |
sum += analogRead(pinAdc); | |
} | |
sum >>= 5; // Bitwise operation | |
// * GET PRESSURE VALUES * | |
float pascals = baro.getPressure(); | |
Serial.print(pascals); | |
Serial.println(" Pascals"); | |
// * GET TEMPERATURE VALUES * | |
float tempC = baro.getTemperature(); | |
Serial.print(tempC); | |
Serial.println("*C"); | |
// * PREPARE DATA FOR EXPORT * | |
JsonArray& active = JSONencoder.createNestedArray("active"); //JSON array | |
active.add("1"); | |
JsonArray& temperature = JSONencoder.createNestedArray("temp"); //JSON array | |
temperature.add(tempC); | |
JsonArray& pressure = JSONencoder.createNestedArray("pressure"); | |
pressure.add(pascals); | |
JsonArray& decibels = JSONencoder.createNestedArray("decibels"); | |
decibels.add(sum); | |
char JSONmessageBuffer[300]; | |
JSONencoder.prettyPrintTo(JSONmessageBuffer, sizeof(JSONmessageBuffer)); | |
Serial.println(JSONmessageBuffer); | |
HTTPClient http; //Declare object of class HTTPClient | |
http.begin("http://hurrihome.herokuapp.com/home/update"); //Specify request destination | |
http.addHeader("Content-Type", "application/json"); //Specify content-type header | |
int httpCode = http.POST(JSONmessageBuffer); //Send the request | |
String payload = http.getString(); //Get the response payload | |
Serial.println(httpCode); //Print HTTP return code | |
Serial.println(payload); //Print request response payload | |
http.end(); //Close connection | |
} else { | |
Serial.println("Error in WiFi connection"); | |
} | |
delay(5000); //Send a request every 5 seconds | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment