wifi_lux_sensor.ino 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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.println("setup()");
  23. Serial.begin(38400); //Opens USB-Serial connection for terminal
  24. delay(2000);
  25. Serial.println("Serial interface is ready");
  26. connectWifi();
  27. Wire.begin(4, 5); // sda on pin D2, scl on pin D1
  28. }
  29. void loop() {
  30. // put your main code here, to run repeatedly:
  31. byte error, address;
  32. int nDevices;
  33. Serial.print("WiFi heartbeat - ms since boot: ");
  34. Serial.print(millis());
  35. Serial.println();
  36. Serial.println("Scanning i2c devices... ");
  37. nDevices = 0;
  38. for(address = 1; address < 127; address++) {
  39. Wire.beginTransmission(address);
  40. error = Wire.endTransmission();
  41. if (error == 0) {
  42. Serial.print("I2C device found at address 0x");
  43. if (address<16)
  44. Serial.print("0");
  45. Serial.print(address,HEX);
  46. Serial.println(" !");
  47. nDevices++;
  48. }
  49. else if (error==4) {
  50. Serial.print("--Unknown error at address 0x");
  51. if (address<16)
  52. Serial.print("0");
  53. Serial.println(address,HEX);
  54. }
  55. }
  56. if (nDevices == 0)
  57. Serial.println("--No I2C devices found\n");
  58. else
  59. Serial.println("done\n");
  60. delay(1000);
  61. }