Advertisement
apl-mhd

NodeMcu Clean Code

Jul 8th, 2021
1,814
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.65 KB | None | 0 0
  1. /*
  2.  * Testing
  3.  */
  4.  
  5. // Libraries
  6. #include <ESP8266HTTPClient.h>
  7. #include<ESP8266WiFi.h>
  8.  
  9. void setup()
  10. {
  11.   Serial.begin(9600);
  12.   WiFi.begin("Fast Lock","fastlock");  // add ssid and password here
  13.  
  14.   while(WiFi.status() !=WL_CONNECTED)
  15.   {
  16.     delay(500);
  17.    
  18.     Serial.println("Waiting for connection");
  19.   }
  20.  
  21.   Serial.println("Connected...");
  22.   delay(1000);
  23.   if (WiFi.status() ==WL_CONNECTED)
  24.   {
  25.     Serial.println("Wi-Fi Connected!");
  26.   }
  27.  
  28.   delay(2000);
  29.   Serial.println("Sending message to server espcomm");
  30.   delay(5000);
  31.   int res=sendmessage("Hi,Server");
  32.   delay(1000);
  33.   if (res==1)
  34.   {
  35.    
  36.     Serial.println("Send Successfully");
  37.   }
  38.   else
  39.   {
  40.    
  41.     Serial.println("Error on Server side or client side.");
  42.   }
  43.  
  44. }
  45.  
  46. void loop()
  47. {
  48.  
  49.   delay(5000);
  50.   sendmessage("aa");
  51.  
  52. }
  53.  
  54.  
  55.  
  56. int sendmessage(String d)
  57. {
  58.   int sres;
  59.   int net;
  60.   if (WiFi.status()==WL_CONNECTED)
  61.   {
  62.     HTTPClient http;
  63.    // http://localhost:8080/buskoi/insertlatlong.php.php?lat=1&long=1
  64.  
  65.    
  66.     String lat = "2";
  67.     String longi = "3";
  68.     postData = "lat=" + lat + "&long=" + longi +"&submit=enter";
  69.    
  70.     http.begin("http://3e50a5b55040.ngrok.io/insertlatlong.php"); //https not wokr here
  71.     http.addHeader("Content-Type", "application/x-www-form-urlencoded");  //Specify content-type header
  72.    int httpCode = http.POST(postData);   //Send the request
  73.    String payload = http.getString();                  //Get the response payload
  74.  
  75.    Serial.println(httpCode);   //Print HTTP return code
  76.    Serial.println(payload);    //Print request response payload
  77.  
  78.    http.end();  //Close connection
  79.   }
  80.  
  81.   return 0;
  82. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement