Guest User

Código do cara

a guest
Apr 6th, 2019
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.81 KB | None | 0 0
  1. #include <ESP8266WiFi.h>
  2.  
  3. String apiWritekey = "2LR2R768M183GPHH"; // replace with your THINGSPEAK WRITEAPI key here
  4. const char* ssid = "NETO 2.4"; // your wifi SSID name
  5. const char* password = "netoanto" ;// wifi pasword
  6.  
  7. const char* server = "api.thingspeak.com";
  8. float resolution=3.3/1023;// 3.3 is the supply volt  & 1023 is max analog read value
  9. WiFiClient client;
  10.  
  11. void setup() {
  12.   Serial.begin(9600);
  13.  
  14.   WiFi.begin(ssid, password);
  15.   Serial.println();
  16.   Serial.println();
  17.   Serial.print("Connecting to ");
  18.   Serial.println(ssid);
  19.  
  20.   WiFi.begin(ssid, password);
  21.  
  22.   while (WiFi.status() != WL_CONNECTED) {
  23.     delay(500);
  24.     Serial.print(".");
  25.   }
  26.   Serial.println("");
  27.   Serial.print("NodeMcu connected to wifi...");
  28.   Serial.println(ssid);
  29.   Serial.println();
  30. }
  31.  
  32. void loop() {
  33.   float temp = (analogRead(A0) * resolution) * 100;
  34.  
  35.   if (client.connect(server,80))
  36.   {  
  37.     String tsData = apiWritekey;
  38.            tsData +="&field1=";
  39.            tsData += String(temp);
  40.            tsData += "\r\n\r\n";
  41.  
  42.      client.print("POST /update HTTP/1.1\n");
  43.      client.print("Host: api.thingspeak.com\n");
  44.      client.print("Connection: close\n");
  45.      client.print("X-THINGSPEAKAPIKEY: "+apiWritekey+"\n");
  46.      client.print("Content-Type: application/x-www-form-urlencoded\n");
  47.      client.print("Content-Length: ");
  48.      client.print(tsData.length());
  49.      client.print("\n\n");  // the 2 carriage returns indicate closing of Header fields & starting of data
  50.      client.print(tsData);
  51.  
  52.      Serial.print("Temperature: ");
  53.      Serial.print(temp);
  54.      Serial.println("uploaded to Thingspeak server....");
  55.   }
  56.   client.stop();
  57.  
  58.   Serial.println("Waiting to upload next reading...");
  59.   Serial.println();
  60.   // thingspeak needs minimum 15 sec delay between updates
  61.   //delay(15000);
  62. }
Advertisement
Add Comment
Please, Sign In to add comment