Skip to content

Commit

Permalink
Update
Browse files Browse the repository at this point in the history
  • Loading branch information
zipou committed Nov 3, 2017
1 parent 3080ee9 commit 0a5897e
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 38 deletions.
35 changes: 35 additions & 0 deletions lib/Ble/src/Ble.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
Based on Neil Kolban example for IDF: https://github.com/nkolban/esp32-snippets/blob/master/cpp_utils/tests/BLE%20Tests/SampleScan.cpp
Ported to Arduino ESP32 by Evandro Copercini
*/

#include "Ble.h"
#include <Arduino.h>

int scanTime = 5; //In seconds

class MyAdvertisedDeviceCallbacks: public BLEAdvertisedDeviceCallbacks {
void onResult(BLEAdvertisedDevice advertisedDevice) {
Serial.printf("Advertised Device: %s \n", advertisedDevice.toString().c_str());
}
};

void Ble::init() {
// Serial.begin(115200);
BLEDevice::init("");
}

void Ble::scan() {
Serial.println("Scanning...");
BLEScan* pBLEScan = BLEDevice::getScan(); //create new scan
pBLEScan->setAdvertisedDeviceCallbacks(new MyAdvertisedDeviceCallbacks());
pBLEScan->setActiveScan(true); //active scan uses more power, but get results faster
BLEScanResults foundDevices = pBLEScan->start(scanTime);
// Serial.print("Devices found: ");
// Serial.println(foundDevices.getCount());
Serial.println("Scan done!");
}

Ble::Ble() {

}
12 changes: 12 additions & 0 deletions lib/Ble/src/Ble.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#include <BLEDevice.h>
#include <BLEUtils.h>
#include <BLEScan.h>
#include <BLEAdvertisedDevice.h>

class Ble {

public:
Ble();
void init();
void scan();
};
64 changes: 32 additions & 32 deletions lib/DS18B20/src/DS18B20.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ void DS18B20::init(int pin) {

float DS18B20::getTemperature() {
while(!readTemperature()) {
Serial.println("Looping...");
// Serial.println("Looping...");
}
return _temperature;

Expand All @@ -23,17 +23,17 @@ boolean DS18B20::readTemperature() {
float celsius, fahrenheit;

if ( !_oneWire->search(addr)) {
Serial.println("No more addresses.");
Serial.println();
// Serial.println("No more addresses.");
// Serial.println();
_oneWire->reset_search();
delay(250);
return false;
}

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

if (OneWire::crc8(addr, 7) != addr[7]) {
Expand All @@ -44,21 +44,21 @@ boolean DS18B20::readTemperature() {

// the first ROM byte indicates which chip
switch (addr[0]) {
case 0x10:
Serial.println(" Chip = DS18S20"); // or old DS1820
type_s = 1;
break;
// case 0x10:
// Serial.println(" Chip = DS18S20"); // or old DS1820
// type_s = 1;
// break;
case 0x28:
Serial.println(" Chip = DS18B20");
type_s = 0;
break;
case 0x22:
Serial.println(" Chip = DS1822");
// Serial.println(" Chip = DS18B20");
type_s = 0;
break;
default:
Serial.println("Device is not a DS18x20 family device.");
return false;
// case 0x22:
// Serial.println(" Chip = DS1822");
// type_s = 0;
// break;
// default:
// Serial.println("Device is not a DS18x20 family device.");
// return false;
}

_oneWire->reset();
Expand All @@ -72,17 +72,17 @@ boolean DS18B20::readTemperature() {
_oneWire->select(addr);
_oneWire->write(0xBE); // Read Scratchpad

Serial.print(" Data = ");
Serial.print(present, HEX);
Serial.print(" ");
// Serial.print(" Data = ");
// Serial.print(present, HEX);
// Serial.print(" ");
for ( i = 0; i < 9; i++) { // we need 9 bytes
data[i] = _oneWire->read();
Serial.print(data[i], HEX);
Serial.print(" ");
// Serial.print(data[i], HEX);
// Serial.print(" ");
}
Serial.print(" CRC=");
Serial.print(OneWire::crc8(data, 8), HEX);
Serial.println();
// Serial.print(" CRC=");
// Serial.print(OneWire::crc8(data, 8), HEX);
// Serial.println();

// Convert the data to actual temperature
// because the result is a 16 bit signed integer, it should
Expand All @@ -105,14 +105,14 @@ boolean DS18B20::readTemperature() {
}
celsius = (float)raw / 16.0;
fahrenheit = celsius * 1.8 + 32.0;
Serial.print(" Temperature = ");
Serial.print(celsius);
Serial.print(" Celsius, ");
Serial.print(fahrenheit);
Serial.println(" Fahrenheit");
// Serial.print(" Temperature = ");
// Serial.print(celsius);
// Serial.print(" Celsius, ");
// Serial.print(fahrenheit);
// Serial.println(" Fahrenheit");
_temperature = celsius;

if ( celsius > 100 && celsius > 0){
if ( celsius > 100 || celsius < 0){
return false;
}

Expand Down
5 changes: 3 additions & 2 deletions platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,16 @@
; platform = espressif8266
; board = nodemcuv2
; framework = arduino
platform = espressif32
; platform = espressif32
platform = https://github.com/platformio/platform-espressif32.git#feature/stage
board = node32s
framework = arduino
; lib_extra_dirs = ./lib
upload_speed = 921600
; upload_flags = --auth=myesp8266
lib_deps=
Blynk
ArduinoJson
MQTT
ESPiLight
ESP32 BLE Arduino
19
19 changes: 15 additions & 4 deletions src/EspGw.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
// #include <ArduinoOTA.h>
#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>
// #include <BlynkSimpleEsp32.h>

#include <ArduinoJson.h>

Expand All @@ -29,6 +29,9 @@ Ticker flipper;
#include <DS18B20.h>
DS18B20 tempChip;

// #include <Ble.h>
// Ble ble;

String getChipId() {
uint8_t chipid[6];
esp_efuse_read_mac(chipid);
Expand Down Expand Up @@ -67,10 +70,8 @@ void mqttCallback(const char* topic, const char* message) {
JsonObject& root = jsonBuffer.parseObject(message);
const char* protocol = root["protocol"];
const char* data = root["data"];

rf.send((char*) protocol, (char*) data);
// Serial.println(protocol);
// Serial.println(data);
// Serial.println("PROTOCOL CODE SENT");
}
}

Expand All @@ -95,7 +96,14 @@ float readTemperature() {
return temp;
}

// void scanBle() {
// ble.scan();
// }

void setup() {

// ble.init();

Serial.begin(SERIAL_CONSOLE_SPEED);
pinMode(BUILTIN_LED, OUTPUT);
digitalWrite(BUILTIN_LED, HIGH);
Expand All @@ -109,6 +117,7 @@ void setup() {
mqttlib.setCallback(mqttCb);
mqttlib.subscribe(MQTT_TOPIC_IN);
mqttlib.subscribe(MQTT_TOPIC_IN_RAW);

rf.init(RF_TRANSMITTER_PIN, RF_RECEIVER_PIN);
RFLibCallback afunc = &rfCallback;
rf.setCallback(afunc);
Expand All @@ -130,7 +139,9 @@ void loop() {
// digitalWrite(D4, LOW);
// delay(500);
// digitalWrite(D4, HIGH);

rf.loop();

// delay(10);
mqttlib.loop();
// delay(10);
Expand Down

0 comments on commit 0a5897e

Please sign in to comment.