mirror of
https://github.com/alexandrebobkov/ESP-Nodes.git
synced 2024-10-05 20:47:50 -04:00
.
Overall configuration
This commit is contained in:
parent
511f248582
commit
d02532787d
@ -12,3 +12,9 @@
|
||||
platform = espressif32
|
||||
board = esp32doit-devkit-v1
|
||||
framework = arduino
|
||||
monitor_speed = 115200
|
||||
lib_deps =
|
||||
knolleary/PubSubClient@^2.8
|
||||
|
||||
[platformio]
|
||||
description = ESP32 Sensors node
|
||||
|
@ -1,7 +1,7 @@
|
||||
// Uncomment modules as required
|
||||
//#define RTC
|
||||
//#define MICRO_SD
|
||||
#define BMP280 // Adafruit BMP280; temp & pressure
|
||||
//#define BMP280 // Adafruit BMP280; temp & pressure
|
||||
//#define BME280 // Generic BME280; temp, pressure & humidity
|
||||
//#define AWSIoT
|
||||
|
||||
|
@ -23,6 +23,7 @@ struct {
|
||||
float temperature = 0.0;
|
||||
} sensors_values;
|
||||
|
||||
// Sensors modules specified in config.h file.
|
||||
// BME280
|
||||
#ifdef BME280
|
||||
// WaveShare BME280
|
||||
@ -35,7 +36,7 @@ Adafruit_BME280 bme;
|
||||
#define BMP_MISO (19)
|
||||
#define BMP_MOSI (23)
|
||||
#define BMP_CS (5)
|
||||
Adafruit_BMP280 bmp(BMP_CS);
|
||||
//Adafruit_BMP280 bmp(BMP_CS);
|
||||
#endif
|
||||
|
||||
// Mosquitto
|
||||
@ -192,7 +193,142 @@ void mosquitto_connect ()
|
||||
}
|
||||
|
||||
void setup() {
|
||||
Serial.begin(115200);
|
||||
Serial.println();
|
||||
sensors_values.humidity = 0.0;
|
||||
sensors_values.pressure = 0.0;
|
||||
sensors_values.temperature = 0.0;
|
||||
|
||||
// Initialize GPIO
|
||||
#ifdef devkit_36pin_001
|
||||
#endif
|
||||
#ifdef devkit_30pin_001
|
||||
pinMode(LED_PIN, OUTPUT);
|
||||
pinMode(PING_PIN, OUTPUT);
|
||||
pinMode(SWITCH_1, OUTPUT);
|
||||
pinMode(SWITCH_2, OUTPUT);
|
||||
|
||||
pinMode(FAN_RPM, INPUT);
|
||||
digitalWrite(FAN_RPM, HIGH);
|
||||
//attachInterrupt(digitalPinToInterrupt(FAN_RPM), rpm_fan, FALLING);
|
||||
//pinMode(DAC_CH1, OUTPUT);
|
||||
// Active level is LOW
|
||||
digitalWrite(SWITCH_1, LOW);
|
||||
digitalWrite(SWITCH_2, LOW);
|
||||
//digitalWrite(DAC_CH1, LOW);
|
||||
dacWrite(DAC1, 0);
|
||||
#endif
|
||||
|
||||
Serial.println("setup");
|
||||
Serial.println("setup done");
|
||||
|
||||
// WaveShare BME280
|
||||
#ifdef BME280
|
||||
unsigned status = bme.begin();
|
||||
if (!status) {
|
||||
Serial.println("Could not find a valid BME/BMP280 sensor, check wiring!");
|
||||
Serial.print("SensorID was: 0x"); Serial.println(bme.sensorID());//,16);
|
||||
Serial.print(" ID of 0xFF probably means a bad address, a BMP 180 or BMP 085\n");
|
||||
Serial.print(" ID of 0x56-0x58 represents a BMP 280,\n");
|
||||
Serial.print(" ID of 0x60 represents a BME 280.\n");
|
||||
Serial.print(" ID of 0x61 represents a BME 680.\n");
|
||||
while (1);
|
||||
}
|
||||
else {
|
||||
humidity = bme.readHumidity();
|
||||
pressure = bme.readPressure() / 100.0F;
|
||||
}
|
||||
#endif
|
||||
|
||||
// BMP280
|
||||
#ifdef BMP280
|
||||
unsigned status_bmp280;
|
||||
status_bmp280 = bmp.begin();
|
||||
if (!status_bmp280) {
|
||||
Serial.println("Could not find BMP280");
|
||||
Serial.println(bmp.sensorID(),16);
|
||||
while (1);
|
||||
}
|
||||
else {
|
||||
Serial.println(bmp.sensorID(),16);
|
||||
}
|
||||
#endif
|
||||
|
||||
// Initialize RTC module, if defined
|
||||
#ifdef RTC
|
||||
rtc.begin();
|
||||
if (! rtc.begin())
|
||||
{
|
||||
Serial.println("Couldn't find RTC");
|
||||
while (1);
|
||||
}
|
||||
temp = rtc.getTemperature();
|
||||
#endif
|
||||
// Uncomment when compiling for the first time
|
||||
//rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
|
||||
/*if (rtc.lostPower()) {
|
||||
Serial.println("RTC lost power, lets set the time!");
|
||||
// following line sets the RTC to the date & time this sketch was compiled
|
||||
rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
|
||||
// This line sets the RTC with an explicit date & time, for example to set
|
||||
// January 21, 2014 at 3am you would call:
|
||||
// rtc.adjust(DateTime(2014, 1, 21, 3, 0, 0));
|
||||
}*/
|
||||
|
||||
String hostname = "ESP32LF";
|
||||
WiFi.config(INADDR_NONE, INADDR_NONE, INADDR_NONE, INADDR_NONE);
|
||||
WiFi.setHostname(hostname.c_str());
|
||||
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
|
||||
WiFi.mode(WIFI_STA);
|
||||
|
||||
Serial.println("Connecting to Wi-Fi");
|
||||
|
||||
// Connect to wifi.
|
||||
while (WiFi.status() != WL_CONNECTED) {
|
||||
delay(500);
|
||||
Serial.print("#");
|
||||
}
|
||||
Serial.print("\nCONNECTED\nIP: ");
|
||||
Serial.println(WiFi.localIP());
|
||||
|
||||
Serial.print("Connecting to Mosquitto at IP: ");
|
||||
Serial.print(mqtt_server);
|
||||
#ifdef MQTT // MOSQUITTO MQTT port 1883
|
||||
Serial.println(":1883");
|
||||
connection.setServer(mqtt_server, 1883);
|
||||
if(connection.connect("ESP32")) {
|
||||
Serial.println("Mosquitto Connected!");
|
||||
connection.setCallback(mosquito_callback);
|
||||
digitalWrite(LED_PIN, HIGH);
|
||||
}
|
||||
else {
|
||||
Serial.print("Mosquitto state: ");
|
||||
digitalWrite(LED_PIN, LOW);
|
||||
}
|
||||
Serial.println(connection.state());
|
||||
#endif
|
||||
#ifdef MQTT_SSL // MOSQUITTO MQTT port 8883
|
||||
Serial.println(":8883");
|
||||
connection.setServer(mqtt_server, 8883);
|
||||
espClientSSL.setCACert(NODE_CERT_CA);
|
||||
espClientSSL.setCertificate(NODE_CERT_CRT);
|
||||
espClientSSL.setPrivateKey(NODE_CERT_PRIVATE);
|
||||
//connection.setCallback(mosquito_callback);
|
||||
if(connection.connect("esp32")) {
|
||||
Serial.println("Mosquitto Connected!");
|
||||
connection.subscribe("esp32/sw1");
|
||||
connection.subscribe("esp32/sw2");
|
||||
connection.subscribe(MQTT_IOT_CHANNEL_OUTPUT_SWITCH_2);
|
||||
connection.subscribe(MQTT_IOT_CHANNEL_OUTPUT_PWM_1);
|
||||
connection.setCallback(mosquito_callback);
|
||||
digitalWrite(LED_PIN, HIGH);
|
||||
}
|
||||
else {
|
||||
Serial.print("Mosquitto state: ");
|
||||
digitalWrite(LED_PIN, LOW);
|
||||
}
|
||||
Serial.println(connection.state());
|
||||
#endif
|
||||
}
|
||||
|
||||
void loop() {
|
||||
|
Loading…
Reference in New Issue
Block a user