0% found this document useful (0 votes)
97 views2 pages

Esp 8266

This Arduino sketch controls two actuators (a pump and a lamp) based on commands received from ThingSpeak every 10 seconds. It initializes serial communication and the actuators, then enters a loop where it reads the most recent commands for each actuator channel, takes the appropriate action to turn the actuators on or off, and resets the timing to read again in 10 seconds. If an error occurs, it prints a message and restarts the loop.

Uploaded by

Hendra Sondrett
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
97 views2 pages

Esp 8266

This Arduino sketch controls two actuators (a pump and a lamp) based on commands received from ThingSpeak every 10 seconds. It initializes serial communication and the actuators, then enters a loop where it reads the most recent commands for each actuator channel, takes the appropriate action to turn the actuators on or off, and resets the timing to read again in 10 seconds. If an error occurs, it prints a message and restarts the loop.

Uploaded by

Hendra Sondrett
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

String canalID1 = "777384"; //Actuator1

String canalID2 = "792893"; //Actuator2

#include <SoftwareSerial.h>
SoftwareSerial EspSerial(2, 3); // Rx, Tx

// Variables to be used with timers


long readTimingSeconds = 10; // ==> Define Sample time in seconds to receive data
long startReadTiming = 0;
long elapsedReadTime = 0;

//Relays
#define ACTUATOR1 10 // RED LED ==> Pump
#define ACTUATOR2 12 // GREEN LED ==> Lamp
boolean pump = 0;
boolean lamp = 0;

int spare = 0;
boolean error;

void setup()
{
Serial.begin(115200);

pinMode(ACTUATOR1,OUTPUT);
pinMode(ACTUATOR2,OUTPUT);

digitalWrite(ACTUATOR1, HIGH);
digitalWrite(ACTUATOR2, HIGH);

EspSerial.begin(115200); // Comunicacao com Modulo WiFi


EspHardwareReset();
startReadTiming = millis(); // starting the "program clock"
}

void loop()
{
start:
error=0;

elapsedReadTime = millis()-startReadTiming;

if (elapsedReadTime > (readTimingSeconds*1000))


{
int command = readThingSpeak(canalID1);
if (command != 9) pump = command;
delay (5000);
command = readThingSpeak(canalID2);
if (command != 9) lamp = command;
takeActions();
startReadTiming = millis();
}

if (error==1)
{
Serial.println(" <<<< ERROR >>>>");
delay (2000);
goto start; //go to label "start"
}
}

/********* Take actions based on ThingSpeak Commands *************/


void takeActions(void)
{
Serial.print("Pump: ");
Serial.println(pump);
Serial.print("Lamp: ");
Serial.println(lamp);
if (pump == 1) digitalWrite(ACTUATOR1, LOW);
else digitalWrite(ACTUATOR1, HIGH);
if (lamp == 1) digitalWrite(ACTUATOR2, LOW);
else digitalWrite(ACTUATOR2, HIGH);
}

/********* Read Actuators command from ThingSpeak *************/


int readThingSpeak(String channelID)
{
startThingSpeakCmd();
int command;
String getStr = "GET /channels/792893";
getStr +="/fields/1/last";
getStr += "\r\n";
String messageDown = sendThingSpeakGetCmd(getStr);
if (messageDown[5] == 49)
{
command = messageDown[7]-48;
Serial.print("Command received: ");
Serial.println(command);
}
else command = 9;
return command;
}

You might also like