[ThingSpeak] ESP8266 and Mega2560

I’ve been with this project for weeks and I still can’t figure out what I’m doing wrong. I’ve google A LOT trying to find information because I’m so lost but I’have found the answers. I decided to post my problem here and hope that someone can help me. PLEASE.

I’m trying to upload data information to ThingSpeak, using Arduino Mega 2560 and the ESP8266 for the WiFi connection. For now I’m just trying to upload data using a SHT15 for temperature/humidity but I can’t do it and I’m not sure where the error is.

I don’t know exactly what library to use, because from the mayority of codes that I’ve seen, people use the ESP8266WiFi.h or WiFi101.h but I don’t think those work (I’ve tried both and some more). Right now I’m using the WiFi.h, with WiFiClient.h too and these are the 2 that works the best (I mean, give less trouble).

I have to use that my programmin skills are… bad. I’ve had problems compiling some things that I didn’t know why I couldn’t compile. The code is this one:

#include <WiFi.h>
#include “ThingSpeak.h”
#include <SHT1X.h>
#include <WiFiClient.h>

int power = A3;
int gnd = A2;
float tempC = 0;
float humidity = 0;

SHT1x sht15(A4, A5);//Data, SCK

char ssid = “XXXXXXXX”;
const char* pass = “XXXXXXXX”;
int status = WL_IDLE_STATUS;
unsigned long myChannelNumber = XXXXXX;
const char* myWriteAPIKey = “XXXXXXXXXXXX”;
const char* server = “api.thingspeak.com”;

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

WiFi.begin(ssid, pass);
WiFiClient client;
pinMode(power, OUTPUT);
pinMode(gnd, OUTPUT);

digitalWrite(power, HIGH);
digitalWrite(gnd, LOW);
ThingSpeak.begin(client);
}

void loop() {
delay(500);
float temp = sht15.readTemperatureC();
float humidity = sht15.readHumidity();
Serial.print(" Humidity = “);
Serial.print(humidity);
Serial.println(”%");
Serial.print(" Temp = ");
Serial.print(temp);
Serial.println(“C”);

delay(1000);

ThingSpeak.setField(1,temp);
ThingSpeak.setField(2,humidity);

ThingSpeak.writeFields(myChannelNumber,myWriteAPIKey);
ThingSpeak.writeField(myChannelNumber, 1, float(temp), myWriteAPIKey);

ThingSpeak.writeField(myChannelNumber, 2, float(humidity), myWriteAPIKey);
delay(20000);
}

The SHT15 part works fine. The serial connection gives me the data I want constantly, the problem is sending to ThingSpeak (or connecting to WiFi, I don’t know). This is what I get on the serial port:

Humidity = 60.53%
Temp = 30.40C (after sending this it takes like 40 seconds to send the next thing)
Humidity = 60.37%
Temp = 30.39C
No Socket available
No Socket available (the "No Socket available is repeated 2 times the first time)
Humidity = 60.59%
Temp = 30.39C
No Socket available (after this, is repeated 3 times and continues like this)
No Socket available
No Socket available
Humidity = 60.47%
Temp = 30.40C
No Socket available
No Socket available
No Socket available

About the wiring. The SHT is conected to pings A2, A3, A4, A5. No deal, working fine.
The ESP8266 is have the RX connected to the TX18 and the TX with the RX19. Ground to ground, Vcc together with CH_PD are connected to a LM2596, that has the 5v port of the Arduino as Vin and can get 3.3v as Vout that the ESP8266 needs.

When I try to use AT commands, it seems working fine. I can connect to my network, I can read my IP and my mac. I even tried looking at my router page setting and I can see that the ESP8266 is connected.

Also, I’ve been trying different programs and non of them get a good result, but one of them got something weird. My first ThingSpeak channel which I was testing with, got just a dot, with a 0.0 value that I don’t know how it got it.

https://thingspeak.com/channels/308342/charts/1?bgcolor=%23ffffff&color=%23d62020&dynamic=true&results=60&type=line&update=15

I’m doing my testing with a different channel now and is still blank.

Another doubt is that I don’t know what command I have to use for uploading. I don’t know if I have to use ThingSpeak.setField or ThingSpeak.writeField. Or both.

I don’t know what I’m doing wrong and I’d appreciate some help. I’ve been doing this alone with no help, just watching tutorials and reading guides, but I can’t find a solution to this. Thanks for reading and sorry for the long post.

Hi
WiFi.h is for the original WiFi shield, not for the ESP8266.
For the ESP8266 with AT commands firmware try WiFiEsp library if you don't want use AT commands.

You just saved my life. I’ve changed the library to the WiFiEsp and now it works.
The thing is that I’ve tried this library before and it didn’t work or it didn’t let me compile the program, I don’t remember, but I must have changed something on the program and now it works properly.

https://thingspeak.com/channels/326217

This is my testing channel, now I just have to put the settings right.

Thank you so so much :smiley:

You two guys seem to have a good handle on this whole ESP thing, while I'm only about to start. I don't want to hijack this thread and would appreciate if you could list some ESP-n00b-friendly sites over on this thread.