PlatformIO package of the Teensy core framework compatible with GCC 10 & C++20
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

172 lines
5.4KB

  1. /*
  2. Pachube sensor client
  3. This sketch connects an analog sensor to Pachube (http://www.pachube.com)
  4. using a Wiznet Ethernet shield. You can use the Arduino Ethernet shield, or
  5. the Adafruit Ethernet shield, either one will work, as long as it's got
  6. a Wiznet Ethernet module on board.
  7. This example has been updated to use version 2.0 of the Pachube.com API.
  8. To make it work, create a feed with a datastream, and give it the ID
  9. sensor1. Or change the code below to match your feed.
  10. Circuit:
  11. * Analog sensor attached to analog in A0
  12. * Ethernet shield attached to pins 10, 11, 12, 13
  13. created 15 March 2010
  14. modified 9 Apr 2012
  15. by Tom Igoe with input from Usman Haque and Joe Saavedra
  16. http://arduino.cc/en/Tutorial/PachubeClient
  17. This code is in the public domain.
  18. */
  19. #include <SPI.h>
  20. #include <NativeEthernet.h>
  21. #define APIKEY "YOUR API KEY GOES HERE" // replace your pachube api key here
  22. #define FEEDID 00000 // replace your feed ID
  23. #define USERAGENT "My Project" // user agent is the project name
  24. // assign a MAC address for the ethernet controller.
  25. // Newer Ethernet shields have a MAC address printed on a sticker on the shield
  26. // fill in your address here:
  27. byte mac[] = {
  28. 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};
  29. // fill in an available IP address on your network here,
  30. // for manual configuration:
  31. IPAddress ip(10,0,1,20);
  32. // initialize the library instance:
  33. EthernetClient client;
  34. // if you don't want to use DNS (and reduce your sketch size)
  35. // use the numeric IP instead of the name for the server:
  36. IPAddress server(216,52,233,122); // numeric IP for api.pachube.com
  37. //char server[] = "api.pachube.com"; // name address for pachube API
  38. unsigned long lastConnectionTime = 0; // last time you connected to the server, in milliseconds
  39. boolean lastConnected = false; // state of the connection last time through the main loop
  40. const unsigned long postingInterval = 10*1000; //delay between updates to Pachube.com
  41. void setup() {
  42. // You can use Ethernet.init(pin) to configure the CS pin
  43. //Ethernet.init(10); // Most Arduino shields
  44. //Ethernet.init(5); // MKR ETH shield
  45. //Ethernet.init(0); // Teensy 2.0
  46. //Ethernet.init(20); // Teensy++ 2.0
  47. //Ethernet.init(15); // ESP8266 with Adafruit Featherwing Ethernet
  48. //Ethernet.init(33); // ESP32 with Adafruit Featherwing Ethernet
  49. // Open serial communications and wait for port to open:
  50. Serial.begin(9600);
  51. while (!Serial) {
  52. ; // wait for serial port to connect. Needed for Leonardo only
  53. }
  54. // start the Ethernet connection:
  55. if (Ethernet.begin(mac) == 0) {
  56. Serial.println("Failed to configure Ethernet using DHCP");
  57. // DHCP failed, so use a fixed IP address:
  58. Ethernet.begin(mac, ip);
  59. }
  60. }
  61. void loop() {
  62. // read the analog sensor:
  63. int sensorReading = analogRead(A0);
  64. // if there's incoming data from the net connection.
  65. // send it out the serial port. This is for debugging
  66. // purposes only:
  67. if (client.available()) {
  68. char c = client.read();
  69. Serial.print(c);
  70. }
  71. // if there's no net connection, but there was one last time
  72. // through the loop, then stop the client:
  73. if (!client.connected() && lastConnected) {
  74. Serial.println();
  75. Serial.println("disconnecting.");
  76. client.stop();
  77. }
  78. // if you're not connected, and ten seconds have passed since
  79. // your last connection, then connect again and send data:
  80. if(!client.connected() && (millis() - lastConnectionTime > postingInterval)) {
  81. sendData(sensorReading);
  82. }
  83. // store the state of the connection for next time through
  84. // the loop:
  85. lastConnected = client.connected();
  86. }
  87. // this method makes a HTTP connection to the server:
  88. void sendData(int thisData) {
  89. // if there's a successful connection:
  90. if (client.connect(server, 80)) {
  91. Serial.println("connecting...");
  92. // send the HTTP PUT request:
  93. client.print("PUT /v2/feeds/");
  94. client.print(FEEDID);
  95. client.println(".csv HTTP/1.1");
  96. client.println("Host: api.pachube.com");
  97. client.print("X-PachubeApiKey: ");
  98. client.println(APIKEY);
  99. client.print("User-Agent: ");
  100. client.println(USERAGENT);
  101. client.print("Content-Length: ");
  102. // calculate the length of the sensor reading in bytes:
  103. // 8 bytes for "sensor1," + number of digits of the data:
  104. int thisLength = 8 + getLength(thisData);
  105. client.println(thisLength);
  106. // last pieces of the HTTP PUT request:
  107. client.println("Content-Type: text/csv");
  108. client.println("Connection: close");
  109. client.println();
  110. // here's the actual content of the PUT request:
  111. client.print("sensor1,");
  112. client.println(thisData);
  113. }
  114. else {
  115. // if you couldn't make a connection:
  116. Serial.println("connection failed");
  117. Serial.println();
  118. Serial.println("disconnecting.");
  119. client.stop();
  120. }
  121. // note the time that the connection was made or attempted:
  122. lastConnectionTime = millis();
  123. }
  124. // This method calculates the number of digits in the
  125. // sensor reading. Since each digit of the ASCII decimal
  126. // representation is a byte, the number of digits equals
  127. // the number of bytes:
  128. int getLength(int someValue) {
  129. // there's at least one byte:
  130. int digits = 1;
  131. // continually divide the value by ten,
  132. // adding one to the digit count for each
  133. // time you divide, until you're at 0:
  134. int dividend = someValue /10;
  135. while (dividend > 0) {
  136. dividend = dividend /10;
  137. digits++;
  138. }
  139. // return the number of digits:
  140. return digits;
  141. }