Advertisement
Guest User

Untitled

a guest
Jul 18th, 2019
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.67 KB | None | 0 0
  1. #include <SoftwareSerial.h> //Software Serial library
  2. SoftwareSerial espSerial(2, 3);//Pin 2 and 3 act as RX and TX.
  3. #define DEBUG true
  4. String mySSID = "xxxx"; // WiFi SSID
  5. String myPWD = "yyyyyy"; // WiFi Password
  6. String myAPI = "xyxyxyyxxyyxxxyy"; // API Key
  7. String myHOST = "api.thingspeak.com";
  8. String myPORT = "80";
  9. String myFIELD = "field1";
  10. int sendVal;
  11.  
  12.  
  13. void setup()
  14. {
  15. Serial.begin(9600);
  16. espSerial.begin(115200);
  17. espData("AT+RST", 1000, DEBUG); //Reset the ESP8266 module
  18. espData("AT+CWMODE=1", 1000, DEBUG); //Set the ESP mode as station mode
  19. espData("AT+CWJAP=""+ mySSID +"",""+ myPWD +""", 1000, DEBUG);
  20.  
  21. delay(1000);
  22.  
  23. }
  24. void loop(){
  25. sendVal = random(1000); // Send a random number between 1 and 1000
  26. String sendData = "GET /update?api_key="+ myAPI +"&"+myFIELD+"="+String(sendVal);
  27. espData("AT+CIPMUX=1", 1000, DEBUG); //Allow multiple connections
  28. espData("AT+CIPSTART=0,"TCP",""+ myHOST +"","+ myPORT, 1000, DEBUG);
  29. espData("AT+CIPSEND=0," +String(sendData.length()+4),1000,DEBUG);
  30. espSerial.find(">");
  31. espSerial.println(sendData);
  32. Serial.print("Value to be sent: ");
  33. Serial.println(sendVal);
  34.  
  35. espData("AT+CIPCLOSE=0",1000,DEBUG);
  36. delay(10000);
  37. }
  38.  
  39. String espData(String command, const int timeout, boolean debug)
  40. {
  41. Serial.print("AT Command ==> ");
  42. Serial.print(command);
  43. Serial.println(" ");
  44.  
  45. String response = "";
  46. espSerial.println(command);
  47. long int time = millis();
  48. while ( (time + timeout) > millis())
  49. {
  50. while (espSerial.available())
  51. {
  52. char c = espSerial.read();
  53. response += c;
  54. }
  55. }
  56. if (debug)
  57. {
  58. //Serial.print(response);
  59. }
  60. return response;
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement