Skip to content

Commit

Permalink
Update
Browse files Browse the repository at this point in the history
  • Loading branch information
zipou committed Oct 27, 2017
1 parent bb583fd commit 6d00537
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 21 deletions.
19 changes: 17 additions & 2 deletions lib/MqttLib/src/MqttLib.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,15 @@ WiFiClientSecure _wificlient;
MQTTClient _client;

MqttLibCallback MqttLib::_callback;
MqttLibErrorCallback MqttLib::_errorCallback;

char* _username;
char* _password;

void MqttLib::setErrorCallback(MqttLibErrorCallback callback) {
MqttLib::_errorCallback = callback;
}

void MqttLib::setCallback(MqttLibCallback callback) {
MqttLib::_callback = callback;
struct call {
Expand All @@ -30,17 +35,27 @@ void MqttLib::init(char* host, int port, char* username, char* password) {

void MqttLib::publish(const char* topic, const char* message) {
if (! _client.connected()) {
MqttLib::connect();
if (! MqttLib::connect()) {
return;
}
}
_client.publish(topic, message);
}

void MqttLib::connect() {
bool MqttLib::connect() {
int i = 0;
while (!_client.connect("arduino", _username, _password)) {
Serial.print(".");
delay(1000);
if (i >= 60) {
if (MqttLib::_errorCallback != NULL) {
(*MqttLib::_errorCallback)();
}
return false;
}
}
Serial.println("MQTT connected");
return true;
}

void MqttLib::subscribe(const char *topic) {
Expand Down
5 changes: 4 additions & 1 deletion lib/MqttLib/src/MqttLib.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,20 @@
#include <MQTTClient.h>

typedef void (*MqttLibCallback)(const char* topic, const char* payload);
typedef void (*MqttLibErrorCallback)();

class MqttLib {

public:
MqttLib();
void init(char* host, int port, char* username, char* password);
void connect();
bool connect();
void loop();
void publish(const char* topic, const char* message);
void subscribe(const char* topic);
void setCallback(MqttLibCallback _callback);
void setErrorCallback(MqttLibErrorCallback _errorCallback);
static MqttLibCallback _callback;
static MqttLibErrorCallback _errorCallback;
protected:
};
4 changes: 4 additions & 0 deletions lib/WifiLib/src/WifiLib.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include <WifiLib.h>

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

void WifiLib::connect(char* ssid, char* password) {
WiFi.begin(ssid, password);
Expand All @@ -11,9 +12,12 @@ void WifiLib::connect(char* ssid, char* password) {
delay(1000);
}

// WiFi.setDNS(dns);
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
Serial.println("Gateway: ");
Serial.println(WiFi.gatewayIP());
}

WiFiClientSecure WifiLib::getClient() {
Expand Down
3 changes: 2 additions & 1 deletion platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
; board = nodemcuv2
; framework = arduino
platform = espressif32
board = nodemcu-32s
board = node32s
framework = arduino
; lib_extra_dirs = ./lib
upload_speed = 921600
Expand All @@ -26,3 +26,4 @@ lib_deps=
ArduinoJson
MQTT
ESPiLight
19
72 changes: 55 additions & 17 deletions src/EspGw.cpp
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@

#define ICACHE_RAM_ATTR IRAM_ATTR
/* Comment this out to disable prints and save space */
#define BLYNK_PRINT Serial

#include <const.h>
#include <Arduino.h>
// #include <ESP8266mDNS.h>
// #include <ArduinoOTA.h>
#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>

#include <ArduinoJson.h>

Expand All @@ -19,6 +20,47 @@ RFLib rf;
#include <WifiLib.h>
WifiLib wifi;

#include <DHT.h>
DHT dht(DHTPIN, DHT22);

#include <Ticker.h>
Ticker flipper;

void sendSensor() {

int i = 0;
int tryHarder = 0;
float h = 0;
float t = 0;
while (i <= tryHarder && (isnan(h) || h==0 ) ) {
Serial.println("Trying to read from DHT sensor!");
// Serial.println(i);
h = dht.readHumidity();
t = dht.readTemperature(); // or dht.readTemperature(true) for Fahrenheit
i = i + 1;
}

if (isnan(h) || isnan(t)) {
Serial.println("Failed to read from DHT sensor!");
return;
}

Serial.println("Sending Info through mqtt");

StaticJsonBuffer<200> jsonBuffer;
JsonObject& message = jsonBuffer.createObject();
message["temperature"] = t;
message["humidity"] = h;

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

}

void mqttCallback(const char* topic, const char* message) {
// Serial.println("MQTTCallback");
// Serial.println(topic);
Expand All @@ -33,16 +75,6 @@ void mqttCallback(const char* topic, const char* message) {
// Serial.println(data);
// Serial.println("PROTOCOL CODE SENT");
}
if (strcmp(topic, MQTT_TOPIC_IN_RAW) == 0) {
rf.sendRaw((char*) message);
// Serial.println("RAW CODE SENT");
}
}

void serialCallback(const char* json) {
// Serial.println("SerialCallback");
// Serial.println(json);
mqttlib.publish(MQTT_TOPIC_OUT, json);
}

void rfCallback(const char* protocol, const char* message) {
Expand All @@ -61,7 +93,7 @@ void rfCallback(const char* protocol, const char* message) {
void setup() {
Serial.begin(SERIAL_CONSOLE_SPEED);
pinMode(BUILTIN_LED, OUTPUT);
digitalWrite(BUILTIN_LED, LOW);
digitalWrite(BUILTIN_LED, HIGH);

wifi.connect(WIFISSID, WIFIPASSWD);
// ArduinoOTA.setPassword(ARDUINO_PASS);
Expand All @@ -76,18 +108,24 @@ void setup() {
RFLibCallback afunc = &rfCallback;
rf.setCallback(afunc);

digitalWrite(BUILTIN_LED, HIGH);
dht.begin();

flipper.setCallback(sendSensor);
flipper.setInterval(TEMP_INTERVAL);
flipper.start();

digitalWrite(BUILTIN_LED, LOW);
}

void loop() {
// ArduinoOTA.handle();

// delay(1000);
flipper.update();
// digitalWrite(D4, LOW);
// delay(500);
// digitalWrite(D4, HIGH);
rf.loop();
delay(10);
// delay(10);
mqttlib.loop();
delay(10);
// delay(10);
}

0 comments on commit 6d00537

Please sign in to comment.