Guest User

Untitled

a guest
Mar 26th, 2020
187
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.70 KB | None | 0 0
  1. #include <WiFi.h>
  2. #include <HTTPClient.h>
  3.  
  4. // Replace with your network credentials
  5. const char* ssid = "REPLACE_WITH_YOUR_SSID";
  6. const char* password = "REPLACE_WITH_YOUR_PASSWORD";
  7.  
  8. // REPLACE with your Domain name and URL path or IP address with path
  9. const char* serverName = "https://api.thingspeak.com/update?";
  10.  
  11. String apiKeyValue = "YOUR_THINGSPEAK_API_KEY";
  12.  
  13. void setup() {
  14. Serial.begin(115200);
  15.  
  16. WiFi.begin(ssid, password);
  17. Serial.println("Connecting");
  18. while(WiFi.status() != WL_CONNECTED) {
  19. delay(500);
  20. Serial.print(".");
  21. }
  22. Serial.println("");
  23. Serial.print("Connected to WiFi network with IP Address: ");
  24. Serial.println(WiFi.localIP());
  25. }
  26.  
  27. void loop() {
  28. //Check WiFi connection status
  29. if(WiFi.status()== WL_CONNECTED){
  30. HTTPClient http;
  31.  
  32. // Your Domain name with URL path or IP address with path
  33. http.begin(serverName);
  34.  
  35. // Specify content-type header
  36. http.addHeader("Content-Type", "application/x-www-form-urlencoded");
  37.  
  38. // Prepare your HTTP POST request data
  39. String httpRequestData = "api_key=" + apiKeyValue + "&field1=" + random(0,20);
  40. Serial.print("httpRequestData: ");
  41. Serial.println(httpRequestData);
  42.  
  43.  
  44. // Send HTTP POST request
  45. int httpResponseCode = http.POST(httpRequestData);
  46.  
  47. if (httpResponseCode>0) {
  48. Serial.print("HTTP Response code: ");
  49. Serial.println(httpResponseCode);
  50. }
  51. else {
  52. Serial.print("Error code: ");
  53. Serial.println(httpResponseCode);
  54. }
  55. // Free resources
  56. http.end();
  57. }
  58. else {
  59. Serial.println("WiFi Disconnected");
  60. }
  61. //Send an HTTP POST request every 30 seconds
  62. delay(10000);
  63. }
Add Comment
Please, Sign In to add comment