tomsim

Dallas sensor to ThingSpeak on ESP8266 Arduino

Aug 15th, 2016
458
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.34 KB | None | 0 0
  1. /* Code snippet to use Dallas sensor with ThingSpeak
  2.  * Use lib Arduino manager to install ThingSpeak and DallasTemperature libraries
  3.  */
  4.  
  5. #include "ThingSpeak.h"
  6. #include <ESP8266WiFi.h>
  7. #include <OneWire.h>
  8. #include <DallasTemperature.h>
  9.  
  10. ...
  11. /*============ Dallas device setup ==================*/
  12. // Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs)
  13. OneWire oneWire(ONE_WIRE_BUS);
  14.  
  15. // Pass our oneWire reference to Dallas Temperature.
  16. DallasTemperature tempSensor(&oneWire);
  17.  
  18. const int MAX_DEVICES = 3;
  19. static int nDeviceCount = 0;
  20. static DeviceAddress tempDev[MAX_DEVICES];
  21.  
  22. static void findDeviceAddr(void)
  23. {
  24.     tempSensor.begin();
  25.     nDeviceCount = tempSensor.getDeviceCount();
  26.  
  27.     if (nDeviceCount > MAX_DEVICES)
  28.         nDeviceCount = MAX_DEVICES;
  29.  
  30.     for (int i=0; i < nDeviceCount; i++)
  31.     {
  32.         uint8_t *devAddr = tempDev[i];
  33.         if (tempSensor.getAddress(devAddr,i))
  34.             Serial.println("getAddress ok");
  35.     }
  36. }
  37. static float getTempF( DeviceAddress deviceAddress)
  38. {
  39.     float tempF = DallasTemperature::toFahrenheit( tempSensor.getTempC(deviceAddress));
  40.     return tempF;
  41. }
  42.  
  43. ...
  44. /*============ ThingSpeak setup ==================*/
  45. // Note:  Each channel has its own number and write API key
  46. // API key is what get used - wrong channel number doesn't matter
  47.  
  48. static unsigned long myChannelNumber = YOUR_CHANNEL_NUMBER; // Customize
  49. static const char   *myWriteAPIKey = "Your API KEY";        // Customize
  50. ...
  51.  
  52. /*== Global client for ThingSpeak */
  53. static WiFiClient  client;
  54.  
  55. static void sampleTemperature()
  56. {
  57.     // request to all devices on the bus
  58.     tempSensor.requestTemperatures(); // Send the command to get temperatures
  59.  
  60.     for (int i=0; i < nDeviceCount; i++)
  61.     {
  62.         float tempF = getTempF(tempDev[i]);
  63.         Serial.print(String(i+1)+": "+tempF+"F ");
  64.         ThingSpeak.setField( i+1, tempF);
  65.     }
  66.     Serial.println();
  67.     if (nDeviceCount > 0)
  68.     {
  69.         int rc = ThingSpeak.writeFields(myChannelNumber, myWriteAPIKey);
  70.         Serial.println(String("Post rc=")+rc);
  71.     }
  72. }
  73.  
  74.  
  75. void setup ()
  76. {
  77.     Serial.begin(115200);
  78.  
  79.     /* Must Do Wifi setup first... */
  80. ...
  81.     /* Wifi should be ready after this */
  82. ...
  83.     ThingSpeak.begin(client);   // initialize ThingSpeak lib
  84. ...
  85.     findDeviceAddr();   // locate OneWire devices on the bus
  86. ...
  87. }
  88.  
  89. void loop ()
  90. {
  91. ...
  92.             if (itsTimeToSample)
  93.                 sampleTemperature();
  94. ...
  95. }
Add Comment
Please, Sign In to add comment