Sensors (or lab scale) readings to send to the Cloud

Hi everyone,

I completed the T/RH, Oxygen and scale readings projects.
http://forum.arduino.cc/index.php?topic=440999.30
http://forum.arduino.cc/index.php?topic=464164.15

What I would like to do now is to have all the data available from these sensors/readings on the cloud.

I am using ThingSpeak.

I used a sample code and adapted to my library and adding some more comments for my learning.

//Libraires
#include <stdlib.h> // Includes the C library performing general functions
#include <dht.h> // Includes the libraries for the sensor

// Interface and Objects definitions
#define dataPin 2; // Defines pin number to which the sensor is connected (avoid D4-9 and A0 used by LCD)
dht DHT; // Creates a DHT object

// WEB Interface with ThingSpeak and ESP8266 Wi-Fi module definitions
#define SSID "name"     // "SSID-WiFiname" 
#define PASS "password"       // "password"
#define IP "184.106.153.149"// thingspeak.com ip
String msg = "GET /update?key=YOUR_WRITE_KEY"; // I will put my KEY from ThingSpeak here

// Global variables definitions
float temp;
int hum;
String tempC;
byte error;

//=-------------------------------------------------------------=

void setup()
{
  Serial.begin(9600);

  Serial.println("AT"); // Tests if AT system works correctly (we expect "OK" answer)
  delay(5000);
  if (Serial.find("OK"))
  {
    connectWiFi();
  }
}

//=-------------------------------------------------------------=

void loop() {
start: // label for the goto instruction

  error = 0; // Initialises the flag for the transmission error

  temp = DHT.temperature; // Gets the values of the temperature
  hum = DHT.humidity; // Gets the values of the humidity
  char buffer[10]; // It will save the char array converted from the float variables temp and hum
  tempC = dtostrf(temp, 4, 1, buffer); // It will convert a float to a char array so it can then be easily printed. The format is: dtostrf(floatvar, StringLengthIncDecimalPoint, numVarsAfterDecimal, charbuf)

  updateTemp(); // Calls the function that will print the data on the web

  if (error == 1) //Resend if transmission is not completed
  {
    goto start; //go to label "start"
  }

  delay(60000); // Update every 1 min
}

//=-------------------------------------------------------------=

void updateTemp()
{
  String cmd = "AT+CIPSTART=\"TCP\",\""; // Our command string we will use to communicate with the ESP8266 Wi-Fi module
  cmd += IP; // In C, x += y is equivalent x = x + y
  cmd += "\",80";
  Serial.println(cmd); // We send the command to the ESP8266
  delay(2000);
  if (Serial.find("Error"))
  {
    return;
  }
  cmd = msg ;
  cmd += "&field1=";    // field 1 for temperature
  cmd += tempC;
  cmd += "&field2=";  // field 2 for humidity
  cmd += String(hum);
  cmd += "\r\n";
  Serial.print("AT+CIPSEND=");
  Serial.println(cmd.length());
  if (Serial.find(">"))
  {
    Serial.print(cmd);
  }
  else
  {
    Serial.println("AT+CIPCLOSE");
    error = 1; // Resend...
  }
}

//=-------------------------------------------------------------=

boolean connectWiFi()
{
  Serial.println("AT+CWMODE=1");
  delay(2000);
  String cmd = "AT+CWJAP=\"";
  cmd += SSID;
  cmd += "\",\"";
  cmd += PASS;
  cmd += "\"";
  Serial.println(cmd);
  delay(5000);
  if (Serial.find("OK"))
  {
    return true;
  } else
  {
    return false;
  }
}

I still receive an error saying deprecated conversion from string constant to 'char*' but it compiles now (after some adjustment from the original template.
I am investigating this error through this link. It seems related to the definition of the string that is read-only and not read-write but I could not identify in the code this issue.

I would also like please if someone can troubleshoot the code or pointing me in the right direction if they notice something "wrong" or non-accurate or that could generate problems.

I am novice on this Wireless connectivity.

My next step will be to study all the commands the code is sending through serial.print, in particular in the updateTemp() and connectWiFi() (why is definied as "boolean" instead of "void" as usual???) functions.

PLEASE NOTE: IDEALLY I would like to write the Wireless connectivity just in a FUNCTION that I could reuse for all the other 3 projects (and call it inside the main loop for example) instead of "adapting" everytime... Not sure if this will be too advanced for me at my stage of knowledge...

Thanks a lot for any help.

Mario

Before running that code, that compiles, but still with the warning:

C:\Users\mario\Desktop\Wireless1\Wireless1.ino: In function 'void setup()':

C:\Users\mario\Desktop\Wireless1\Wireless1.ino:35:23: warning: deprecated conversion from string constant to 'char*' [-Wwrite-strings]

   if (Serial.find("OK"))

                       ^
C:\Users\mario\Desktop\Wireless1\Wireless1.ino: In function 'void updateTemp()':

C:\Users\mario\Desktop\Wireless1\Wireless1.ino:72:26: warning: deprecated conversion from string constant to 'char*' [-Wwrite-strings]

   if (Serial.find("Error"))

                         ^
C:\Users\mario\Desktop\Wireless1\Wireless1.ino:84:22: warning: deprecated conversion from string constant to 'char*' [-Wwrite-strings]

   if (Serial.find(">"))

                      ^
C:\Users\mario\Desktop\Wireless1\Wireless1.ino: In function 'boolean connectWiFi()':

C:\Users\mario\Desktop\Wireless1\Wireless1.ino:108:23: warning: deprecated conversion from string constant to 'char*' [-Wwrite-strings]

   if (Serial.find("OK"))

                       ^
Sketch uses 6150 bytes (19%) of program storage space. Maximum is 32256 bytes.
Global variables use 379 bytes (18%) of dynamic memory, leaving 1669 bytes for local variables. Maximum is 2048 bytes.

However the main thing that is concerning me is that with the BareMinimum code and trying to send AT commands to the serial monitor (any baudrate) I do not receive any answer at all.
The led is on on the ESP8266 module.

I am using this connections:

Thanks a lot for any help.

M.

but still with the warning:

So fix it.

   if (Serial.find((char *)"OK"))

It is a mystery why the clueless f**k that wrote the find function didn't declare the argument type to be const char *. The find function sure as hell should NOT be changing either where the pointer points to OR what it points to.

TX on the uno should go to RX on the ESP8266. Looks like you have TX to TX.

Also, the ESP8266 draws a lot of current - google for projects using it - they usually have external power and often decoupling capacitors as well. If you power it with batteries, look into the sleep modes for the device to make it a big more frugal.

PaulS:
So fix it.

   if (Serial.find((char *)"OK"))

It is a mystery why the clueless f**k that wrote the find function didn't declare the argument type to be const char *. The find function sure as hell should NOT be changing either where the pointer points to OR what it points to.

Thanks Paul, always ready to help. It seems that stuff annoyed you a bit. Sorry I was not able to find out what you explained.

wildbill:
TX on the uno should go to RX on the ESP8266. Looks like you have TX to TX.

Also, the ESP8266 draws a lot of current - google for projects using it - they usually have external power and often decoupling capacitors as well. If you power it with batteries, look into the sleep modes for the device to make it a big more frugal.

I did the first thing, swapping TX with RX (those instructions were taken from an instructables online on ESP8266).
Now I need to understand what to buy as an external power source for the Wi-Fi module.

Any advice for something not too complicated just to plug it in guys please?

I also have this one, that might make thing easier?

ESP8266 shield

And I think the specs are here:

ESP8266 shield specs

My concern is that I would like to use ALSO the LCD screen:

LCD shield

The LCD screen uses pins: D4-D9 and A0.

I would like to use both if it is simpler otherwise LET'S STICK to learn how to make it works the ESP8266 module.

EDIT: I read someone is using this ld1117v33 regulator and connect the VCC to 5V to have enough current.

I also read about bi directional level converter that will allow to connect TX and RX in serial without a problem.

Not sure if I am just getting lost in all of this :slight_smile: :slight_smile: :slight_smile:

Thanks a lot guys

EDIT2: Really sorry if this part of the thread is not particularly "programming related" but I did not feel to open another thread on another section and then come back here with the same project (or perhaps that would be the better way to do for all the community?)

Guys, I made the decision to bring this problem with the ESP8266 in the "Networking, Protocols, and Devices" section. It is not programming related now. I will come back here as soon as all the hardware will work properly.

Thanks a lot guys, cu soon

NEW esp8266 hardware thread