#include //needed library for servo control #include "dht.h" //needed library for DHT11 sensor. Should be downloaded online and placed in the same directory as this Arduino code #define DHT11_PIN 8 //Connect DHT11 data line to Arduino pin 8 String AP = "D_LINK1234"; // SSID name, enter your own network name String PASS = "Abracadabra@2"; // and enter your network Password String REG_KEY = "witjmvmox0x9"; // hardware registration key on takano (do not confuse with user registration key!) String HOST = "iotsprint.com"; //website Arduino should send http request to String PORT = "80"; //default port String string_number_of_characters_to_send; String command_to_send; int number_of_characters_to_send; int countTrueCommand; int countTimeCommand; int steps; boolean found = false; int tempPin = 1; int timeout = 0; int first, last, first1, last1; String stringT2, stringT3, stringH2, stringH3, stringL2, stringL3, data, content; Servo myservo; // create servo object to control a servo dht DHT; int photoresistor = A0; //Photoresistor connected to A0 Arduino analogue pin int light = 0; // variable to store the value read on A0 int pos; void negotiate_modem() //sequence of AT commands from Arduino to ESP8266 { //this function allows the Arduino to connect to the router sendCommand("AT+CWMODE=1", 50, "OK"); sendCommand("AT+CIPMUX=1", 50, "OK"); sendCommand("AT+CWJAP=\"" + AP + "\",\"" + PASS + "\"", 150, "OK"); } boolean isNumeric(String str) { //function used to test if a string can be converted to a number unsigned int stringLength = str.length(); if (stringLength == 0) { return false; } boolean seenDecimal = false; for(unsigned int i = 0; i < stringLength; ++i) { if (isDigit(str.charAt(i))) { continue; } if (str.charAt(i) == '.') { if (seenDecimal) { return false; } seenDecimal = true; continue; } return false; } return true; } void setup(){ // open the serial port at 9600 bps: MAKE SURE ESP8266 HAS SAME BAUD RATE! Serial.begin(9600); } void loop() { data = ""; content = ""; int chk = DHT.read11(DHT11_PIN); //read DHT11 data light = analogRead(photoresistor); //read the photoresistor if (found == false) negotiate_modem(); //if Arduino did not get a proper string back from cloud, then try to connect to modem again. float float_temp = DHT.temperature; stringT2 = String(float_temp); stringT3= String(stringT2); if (stringT2.length() == 5) stringT3 = stringT2.substring(0, stringT2.length() - 1); //trim it to 4 characters if (stringT3.length() < 2) stringT3 = "000" + stringT3; //to make sure it has 4 characters length else if (stringT3.length() < 3) stringT3 = "00" + stringT3; //to make sure it has 4 characters length else if (stringT3.length() < 4) stringT3 = "0" + stringT3; //to make sure it has 4 characters length stringT3 = "T=" + stringT3; // stringT3 = “T=23.4” for example float float_humi = DHT.humidity; stringH2 = String(float_humi); stringH3= String(stringH2); if (stringH2.length() == 5) stringH3 = stringH2.substring(0, stringH2.length() - 1); //trim it to 4 characters if (stringH3.length() < 2) stringH3 = "000" + stringH3; //to make sure it has 4 characters length else if (stringH3.length() < 3) stringH3 = "00" + stringH3; //to make sure it has 4 characters length else if (stringH3.length() < 4) stringH3 = "0" + stringH3; //to make sure it has 4 characters length stringH3 = "H=" + stringH3; // stringH3 = “H=64.5” for example stringL2 = String(light); stringL3= String(stringL2); if (stringL2.length() == 5) stringL3 = stringL2.substring(0, stringL2.length() - 1); //trim it to 4 characters if (stringL3.length() < 2) stringL3 = "000" + stringL3; //to make sure it has 4 characters length else if (stringL3.length() < 3) stringL3 = "00" + stringL3; //to make sure it has 4 characters length else if (stringL3.length() < 4) stringL3 = "0" + stringL3; //to make sure it has 4 characters length stringL3 = "L=" + stringL3; // stringH3 = “L=0456” for example String getData = "GET /tk.php?r=" + REG_KEY + "&m=" + stringT3 + "," + stringH3 + "," + stringL3 + " HTTP/1.0"; //String getData = "GET /tk.php?r=witjmvmox0x9&m=T=23.4,H=64.5,L=0456 HTTP/1.0"; //58 +2 sendCommand("AT+CIPSTART=4,\"TCP\",\"iotsprint.com\",80", 10, "OK"); number_of_characters_to_send= getData.length()+27; //unlike previous Arduino examples, here we automatically //calculate the number of characters that ESP8266 is to send to the cloud string_number_of_characters_to_send= ""; string_number_of_characters_to_send = String(number_of_characters_to_send); command_to_send= "AT+CIPSEND=4," + string_number_of_characters_to_send; sendCommand(command_to_send, 10, ">"); Serial.println(getData); delay(2000); countTrueCommand++; read_cloud("Host:iotsprint.com:80"); delay(3000); //wait 3seconds before rehearsing } void read_cloud(String command) { data = ""; Serial.println(command); Serial.println(); timeout = 0; while (Serial.available() == 0) { if ( ++timeout > 10000) { // set this to your timeout value in milliseconds break; } } timeout = 0; // got a char so reset timeout content = Serial.readString(); // read the incoming data as string if ((content.indexOf("M=") >= 0) && (content.indexOf("M=") <= 180)) //servo angle value is between 0 and 180 degrees { first = content.indexOf("M=") + 2; last = content.lastIndexOf("4,CLOSE"); data = content.substring(first, last); if (isNumeric(data)) { myservo.attach(2); //servo is connected to pin 2 steps= data.toInt(); myservo.write(steps); // tell servo to go to position in variable ‘steps’ delay(1000); // waits 1000ms for the servo to reach the position myservo.detach(); //unconnect servo to stop it. Please read about servo motors online to know more about their behavior.. } } } void sendCommand(String command, int maxTime, char readReplay[]) { found = false; while (countTimeCommand < (maxTime * 1)) { Serial.println(command); if (Serial.find(readReplay)) //ok { found = true; break; } countTimeCommand++; } if (found == true) { countTrueCommand++; countTimeCommand = 0; } if (found == false) { countTrueCommand = 0; countTimeCommand = 0; } }