From c16f5a928424980ff77e72ae4ed944c018ebc132 Mon Sep 17 00:00:00 2001 From: Gerardo Marx Date: Tue, 22 Jun 2021 15:33:26 +0000 Subject: [PATCH] initial commit --- build | 2 + example_crontab/crontab-backup | 23 +++++++++++ network/SocketClient.cpp | 72 +++++++++++++++++++++++++++++++++ network/SocketClient.h | 33 +++++++++++++++ thingSpeak | Bin 0 -> 21156 bytes thingSpeak.cpp | 44 ++++++++++++++++++++ 6 files changed, 174 insertions(+) create mode 100755 build create mode 100644 example_crontab/crontab-backup create mode 100644 network/SocketClient.cpp create mode 100644 network/SocketClient.h create mode 100755 thingSpeak create mode 100644 thingSpeak.cpp 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 0000000000000000000000000000000000000000..a20386a3c25ae53e742cecb91711acfc7166783f GIT binary patch literal 21156 zcmeHPe|%KcmA{jjBm+hXKcoQ>N1&F9nPfAg5L}Tcl-ck1-uEVV zUP4Oj?jO6KT(~*s+;i@^=bn4so%im$_iZa#>$TZz!c2BCUy%CG$wCwY9$tq(4v}6c zTw(%vSBTlybw%J#Z}6yV>)>w!Z(kp}Kt5ovdqSprfW;lN8%7E|SQxeAcA?9PvjsVx%d6 zdoA=1({udv8x zTKT3ifG*?(mhnDnk>_=1CKr&UCILwLfF?f&^i)fK1g`5?yrF)<0$$G73ZHLdb0qAG z#r@H^&nJAPRqK2WfoNc3C>9SytJbY*ii87I{<@}s;*HFyDX)w#_WA1D+B_c5!a9E} zR1cNWPEVxKz|h#h8bu7L8Iu{P&*Afx zMq&Bm zG}>QTf@mv*GkqJ|+I%g6Xe<);H-+L`eVg-D2wsR0g%J!dLwAJnW*DbB*@&yEo69}< za_&GpZ1#9Kk2LvXF<*RZOTZTjH%53F%bDT4bIAzvgApK$eH+ToKRKbxm_w>Oz7jcA z49zm5xmpDWCOf|m5es7j{%~uH$V_d*YTQ<>6m_vt`RlUTaMGDN0Pl8*cQbZ}Irl*)EU zP*zdo@*hjmg;Un%U9Gw$Vkz&DZK4vt>HOVyA%FI&t+)wxiaz6ap8PhAKP?oxm`@q+ zC;91+Q1t=CZ})D3`b=@osI&E@z0j8-CS)p^@9r&i@(zBwLB~IRA85PyltJG<$UEXR zQ2?5W&o7?V>^9UyjF`p0~yM{0e&uoP7cp)L4Q;G;NSjz};gSwm$H=4zSQ-xSVh=-%KgqI3Y zK{y@Hk%Z`QHDQhrw-DlivX*eB5DkQv2@xcmB}5b9<#_%hyJo=cw~ z%on1I5En%^A^iO;A^hxIp8MzT2+`~8Jn*)oC-=Cc=h5Sr^xWIyJn`J|3Kt^sC-=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BErCj15D||Ua%_t0^)M%TT4qq?C8<&`r U(nAVinIiY2wa~>zjAWw!4QU;zrvLx| literal 0 HcmV?d00001 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; +}