ESP8266
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 |
#include <ESP8266WiFi.h> // Include the ESP8266 Library #include "ThingSpeak.h" // Include the ThingSpeak library WiFiClient client; const char* ssid = "SSID"; // Enter your WiFi network name const char* password = "PASSWD"; // Enter your WiFi password unsigned long myChannelNumber = 237783; const char * myWriteAPIKey = "WRITE_API"; const char * myReadAPIKey = "READ_API"; void setup() { Serial.begin(9600); // Get ready for serial communications and display the connetion status delay(100); Serial.println(); Serial.println(); Serial.print("Connecting to WiFi network - "); Serial.println(ssid); WiFi.begin(ssid, password); while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); } Serial.println(""); Serial.println("WiFi connected"); Serial.println("IP address: "); Serial.println(WiFi.localIP()); ThingSpeak.begin(client); } void loop() { int potentiometerValue = analogRead(A0); ThingSpeak.writeField(myChannelNumber, 1, potentiometerValue, myWriteAPIKey); int readField = ThingSpeak.readFloatField(myChannelNumber, 1, myReadAPIKey); Serial.print("Current potentiometer value is: "); Serial.println(readField); delay(16000); } |
[collapse]
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 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 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 |
#include <WiFi.h> #include <Wire.h> #include <Adafruit_Sensor.h> #include <Adafruit_BME280.h> #define SEALEVELPRESSURE_HPA (1013.25) const char* ssid = "SSID"; const char* password = "PASS"; const char * hostDomain = "api.thingspeak.com"; const int hostPort = 80; String myWriteAPIKey = "YNYD7OQXR8SZVF6U"; WiFiClient client; Adafruit_BME280 bme; float bmeTemperature = 0; float bmePressure = 0; float bmeHumidity =0; void setup() { Serial.begin(9600); delay(100); if (!bme.begin()) { Serial.println("Could not find a valid BME280 sensor, check wiring!"); while (1); } Serial.println(); Serial.println(); Serial.print("Connecting to "); Serial.println(ssid); WiFi.begin(ssid, password); while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); } Serial.println(""); Serial.println("WiFi connected"); Serial.println("IP address: "); Serial.println(WiFi.localIP()); } void requestURL(const char * host, uint8_t port, float Temperature, float Pressure, float Humidity) { Serial.println("Connecting to domain: " + String(host)); if (!client.connect(host, port)) { Serial.println("connection failed"); return; } Serial.println("Connected!"); // This will send the request to the server String getStr = myWriteAPIKey; getStr +="&field1="; getStr += String(Temperature); getStr +="&field2="; getStr += String(Pressure); getStr +="&field3="; getStr += String(Humidity); getStr += "\r\n\r\n"; Serial.println(getStr); client.print("GET /update HTTP/1.1\n"); client.print("Host: api.thingspeak.com\n"); client.print("Connection: close\n"); client.print("X-THINGSPEAKAPIKEY: "+myWriteAPIKey+"\n"); client.print("Content-Type: application/x-www-form-urlencoded\n"); client.print("Content-Length: "); client.print(getStr.length()); client.print("\n\n"); client.print(getStr); unsigned long timeout = millis(); while (client.available() == 0) { if (millis() - timeout > 5000) { Serial.println(">>> Client Timeout !"); client.stop(); return; } } // Read all the lines of the reply from server and print them to Serial while (client.available()) { String line = client.readStringUntil('\r'); Serial.print(line); } Serial.println(); Serial.println("closing connection"); client.stop(); } void loop() { bmeTemperature = bme.readTemperature(); bmePressure = bme.readPressure()*0.007500617; bmeHumidity = bme.readHumidity(); Serial.print("Temperature = "); Serial.print(bmeTemperature); Serial.println(" *C"); Serial.print("Pressure = "); Serial.print(bmePressure); Serial.println(" hPa"); Serial.print("Humidity = "); Serial.print(bmeHumidity); Serial.println(" %"); Serial.println(); requestURL(hostDomain, hostPort, bmeTemperature, bmePressure, bmeHumidity); delay(60000); } |
[collapse]