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.

44 lines
1.4 KiB

3 years ago
3 years ago
3 years ago
  1. #include <iostream>
  2. #include <sstream>
  3. #include <fstream>
  4. #include "network/SocketClient.h"
  5. #define ADC_PATH "/sys/bus/iio/devices/iio:device0/in_voltage"
  6. #define ADC 0
  7. using namespace std;
  8. using namespace exploringBB;
  9. float getTemperature(int adc_value) { // from the TMP36 datasheet
  10. float cur_voltage = adc_value * (1.80f/4096.0f); // Vcc = 1.8V, 12-bit
  11. float diff_degreesC = (cur_voltage-0.75f)/0.01f;
  12. return (25.0f + diff_degreesC);
  13. }
  14. int readAnalog(int number){
  15. stringstream ss;
  16. ss << ADC_PATH << number << "_raw";
  17. fstream fs;
  18. fs.open(ss.str().c_str(), fstream::in);
  19. fs >> number;
  20. fs.close();
  21. return number;
  22. }
  23. int main() {
  24. ostringstream head, data;
  25. cout << "Starting ThingSpeak Example" << endl;
  26. SocketClient sc("api.thingspeak.com",80);
  27. data << "field1=" << getTemperature(readAnalog(ADC)) << endl;
  28. cout << "Sending the temperature: " << getTemperature(readAnalog(ADC)) << endl;
  29. sc.connectToServer();
  30. head << "POST /update HTTP/1.1\n"
  31. << "Host: api.thingspeak.com\n"
  32. << "Connection: close\n"
  33. << "X-THINGSPEAKAPIKEY: 621LKPK58GD7PHSB\n"
  34. << "Content-Type: application/x-www-form-urlencoded\n"
  35. << "Content-Length:" << string(data.str()).length() << "\n\n";
  36. sc.send(string(head.str()));
  37. sc.send(string(data.str()));
  38. string rec = sc.receive(1024);
  39. cout << "[" << rec << "]" << endl;
  40. cout << "End of ThingSpeak Example" << endl;
  41. }