Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <SoftwareSerial.h>
- #define RX 10
- #define TX 11
- SoftwareSerial esp(RX, TX);
- //cmd
- int cTrCmd;
- int cTmCmd;
- boolean f = false;
- //Wifi
- String W_SSID = "TP-LINK_F1D01E";
- String W_PASS = "4188370026563794";
- //ThingSpeak
- String API_KEY = "XMISMZZSMUYC33E9";
- String THING_IP = "api.thingspeak.com";
- String PORT = "80";
- void setup() {
- Serial.begin(9600);
- esp.begin(115200);
- debug();
- connectWiFi();
- }
- void loop() {
- delay(13000);
- sendData();
- }
- void sendData() {
- sendCmd("AT+CIPMUX=1","OK");
- String cmd = "AT+CIPSTART=0,\"TCP\",\"" + THING_IP + "\"," + PORT;
- sendCmd(cmd, "OK");
- int i = random(100);
- String getMethod = "GET /update?key=" + API_KEY + "&field1=" + i + " \r\n";
- sendCmd("AT+CIPSEND=0," + String(getMethod.length() + 4), ">");
- sendCmd(getMethod);
- delay(2000);
- sendCmd("AT+CIPCLOSE=0", "OK");
- }
- void connectWiFi() {
- if (sendCmd("AT+CWMODE=1", "OK")) {
- Serial.println("----> Module mod set to Station");
- }
- if (sendCmd("AT+CWJAP=\"" + W_SSID + "\",\"" + W_PASS + "\"", "OK")) {
- Serial.println("----> Connected to WiFI: " + W_SSID);
- } else {
- Serial.println("----> Connection failed: " + W_SSID);
- }
- }
- void debug() {
- if (sendCmd("AT", "OK")) {
- Serial.println("----> Module connection DONE");
- } else {
- Serial.println("----> Module connection FAILED");
- }
- }
- boolean sendCmd(String cmd, char repl[]) {
- Serial.print(cTrCmd);
- Serial.print(". Command: " + cmd + " ");
- while (cTmCmd < 20) {
- esp.println(cmd);
- if (esp.find(repl)) {
- f = true;
- break;
- }
- cTmCmd++;
- }
- if (f == true) {
- Serial.println("DONE");
- cTrCmd++;
- cTmCmd = 0;
- }
- if (f == false) {
- Serial.println("FAIL");
- cTrCmd = 0;
- cTmCmd = 0;
- }
- boolean f_l = f;
- f = false;
- return f_l;
- }
- boolean sendCmd(String cmd) {
- Serial.print(cTrCmd);
- Serial.println(". Command: " + cmd + " ");
- esp.println(cmd);
- cTrCmd++;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement