# Kombi Thingspeak und SCD30 ```cpp= /* WriteMultipleFields Description: Writes values to fields 1,2,3,4 and status in a single ThingSpeak update every 20 seconds. Hardware: ESP8266 based boards !!! IMPORTANT - Modify the secrets.h file for this project with your network connection and ThingSpeak channel details. !!! Note: - Requires ESP8266WiFi library and ESP8622 board add-on. See https://github.com/esp8266/Arduino for details. - Select the target hardware from the Tools->Board menu - This example is written for a network using WPA encryption. For WEP or WPA, change the WiFi.begin() call accordingly. ThingSpeak ( https://www.thingspeak.com ) is an analytic IoT platform service that allows you to aggregate, visualize, and analyze live data streams in the cloud. Visit https://www.thingspeak.com to sign up for a free account and create a channel. Documentation for the ThingSpeak Communication Library for Arduino is in the README.md folder where the library was installed. See https://www.mathworks.com/help/thingspeak/index.html for the full ThingSpeak documentation. For licensing information, see the accompanying license file. Copyright 2018, The MathWorks, Inc. */ #include <Wire.h> #include "SparkFun_SCD30_Arduino_Library.h" //Click here to get the library: http://librarymanager/All#SparkFun_SCD30 SCD30 airSensor; #include "ThingSpeak.h" #include "secrets.h" #include <ESP8266WiFi.h> char ssid[] = SECRET_SSID; // your network SSID (name) char pass[] = SECRET_PASS; // your network password int keyIndex = 0; // your network key Index number (needed only for WEP) WiFiClient client; unsigned long myChannelNumber = SECRET_CH_ID; const char * myWriteAPIKey = SECRET_WRITE_APIKEY; // Initialize our values int number4 = 12; String myStatus = ""; void setup() { Serial.begin(115200); // Initialize serial Serial.println("SCD30 Example"); Wire.begin(); WiFi.mode(WIFI_STA); ThingSpeak.begin(client); // Initialize ThingSpeak if (airSensor.begin() == false) { Serial.println("Air sensor not detected. Please check wiring. Freezing..."); while (1) ; } airSensor.setMeasurementInterval(4); //Change number of seconds between measurements: 2 to 1800 (30 minutes) //My desk is ~1600m above sealevel airSensor.setAltitudeCompensation(1600); //Set altitude of the sensor in m //Pressure in Boulder, CO is 24.65inHg or 834.74mBar airSensor.setAmbientPressure(835); //Current ambient pressure in mBar: 700 to 1200 float offset = airSensor.getTemperatureOffset(); Serial.print("Current temp offset: "); Serial.print(offset, 2); Serial.println("C"); //airSensor.setTemperatureOffset(5); //Optionally we can set temperature offset to 5°C } void loop() { // Connect or reconnect to WiFi if(WiFi.status() != WL_CONNECTED){ Serial.print("Attempting to connect to SSID: "); Serial.println(SECRET_SSID); while(WiFi.status() != WL_CONNECTED){ WiFi.begin(ssid, pass); // Connect to WPA/WPA2 network. Change this line if using open or WEP network Serial.print("."); delay(5000); } Serial.println("\nConnected."); } if (airSensor.dataAvailable()) { Serial.print("co2(ppm):"); Serial.print(airSensor.getCO2()); Serial.print(" temp(C):"); Serial.print(airSensor.getTemperature(), 1); Serial.print(" humidity(%):"); Serial.print(airSensor.getHumidity(), 1); Serial.println(); } else Serial.print("."); delay(1000); // set the fields with the values ThingSpeak.setField(1, airSensor.getCO2()); ThingSpeak.setField(2, airSensor.getTemperature()); ThingSpeak.setField(3, airSensor.getHumidity()); ThingSpeak.setField(4, number4); // set the status ThingSpeak.setStatus(myStatus); // write to the ThingSpeak channel int x = ThingSpeak.writeFields(myChannelNumber, myWriteAPIKey); if(x == 200){ Serial.println("Channel update successful."); } else{ Serial.println("Problem updating channel. HTTP error code " + String(x)); } // change the values delay(20000); // Wait 20 seconds to update the channel again } ``` ## secrets.h von Jörg (WLAN und Thingspeak-API) ```cpp // Use this file to store all of the private credentials // and connection details #define SECRET_SSID "Jorgs-iMac" // replace MySSID with your WiFi network name #define SECRET_PASS "12341007" // replace MyPassword with your WiFi password #define SECRET_CH_ID 1165964 // replace 0000000 with your channel number #define SECRET_WRITE_APIKEY "XFH8DFHAA2TVH48K" // replace XYZ with your channel write API Key ``` ## Public View Thingspeak https://thingspeak.com/channels/1165964