PlatformIO package of the Teensy core framework compatible with GCC 10 & C++20
Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

172 Zeilen
5.4KB

  1. /*
  2. Xively sensor client
  3. This sketch connects an analog sensor to Xively (http://www.xively.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 Xively.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 0
  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/XivelyClient
  17. This code is in the public domain.
  18. */
  19. #include <SPI.h>
  20. #include <Ethernet.h>
  21. #define APIKEY "YOUR API KEY GOES HERE" // replace your xively 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. };
  30. // fill in an available IP address on your network here,
  31. // for manual configuration:
  32. IPAddress ip(10, 0, 1, 20);
  33. // initialize the library instance:
  34. EthernetClient client;
  35. // if you don't want to use DNS (and reduce your sketch size)
  36. // use the numeric IP instead of the name for the server:
  37. IPAddress server(216, 52, 233, 122); // numeric IP for api.xively.com
  38. //char server[] = "api.xively.com"; // name address for xively API
  39. unsigned long lastConnectionTime = 0; // last time you connected to the server, in milliseconds
  40. boolean lastConnected = false; // state of the connection last time through the main loop
  41. const unsigned long postingInterval = 10*1000; //delay between updates to Xively.com
  42. void setup() {
  43. // You can use Ethernet.init(pin) to configure the CS pin
  44. //Ethernet.init(10); // Most Arduino shields
  45. //Ethernet.init(5); // MKR ETH shield
  46. //Ethernet.init(0); // Teensy 2.0
  47. //Ethernet.init(20); // Teensy++ 2.0
  48. //Ethernet.init(15); // ESP8266 with Adafruit Featherwing Ethernet
  49. //Ethernet.init(33); // ESP32 with Adafruit Featherwing Ethernet
  50. // Open serial communications and wait for port to open:
  51. Serial.begin(9600);
  52. while (!Serial) {
  53. ; // wait for serial port to connect. Needed for native USB port only
  54. }
  55. // start the Ethernet connection:
  56. if (Ethernet.begin(mac) == 0) {
  57. Serial.println("Failed to configure Ethernet using DHCP");
  58. // DHCP failed, so use a fixed IP address:
  59. Ethernet.begin(mac, ip);
  60. }
  61. }
  62. void loop() {
  63. // read the analog sensor:
  64. int sensorReading = analogRead(A0);
  65. // if there's incoming data from the net connection.
  66. // send it out the serial port. This is for debugging
  67. // purposes only:
  68. if (client.available()) {
  69. char c = client.read();
  70. Serial.print(c);
  71. }
  72. // if there's no net connection, but there was one last time
  73. // through the loop, then stop the client:
  74. if (!client.connected() && lastConnected) {
  75. Serial.println();
  76. Serial.println("disconnecting.");
  77. client.stop();
  78. }
  79. // if you're not connected, and ten seconds have passed since
  80. // your last connection, then connect again and send data:
  81. if(!client.connected() && (millis() - lastConnectionTime > postingInterval)) {
  82. sendData(sensorReading);
  83. }
  84. // store the state of the connection for next time through
  85. // the loop:
  86. lastConnected = client.connected();
  87. }
  88. // this method makes a HTTP connection to the server:
  89. void sendData(int thisData) {
  90. // if there's a successful connection:
  91. if (client.connect(server, 80)) {
  92. Serial.println("connecting...");
  93. // send the HTTP PUT request:
  94. client.print("PUT /v2/feeds/");
  95. client.print(FEEDID);
  96. client.println(".csv HTTP/1.1");
  97. client.println("Host: api.xively.com");
  98. client.print("X-XivelyApiKey: ");
  99. client.println(APIKEY);
  100. client.print("User-Agent: ");
  101. client.println(USERAGENT);
  102. client.print("Content-Length: ");
  103. // calculate the length of the sensor reading in bytes:
  104. // 8 bytes for "sensor1," + number of digits of the data:
  105. int thisLength = 8 + getLength(thisData);
  106. client.println(thisLength);
  107. // last pieces of the HTTP PUT request:
  108. client.println("Content-Type: text/csv");
  109. client.println("Connection: close");
  110. client.println();
  111. // here's the actual content of the PUT request:
  112. client.print("sensor1,");
  113. client.println(thisData);
  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. }