Thanks for your kindness!
Here is my code and I have problem at receiving motor speed(Mspeed) and switch function from Ubidots.
#include <DHT.h>
#define DHTPIN 2 // SDA pin setting
#define DHTTYPE DHT22 // DHT22 (AM2302) sensor type
DHT dht(DHTPIN, DHTTYPE);
// Motor control
int IN1 = 7;
int IN2 = 8;
int ENA = 6; // Motor speed control pin
int TempLimit = 30; // Temperature condition to run motor
int HumiLimit = 50; // Humidity condition to run motor
#define ssid "****" // Put here your SSID
#define password "****" // Put here your PASSWORD
#define id1 "*************" // Put here your Ubidots variable ID1 temp
#define id2 "*************" // Put here your Ubidots variable ID2 humi
#define id3 "*************" // Put here your Ubidots variable ID3 speed
#define id4 "*************" // Put here your Ubidots variable ID4 speed control
#define id5 "*************" // Put here your Ubidots variable ID5 switch
#define token "**************" // Put here your Ubidots TOKEN
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
Serial.println("\nModule Settings WiFi ESP8266");
pinMode(IN1,OUTPUT);
pinMode(IN2,OUTPUT);
sendCmd("AT+RESTORE", "R", 1000); // cmd AT+RESTORE: Restores the Factory Default Settings
sendCmd("AT+RST", "WiFi Module Ready!", 1000); // cmd AT+RST: Restarts the Module
sendCmd("AT+CWMODE=3", "Ap+Station Mode", 1000); // cmd AT+CWMODE=3: Sets the Wi-Fi Ap + Station Mode
sendCmd("AT+CWJAP=\""ssid"\",\""password"\"", "Successful WiFi Connection!", 5000); // cmd AT+CWJAP: Connects to an AP
sendCmd("AT+CIPMUX=1", "Enable Multiple Connections!", 500); // cmd AT+CIPMUX=1: Enable Multiple Connections
}
void loop() {
// put your main code here, to run repeatedly:
float dato1 = dht.readTemperature();
float dato2 = dht.readHumidity();
float Mspeed;
float Switch;
receive_D(Mspeed, Switch, 2);
send_D(dato1, dato2, Mspeed, 3); // The last parameter "3", is for choose the number of variables to send to Ubidots: (1-3)
//Motor control process
if (((float)dato1) > TempLimit || ((float)dato2) > HumiLimit) {
if(Switch==1){
digitalWrite(IN1, HIGH);
digitalWrite(IN2, LOW);
analogWrite(ENA, Mspeed);
}
else{
digitalWrite(IN1, LOW);
digitalWrite(IN2, LOW);
}
}
else{
digitalWrite(IN1, LOW);
digitalWrite(IN2, LOW);
}
}
//Maybe, here I need code to receive motor speed and switch float data from Ubidots
// Function to send data from Ubidots *************************************************************************************************************
void send_D(float d1, float d2, int d3, int n_var) { // Send float data
int string_length = 0, long_json = 0, SEND = 0;
String Inicio, Final, variable[10];
// JSON
Inicio = "[";
variable[1] = "{\"variable\": \"" + String(id1) + "\",\"value\":" + String(d1);
variable[2] = "}, {\"variable\": \"" + String(id2) + "\",\"value\":" + String(d2);
variable[3] = "}, {\"variable\": \"" + String(id3) + "\",\"value\":" + String(d3);
Final = "}]";
for(int i = 1; i <= n_var; i++) {
string_length += variable[i].length();
}
long_json = string_length + Inicio.length() + Final.length();
// Serial.println(long_json);
String send1, send2, send3, send4, send5;
int long_post = 0;
// POST
send1 = "POST /api/v1.6/collections/values/ HTTP/1.1\r\n";
long_post = send1.length();
send2 = "Content-Type: application/json\r\nContent-Length: ";
long_post += send2.length();
send3 = String(long_json) + "\r\nX-Auth-Token: ";
long_post += send3.length();
send4 = String(token);
long_post += send4.length();
send5 = "Host: things.ubidots.com\r\n";
long_post += send5.length();
SEND = long_json + long_post + 5;
// Serial.println(SEND);
Serial.println(F("\n**********************************************************************************************************************************************************************\n"));
Flush();
Serial.println(F("AT+CIPSTART=4,\"TCP\",\"things.ubidots.com\",80")); // cmd AT+CIPSTART: Establishes TCP Connection
delay(5000);
Flush();
Serial.println("AT+CIPSEND=4," + String(SEND)); // cmd AT+CIPSEND: Sends Data
delay(500);
Serial.print(send1);
Serial.print(send2);
Serial.print(send3);
Serial.println(send4);
Serial.println(send5);
delay(500);
Serial.print(Inicio);
for(int i = 1; i <= n_var; i++) {
Serial.print(variable[i]);
}
Serial.println(Final);
}
// Function for AT commands *********************************************************************************************************************
void sendCmd(char* cmd, char* ans, int t) {
boolean Er = true;
Flush();
Serial.println(cmd);
delay(t);
while(Serial.available()) {
if(Serial.find("OK")) {
if(ans == "R") {
delay(500);
Serial.print(".");
delay(500);
Serial.print(".");
delay(500);
Serial.println(".\n");
delay(1000);
}
else {
Serial.println(ans);
delay(500);
}
Er = false;
}
}
if(Er == true) {
Serial.println("Error");
delay(500);
if(ans == "Successful WiFi Connection!") {
if(Er == true) {
Serial.println("WiFi Disconnected!");
delay(500);
}
}
}
}
// Function to clean Buffer Serial **************************************************************************************************************
void Flush() {
byte w = 0;
for (int i = 0; i < 10; i++) {
while (Serial.available() > 0) {
char k = Serial.read();
w++;
delay(1);
}
delay(1);
}
}