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

Esp 4

This code configures an ESP8266 module to connect to a WiFi network and send sensor data to Thingspeak using HTTP GET requests. It defines the WiFi credentials, API key, and host/port. In setup() it initializes serial and WiFi communication. In loop() it gets sensor data, builds the GET request URL, sends the request, and closes the connection. It uses a sendCommand() function to send AT commands and check for responses.

Uploaded by

prasun2k
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)
35 views2 pages

Esp 4

This code configures an ESP8266 module to connect to a WiFi network and send sensor data to Thingspeak using HTTP GET requests. It defines the WiFi credentials, API key, and host/port. In setup() it initializes serial and WiFi communication. In loop() it gets sensor data, builds the GET request URL, sends the request, and closes the connection. It uses a sendCommand() function to send AT commands and check for responses.

Uploaded by

prasun2k
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

#include <SoftwareSerial.

h>
#define RX 2
#define TX 3
String AP = "ZTE-5jHykc"; // AP NAME
String PASS = "gsucskkc"; // AP PASSWORD
String API = "X356ZSIAP7VM8URT"; // Write API KEY
String HOST = "api.thingspeak.com";
String PORT = "80";
String field = "field1";
int countTrueCommand;
int countTimeCommand;
boolean found = false;
int valSensor = 1;
SoftwareSerial esp8266(RX,TX);

void setup() {
Serial.begin(9600);
esp8266.begin(115200);
sendCommand("AT",5,"OK");
sendCommand("AT+CWMODE=1",5,"OK");
sendCommand("AT+CWJAP=\""+ AP +"\",\""+ PASS +"\"",20,"OK");
}

void loop() {
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,">");
esp8266.println(getData);delay(1500);countTrueCommand++;
sendCommand("AT+CIPCLOSE=0",5,"OK");
}

int getSensorData(){
return random(1000); // Replace with your own sensor code
}

void sendCommand(String command, int maxTime, char readReplay[]) {


Serial.print(countTrueCommand);
Serial.print(". at command => ");
Serial.print(command);
Serial.print(" ");
while(countTimeCommand < (maxTime*1))
{
esp8266.println(command);//at+cipsend
if(esp8266.find(readReplay))//ok
{
found = true;
break;
}

countTimeCommand++;
}

if(found == true)
{
Serial.println("OYI");
countTrueCommand++;
countTimeCommand = 0;
}

if(found == false)
{
Serial.println("Fail");
countTrueCommand = 0;
countTimeCommand = 0;
}

found = false;
}

You might also like