ESP8266 WiFi – HTTP GET request maken (ArduinoIDE)
Op deze pagina staat een voorbeeld om met de ESP module in Arduino code (c++) een GET request te maken naar een website.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
#include <ESP8266WiFi.h> #include <ESP8266HTTPClient.h> // WiFi parameters to be configured const char* ssid = "Pi_AP"; // Hoofdlettergevoelig const char* password = "Raspberry"; // Hoofdlettergevoelig const char* http_site = "http://jsonplaceholder.typicode.com/users"; const int http_port = 80; void setup(void) { Serial.begin(9600); Serial.print("Bezig met verbinden"); WiFi.begin(ssid, password); // Connect to WiFi // while wifi not connected yet, print '.' // then after it connected, get out of the loop while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); } // Verbonden. Serial.println("OK!"); // Access Point (SSID). Serial.print("SSID: "); Serial.println(WiFi.SSID()); // IP adres. Serial.print("IP: "); Serial.println(WiFi.localIP()); // Signaalsterkte. long rssi = WiFi.RSSI(); Serial.print("Signaal sterkte (RSSI): "); Serial.print(rssi); Serial.println(" dBm"); Serial.println(""); } void loop() { if(WiFi.status()== WL_CONNECTED){ //Check WiFi connection status HTTPClient http; //Declare an object of class HTTPClient http.begin("http://jsonplaceholder.typicode.com/users/1"); //Specify request destination int httpCode = http.GET(); //Send the request if (httpCode > 0) { //Check the returning code String payload = http.getString(); //Get the request response payload Serial.println(payload); //Print the response payload } http.end(); //Close connection } else { Serial.println("Error in WiFi connection"); } delay(30000); //Send a request every 30 seconds } |
Output:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
Bezig met verbinden........OK! SSID: Swiebeltje IP: 192.168.0.12 Signaal sterkte (RSSI): -44 dBm { "id": 1, "name": "Leanne Graham", "username": "Bret", "email": "Sincere@april.biz", "address": { "street": "Kulas Light", "suite": "Apt. 556", "city": "Gwenborough", "zipcode": "92998-3874", "geo": { "lat": "-37.3159", "lng": "81.1496" } }, "phone": "1-770-736-8031 x56442", "website": "hildegard.org", "company": { "name": "Romaguera-Crona", "catchPhrase": "Multi-layered client-server neural-net", "bs": "harness real-time e-markets" } } |
Bronnen: techtutorialsx.com
Een ander voorbeeld te gebruiken voor een GET commando met een PHP script, voorbeeld:
http://192.168.0.2/meting.php?temp=21
1 2 3 4 5 6 7 8 9 10 11 |
<?php //meting.php?temp=200 chdir(dirname(__FILE__)); $temp = $_GET['temp']; if ($temp != "") { $bestand = fopen("meetdata.csv", "a"); fwrite($bestand, date("Y").";".date("m").";".date("d").";".date("H").";".date("i").";".date("s").";".$temp."\r\n"); fclose($bestand); } ?> |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
#include <ESP8266WiFi.h> #include <ESP8266HTTPClient.h> // WiFi parameters to be configured const char* ssid = "ssid"; // Hoofdlettergevoelig const char* password = "wachtwoord"; // Hoofdlettergevoelig // Data webstite (php GET) String httpsite = "http://192.168.0.2/meting.php?temp="; void setup(void) { Serial.begin(9600); Serial.print("Bezig met verbinden"); WiFi.begin(ssid, password); // Connect to WiFi // while wifi not connected yet, print '.' // then after it connected, get out of the loop while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); } // Verbonden. Serial.println("OK!"); // Access Point (SSID). Serial.print("SSID: "); Serial.println(WiFi.SSID()); // IP adres. Serial.print("IP: "); Serial.println(WiFi.localIP()); // Signaalsterkte. long rssi = WiFi.RSSI(); Serial.print("Signaal sterkte (RSSI): "); Serial.print(rssi); Serial.println(" dBm"); Serial.println(""); } void loop() { if(WiFi.status()== WL_CONNECTED){ //Check WiFi connection status HTTPClient http; //Declare an object of class HTTPClient int sensordata = random(30); //Willekeurig getal t/m 30 String httpget = httpsite + sensordata; http.begin(httpget); //Specify request destination int httpCode = http.GET(); //Send the request Serial.println(httpCode); //Print the response http.end(); //Close connection } else { Serial.println("Fout in WiFi connectie!"); } delay(30000); //Send a request every 30 seconds } |
Output Arduino:
1 2 3 4 5 6 7 8 9 10 |
Bezig met verbinden..........OK! SSID: Wieseltje IP: 192.168.42.12 Signaal sterkte (RSSI): -46 dBm 200 200 200 200 etc... |
Output CSV bestand:
1 2 3 |
2017;05;12;14;44;06;11 2017;05;12;14;44;36;16 2017;05;12;14;45;06;28 |
Bronnen: techtutorialsx.com