Skip to content

Commit

Permalink
added connect()
Browse files Browse the repository at this point in the history
  • Loading branch information
justind000 committed Nov 11, 2019
1 parent bfff68f commit ed83782
Show file tree
Hide file tree
Showing 10 changed files with 211 additions and 5 deletions.
6 changes: 4 additions & 2 deletions examples/Minimal/Minimal.ino
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ void setup()

void loop()
{
Serial.println((String)sht20.temperature() + "°");
Serial.println((String)sht20.temperature() + "°C");
Serial.println((String)sht20.temperature_f() + "°F");
Serial.println((String)sht20.humidity() + " RH%");
delay(1000);
Serial.println();
delay(5000);
}
2 changes: 1 addition & 1 deletion library.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"maintainer": true
}
],
"version": "1.0.0",
"version": "1.0.2",
"frameworks": "arduino",
"platforms": "*"
}
2 changes: 1 addition & 1 deletion library.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name=uFire SHT20
version=1.0.0
version=1.0.2
author=uFire
maintainer[email protected]
sentence=Measure atmospheric temperature and humdity.
Expand Down
10 changes: 9 additions & 1 deletion python/RaspberryPi/SHT20.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def __init__(self, bus=3, resolution=RESOLUTION_12BITS):
self.bus.write_byte(SHT20_I2C, SHT20_RESET)
time.sleep(self.SOFT_RESET_DELAY)

config = self.bus.read_byte_data(SHT20_I2C, SHT20_READ_USER_REG)
config = self.bus.read_byte_data(SHT20_I2C, SHT20_READ_USER_REG)config = self.bus.read_byte_data(SHT20_I2C, SHT20_READ_USER_REG)
config = ((config & _RESERVED_BITMASK) | self._resolution | self._onchip_heater | self._otp_reload)
#self.bus.write_byte(SHT20_I2C, SHT20_WRITE_USER_REG)
self.bus.write_byte_data(SHT20_I2C, SHT20_WRITE_USER_REG, config)
Expand All @@ -48,6 +48,14 @@ def temperature(self):
msb, lsb, crc = self.bus.read_i2c_block_data(SHT20_I2C, SHT20_TEMP_HM, 3)
return -46.85 + 175.72 * (msb * 256.0 + lsb) / 65536

def temperature_f(self):
return (self.temperature * 1.8 + 32)

def connected(self):
retval = self.bus.read_byte_data(SHT20_I2C, SHT20_READ_USER_REG)
if retval != 0xFF:
return True
else:
return False


30 changes: 30 additions & 0 deletions src/uFire_SHT20.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,20 @@ float uFire_SHT20::temperature()
return value * (175.72 / 65536.0)- 46.85;
}

float uFire_SHT20::temperature_f()
{
_reset();
Wire.beginTransmission(SHT20_I2C);
Wire.write(SHT20_TEMP);
Wire.endTransmission();
delay(TEMPERATURE_DELAY);
Wire.requestFrom(SHT20_I2C, 2);
uint8_t msb = Wire.read();
uint8_t lsb = Wire.read();
uint16_t value = msb << 8 | lsb;
return ((value * (175.72 / 65536.0)- 46.85) * 1.8) + 32;
}

float uFire_SHT20::humidity()
{
_reset();
Expand All @@ -72,4 +86,20 @@ float uFire_SHT20::humidity()
uint8_t lsb = Wire.read();
uint16_t value = msb << 8 | lsb;
return value * (125.0 / 65536.0) - 6.0;
}

bool uFire_SHT20::connected()
{
Wire.beginTransmission(SHT20_I2C);
Wire.write(SHT20_READ_USER_REG);
Wire.endTransmission();
Wire.requestFrom(SHT20_I2C, 1);
uint8_t config = Wire.read();

if (config != 0xFF) {
return true;
}
else {
return false;
}
}
2 changes: 2 additions & 0 deletions src/uFire_SHT20.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,9 @@ class uFire_SHT20
#endif // ifndef ESP32
~uFire_SHT20(){};
float temperature();
float temperature_f();
float humidity();
bool connected();

private:
void _reset();
Expand Down
65 changes: 65 additions & 0 deletions src/uFire_SHT20_JSON.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
#if __has_include("ArduinoJson.h")
#include "uFire_SHT20_JSON.h"
#include <ArduinoJson.h>

void uFire_SHT20_JSON::begin(uFire_SHT20 *p_sht20)
{
sht20 = p_sht20;
}

String uFire_SHT20_JSON::processJSON(String json)
{
String cmd = json.substring(0, json.indexOf(" ", 0));
cmd.trim();
json.remove(0, json.indexOf(" ", 0));
json.trim();
String parameter = json.substring(0, json.indexOf(" ", 0));
parameter.trim();

String value = "";
if (cmd == "at") value = air_temp();
if (cmd == "ah") value = air_humidity();
if (cmd == "ac") value = air_connected();

if (value != "")
{
this->value = value.toFloat();
return value;
} else
{
this->value = -1;
return "";
}
}

String uFire_SHT20_JSON::air_temp()
{
String output;
const size_t bufferSize = JSON_OBJECT_SIZE(1) + 20;
DynamicJsonDocument doc(bufferSize);
doc["at"] = sht20->temperature();
serializeJson(doc, output);
return output;
}

String uFire_SHT20_JSON::air_humidity()
{
String output;
const size_t bufferSize = JSON_OBJECT_SIZE(1) + 20;
DynamicJsonDocument doc(bufferSize);
doc["ah"] = sht20->humidity();
serializeJson(doc, output);
return output;
}

String uFire_SHT20_JSON::air_connected()
{
String output;
const size_t bufferSize = JSON_OBJECT_SIZE(1) + 20;
DynamicJsonDocument doc(bufferSize);
doc["ac"] = sht20->connected();
serializeJson(doc, output);
return output;
}

#endif
17 changes: 17 additions & 0 deletions src/uFire_SHT20_JSON.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#pragma once

#include <uFire_SHT20.h>

class uFire_SHT20_JSON
{
public:
float value;
uFire_SHT20_JSON(){}
void begin(uFire_SHT20 *sht20);
String processJSON(String json);
private:
uFire_SHT20 *sht20;
String air_temp();
String air_humidity();
String air_connected();
};
65 changes: 65 additions & 0 deletions src/uFire_SHT20_MP.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
#if __has_include("ArduinoJson.h")
#include "uFire_SHT20_MP.h"
#include <ArduinoJson.h>

void uFire_SHT20_MP::begin(uFire_SHT20 *p_sht20)
{
sht20 = p_sht20;
}

String uFire_SHT20_MP::processMsgPack(String json)
{
String cmd = json.substring(0, json.indexOf(" ", 0));
cmd.trim();
json.remove(0, json.indexOf(" ", 0));
json.trim();
String parameter = json.substring(0, json.indexOf(" ", 0));
parameter.trim();

String value = "";
if (cmd == "at") value = air_temp();
if (cmd == "ah") value = air_humidity();
if (cmd == "ac") value = air_connected();

if (value != "")
{
this->value = value.toFloat();
return value;
} else
{
this->value = -1;
return "";
}
}

String uFire_SHT20_MP::air_temp()
{
String output;
const size_t bufferSize = JSON_OBJECT_SIZE(1) + 20;
DynamicJsonDocument doc(bufferSize);
doc["at"] = sht20->temperature();
serializeMsgPack(doc, output);
return output;
}

String uFire_SHT20_MP::air_humidity()
{
String output;
const size_t bufferSize = JSON_OBJECT_SIZE(1) + 20;
DynamicJsonDocument doc(bufferSize);
doc["ah"] = sht20->humidity();
serializeMsgPack(doc, output);
return output;
}

String uFire_SHT20_MP::air_connected()
{
String output;
const size_t bufferSize = JSON_OBJECT_SIZE(1) + 20;
DynamicJsonDocument doc(bufferSize);
doc["ac"] = sht20->connected();
serializeMsgPack(doc, output);
return output;
}

#endif
17 changes: 17 additions & 0 deletions src/uFire_SHT20_MP.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#pragma once

#include <uFire_SHT20.h>

class uFire_SHT20_MP
{
public:
float value;
uFire_SHT20_MP(){}
void begin(uFire_SHT20 *sht20);
String processMsgPack(String json);
private:
uFire_SHT20 *sht20;
String air_temp();
String air_humidity();
String air_connected();
};

0 comments on commit ed83782

Please sign in to comment.