Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <WiFi.h>
- #include <HTTPClient.h>
- // Replace with your network credentials
- const char* ssid = "REPLACE_WITH_YOUR_SSID";
- const char* password = "REPLACE_WITH_YOUR_PASSWORD";
- // REPLACE with your Domain name and URL path or IP address with path
- const char* serverName = "https://api.thingspeak.com/update?";
- String apiKeyValue = "YOUR_THINGSPEAK_API_KEY";
- 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());
- }
- void loop() {
- //Check WiFi connection status
- if(WiFi.status()== WL_CONNECTED){
- HTTPClient http;
- // Your Domain name with URL path or IP address with path
- http.begin(serverName);
- // Specify content-type header
- http.addHeader("Content-Type", "application/x-www-form-urlencoded");
- // Prepare your HTTP POST request data
- String httpRequestData = "api_key=" + apiKeyValue + "&field1=" + random(0,20);
- Serial.print("httpRequestData: ");
- Serial.println(httpRequestData);
- // Send HTTP POST request
- int httpResponseCode = http.POST(httpRequestData);
- if (httpResponseCode>0) {
- Serial.print("HTTP Response code: ");
- Serial.println(httpResponseCode);
- }
- else {
- Serial.print("Error code: ");
- Serial.println(httpResponseCode);
- }
- // Free resources
- http.end();
- }
- else {
- Serial.println("WiFi Disconnected");
- }
- //Send an HTTP POST request every 30 seconds
- delay(10000);
- }
Add Comment
Please, Sign In to add comment