Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* Code snippet to use Dallas sensor with ThingSpeak
- * Use lib Arduino manager to install ThingSpeak and DallasTemperature libraries
- */
- #include "ThingSpeak.h"
- #include <ESP8266WiFi.h>
- #include <OneWire.h>
- #include <DallasTemperature.h>
- ...
- /*============ Dallas device setup ==================*/
- // Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs)
- OneWire oneWire(ONE_WIRE_BUS);
- // Pass our oneWire reference to Dallas Temperature.
- DallasTemperature tempSensor(&oneWire);
- const int MAX_DEVICES = 3;
- static int nDeviceCount = 0;
- static DeviceAddress tempDev[MAX_DEVICES];
- static void findDeviceAddr(void)
- {
- tempSensor.begin();
- nDeviceCount = tempSensor.getDeviceCount();
- if (nDeviceCount > MAX_DEVICES)
- nDeviceCount = MAX_DEVICES;
- for (int i=0; i < nDeviceCount; i++)
- {
- uint8_t *devAddr = tempDev[i];
- if (tempSensor.getAddress(devAddr,i))
- Serial.println("getAddress ok");
- }
- }
- static float getTempF( DeviceAddress deviceAddress)
- {
- float tempF = DallasTemperature::toFahrenheit( tempSensor.getTempC(deviceAddress));
- return tempF;
- }
- ...
- /*============ ThingSpeak setup ==================*/
- // Note: Each channel has its own number and write API key
- // API key is what get used - wrong channel number doesn't matter
- static unsigned long myChannelNumber = YOUR_CHANNEL_NUMBER; // Customize
- static const char *myWriteAPIKey = "Your API KEY"; // Customize
- ...
- /*== Global client for ThingSpeak */
- static WiFiClient client;
- static void sampleTemperature()
- {
- // request to all devices on the bus
- tempSensor.requestTemperatures(); // Send the command to get temperatures
- for (int i=0; i < nDeviceCount; i++)
- {
- float tempF = getTempF(tempDev[i]);
- Serial.print(String(i+1)+": "+tempF+"F ");
- ThingSpeak.setField( i+1, tempF);
- }
- Serial.println();
- if (nDeviceCount > 0)
- {
- int rc = ThingSpeak.writeFields(myChannelNumber, myWriteAPIKey);
- Serial.println(String("Post rc=")+rc);
- }
- }
- void setup ()
- {
- Serial.begin(115200);
- /* Must Do Wifi setup first... */
- ...
- /* Wifi should be ready after this */
- ...
- ThingSpeak.begin(client); // initialize ThingSpeak lib
- ...
- findDeviceAddr(); // locate OneWire devices on the bus
- ...
- }
- void loop ()
- {
- ...
- if (itsTimeToSample)
- sampleTemperature();
- ...
- }
Add Comment
Please, Sign In to add comment