1 | #include <Basecamp.hpp>
|
2 | #include <DHTesp.h>
|
3 | #include <AsyncMqttClient.h>
|
4 |
|
5 | Basecamp iot;
|
6 | #define LED_pin 2
|
7 |
|
8 | AsyncMqttClient mqttClient;
|
9 | /** Initialize DHT sensor */
|
10 | DHTesp dht;
|
11 | /** Task handle for the light value read task */
|
12 | TaskHandle_t tempTaskHandle = NULL;
|
13 | /** Pin number for DHT11 data pin */
|
14 | int dhtPin = 25;
|
15 | char tempBuf[2]={0};
|
16 |
|
17 | TempAndHumidity lastValues;
|
18 | char feuchtBuf[2]={0};
|
19 |
|
20 | /*
|
21 | hw_timer_t * timer = NULL;
|
22 | volatile uint8_t ledstat = 0;
|
23 |
|
24 | void IRAM_ATTR onTimer(){
|
25 | // ledstat=1-ledstat;
|
26 | // digitalWrite(16, ledstat); // turn the LED on or off
|
27 | // lastValues = dht.getTempAndHumidity();
|
28 | Serial.println("*************************auslesen*********************");
|
29 | }
|
30 | */
|
31 | void setup() {
|
32 | // put your setup code here, to run once:
|
33 |
|
34 |
|
35 | // Initialize temperature sensor
|
36 | dht.setup(dhtPin, DHTesp::DHT22);
|
37 |
|
38 | iot.begin();
|
39 | pinMode(LED_pin, OUTPUT);
|
40 | Serial.begin(115200);
|
41 | iot.mqtt.onConnect(mqttConnected);
|
42 | iot.mqtt.onMessage(mqttMessage);
|
43 |
|
44 | /* Serial.println("start timer ");
|
45 | timer = timerBegin(0, 80, true); // timer 0, MWDT clock period = 12.5 ns * TIMGn_Tx_WDT_CLK_PRESCALE -> 12.5 ns * 80 -> 1000 ns = 1 us, countUp
|
46 | timerAttachInterrupt(timer, &onTimer, true); // edge (not level) triggered
|
47 | timerAlarmWrite(timer, 2000000, true); // 1000000 * 1 us = 1 s, autoreload true
|
48 | timerAlarmEnable(timer); // enable
|
49 | */
|
50 | }
|
51 | void mqttConnected(bool sessionPresent) {
|
52 | iot.mqtt.subscribe("example1/led", 0);
|
53 | iot.mqtt.publish("stromzaehler/temp", 1, true,tempBuf );
|
54 | iot.mqtt.publish("stromzaehler/relFeucht", 1, true, feuchtBuf );
|
55 | Serial.print("*****************");
|
56 | Serial.println(tempBuf);
|
57 | }
|
58 |
|
59 | void mqttMessage(char* topic, char* payload, AsyncMqttClientMessageProperties properties, size_t len,
|
60 | size_t index, size_t total) {
|
61 | payload[len] = '\0';
|
62 | String strTopic = String(topic);
|
63 | String strPayload = String((char * ) payload);
|
64 |
|
65 | if (strTopic == "example1/led") {
|
66 | if (strPayload == "off" || strPayload == "0" || strPayload == "false") {
|
67 | digitalWrite(LED_pin, LOW);
|
68 | Serial.println("Lampe aus");
|
69 | }
|
70 | if (strPayload == "on" || strPayload == "1" || strPayload == "true") {
|
71 | digitalWrite(LED_pin, HIGH);
|
72 | Serial.println("Lampe an");
|
73 | }
|
74 | }
|
75 |
|
76 |
|
77 | }
|
78 |
|
79 | void loop() {
|
80 | // put your main code here, to run repeatedly:
|
81 | // Reading temperature and humidity takes about 250 milliseconds!
|
82 | // Sensor readings may also be up to 2 seconds 'old' (it's a very slow sensor)
|
83 |
|
84 | lastValues = dht.getTempAndHumidity();
|
85 | Serial.println("Temperature: " + String(lastValues.temperature, 0));
|
86 | Serial.println("Humidity: " + String(lastValues.humidity, 0));
|
87 | dtostrf((lastValues.humidity),2, 0, feuchtBuf);
|
88 | dtostrf((lastValues.temperature),2, 0, tempBuf);
|
89 | delay(1000);
|
90 | }
|