// The code includes reading the voltage from the temperature sensor (pin A0),
//calculating the temperature and sending the data to ThingSpeak using the ESP8266 module.
#include <SoftwareSerial.h>
SoftwareSerial esp8266(2, 3); // RX, TX virtual
// Access Point Connection Data
String AP = "HPY413"; // Replaced with SSID: HPY413
String PASS = "HpyN0!@#"; // Replaced with WiFi password: HpyN0!@#
// ThingSpeak parameters for the first account
String API1 = "0BZ0QZFDWLLCZFVY"; // my Write API key 0BZ0QZFDWLLCZFVY
String field1 = "field1";
String field2 = "field2";
// ThingSpeak parameters for the second account
String API2 = "OI39DD1R52C0ZEHC"; // second Write API key OI39DD1R52C0ZEHC
String field4 = "field4";
String HOST = "api.thingspeak.com";
String PORT = "80";
// Global variables
int retransmissions_counter;
boolean response_found = false;
int sensorValue;
void setup() {
Serial.begin(9600); // Communication between PC and Arduino using the native serial port at 9600 baud
esp8266.begin(115200); // Communication between Arduino and ESP8266 using the virtual serial port at 115200 baud
// Check the connection
sendCommand("AT", 5, "OK");
delay(2000);
// Set the WiFi mode
sendCommand("AT+CWMODE=1", 5, "OK");
delay(2000);
// Connect to the wireless Access Point (AP)
sendCommand("AT+CWJAP=\"" + AP + "\",\"" + PASS + "\"", 20, "OK");
delay(2000);
}
void loop() {
int sensorValue = analogRead(A0); // Read the value of the sensor 0-1023
float voltage = sensorValue * (5.0 / 1023.0); // Convert it to Voltage 0-5 V
float temperatureC = (voltage * 100.0) - 273.2; // Calculate Temperature
Serial.println(temperatureC); // Print Temperature
// Send data to the first ThingSpeak account
sendDataToThingSpeak(API1, field1, voltage);
delay(15000);
sendDataToThingSpeak(API1, field2, temperatureC);
delay(15000);
// Send data to the second ThingSpeak account
sendDataToThingSpeak(API2, field4, temperatureC);
delay(15000); // Delay between successive packet transmissions
}
void sendDataToThingSpeak(String API, String field, float value) {
// The GET command is used to send data to the ThingSpeak channel
String getData = "GET /update?api_key=" + API + "&" + field + "=" + String(value);
//String getData = "GET /update?api_key=" + API + "&" + field1 + "=" + String(value1) + + "&" + field2 + "=" + String(value2);
// Enable multiple connections
sendCommand("AT+CIPMUX=1", 5, "OK");
delay(2000);
// Establish a TCP connection with the ThingSpeak platform
sendCommand("AT+CIPSTART=0,\"TCP\",\"" + HOST + "\"," + PORT, 15, "OK");
delay(2000);
// Send the data
sendCommand("AT+CIPSEND=0," + String(getData.length() + 4), 4, ">");
esp8266.println(getData);
delay(5000);
// Close the TCP connection
sendCommand("AT+CIPCLOSE=0", 5, "OK");
}
void sendCommand(String command, int maxRetransmissions, char expectedResponse[]) {
Serial.print(command);
Serial.print(" ");
while (retransmissions_counter < maxRetransmissions) {
esp8266.println(command);
if (esp8266.find(expectedResponse)) {
response_found = true;
break;
}
retransmissions_counter++;
}
if (response_found == true) {
Serial.println("Success!");
retransmissions_counter = 0;
} else if (response_found == false) {
Serial.println("Fail!");
retransmissions_counter = 0;
}
response_found = false;
}