commit c16f5a928424980ff77e72ae4ed944c018ebc132 Author: Gerardo Marx Date: Tue Jun 22 15:33:26 2021 +0000 initial commit diff --git a/build b/build new file mode 100755 index 0000000..88ab826 --- /dev/null +++ b/build @@ -0,0 +1,2 @@ +#!/bin/bash +g++ thingSpeak.cpp network/SocketClient.cpp -o thingSpeak diff --git a/example_crontab/crontab-backup b/example_crontab/crontab-backup new file mode 100644 index 0000000..c73d8a0 --- /dev/null +++ b/example_crontab/crontab-backup @@ -0,0 +1,23 @@ +# Edit this file to introduce tasks to be run by cron. +# +# Each task to run has to be defined through a single line +# indicating with different fields when the task will be run +# and what command to run for the task +# +# To define the time you can provide concrete values for +# minute (m), hour (h), day of month (dom), month (mon), +# and day of week (dow) or use '*' in these fields (for 'any').# +# Notice that tasks will be started based on the cron's system +# daemon's notion of time and timezones. +# +# Output of the crontab jobs (including errors) is sent through +# email to the user the crontab file belongs to (unless redirected). +# +# For example, you can run a backup of all your user accounts +# at 5 a.m every week with: +# 0 5 * * 1 tar -zcf /var/backups/home.tgz /home/ +# +# For more information see the manual pages of crontab(5) and cron(8) +# +# m h dom mon dow command +*/30 * * * * ~/exploringBB/chp11/thingSpeak/thingSpeak > /dev/null 2>&1 diff --git a/network/SocketClient.cpp b/network/SocketClient.cpp new file mode 100644 index 0000000..52ed19c --- /dev/null +++ b/network/SocketClient.cpp @@ -0,0 +1,72 @@ +#include "SocketClient.h" +#include +#include +#include +using namespace std; + +namespace exploringBB { + +SocketClient::SocketClient(std::string serverName, int portNumber) { + this->socketfd = -1; + this->server = NULL; + this->serverName = serverName; + this->portNumber = portNumber; + this->isConnected = false; +} + +int SocketClient::connectToServer(){ + socketfd = socket(AF_INET, SOCK_STREAM, 0); + if (socketfd < 0){ + perror("Socket Client: error opening socket.\n"); + return 1; + } + server = gethostbyname(serverName.data()); + if (server == NULL) { + perror("Socket Client: error - no such host.\n"); + return 1; + } + bzero((char *) &serverAddress, sizeof(serverAddress)); + serverAddress.sin_family = AF_INET; + bcopy((char *)server->h_addr,(char *)&serverAddress.sin_addr.s_addr, server->h_length); + serverAddress.sin_port = htons(portNumber); + + if (connect(socketfd, (struct sockaddr *) &serverAddress, sizeof(serverAddress)) < 0){ + perror("Socket Client: error connecting to the server.\n"); + return 1; + } + this->isConnected = true; + return 0; +} + +int SocketClient::send(std::string message){ + const char *writeBuffer = message.data(); + int length = message.length(); + int n = write(this->socketfd, writeBuffer, length); + if (n < 0){ + perror("Socket Client: error writing to socket"); + return 1; + } + return 0; +} + +string SocketClient::receive(int size=1024){ + char readBuffer[size]; + int n = read(this->socketfd, readBuffer, sizeof(readBuffer)); + if (n < 0){ + perror("Socket Client: error reading from socket"); + } + return string(readBuffer); +} + +int SocketClient::disconnectFromServer(){ + this->isConnected = false; + close(this->socketfd); + return 0; +} + +SocketClient::~SocketClient() { + if (this->isConnected == true){ + disconnectFromServer(); + } +} +} diff --git a/network/SocketClient.h b/network/SocketClient.h new file mode 100644 index 0000000..9b00609 --- /dev/null +++ b/network/SocketClient.h @@ -0,0 +1,33 @@ +#ifndef SOCKETCLIENT_H_ +#define SOCKETCLIENT_H_ + +#include +#include +#include +#include +#include + +namespace exploringBB { + +class SocketClient { + +private: + int socketfd; + struct sockaddr_in serverAddress; + struct hostent *server; + std::string serverName; + int portNumber; + bool isConnected; + +public: + SocketClient(std::string serverName, int portNumber); + virtual int connectToServer(); + virtual int disconnectFromServer(); + virtual int send(std::string message); + virtual std::string receive(int size); + bool isClientConnected() { return this->isConnected; } + virtual ~SocketClient(); +}; + +} +#endif /* SOCKETCLIENT_H_ */ diff --git a/thingSpeak b/thingSpeak new file mode 100755 index 0000000..a20386a Binary files /dev/null and b/thingSpeak differ diff --git a/thingSpeak.cpp b/thingSpeak.cpp new file mode 100644 index 0000000..779c2fc --- /dev/null +++ b/thingSpeak.cpp @@ -0,0 +1,44 @@ +#include +#include +#include +#include "network/SocketClient.h" +#define ADC_PATH "/sys/bus/iio/devices/iio:device0/in_voltage" +#define ADC 0 +using namespace std; +using namespace exploringBB; + +float getTemperature(int adc_value) { // from the TMP36 datasheet + float cur_voltage = adc_value * (1.80f/4096.0f); // Vcc = 1.8V, 12-bit + float diff_degreesC = (cur_voltage-0.75f)/0.01f; + return (25.0f + diff_degreesC); +} + +int readAnalog(int number){ + stringstream ss; + ss << ADC_PATH << number << "_raw"; + fstream fs; + fs.open(ss.str().c_str(), fstream::in); + fs >> number; + fs.close(); + return number; +} + +int main() { + ostringstream head, data; + cout << "Starting ThingSpeak Example" << endl; + SocketClient sc("api.thingspeak.com",80); + data << "field1=" << getTemperature(readAnalog(ADC)) << endl; + cout << "Sending the temperature: " << getTemperature(readAnalog(ADC)) << endl; + sc.connectToServer(); + head << "POST /update HTTP/1.1\n" + << "Host: api.thingspeak.com\n" + << "Connection: close\n" + << "X-THINGSPEAKAPIKEY: Z7PNWRYKDEBRX5F7\n" + << "Content-Type: application/x-www-form-urlencoded\n" + << "Content-Length:" << string(data.str()).length() << "\n\n"; + sc.send(string(head.str())); + sc.send(string(data.str())); + string rec = sc.receive(1024); + cout << "[" << rec << "]" << endl; + cout << "End of ThingSpeak Example" << endl; +}