PlatformIO package of the Teensy core framework compatible with GCC 10 & C++20
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

164 lines
5.1KB

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