Professional Documents
Culture Documents
Prog Iot tp3
Prog Iot tp3
Compte rendu tp 3
Programmation
IOT
1. Code
2. /**
3. BasicHTTPClient.ino
4.
5. Created on: 24.05.2015
6.
7. */
8. #include <ArduinoJson.h>
9.
10.#include <Arduino.h>
11.
12.#include <WiFi.h>
13.
14.#include <HTTPClient.h>
15.
16.#include <WiFiClient.h>
17.#include "DHTesp.h"
18.
19.const int DHT_PIN = 15;
20.
21.DHTesp dht;
22.
26.void setup() {
27. dht.setup(DHT_PIN, DHTesp::DHT22);
28. Serial.begin(115200);
29. // Serial.setDebugOutput(true);
30.
31. Serial.println();
32. Serial.println();
33. Serial.println();
34. for (uint8_t t = 4; t > 0; t--) {
35. Serial.printf("[SETUP] WAIT %d...\n", t);
36. Serial.flush();
37. delay(1000);
38. }
39.
40. Serial.print("Connecting to WiFi");
41. WiFi.begin("Wokwi-GUEST", "", 6);
42. while(WiFi.status() != WL_CONNECTED) {
43. delay(500);
44. Serial.print(".");
45. }
46. Serial.println("");
47. Serial.print("Connected to WiFi network with IP Address: ");
48. Serial.println(WiFi.localIP());
49.
50. Serial.println("Timer set to 5 seconds (timerDelay variable), it will
take 5 seconds before publishing the first reading.");
51. Serial.println("Status\tHumidity (%)\tTemperature (C) ");
52.}
53.
54.void loop() {
55. delay(dht.getMinimumSamplingPeriod());
56.
57. float humidity = dht.getHumidity();
58. float temperature = dht.getTemperature();
59.
60. Serial.print(dht.getStatusString());
61. Serial.print("\t");
62. Serial.print(humidity, 1);
63. Serial.print("\t\t");
64. Serial.print(temperature, 1);
65.
66.if((WiFi.status()== WL_CONNECTED)){
67.
68. WiFiClient client;
69.
70. HTTPClient http;
71.Serial.print("\n");
72. Serial.print("\t[HTTP] begin...\t");
73.
74. if (http.begin(client, server)){
75.
76. http.addHeader("Content-Type", "application/json");
77. String httpRequestData ="{\"api_key\":\"" + myApikey +
"\",\"field1\" :\"" + String(temperature) + "\",\"field2\" :\"" +
String(humidity) + "\" }";
78.int httpResponseCode = http.POST(httpRequestData);
79.
80.Serial.print("\n");
81.
82. Serial.println(" sending to thingSpeak ");
83.
84.
85.
86. http.end();
87.
88.
89.
90. delay(15000);
91.
92.}
93.}
94.}
2. Wokwi code
3. Graphe en ThingSpeak