Skip to content

Commit

Permalink
Update Wifi and Temp
Browse files Browse the repository at this point in the history
  • Loading branch information
zipou committed Dec 9, 2017
1 parent 0a5897e commit 5a72481
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 23 deletions.
26 changes: 18 additions & 8 deletions lib/DS18B20/src/DS18B20.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,19 @@ void DS18B20::init(int pin) {
}

float DS18B20::getTemperature() {
while(!readTemperature()) {
// Serial.println("Looping...");
int maxLoop = 30;
int loop = 0;
while(!readTemperature() && loop < maxLoop ) {
Serial.print(".");
// Serial.print(loop);
loop = loop+ 1;
}

if (loop == maxLoop) {
Serial.println("Max Loop Reached");
}

Serial.println(_temperature);
return _temperature;

}
Expand All @@ -26,21 +36,21 @@ boolean DS18B20::readTemperature() {
// Serial.println("No more addresses.");
// Serial.println();
_oneWire->reset_search();
delay(250);
// delay(250);
return false;
}

// Serial.print("ROM =");
for( i = 0; i < 8; i++) {
// Serial.write(' ');
// Serial.print(addr[i], HEX);
}
// for( i = 0; i < 8; i++) {
// // Serial.write(' ');
// // Serial.print(addr[i], HEX);
// }

if (OneWire::crc8(addr, 7) != addr[7]) {
Serial.println("CRC is not valid!");
return false;
}
Serial.println();
// Serial.println();

// the first ROM byte indicates which chip
switch (addr[0]) {
Expand Down
22 changes: 19 additions & 3 deletions lib/WifiLib/src/WifiLib.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,14 @@
#include <WifiLib.h>

WiFiClientSecure netSsl;

char* WifiLib::_ssid;
char* WifiLib::_password;

// IPAddress dns(8, 8, 8, 8);

void WifiLib::connect(char* ssid, char* password) {
WiFi.begin(ssid, password);
void WifiLib::connect() {
WiFi.begin(_ssid, _password);
Serial.print("Checking wifi...");
while (WiFi.status() != WL_CONNECTED) {
Serial.print(".");
Expand All @@ -20,9 +24,21 @@ void WifiLib::connect(char* ssid, char* password) {
Serial.println(WiFi.gatewayIP());
}

boolean WifiLib::isConnected() {
return (WiFi.status() == WL_CONNECTED);
}

void WifiLib::checkAndReconnect() {
if (!isConnected()) {
connect();
}
}

WiFiClientSecure WifiLib::getClient() {
return netSsl;
}

WifiLib::WifiLib() {
WifiLib::WifiLib(char* ssid, char* password) {
WifiLib::_ssid = ssid;
WifiLib::_password = password;
}
9 changes: 6 additions & 3 deletions lib/WifiLib/src/WifiLib.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,13 @@
class WifiLib {

public:
static char* _ssid;
static char* _password;
WifiLib(char* ssid, char* password);

WifiLib();

void connect(char* ssid, char* password);
void connect();
void checkAndReconnect();
boolean isConnected();

WiFiClientSecure getClient();

Expand Down
19 changes: 10 additions & 9 deletions src/EspGw.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ MqttLib mqttlib;
RFLib rf;

#include <WifiLib.h>
WifiLib wifi;
WifiLib wifi = WifiLib(WIFISSID, WIFIPASSWD);

// #include <DHT.h>
// DHT dht(DHTPIN, DHT22);
Expand Down Expand Up @@ -47,15 +47,15 @@ void sendSensor() {

float temp = tempChip.getTemperature();

StaticJsonBuffer<200> jsonBuffer;
StaticJsonBuffer<250> jsonBuffer;
JsonObject& message = jsonBuffer.createObject();
message["temperature"] = temp;

JsonObject& root = jsonBuffer.createObject();
root["sensor"] = getChipId();
root["protocol"] = "temperature";
root["data"] = message;
char buffer[150];
char buffer[250];
root.printTo(buffer);
mqttlib.publish(MQTT_TOPIC_OUT, buffer);

Expand All @@ -76,15 +76,15 @@ void mqttCallback(const char* topic, const char* message) {
}

void rfCallback(const char* protocol, const char* message) {
// Serial.println("RF Callback HAS BEEN CALED");
// Serial.println(protocol);
// Serial.println(message);
StaticJsonBuffer<200> jsonBuffer;
Serial.println("RF Callback HAS BEEN CALED");
Serial.println(protocol);
Serial.println(message);
StaticJsonBuffer<250> jsonBuffer;
JsonObject& root = jsonBuffer.createObject();
root["sensor"] = getChipId();
root["protocol"] = protocol;
root["data"] = message;
char buffer[150];
char buffer[250];
root.printTo(buffer);
mqttlib.publish(MQTT_TOPIC_OUT, buffer);
}
Expand All @@ -108,7 +108,7 @@ void setup() {
pinMode(BUILTIN_LED, OUTPUT);
digitalWrite(BUILTIN_LED, HIGH);

wifi.connect(WIFISSID, WIFIPASSWD);
wifi.connect();
// ArduinoOTA.setPassword(ARDUINO_PASS);
// ArduinoOTA.begin();

Expand Down Expand Up @@ -144,5 +144,6 @@ void loop() {

// delay(10);
mqttlib.loop();
wifi.checkAndReconnect();
// delay(10);
}

0 comments on commit 5a72481

Please sign in to comment.