Created
November 14, 2019 12:27
-
-
Save mariopesch/3d4b327280b484efc082cc4a7a7d88ee 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
/* | |
SenseBox with Wemos D1 R2 and Wifi Support | |
Version: 2.2 | |
Date: 2016-06-20 | |
Homepage: http://www.sensebox.de | |
Author: Mario Pesch, Institute for Geoinformatics, University of Muenster | |
Email: support@sensebox.de | |
note: enter your wifi settings in line OTA Update is acitvated by default | |
*/ | |
//Change the libraries according to your sensor Setup | |
#include <Wire.h> | |
#include "HDC100X.h" //might be removed | |
#include "Adafruit_BMP280.h" //might be removed | |
#include "Adafruit_Sensor.h" //might be removed | |
#include <Makerblog_TSL45315.h> //might be removed | |
#include <SPI.h> | |
#include <ESP8266WiFi.h> | |
#include <ESP8266mDNS.h> | |
#include "SDS011.h" //might be revmoved | |
//Copy your senseBox ID's | |
#define SENSEBOX_ID "" | |
//Sensor IDs | |
#define TEMPSENSOR_ID "" | |
#define HUMISENSOR_ID "" | |
#define PRESSURESENSOR_ID "" | |
#define LUXSENSOR_ID "" | |
#define UVSENSOR_ID "" | |
#define WIFI_ID "" | |
#define p25_ID "" | |
#define p10_ID "" | |
//Configure ethernet connection | |
const char* ssid = "dd-"; | |
const char* password = ""; | |
char server[] = "ingress.opensensemap.org"; | |
WiFiClient client; | |
SDS011 my_sds; | |
//Load sensors | |
Makerblog_TSL45315 TSL = Makerblog_TSL45315(TSL45315_TIME_M4); | |
HDC100X HDC(0x43); | |
Adafruit_BMP280 BMP; | |
//measurement variables | |
float temperature = 0; | |
float humidity = 0; | |
float wifi = 0; | |
float p10,p25; | |
int error; | |
double tempBaro, pressure; | |
uint32_t lux; | |
uint16_t uv; | |
int messTyp; | |
#define UV_ADDR 0x38 | |
#define IT_1 0x1 | |
unsigned long oldTime = 0; | |
const unsigned long postingInterval = 600000; | |
void setup() { | |
// Open serial communications and wait for port to open: | |
Serial.begin(115200); | |
// start the Ethernet connection: | |
Serial.println("SenseBox Home software version 2.1"); | |
Serial.println(); | |
Serial.print("Starting ethernet connection..."); | |
WiFi.begin(ssid, password); | |
delay(1000); | |
//Initialize sensors | |
Serial.print("Initializing sensors..."); | |
Wire.begin(); | |
Wire.beginTransmission(UV_ADDR); | |
Wire.write((IT_1<<2) | 0x02); | |
Wire.endTransmission(); | |
delay(500); | |
my_sds.begin(D6,D7); | |
HDC.begin(HDC100X_TEMP_HUMI,HDC100X_14BIT,HDC100X_14BIT,DISABLE); | |
TSL.begin(); | |
BMP.begin(); | |
Serial.println("done!"); | |
Serial.println("Starting loop."); | |
} | |
void loop() | |
{ | |
// if there are incoming bytes available | |
// from the server, read them and print them: | |
if (client.available()) { | |
char c = client.read(); | |
Serial.print(c); | |
//Serial.write(c); | |
} | |
if (millis() - oldTime > postingInterval) { | |
oldTime = millis(); | |
//-----Pressure-----// | |
Serial.println("Posting pressure"); | |
messTyp = 2; | |
pressure = BMP.readPressure(); | |
pressure = pressure/100; | |
postObservation(pressure, PRESSURESENSOR_ID, SENSEBOX_ID); | |
delay(2000); | |
//-----Humidity-----// | |
Serial.println("Posting humidity"); | |
messTyp = 2; | |
humidity = HDC.getHumi(); | |
//Serial.print("Humidity = "); Serial.println(humidity); | |
postObservation(humidity, HUMISENSOR_ID, SENSEBOX_ID); | |
delay(2000); | |
//-----Temperature-----// | |
Serial.println("Posting temperature"); | |
messTyp = 2; | |
temperature = HDC.getTemp(); | |
//Serial.println(temperature,2); | |
//Serial.print("Temperature = ");Serial.println(temperature); | |
postObservation(temperature, TEMPSENSOR_ID, SENSEBOX_ID); | |
delay(2000); | |
//-----Lux-----// | |
Serial.println("Posting illuminance"); | |
messTyp = 1; | |
lux = TSL.readLux(); | |
//Serial.print("Illumi = "); Serial.println(lux); | |
postObservation(lux, LUXSENSOR_ID, SENSEBOX_ID); | |
delay(2000); | |
//UV intensity | |
messTyp = 1; | |
uv = getUV(); | |
postObservation(uv, UVSENSOR_ID, SENSEBOX_ID); | |
//-----WiFi-----// | |
messTyp = 2; | |
wifi = WiFi.RSSI(); | |
postObservation (wifi, WIFI_ID, SENSEBOX_ID); | |
//-----p10----// | |
messTyp = 2; | |
my_sds.wakeup(); | |
delay(30000); | |
error = my_sds.read(&p25,&p10); | |
if (!error) { | |
postObservation(p10, p10_ID, SENSEBOX_ID); | |
postObservation(p25, p25_ID, SENSEBOX_ID); | |
my_sds.sleep(); | |
} | |
} | |
} | |
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"} | |
//post observation to: http://opensensemap.org:8000/boxes/boxId/sensorId | |
Serial.println("connecting..."); | |
String value = "{\"value\":"; | |
value += obs; | |
value += "}"; | |
if (client.connect(server, 8000)) | |
{ | |
Serial.println("connected"); | |
// Make a HTTP Post request: | |
client.print("POST /boxes/"); | |
client.print(boxId); | |
client.print("/"); | |
client.print(sensorId); | |
client.println(" HTTP/1.1"); | |
// Send the required header parameters | |
client.println("Host:opensensemap.org"); | |
client.println("Content-Type: application/json"); | |
client.println("Connection: close"); | |
client.print("Content-Length: "); | |
client.println(value.length()); | |
client.println(); | |
// Send the data | |
client.print(value); | |
client.println(); | |
} | |
waitForResponse(); | |
} | |
void waitForResponse() | |
{ | |
// 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(); | |
Serial.print(c); | |
} | |
// if the servers disconnected, stop the client: | |
if (!client.connected()) | |
{ | |
Serial.println(); | |
Serial.println("disconnecting."); | |
client.stop(); | |
repeat = false; | |
} | |
} | |
while (repeat); | |
} | |
uint16_t getUV(){ | |
byte msb=0, lsb=0; | |
uint16_t uvValue; | |
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(); | |
uvValue = (msb<<8) | lsb; | |
return uvValue*5; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment