Go Down

Topic: Arduino DUE + ESP8266 + ThingSpeak  (Read 304 times) previous topic - next topic

johnnye

Can someone help me connect Arduino DUE to the ThingSpeak Cloud Platform via esp8266? I'm trying to read and analyze the voltage data provided on my serial Monitor on the ThingSpeak channel.


Here's the code that I have so far:

#define ESP8266 Serial3
#define RX3 15
#define TX3 14

String AP = "XXXXXX";     
String PASS = "XXXXXXXXX";

String API = "XXXXXXXXXXXXXXXXXX";
String HOST = "api.thingspeak.com";
String PORT = "80";
String field = "field1";

int countTrueCommand;
int countTimeCommand;
boolean found = true;
int valSensor = 1;
const float referenceVolts = 3.3;
const int batteryPin = 0; 

void setup() {
  Serial.begin(9600);
  Serial3.begin(9600);
  sendCommand("AT",5,"OK");
  sendCommand("AT+CWMODE=1",5,"OK");
  sendCommand("AT+CWJAP=\""+ AP +"\",\""+ PASS +"\"",20,"OK");
}
void loop() {
 int val = analogRead(batteryPin);
 analogWrite(DAC0, 300);
 analogWrite(DAC1, 800);
 float volts = (val / 1023.0) * referenceVolts;
 valSensor = getSensorData();
 String getData = "GET /update?api_key="+ API +"&"+ field +"="+String(valSensor);
 
 sendCommand("AT+CIPMUX=1",5,"OK");
 sendCommand("AT+CIPSTART=0,\"TCP\",\""+ HOST +"\","+ PORT,15,"OK");
 sendCommand("AT+CIPSEND=0," +String(getData.length()+4),4,">");
 Serial3.println(getData);
 delay(1500);
 countTrueCommand++;
 sendCommand("AT+CIPCLOSE=0",5,"OK");
}
int getSensorData(){
  return random(1000);
}
void sendCommand(String command, int maxTime, char readReplay[]) {
  Serial.print(countTrueCommand);
  Serial.print(". at command => ");
  Serial.print(command);
  Serial.print(" ");
  while(countTimeCommand < (maxTime*1))
  {
    Serial3.println(command);//at+cipsend
    if(Serial3.find(readReplay))//ok
    {
      found = true;
      break;
    }
 
    countTimeCommand++;
  }
 
  if(found == true)
  {
    Serial.println("SUCCESS");
    countTrueCommand++;
    countTimeCommand = 0;
  }
 
  if(found == false)
  {
    Serial.println("Fail");
    countTrueCommand = 0;
    countTimeCommand = 0;
  }
 }

ard_newbie

There are lots of tutorials with a UNO and an ESP8266 to interface with ThingSpeak. Replace Software serial by an hardware serial and you are done.

https://medium.com/@cgrant/using-the-esp8266-wifi-module-with-arduino-uno-publishing-to-thingspeak-99fc77122e82


johnnye

Isn't that what I did? replacing SoftwareSerial with Serial3.

Go Up