iot_esp8266_cloud_dweet.ino 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. /*
  2. * https://www.freeboard.io/account/settings
  3. *
  4. * account: cigarbar@gmx.com
  5. *
  6. * https://dweet.io/play/#!/dweets/postDweet_post_0
  7. *
  8. *
  9. */
  10. // Import required libraries
  11. #include "ESP8266WiFi.h"
  12. // WiFi parameters
  13. const char* ssid = "hazard18";
  14. const char* password = "pu1f0xylu1";
  15. // Pin
  16. #define LIGHTSENSOR A0
  17. // Host
  18. const char* host = "dweet.io";
  19. void setup() {
  20. // Start Serial
  21. Serial.begin(115200);
  22. delay(10);
  23. pinMode(LIGHTSENSOR, INPUT);
  24. // We start by connecting to a WiFi network
  25. Serial.println();
  26. Serial.println();
  27. Serial.print("Connecting to ");
  28. Serial.println(ssid);
  29. WiFi.begin(ssid, password);
  30. while (WiFi.status() != WL_CONNECTED) {
  31. delay(500);
  32. Serial.print(".");
  33. }
  34. Serial.println("");
  35. Serial.println("WiFi connected");
  36. Serial.println("IP address: ");
  37. Serial.println(WiFi.localIP());
  38. }
  39. void loop() {
  40. Serial.print("Connecting to ");
  41. Serial.println(host);
  42. // Use WiFiClient class to create TCP connections
  43. WiFiClient client;
  44. const int httpPort = 80;
  45. if (!client.connect(host, httpPort)) {
  46. Serial.println("connection failed");
  47. return;
  48. }
  49. int h = (unsigned int) analogRead(LIGHTSENSOR);
  50. Serial.println("a0 val: " + String(h));
  51. // This will send the request to the server
  52. client.print(String("GET /dweet/for/esp1881?light_intensity=") + String(h) + " HTTP/1.1\r\n" +
  53. "Host: " + host + "\r\n" +
  54. "Connection: close\r\n\r\n");
  55. delay(10);
  56. // Read all the lines of the reply from server and print them to Serial
  57. while(client.available()){
  58. String line = client.readStringUntil('\r');
  59. Serial.print(line);
  60. }
  61. Serial.println();
  62. Serial.println("closing connection");
  63. // Repeat every 10 seconds
  64. delay(1000);
  65. }