Skip to content

Commit

Permalink
Initial Commit
Browse files Browse the repository at this point in the history
  • Loading branch information
zipou committed Aug 22, 2017
0 parents commit 0a2d6bc
Show file tree
Hide file tree
Showing 15 changed files with 521 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
.pioenvs
.piolibdeps
.clang_complete
.gcc-flags.json
src/const.h
65 changes: 65 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
# Continuous Integration (CI) is the practice, in software
# engineering, of merging all developer working copies with a shared mainline
# several times a day < https://docs.platformio.org/page/ci/index.html >
#
# Documentation:
#
# * Travis CI Embedded Builds with PlatformIO
# < https://docs.travis-ci.com/user/integration/platformio/ >
#
# * PlatformIO integration with Travis CI
# < https://docs.platformio.org/page/ci/travis.html >
#
# * User Guide for `platformio ci` command
# < https://docs.platformio.org/page/userguide/cmd_ci.html >
#
#
# Please choice one of the following templates (proposed below) and uncomment
# it (remove "# " before each line) or use own configuration according to the
# Travis CI documentation (see above).
#


#
# Template #1: General project. Test it using existing `platformio.ini`.
#

# language: python
# python:
# - "2.7"
#
# sudo: false
# cache:
# directories:
# - "~/.platformio"
#
# install:
# - pip install -U platformio
#
# script:
# - platformio run


#
# Template #2: The project is intended to by used as a library with examples
#

# language: python
# python:
# - "2.7"
#
# sudo: false
# cache:
# directories:
# - "~/.platformio"
#
# env:
# - PLATFORMIO_CI_SRC=path/to/test/file.c
# - PLATFORMIO_CI_SRC=examples/file.ino
# - PLATFORMIO_CI_SRC=path/to/test/directory
#
# install:
# - pip install -U platformio
#
# script:
# - platformio ci --lib="." --board=ID_1 --board=ID_2 --board=ID_N
19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
ESPGW
=====

## What is it ?

Playground project to make a 2 ways RF 443 gateway through MQTT.
Used to learn c++ and play with Arduino & ESP.

## How does it work

Due to the size of the great RF Lib [https://github.com/puuu/ESPiLight](ESPiLight) and the lack of RAM on esp8866, you can't use SSL with MQTT *AND* load the RF library.
So just for fun, I've had another ESP which talk to the first one through serial communication.

The server will hold the SSL and the MQTT Connexion.
The client esp will manage RF Signal (send & receive).

## Installation

All you need to do is to create a `const.h` file based on `const.example.h` and you should be good to go as long as you have the hardware set.
54 changes: 54 additions & 0 deletions lib/MqttLib/src/MqttLib.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#include "MqttLib.h"

WiFiClientSecure _wificlient;
MQTTClient _client;

MqttLibCallback MqttLib::_callback;

char* _username;
char* _password;

void MqttLib::setCallback(MqttLibCallback callback) {
MqttLib::_callback = callback;
struct call {
static void a(String &topic, String &payload) {
Serial.println("Message Recived Through MQTT");
if (MqttLib::_callback != NULL) {
(*MqttLib::_callback)(topic.c_str(), payload.c_str());
}
}
};
_client.onMessage(call::a);
}

void MqttLib::init(char* host, int port, char* username, char* password) {
_client.begin(host, port, _wificlient);
_username= username;
_password= password;
MqttLib::connect();
}

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

void MqttLib::connect() {
while (!_client.connect("arduino", _username, _password)) {
Serial.print(".");
delay(1000);
}
Serial.println("MQTT connected");
}

void MqttLib::subscribe(const char *topic) {
Serial.println("Subscribed to");
Serial.println(topic);
_client.subscribe(topic);
}

MqttLib::MqttLib() {

}
17 changes: 17 additions & 0 deletions lib/MqttLib/src/MqttLib.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#include <WiFiClientSecure.h>
#include <MQTTClient.h>

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

class MqttLib {

public:
MqttLib();
void init(char* host, int port, char* username, char* password);
void connect();
void publish(const char* topic, const char* message);
void subscribe(const char* topic);
void setCallback(MqttLibCallback _callback);
static MqttLibCallback _callback;
protected:
};
45 changes: 45 additions & 0 deletions lib/RFLIb/src/RFLib.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#include "RFLib.h"

RFLibCallback RFLib::_callback;

void RFLib::init(int sendPin, int receivePin) {
_rf = new ESPiLight(sendPin);
_rf->initReceiver(receivePin);
Serial.println("Init RF");
}

void RFLib::initReceiver(int receivePin) {
_rf->initReceiver(receivePin);
}

void RFLib::setCallback( RFLibCallback callback ) {
Serial.println("Setting Callback");
RFLib::_callback = callback;
struct call {
static void a(const String &protocol, const String &message, int status, int repeats, const String &deviceID) {
if(status==VALID) {
// Serial.print("Valid message: [");
// Serial.print(protocol);
// Serial.print("] ");
// Serial.print(message);
// Serial.println();
if (RFLib::_callback != NULL) {
(*RFLib::_callback)(protocol.c_str(), message.c_str());
}
}
}
};
_rf->setCallback(call::a);
}

void RFLib::send(char* protocol, char* message) {
//_rf.send("arctech_contact", "{\"id\" : 19970622, \"unit\" : 0, \"opened\" : 1 }");
_rf->send(String(protocol), String(message));
};

void RFLib::loop() {
_rf->loop();
}

RFLib::RFLib() {
}
17 changes: 17 additions & 0 deletions lib/RFLIb/src/RFLib.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#include <ESPiLight.h>

typedef void (*RFLibCallback)(const char* protocol, const char* message);

class RFLib {

public:
RFLib();
void init(int sendPin, int receivePin);
void initReceiver(int receivePin);
void loop();
void send(char* protocol, char* message);
void setCallback(RFLibCallback callback );
static RFLibCallback _callback;
private:
ESPiLight* _rf;
};
57 changes: 57 additions & 0 deletions lib/SerialLib/src/SerialLib.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
#include "SerialLib.h"
#include "Arduino.h"

char SerialLib::delimiter = 10;

SerialLib::SerialLib() {
}

SerialLib::~SerialLib() {
if (NULL != _serial){
delete _serial;
}
}

void SerialLib::init(int rx, int tx, int speed, int buffer_size) {
if (NULL != _serial) {
delete _serial;
}
_serial= new SoftwareSerial(rx, tx);
_serial->begin(speed);
_buffer_size = buffer_size;
// Serial.println("BUFFER");
// Serial.println(_buffer_size);

}

void SerialLib::send(const char* json) {
// Serial.println(json);
const char* car = ";";
char buffer[_buffer_size];
strncpy(buffer, json, sizeof(buffer));
strncat(buffer, car, sizeof(buffer));
Serial.println("buffer");
Serial.print(buffer);
_serial->print(buffer);
}

void SerialLib::setCallback(SerialLibCallback callback) {
_callback = callback;
}

void SerialLib::receive() {
if ( _serial->available() ) {
char buffer[_buffer_size];
_serial->readBytesUntil(59, buffer, sizeof(buffer));
// Serial.println(buffer);
// char c = _serial->read();
// if (c == 13) {
// Serial.println("Carriage Return");
// }
// Serial.write(c);
// Serial.printf("%d", c);
if (NULL != _callback) {
_callback(buffer);
}
}
}
19 changes: 19 additions & 0 deletions lib/SerialLib/src/SerialLib.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#include <SoftwareSerial.h>

typedef void (*SerialLibCallback)(const char* json);

class SerialLib {

public:
SerialLib();
~SerialLib();
SerialLibCallback _callback;
void init(int rx, int tx, int speed, int buffer_size);
void send(const char* json);
void receive();
void setCallback(SerialLibCallback callback);
static char delimiter;
private:
SoftwareSerial* _serial;
int _buffer_size;
};
24 changes: 24 additions & 0 deletions lib/WifiLib/src/WifiLib.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@

#include <WifiLib.h>

WiFiClientSecure netSsl;

void WifiLib::connect(char* ssid, char* password) {
WiFi.begin(ssid, password);
Serial.print("Checking wifi...");
while (WiFi.status() != WL_CONNECTED) {
Serial.print(".");
delay(1000);
}

Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}

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

WifiLib::WifiLib() {
}
14 changes: 14 additions & 0 deletions lib/WifiLib/src/WifiLib.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@

#include <ESP8266WiFi.h>

class WifiLib {

public:

WifiLib();

void connect(char* ssid, char* password);

WiFiClientSecure getClient();

};
36 changes: 36 additions & 0 deletions lib/readme.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@

This directory is intended for the project specific (private) libraries.
PlatformIO will compile them to static libraries and link to executable file.

The source code of each library should be placed in separate directory, like
"lib/private_lib/[here are source files]".

For example, see how can be organized `Foo` and `Bar` libraries:

|--lib
| |--Bar
| | |--docs
| | |--examples
| | |--src
| | |- Bar.c
| | |- Bar.h
| |--Foo
| | |- Foo.c
| | |- Foo.h
| |- readme.txt --> THIS FILE
|- platformio.ini
|--src
|- main.c

Then in `src/main.c` you should use:

#include <Foo.h>
#include <Bar.h>

// rest H/C/CPP code

PlatformIO will find your libraries automatically, configure preprocessor's
include paths and build them.

More information about PlatformIO Library Dependency Finder
- https://docs.platformio.org/page/librarymanager/ldf.html
Loading

0 comments on commit 0a2d6bc

Please sign in to comment.