The use of the ESP8266 in the world of IoT

User avatar
By SurtrTech
#65041 Hi everyone,
I've wired my LM35 temperature sensor to my Node MCU ESP8266 12E I've tried to see the data on my serial monitor it works fine, now I want to show data on ThingSpeak I've actually used this code below, created my channel, but the data are not displayed on the chart even if the entries value keep incrementing.
and on the serial monitor I can see that it's connected to the router and it's sending the data to thingspeak

Code: Select all#include <ESP8266WiFi.h>


String apiKey = "myAPIkey";
const char* ssid = "Myroutername";
const char* password = "myrouterpw";

const char* server = "api.thingspeak.com";
float temp = 0;
int analog = 0;
WiFiClient client;

void setup() {
  // put your setup code here, to run once:
Serial.begin(115200);
delay(10);

WiFi.begin(ssid, password);

Serial.println();
Serial.println();
Serial.print("Connecting to");
Serial.println(ssid);
WiFi.begin(ssid, password);

while (WiFi.status() != WL_CONNECTED){
  delay(500);
  Serial.print(".");
}
Serial.println("");
Serial.println("WiFi conected");
}

void loop() {
  // put your main code here, to run repeatedly:
analog = analogRead(17);
float temp = analog * 0.2785;
if (client.connect(server,80)){
  String postStr = apiKey;
         postStr +="&field1=";
         postStr += String(temp);
         postStr += "\r\n\r\n";

  client.print("POST /update HTTP/1.1\n");
  client.print("Host: api.thingspeak.com\n");
  client.print("Connection: close\n");
  client.print("X-THINGSPEAKAPIKEY: "+apiKey+"\n");
  client.print("Content-Type: application/x-www-form-urlencoded");
  client.print("Content-Length: ");
  client.print(postStr.length());
  client.print("\n\n");
  client.print(postStr);

  Serial.print("Temperature");
  Serial.print(temp);
  Serial.println("% send to Thingspeak");
}
client.stop();

Serial.println("waiting...");
delay(20000);
}


Thanks
User avatar
By TheFossil
#66008 Hi

It seems nothing I try with the ESP works! :(

I loaded the code you provided and it seems all so correct and simple, yet I get the error: init.lua:4: '=' expected near 'apiKey'.

I provided the Api key in line 4 as: String apiKey = "D6XFDLRN19BBY9YH";

Please help!

Thanks
John
User avatar
By TheFossil
#66022 Oops, so embarrassing! As one gets older the brain seems not to be as alert as before. :|

I managed to confuse Arduino IDE and LuaNodemc :oops:

I've had quite some success with connecting to Thingspeak, but as the previous guy found, my data that I upload is 'nul'.

Any ideas, please? :roll:

Thanks
John
User avatar
By SurtrTech
#66102
TheFossil wrote:Oops, so embarrassing! As one gets older the brain seems not to be as alert as before. :|

I managed to confuse Arduino IDE and LuaNodemc :oops:

I've had quite some success with connecting to Thingspeak, but as the previous guy found, my data that I upload is 'nul'.

Any ideas, please? :roll:

Thanks
John

Hi, Now it works I just modified a code that was actually for a sensor (DS18B20) to send a constant value to thingspeak every 15s.
Code: Select all//nodeMCU v1.0 (black) with Arduino IDE
//stream temperature data DS18B20 with 1wire on ESP8266 ESP12-E (nodeMCU v1.0)
//shin-ajaran.blogspot.com
//nodemcu pinout https://github.com/esp8266/Arduino/issues/584
#include <ESP8266WiFi.h>
#include <OneWire.h>
#include <DallasTemperature.h>

//Def
#define myPeriodic 15 //in sec | Thingspeak pub is 15sec
#define ONE_WIRE_BUS 2  // DS18B20 on arduino pin2 corresponds to D4 on physical board
//#define mySSR 0  // Solid State Relay on pin 0

OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature DS18B20(&oneWire);
float prevTemp = 0;
const char* server = "api.thingspeak.com";
String apiKey ="3HS1BJN55KH3I52W";
const char* MY_SSID = "Death";
const char* MY_PWD = "azerty123456";
int sent = 0;
void setup() {
  Serial.begin(115200);
  connectWifi();
}

void loop() {
  float temp;
  //char buffer[10];
  //DS18B20.requestTemperatures();
  //temp = DS18B20.getTempCByIndex(0);
  temp=1111;
  //String tempC = dtostrf(temp, 4, 1, buffer);//handled in sendTemp()
  Serial.print(String(sent)+" Temperature: ");
  Serial.println(temp);
 
  //if (temp != prevTemp)
  //{
  //sendTeperatureTS(temp);
  //prevTemp = temp;
  //}
 
  sendTeperatureTS(temp);
  int count = myPeriodic;
  while(count--)
  delay(1000);
}

void connectWifi()
{
  Serial.print("Connecting to "+*MY_SSID);
  WiFi.begin(MY_SSID, MY_PWD);
  while (WiFi.status() != WL_CONNECTED) {
  delay(1000);
  Serial.print(".");
  }
 
  Serial.println("");
  Serial.println("Connected");
  Serial.println(""); 
}//end connect

void sendTeperatureTS(float temp)

   WiFiClient client;
 
   if (client.connect(server, 80)) { // use ip 184.106.153.149 or api.thingspeak.com
   Serial.println("WiFi Client connected ");
   
   String postStr = apiKey;
   postStr += "&field1=";
   postStr += String(temp);
   postStr += "\r\n\r\n";
   
   client.print("POST /update HTTP/1.1\n");
   client.print("Host: api.thingspeak.com\n");
   client.print("Connection: close\n");
   client.print("X-THINGSPEAKAPIKEY: " + apiKey + "\n");
   client.print("Content-Type: application/x-www-form-urlencoded\n");
   client.print("Content-Length: ");
   client.print(postStr.length());
   client.print("\n\n");
   client.print(postStr);
   delay(1000);
   
   }//end if
   sent++;
 client.stop();
}//end send


A lot of commands and libraries aren't used here, I just wanted to test sending a value to thingspeak, so I send that "temp=1111", must get modified for other use for sure.