· 6 years ago · Sep 12, 2019, 10:12 PM
1//-------------Electronics-project-hub>com-------------//
2#include <ESP8266WiFi.h>
3//--------------Your Data----------------//
4String apiKey = "2862D3NO11YK9EP0"; // <<<<< YOUR API KEY
5const char* ssid = "a4ffc2"; // <<<<<<<< Your Wi-Fi SSID
6const char* password = "266957124"; // <<<<<<<< Your Wi-Fi Pasword
7//--------------------------------------//
8const char* server = "api.thingspeak.com";
9WiFiClient client;
10void setup()
11{
12 Serial.begin(115200);
13 delay(10);
14 WiFi.begin(ssid, password);
15 Serial.println();
16 Serial.println();
17 Serial.print("Connecting to ");
18 Serial.println(ssid);
19 WiFi.begin(ssid, password);
20 while (WiFi.status() != WL_CONNECTED)
21 {
22 delay(500);
23 Serial.print(".");
24 }
25 Serial.println("");
26 Serial.println("WiFi connected");
27}
28
29float power, impulse, adc;
30
31uint8_t i;
32
33uint8_t on = 1;
34
35long now, last, dt;
36
37
38void loop()
39{
40 adc = 0.99 * adc + 0.01 * analogRead(A0);
41 i++;
42 if (!i) {
43 if (adc > 410 && on == 0) {
44 on = 1;
45 now = millis();
46 if (last > 0 && now > 0) {
47 dt = now - last;
48 power = 9000.0 / dt ;
49 impulse += 1.0 / 400.0;
50 Serial.println(power);
51 Serial.println(impulse);
52 Serial.println();
53
54 if (client.connect(server, 80))
55 {
56 String postStr = apiKey;
57 postStr += "&field1=";
58 postStr += String(power);
59 postStr += "&field2=";
60 postStr += String(impulse);
61 postStr += "\r\n\r\n";
62 client.print("POST /update HTTP/1.1\n");
63 client.print("Host: api.thingspeak.com\n");
64 client.print("Connection: close\n");
65 client.print("X-THINGSPEAKAPIKEY: " + apiKey + "\n");
66 client.print("Content-Type: application/x-www-form-urlencoded\n");
67 client.print("Content-Length: ");
68 client.print(postStr.length());
69 client.print("\n\n");
70 client.print(postStr);
71 Serial.println(postStr);
72 }
73 client.stop();
74 Serial.println("Waiting...");
75
76
77 }
78 last = now;
79
80 }
81 if (adc < 380 && on == 1) {
82 on = 0;
83 }
84 }
85}
86//-------------Electronics-project-hub>com-------------//