Advertisement
_Black_Panther_

IoT_Project

Dec 7th, 2021
189
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.54 KB | None | 0 0
  1. /////////////////////////////////
  2. // Generated with a lot of love//
  3. // with TUNIOT FOR ESP32 //
  4. // Website: Easycoding.tn //
  5. /////////////////////////////////
  6.  
  7.  
  8.  
  9. #include <WiFi.h>
  10.  
  11. #include <ArduinoJson.h>
  12.  
  13. WiFiClient client;
  14.  
  15. String thingSpeakAddress= "YR5YX6E28NJA3EWO";
  16. String tsfield1Name;
  17. String request_string;
  18. String apiKey="YR5YX6E28NJA3EWO";
  19.  
  20. String ThingSpeakGet(String IPcache, String monmessagecache,String field) {
  21. DynamicJsonBuffer jsonBuffer(3000);
  22. String line="";
  23. String myurl = "/";
  24. line="";
  25. client.print(String("GET ") + myurl +monmessagecache + " HTTP/1.1\r\n" +
  26. "Host: " + IPcache + "\r\n" +
  27. "Connection: close\r\n\r\n");
  28. unsigned long timeout = millis();
  29. while (client.available() == 0) {
  30. if (millis() - timeout > 5000) {
  31. client.stop();
  32. return "Client Timeout!";
  33. }
  34. }
  35. while(client.available()) {line += client.readStringUntil('\r');}
  36. //////HERE PROCESSING THE REQUEST
  37. line = (line.substring((line.indexOf("{") - 1)));
  38. JsonObject& JsonResultat = jsonBuffer.parseObject(line);
  39. line = JsonResultat["feeds"][0]["field"+field].as<char*>();
  40. return line;
  41. }
  42.  
  43. int parsing_result (JsonObject& monparam){
  44. if (!monparam.success())return 0;
  45. else
  46. return 1;
  47. }
  48.  
  49. void setup()
  50. {
  51. Serial.begin(9600);
  52.  
  53. WiFi.disconnect();
  54. delay(3000);
  55. Serial.println("============ Program Started ============");
  56. WiFi.begin("TP-Link_BBCC","402805477");
  57. while ((!(WiFi.status() == WL_CONNECTED))){
  58. delay(300);
  59. Serial.print("..");
  60.  
  61. }
  62. Serial.println("You are successfully connected to TP_Link_BBCC");
  63. Serial.println("Your IP is");
  64. Serial.print((WiFi.localIP()));
  65.  
  66. }
  67.  
  68.  
  69. void loop()
  70. {
  71.  
  72. if (client.connect("api.thingspeak.com",80)) {
  73. Serial.println("Sending data to Thingspeak.....");
  74. request_string = thingSpeakAddress;
  75. request_string += "&field1=";
  76. request_string += (random(1,100));
  77. request_string += "&field2=";
  78. request_string += random(0,100);
  79. client.print("POST /update HTTP/1.1\n");
  80. client.print("Host: api.thingspeak.com\n");
  81. client.print("Connection: close\n");
  82. client.print("X-THINGSPEAKAPIKEY: "+apiKey+"\n");
  83. client.print("Content-Type: application/x-www-form-urlencoded\n");
  84. client.print("Content-Length: ");
  85. client.print(request_string.length());
  86. client.print("\n\n");
  87. client.print(request_string);
  88. Serial.println("Recieving data from thingspeak.....");
  89. Serial.print("Temparature: ");
  90. Serial.println((ThingSpeakGet("api.thingspeak.com","channels/1598398/fields/1.json?api_key=0UFZICAXKLBEBQXH&results=1","1")));
  91. Serial.print("Humidity: ");
  92. Serial.println((ThingSpeakGet("api.thingspeak.com","channels/1598398/fields/2.json?api_key=0UFZICAXKLBEBQXH&results=1","2")));
  93.  
  94. } else {
  95. Serial.println("Error occured while connecting to Thingspeak");
  96.  
  97. }
  98. if (client.connect("api.thingspeak.com",80)) {
  99. Serial.println("Recieving data from thingspeak.....");
  100. Serial.print("Temparature: ");
  101. Serial.println((ThingSpeakGet("api.thingspeak.com","channels/1598398/fields/1.json?api_key=0UFZICAXKLBEBQXH&results=1","1")));
  102.  
  103. } else {
  104. Serial.println("Error occured while connecting to Thingspeak");
  105.  
  106. }
  107. if (client.connect("api.thingspeak.com",80)) {
  108. Serial.print("Humidity: ");
  109. Serial.println((ThingSpeakGet("api.thingspeak.com","channels/1598398/fields/2.json?api_key=0UFZICAXKLBEBQXH&results=1","2")));
  110.  
  111. } else {
  112. Serial.println("Error occured while connecting to Thingspeak");
  113.  
  114. }
  115. delay(15000);
  116.  
  117. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement