פיתוח ESP32 – הקמת סביבת פיתוח – חלק 2
לאחר שעשינו מקדום ניסוי כלים שהצורב עובד והכל רץ
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
const int Pin = 12; const int ledPin = 13; // State of the push button int buttonState = 0; void setup() { Serial.begin(115200); //Set the pin as an input pullup pinMode(14, OUTPUT); pinMode(13, OUTPUT); pinMode(12, OUTPUT); } void loop() { digitalWrite(12, LOW); delay(250); digitalWrite(12, HIGH); delay(250); } |
נתקדם לקוד WIFI ESP32 ושליחת נתונים וקבל תושבה , את התושבה ניראה בטרימנל הסיראלי
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 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
/* * https://circuits4you.com * * Connecting DevKit ESP32 to WiFi Example * */ #include <WiFi.h> // Include the Wi-Fi library #include <HTTPClient.h> const char* ssid = "yaniv"; // The SSID (name) of the Wi-Fi network you want to connect to const char* password = "magic111"; // The password of the Wi-Fi network //Your Domain name with URL path or IP address with path String serverName = "https://check-ip.com/"; // the following variables are unsigned longs because the time, measured in // milliseconds, will quickly become a bigger number than can be stored in an int. unsigned long lastTime = 0; // Timer set to 10 minutes (600000) //unsigned long timerDelay = 600000; // Set timer to 5 seconds (5000) unsigned long timerDelay = 5000; void setup() { Serial.begin(115200); // Start the Serial communication to send messages to the computer delay(10); Serial.println("start wifi test...."); Serial.println('\n'); WiFi.begin(ssid, password); // Connect to the network Serial.print("Connecting to "); Serial.print(ssid); while (WiFi.status() != WL_CONNECTED) { // Wait for the Wi-Fi to connect delay(500); Serial.print('.'); } Serial.println('\n'); Serial.println("Connection established!"); Serial.print("IP address:\t"); Serial.println(WiFi.localIP()); // Send the IP address of the ESP8266 to the computer } void loop() { //Send an HTTP POST request every 10 minutes if ((millis() - lastTime) > timerDelay) { //Check WiFi connection status if(WiFi.status()== WL_CONNECTED){ HTTPClient http; String serverPath = serverName + "?temperature=24.37"; // Your Domain name with URL path or IP address with path http.begin(serverPath.c_str()); // Send HTTP GET request int httpResponseCode = http.GET(); if (httpResponseCode>0) { Serial.print("HTTP Response code: "); Serial.println(httpResponseCode); String payload = http.getString(); Serial.println(payload); } else { Serial.print("Error code: "); Serial.println(httpResponseCode); } // Free resources http.end(); } else { Serial.println("WiFi Disconnected"); } lastTime = millis(); } } |
eps32 ניתוח נתונים חוזרים במבנה esp32 JSON
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 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 |
/* Rui Santos Complete project details at Complete project details at https://RandomNerdTutorials.com/esp32-http-get-post-arduino/ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files. The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. */ #include <WiFi.h> #include <HTTPClient.h> #include <Arduino_JSON.h> const char* ssid = "REPLACE_WITH_YOUR_SSID"; const char* password = "REPLACE_WITH_YOUR_PASSWORD"; //Your Domain name with URL path or IP address with path const char* serverName = "http://192.168.1.106:1880/get-sensor"; // the following variables are unsigned longs because the time, measured in // milliseconds, will quickly become a bigger number than can be stored in an int. unsigned long lastTime = 0; // Timer set to 10 minutes (600000) //unsigned long timerDelay = 600000; // Set timer to 5 seconds (5000) unsigned long timerDelay = 5000; String sensorReadings; float sensorReadingsArr[3]; void setup() { Serial.begin(115200); WiFi.begin(ssid, password); Serial.println("Connecting"); while(WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); } Serial.println(""); Serial.print("Connected to WiFi network with IP Address: "); Serial.println(WiFi.localIP()); Serial.println("Timer set to 5 seconds (timerDelay variable), it will take 5 seconds before publishing the first reading."); } void loop() { //Send an HTTP POST request every 10 minutes if ((millis() - lastTime) > timerDelay) { //Check WiFi connection status if(WiFi.status()== WL_CONNECTED){ sensorReadings = httpGETRequest(serverName); Serial.println(sensorReadings); JSONVar myObject = JSON.parse(sensorReadings); // JSON.typeof(jsonVar) can be used to get the type of the var if (JSON.typeof(myObject) == "undefined") { Serial.println("Parsing input failed!"); return; } Serial.print("JSON object = "); Serial.println(myObject); // myObject.keys() can be used to get an array of all the keys in the object JSONVar keys = myObject.keys(); for (int i = 0; i < keys.length(); i++) { JSONVar value = myObject[keys[i]]; Serial.print(keys[i]); Serial.print(" = "); Serial.println(value); sensorReadingsArr[i] = double(value); } Serial.print("1 = "); Serial.println(sensorReadingsArr[0]); Serial.print("2 = "); Serial.println(sensorReadingsArr[1]); Serial.print("3 = "); Serial.println(sensorReadingsArr[2]); } else { Serial.println("WiFi Disconnected"); } lastTime = millis(); } } String httpGETRequest(const char* serverName) { WiFiClient client; HTTPClient http; // Your Domain name with URL path or IP address with path http.begin(client, serverName); // Send HTTP POST request int httpResponseCode = http.GET(); String payload = "{}"; if (httpResponseCode>0) { Serial.print("HTTP Response code: "); Serial.println(httpResponseCode); payload = http.getString(); } else { Serial.print("Error code: "); Serial.println(httpResponseCode); } // Free resources http.end(); return payload; } |