Forum: Mikrocontroller und Digitale Elektronik String Definieren / mehrere Loops? WEMOS D1


von Sebastian Merz (Gast)


Lesenswert?

Hallo liebe Community, hätte mal eine frage und zwar bin ich gerade 
dabei mit einem Wemos D1 mini einen MQTT Client zu bauen, und mir sind 
zwei fragen aufgekommen ^^ ( Neopixels + PubSubclient + BME280 Senor )

1. habe bereits einen bme280 sensor usw drauf die abgefragt werden bei 
änderung ( im loop ), kann ich noch nebenbei eine animation laufen 
lassen für meine Neopixels ? Schedueler Library soll ja nicht bzw nicht 
so toll funktionieren beim ESP8266 ?!

und zweite frage bezieht sich auf den Code, bekomme von dem void 
callback ein hex zugesendet für rgb LEDs wenn ich strPayload = 
"#FFFFFF"; angebe funktioniert es, komme gerade nicht weiter. Wie 
definier ich das richtig ?

Danke schonmal für Antworten :D
1
#include <ESP8266WiFi.h>
2
#include <Wire.h>
3
#include <PubSubClient.h>
4
#include <SPI.h>
5
#include <Adafruit_Sensor.h>
6
#include <Adafruit_BME280.h>
7
#include <Adafruit_NeoPixel.h>
8
9
//PubSubClient
10
#define wifi_ssid "xxxx"
11
#define wifi_password "xxxx"
12
#define mqtt_server "xxxx"
13
const char* mqtt_user = "xxxx";
14
const char* mqtt_password = "xxxx";
15
16
//BME280
17
#define SEALEVELPRESSURE_HPA (1013.25)
18
19
//Subcribe Topics
20
#define humidity_topic "Home/Weatherstation/Humidity"
21
#define temperature_topic "Home/Weatherstation/Temperature"
22
#define pressure_topic "Home/Weatherstation/Pressure"
23
#define altitude_topic "Home/Weatherstation/Altitude"
24
#define light_topic "Home/Weatherstation/Light"
25
26
//Publish Topics
27
#define ir_topic "/Home/Livingroom/IR"
28
#define rf_topic "/Home/Livingroom/RF"
29
#define main_topic "/Home/Livingroom/Light/Main"
30
#define rgb_topic "/Home/Livingroom/Light/RGB"
31
#define rgbfunc_topic "/Home/Livingroom/Light/RGBfunc"
32
33
#define PIN D8
34
Adafruit_NeoPixel pixels = Adafruit_NeoPixel(8, PIN, NEO_GRB + NEO_KHZ800);
35
36
String strTopic;
37
String strPayload;
38
39
WiFiClient espClient;
40
PubSubClient client(espClient);
41
42
const int analogInPin = A0;
43
Adafruit_BME280 bme; // configuring as I2C 
44
45
46
void setup() {
47
  Serial.begin(115200);
48
  setup_wifi();
49
  client.setServer(mqtt_server, 1883);
50
  client.setCallback(callback);
51
  //LED
52
  pinMode(BUILTIN_LED, OUTPUT);
53
  // Starting i2c;
54
  Wire.begin(SDA, SCL);
55
  pixels.begin();
56
  
57
  // Starting Sensor
58
  bool status;
59
60
  status = bme.begin();  
61
    if (!status) {
62
        Serial.println("Could not find a valid BME280 sensor, check wiring!");
63
        while (1);
64
    }
65
}
66
67
void setup_wifi() {
68
  delay(10);
69
  // We start by connecting to a WiFi network
70
  Serial.println();
71
  Serial.print("Connecting to ");
72
  Serial.println(wifi_ssid);
73
74
  WiFi.begin(wifi_ssid, wifi_password);
75
  WiFi.mode(WIFI_STA);
76
77
  while (WiFi.status() != WL_CONNECTED) {
78
    delay(500);
79
    Serial.print(".");
80
  }
81
82
  Serial.println("");
83
  Serial.println("WiFi connected");
84
  Serial.println("IP address: ");
85
  Serial.println(WiFi.localIP());
86
}
87
88
void callback(char* topic, byte* payload, unsigned int length) {
89
  int led;
90
  Serial.print("Message arrived [");
91
  Serial.print(topic);
92
  Serial.print("] ");
93
  for (int i = 0; i < length; i++) {
94
    Serial.print((char)payload[i]);
95
    strPayload = String((char)payload[i]);
96
  }
97
  Serial.println();
98
  strTopic = String((char*)topic);
99
  
100
  
101
  if ((strTopic == main_topic) && ((char)payload[0] == '0')) {
102
    digitalWrite(BUILTIN_LED, LOW);
103
  }
104
  else if ((strTopic == main_topic) && ((char)payload[0] == '1')) {
105
    digitalWrite(BUILTIN_LED, HIGH);
106
  }
107
  else if ((strTopic == rgbfunc_topic) && ((char)payload[0] == '1')) {
108
    setColor(led,255,0,0,100);
109
  }
110
  else if ((strTopic == rgbfunc_topic) && ((char)payload[0] == '0')) {
111
    setColor(led,0,0,0,100);
112
  }
113
  else if (strTopic == rgb_topic) {
114
    String hexstring = strPayload;
115
    long number = strtol( &hexstring[1], NULL, 16);
116
    long r = number >> 16;
117
    long g = number >> 8 & 0xFF;
118
    long b = number & 0xFF;
119
120
    Serial.print("red is ");
121
    Serial.println(r);
122
    Serial.print("green is ");
123
    Serial.println(g);
124
    Serial.print("blue is ");
125
    Serial.println(b);    
126
    Serial.print("");    
127
    Serial.print(hexstring);
128
  }
129
130
  
131
  else {
132
    Serial.print("Subscribed but no function!");
133
  }
134
135
}
136
137
void reconnect() {
138
  // Loop until we're reconnected
139
  while (!client.connected()) {
140
    Serial.print("Attempting MQTT connection...");
141
    // Attempt to connect
142
    // If you do (not) want to use a username and password, change next line to
143
    // if (client.connect("ESP8266Client")) { 
144
    // or (client.connect("ESP8266Client", mqtt_user, mqtt_password)) {
145
    if (client.connect("Wohnzimmer", mqtt_user, mqtt_password)) {
146
      Serial.println("connected");
147
      client.subscribe(ir_topic);
148
      client.subscribe(rf_topic);
149
      client.subscribe(main_topic);
150
      client.subscribe(rgb_topic);
151
      client.subscribe(rgbfunc_topic);
152
    }
153
    else {
154
      Serial.print("failed, rc=");
155
      Serial.print(client.state());
156
      Serial.println(" try again in 5 seconds");
157
      // Wait 5 seconds before retrying
158
      delay(5000);
159
    }
160
  }
161
}
162
163
void setColor(int led, int redValue, int greenValue, int blueValue, int delayValue)
164
{
165
  pixels.setPixelColor(led, pixels.Color(redValue, greenValue, blueValue)); 
166
  pixels.show();
167
}
168
169
bool checkBound(float newValue, float prevValue, float maxDiff) {
170
  return !isnan(newValue) &&
171
         (newValue < prevValue - maxDiff || newValue > prevValue + maxDiff);
172
}
173
174
long lastMsg = 0;
175
float temp = 0.0;
176
float hum = 0.0;
177
float pres = 0.0;
178
float alti = 0.0;
179
float light = 0.0;
180
float diff = 0.3;
181
float diff2 = 1;
182
float diff3 = 2;
183
184
void loop() {
185
  if (!client.connected()) {
186
    reconnect();
187
  }
188
  client.loop();
189
  long now = millis();
190
  if (now - lastMsg > 1000) {
191
    lastMsg = now;
192
193
    float newTemp = bme.readTemperature();
194
    float newHum = bme.readHumidity();
195
    float newPres = bme.readPressure() / 100.0F;
196
    float newAlti = bme.readAltitude(SEALEVELPRESSURE_HPA);
197
    float newLight = analogRead(analogInPin);
198
199
    if (checkBound(newTemp, temp, diff)) {
200
      temp = newTemp;
201
      Serial.print("Neue Temperatur:");
202
      Serial.println(String(temp).c_str());
203
      client.publish(temperature_topic, String(temp).c_str(), true);
204
    }
205
206
    if (checkBound(newHum, hum, diff)) {
207
      hum = newHum;
208
      Serial.print("Neue Luftfeuchtigkeit:");
209
      Serial.println(String(hum).c_str());
210
      client.publish(humidity_topic, String(hum).c_str(), true);
211
    }
212
    
213
    if (checkBound(newPres, pres, diff)) {
214
      pres = newPres;
215
      Serial.print("Neuer Druck:");
216
      Serial.println(String(pres).c_str());
217
      client.publish(pressure_topic, String(pres).c_str(), true);
218
    }
219
     
220
    if (checkBound(newAlti, alti, diff2)) {
221
      alti = newAlti;
222
      Serial.print("Neue Höhe:");
223
      Serial.println(String(alti).c_str());
224
      client.publish(altitude_topic, String(alti).c_str(), true);
225
    }
226
    if (checkBound(newLight, light, diff3)) {
227
      light = newLight;
228
      Serial.print("Neue Helligkeit:");
229
      Serial.println(String(light).c_str());
230
      client.publish(light_topic, String(light).c_str(), true);
231
    }
232
  }
233
}

Bitte melde dich an um einen Beitrag zu schreiben. Anmeldung ist kostenlos und dauert nur eine Minute.
Bestehender Account
Schon ein Account bei Google/GoogleMail? Keine Anmeldung erforderlich!
Mit Google-Account einloggen
Noch kein Account? Hier anmelden.