wifi_lux_sensor.cpp 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. #include <ESP8266WiFi.h>
  2. #include <Wire.h>
  3. #include "config.h"
  4. WiFiClient espClient;
  5. void connectWifi() {
  6. WiFi.disconnect(false);
  7. Serial.printf("Wi-Fi mode set to WIFI_STA: %s\n", WiFi.mode(WIFI_STA) ? "Ok" : "Failed!");
  8. WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
  9. Serial.print("Connecting to ");Serial.println(WIFI_SSID);
  10. while (WiFi.status() != WL_CONNECTED) {
  11. delay(1000);
  12. Serial.print(WiFi.status());
  13. Serial.print(".\r\n");
  14. //WiFi.printDiag(Serial);
  15. }
  16. Serial.println("");
  17. Serial.print("Connected with IP: "),
  18. Serial.println(WiFi.localIP());
  19. Serial.printf("\n");
  20. }
  21. void setup() {
  22. Serial.begin(38400); //Opens USB-Serial connection for terminal
  23. delay(1000);
  24. Serial.println("Init of WiFi-Lux-sensor project");
  25. connectWifi();
  26. Wire.begin(4, 5); // sda on pin D2, scl on pin D1
  27. }
  28. void loop() {
  29. // put your main code here, to run repeatedly:
  30. byte error, address;
  31. int nDevices;
  32. Serial.print("WiFi heartbeat - ms since boot: ");
  33. Serial.print(millis());
  34. Serial.println();
  35. Serial.println("Scanning i2c devices... ");
  36. nDevices = 0;
  37. for(address = 1; address < 127; address++) {
  38. Wire.beginTransmission(address);
  39. error = Wire.endTransmission();
  40. if (error == 0) {
  41. Serial.print("I2C device found at address 0x");
  42. if (address<16)
  43. Serial.print("0");
  44. Serial.print(address,HEX);
  45. Serial.println(" !");
  46. nDevices++;
  47. }
  48. else if (error==4) {
  49. Serial.print("--Unknown error at address 0x");
  50. if (address<16)
  51. Serial.print("0");
  52. Serial.println(address,HEX);
  53. }
  54. }
  55. if (nDevices == 0)
  56. Serial.println("--No I2C devices found\n");
  57. else
  58. Serial.println("done\n");
  59. delay(1000);
  60. }