wifi_lux_sensor.cpp 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. #include <ESP8266WiFi.h>
  2. #include <Wire.h>
  3. #include "config.h"
  4. #include "Adafruit_TSL2591.h"
  5. // ----------- DEFINES ---------------
  6. #define LUX_ID 0x39 // I2C-Id of the lux-sensor
  7. // ----------- GLOBALS ---------------
  8. WiFiClient espClient;
  9. Adafruit_TSL2591 tsl = Adafruit_TSL2591(2591); // pass in a number for the sensor identifier (for your use later)
  10. void connectWifi() {
  11. WiFi.disconnect(false);
  12. Serial.printf("Wi-Fi mode set to WIFI_STA: %s\n", WiFi.mode(WIFI_STA) ? "Ok" : "Failed!");
  13. WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
  14. Serial.print("Connecting to ");Serial.println(WIFI_SSID);
  15. while (WiFi.status() != WL_CONNECTED) {
  16. delay(1000);
  17. Serial.print(WiFi.status());
  18. Serial.print(".\r\n");
  19. //WiFi.printDiag(Serial);
  20. }
  21. Serial.println("");
  22. Serial.print("Connected with IP: "),
  23. Serial.println(WiFi.localIP());
  24. Serial.printf("\n");
  25. }
  26. void setup() {
  27. Serial.begin(38400); //Opens USB-Serial connection for terminal
  28. delay(1000);
  29. Serial.println("Init of WiFi-Lux-sensor project");
  30. connectWifi();
  31. Wire.begin(4, 5); // sda on pin D2, scl on pin D1
  32. tsl.begin();
  33. }
  34. void loop() {
  35. // put your main code here, to run repeatedly:
  36. byte error, address;
  37. Serial.print("WiFi heartbeat - ms since boot: ");
  38. Serial.print(millis());
  39. Serial.println();
  40. Wire.beginTransmission(LUX_ID);
  41. error = Wire.endTransmission();
  42. if (error == 0) {
  43. Serial.println("I2C-Lux device found");
  44. }
  45. else if (error==4) {
  46. Serial.println("--Unknown error.");
  47. }
  48. Serial.println("done\n");
  49. delay(1000);
  50. }