 /*
-SenseBox Citizen Sensingplatform
+ * Jan Wirwahn, Institute for Geoinformatics, Uni Muenster
+ * July 2015 
+ * Arduino Web-Client for pushing SenseBox:Home measurements 
+ * to the OpenSenseMap server. 
+ */ 
 
-Sensor Connections on Base-Shield
- A0: UV-Sensor
- A1: Temperature and Humidity Sensor Pro
-I2C: Pressure Sensor
-I2C: Digital Light Sensor
-
-Version: 1.3.1
-Date: 2015-02-18
-Homepage: http://www.sensebox.de
-Author: Jan Wirwahn, Institute for Geoinformatics, University of Muenster
-Note: Sketch for SB-Photonik-Ethernet
-MAC-address and IP has to be changed according to your setup.
-Disable Serial.print functions for better performance on your stand-alone sensor setup. 
-*/
-
+#include <BMP280.h>
 #include <Wire.h>
+#include <HDC100X.h>
 #include <SPI.h>
 #include <Ethernet.h>
-#include <DHT.h>
-#include <Barometer.h>
-#include <Digital_Light_TSL2561.h>
-#include <avr/wdt.h>
+#include <Makerblog_TSL45315.h>
 
+#define UV_ADDR 0x38
+#define IT_1   0x1
+
+//INDIVIDUAL SETUP
+IPAddress ip(192, 168, 179, 194); //IP Adress
+IPAddress dn(192, 168, 179, 10);  //DNS Server
+IPAddress gw(192, 168, 179, 254); //Gateway
+byte mac[] = { 0x90, 0xA2, 0xDA, 0x0D, 0x86, 0x46 }; //MAC Adress
+
+char server[] = "opensensemap.org";
+EthernetClient client;
+
+//IDs are being generated by the OSM server
+//If you want to update your old sketch, insert the IDs from your old file
 //SenseBox ID
 
 //Sensor IDs
 
-//Sensor pin settings
-#define UVPIN A0
-#define DHTPIN A1
-#define DHTTYPE DHT22
 
-//INDIVIDUAL SETUP
-//Change MAC-adress here:
-byte mac[] = { 0x90, 0xA2, 0xDA, 0x0D, 0x86, 0x46 }; 
+BMP280 bmp;
+HDC100X HDC(0x43);
+//Makerblog_TSL45315 luxsensor = Makerblog_TSL45315(TSL45315_TIME_M4);
 
-//Specify an IP according to your network settings:
-IPAddress ip(192,168,1,111);
-//The following IDs were generated by the OSM-server. Change them to your IDs:
+//measurement variables
+float temperature = 0;
+float humidity = 0;
+double tempBaro, pressure;
+uint32_t lux;
+int messTyp;
+byte msb=0, lsb=0;
+uint16_t uv;
+float uvIndex;
 
-char server[] = "opensensemap.org";
-EthernetClient client; 
-
-String currentSensorId = TEMPERATURESENSOR_ID;
-float temperature, humidity, pressure;
-unsigned long  lux;
-unsigned int analogUv;
-
-DHT dht(DHTPIN, DHTTYPE);
-Barometer barometer;
-
-//Variables for time sync and upload intervall
-int postInterval = 100000;
-boolean lastConnected = false;
-String sensorSample;
-int sampleType = 1; //begin with temperature
-int phenomCount = 5; //1=temp,2=humi,3=pressure,4=lux,5=UV
-
-//post sample each 10 minutes
-//deprecated 
+//post sample each minute
+int postInterval = 60000; 
 long timeOld = 0;
 long timeNew = 0;
 
+
 void setup()
 {
+ Serial.begin(9600); 
+//Try DHCP first 
+ 
+  Ethernet.begin(mac, ip, dn, gw); 
+  HDC.begin(HDC100X_TEMP_HUMI,HDC100X_14BIT,HDC100X_14BIT,DISABLE);
+  //Serial.println(luxsensor.begin());
+  if(!bmp.begin()){
+    Serial.println("BMP init failed!");
+    while(1);
+  }
+  else Serial.println("BMP init success!");
+  bmp.setOversampling(4);
   Wire.begin();
-  Serial.begin(9600); 
-//Try DHCP first 
-  if (Ethernet.begin(mac) == 0) 
-  {
-    Serial.println("Failed to configure Ethernet using DHCP"); 
-    Ethernet.begin(mac, ip); 
-  } 
-  delay(1000);
-  Serial.println("STARTING UP");
-  barometer.init();
-  dht.begin();
-  TSL2561.init();
+  Wire.beginTransmission(UV_ADDR);
+  Wire.write((IT_1<<2) | 0x02);
+  Wire.endTransmission();
+  delay(500);
 }
 
 void loop()
 { 
-
-  // if there's no net connection, but there was one last time
-  // through the loop, then stop the client:
-  if (!client.connected() && lastConnected) {
-    Serial.println();
-    Serial.println("disconnecting.");
-    client.stop();
-  }
-  
   timeNew = millis(); 
-  if (!client.connected() && (timeNew - timeOld > postInterval))
+  if (timeNew - timeOld > postInterval)
   { 
-    Serial.println();
-    delay(1000);
-    sensorSample = "";
-    switch (sampleType)
-    {
-      case 1:
-        //temperature = dht.readTemperature();
-        temperature = barometer.bmp085GetTemperature(barometer.bmp085ReadUT());
-        sensorSample = floatToString(temperature,0);
-        currentSensorId = TEMPERATURESENSOR_ID;
-        break;
-      case 2:
-        humidity = dht.readHumidity();
-        sensorSample = floatToString(humidity,0);
-        currentSensorId = HUMIDITYSENSOR_ID;//bmp085ReadUT MUST be called first
-        break;
-      case 3:
-        pressure = barometer.bmp085GetTemperature(barometer.bmp085ReadUT());
-        pressure = barometer.bmp085GetPressure(barometer.bmp085ReadUP());
-        sensorSample = floatToString(pressure,0);
-        currentSensorId = PRESSURESENSOR_ID;
-        break;
-      case 4:
-        TSL2561.getLux();
-        lux = TSL2561.calculateLux(0,0,1);
-        sensorSample = (String)lux;
-        currentSensorId = LUXSENSOR_ID;
-        break;
-      case 5:
-        analogUv = analogRead(UVPIN);
-        sensorSample = (String)calcUvIndex(analogUv);
-        currentSensorId = UVSENSOR_ID;
-        break;
-    }
-    postObservation(sensorSample, currentSensorId, SENSEBOX_ID); 
+    //-----Pressure-----//
+    messTyp = 1;
+    char result = bmp.startMeasurment();
+    if(result!=0){
+      delay(result);
+      result = bmp.getTemperatureAndPressure(tempBaro,pressure);
+      pressure = pressure * 100;
+      postObservation(pressure, PRESSURESENSOR_ID, SENSEBOX_ID); 
+    } 
+    //-----Humidity-----//
+    messTyp = 2;
+    humidity = HDC.getHumi();
+    postObservation(humidity, HUMIDITYSENSOR_ID, SENSEBOX_ID); 
+    //-----Temperature-----//
+    messTyp = 2;
+    temperature = HDC.getTemp();
+    postObservation(temperature, TEMPERATURESENSOR_ID, SENSEBOX_ID);
+    //-----Lux-----//
+    messTyp = 1;
+    //lux = luxsensor.readLux();
+    //Serial.print("Illumi = "); Serial.println(lux);
+    //postObservation(lux, LUXSENSOR_ID, SENSEBOX_ID);
+    //-----UV-Index-----//
+    messTyp = 2;
+    Wire.requestFrom(UV_ADDR+1, 1); //MSB
+    delay(1);
+    if(Wire.available())
+      msb = Wire.read();
+    Wire.requestFrom(UV_ADDR+0, 1); //LSB
+    delay(1);
+    if(Wire.available())
+      lsb = Wire.read();
+    uv = (msb<<8) | lsb;
+    uvIndex = uv * 5.625;
+    
+    postObservation(uvIndex, UVSENSOR_ID, SENSEBOX_ID); 
+    
     timeOld = millis(); 
   }
-  lastConnected = client.connected();
 }
 
-void postObservation(String measurement, String sensorId, String boxId)
+void postObservation(float measurement, String sensorId, String boxId)
 { 
+  char obs[10]; 
+  if (messTyp == 1) dtostrf(measurement, 5, 0, obs);
+  else if (messTyp == 2) dtostrf(measurement, 5, 2, obs);
+  Serial.println(obs); 
   //json must look like: {"value":"12.5"} 
-  Serial.print("Preparing data: "); 
-  String jsonData = "{\"value\":"; 
-  jsonData += measurement; 
-  jsonData += "}"; 
-  Serial.println(jsonData);
+  //post observation to: http://opensensemap.org:8000/boxes/boxId/sensorId
+  Serial.println("connecting..."); 
+  String value = "{\"value\":"; 
+  value += obs; 
+  value += "}"; 
   // if you get a connection, report back via serial: 
   boxId += "/"; 
-  //post observation to: http://opensensemap.org:8000/boxes/boxId/sensorId
-  Serial.print("Connecting to OSM server...");
   if (client.connect(server, 8000)) 
   {
     Serial.println("connected"); 

  }
  while (repeat);
}

String floatToString(float number, int precision)
{
  String stringNumber = "";
  //int prec;
  //only temperature (case 1) has a decimal place
  //if (sampleType == 1) prec = 1; else prec = 0;
  char tempChar[10];
  dtostrf(number, 3, precision, tempChar);
  stringNumber += tempChar;
  return stringNumber;
}

int calcUvIndex(int analogVal){
 // get voltage from analog value
 float uvindex = map(analogVal, 0, 1023, 0, 5000);
 uvindex /= 1000;
 //illumination intensity => V_sig * 307
 uvindex = uvindex * 307;
 //UVI => illumination intesity / 200
 uvindex = uvindex / 200;
 
 return int(uvindex+0.5);
}
  // if there are incoming bytes available 
  // from the server, read them and print them: 
  boolean repeat = true; 
  do{ 
    if (client.available()) 
    { 
      char c = client.read();
