Hacking the Kokonaut Weather Sensor

Along with different sensors (DHT11, DS18B20) each device uses a NodeMCU microcontroller that sends data wirelessly to an API endpoint.

The code below will allow your device to send Temperature and Humidity data to a custom API endpoint.

Prerequisites: Have Arduino IDE and ESP8266 libraries installed

#include <ESP8266WiFi.h>

#include <DHT.h>
#include <math.h>
#define DHTTYPE DHT11
#define DHTPIN 16 // This is the pin number the sensor is attached to

DHT dht(DHTPIN, DHTTYPE);

const char* host = "yourdomain";
const char* ssid = "yourssid";
const char* password = "yourpassword";
String humidity;
String temp;
String id = "";
String urlId = "?id="; // Sensor Id, Temp, and Humidity
String urlTemp = "&tData=";
String urlHumidity = "&hData=";
String url = "/createWeatherData"; // Location to POST data to
String tempUrl = "";
const int httpPort = 80;

unsigned long previousMillis = 0; 
const long interval = 2000;

void setup() {
  Serial.begin(115200);
 
  WiFi.begin(ssid, password);
 
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  delay(1000);
}

void loop() {
  delay(1000);

  WiFiClient client;
    
  if (!client.connect(host, httpPort)) {
    Serial.println("connection failed");
    return;
  }

  getWeatherData(); 

  tempUrl = url;
  tempUrl.concat(urlId);
  tempUrl.concat(id);
  tempUrl.concat(urlTemp);
  tempUrl.concat(temp);
  tempUrl.concat(urlHumidity);
  tempUrl.concat(humidity);

  Serial.print("Requesting URL: ");
  Serial.println(tempUrl);

  // This will send the request to the server
  client.print(String("POST ") + tempUrl + " HTTP/1.1\r\n" +
  "Host: " + host + "\r\n" + 
  "Connection: close\r\n\r\n");

  delay(900000);

  while(client.available()) {
    String line = client.readStringUntil('\r');
    Serial.print(line);
  }

  Serial.println();
  Serial.println("closing connection");

  delay(3000);
}

void getWeatherData() {
  unsigned long currentMillis = millis();

  if(currentMillis - previousMillis >= interval) {
    previousMillis = currentMillis; 

    humidity = round(dht.readHumidity());
    temp = round(dht.readTemperature());
  }
}

For more information on Kokonaut Weather Sensors see

https://www.facebook.com/kokonautweathersensors/

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s