Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /////////////////////////////////
- // Generated with a lot of love//
- // with TUNIOT FOR ESP32 //
- // Website: Easycoding.tn //
- /////////////////////////////////
- #include <WiFi.h>
- #include <ArduinoJson.h>
- WiFiClient client;
- String thingSpeakAddress= "YR5YX6E28NJA3EWO";
- String tsfield1Name;
- String request_string;
- String apiKey="YR5YX6E28NJA3EWO";
- String ThingSpeakGet(String IPcache, String monmessagecache,String field) {
- DynamicJsonBuffer jsonBuffer(3000);
- String line="";
- String myurl = "/";
- line="";
- client.print(String("GET ") + myurl +monmessagecache + " HTTP/1.1\r\n" +
- "Host: " + IPcache + "\r\n" +
- "Connection: close\r\n\r\n");
- unsigned long timeout = millis();
- while (client.available() == 0) {
- if (millis() - timeout > 5000) {
- client.stop();
- return "Client Timeout!";
- }
- }
- while(client.available()) {line += client.readStringUntil('\r');}
- //////HERE PROCESSING THE REQUEST
- line = (line.substring((line.indexOf("{") - 1)));
- JsonObject& JsonResultat = jsonBuffer.parseObject(line);
- line = JsonResultat["feeds"][0]["field"+field].as<char*>();
- return line;
- }
- int parsing_result (JsonObject& monparam){
- if (!monparam.success())return 0;
- else
- return 1;
- }
- void setup()
- {
- Serial.begin(9600);
- WiFi.disconnect();
- delay(3000);
- Serial.println("============ Program Started ============");
- WiFi.begin("TP-Link_BBCC","402805477");
- while ((!(WiFi.status() == WL_CONNECTED))){
- delay(300);
- Serial.print("..");
- }
- Serial.println("You are successfully connected to TP_Link_BBCC");
- Serial.println("Your IP is");
- Serial.print((WiFi.localIP()));
- }
- void loop()
- {
- if (client.connect("api.thingspeak.com",80)) {
- Serial.println("Sending data to Thingspeak.....");
- request_string = thingSpeakAddress;
- request_string += "&field1=";
- request_string += (random(1,100));
- request_string += "&field2=";
- request_string += random(0,100);
- client.print("POST /update HTTP/1.1\n");
- client.print("Host: api.thingspeak.com\n");
- client.print("Connection: close\n");
- client.print("X-THINGSPEAKAPIKEY: "+apiKey+"\n");
- client.print("Content-Type: application/x-www-form-urlencoded\n");
- client.print("Content-Length: ");
- client.print(request_string.length());
- client.print("\n\n");
- client.print(request_string);
- Serial.println("Recieving data from thingspeak.....");
- Serial.print("Temparature: ");
- Serial.println((ThingSpeakGet("api.thingspeak.com","channels/1598398/fields/1.json?api_key=0UFZICAXKLBEBQXH&results=1","1")));
- Serial.print("Humidity: ");
- Serial.println((ThingSpeakGet("api.thingspeak.com","channels/1598398/fields/2.json?api_key=0UFZICAXKLBEBQXH&results=1","2")));
- } else {
- Serial.println("Error occured while connecting to Thingspeak");
- }
- if (client.connect("api.thingspeak.com",80)) {
- Serial.println("Recieving data from thingspeak.....");
- Serial.print("Temparature: ");
- Serial.println((ThingSpeakGet("api.thingspeak.com","channels/1598398/fields/1.json?api_key=0UFZICAXKLBEBQXH&results=1","1")));
- } else {
- Serial.println("Error occured while connecting to Thingspeak");
- }
- if (client.connect("api.thingspeak.com",80)) {
- Serial.print("Humidity: ");
- Serial.println((ThingSpeakGet("api.thingspeak.com","channels/1598398/fields/2.json?api_key=0UFZICAXKLBEBQXH&results=1","2")));
- } else {
- Serial.println("Error occured while connecting to Thingspeak");
- }
- delay(15000);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement