Coding ..... How? | ESP8266 Wi-Fi Module | Forum

Avatar

Please consider registering
Guest

Search

— Forum Scope —






— Match —





— Forum Options —





Minimum search word length is 3 characters - maximum search word length is 84 characters

Register Lost password?
sp_Feed sp_TopicIcon
Coding ..... How?
Avatar
gillesroszak
New Member
Forum Posts: 3
sp_UserOfflineSmall Offline
1
October 12, 2017 - 8:11 am
sp_Permalink sp_Print

HI Guys, 
I'm new  user of Thingspeak and have some difficulty to send data from ESP8266 NodeMCU with Arduino IDE..
I don't know how to add the right code to The  sketch below for sending the data to thingspeak.

Is there anybody to help me ??

Thanks in advance for your reply

 

Gilles

 

*/

// lowest and highest sensor readings:
const int sensorMin = 0; // sensor minimum
const int sensorMax = 1024; // sensor maximum

void setup() {
// initialize serial communication @ 9600 baud:
Serial.begin(9600);
}
void loop() {
// read the sensor on analog A0:
int sensorReading = analogRead(A0);
// map the sensor range (Six options):
// ex: 'long int map(long int, long int, long int, long int, long int)'
int range = map(sensorReading, sensorMin, sensorMax, 0, 5);

// range value:
switch (range) {
case 0: // Sensor getting wet
Serial.println("Forte pluie");
break;
case 1: // Sensor getting wet
Serial.println("Pluie");
break;
case 2: // Sensor getting wet
Serial.println("Pluie faible");
break;
case 3: // Sensor getting wet
Serial.println("Brumeux");
break;
case 4: // Sensor getting wet
Serial.println("Humide");
break;
case 5: // Sensor dry - To shut this up delete the " Serial.println("Sec"); " below.
Serial.println("Sec");
break;
}
delay(1); // delay between reads
}

Avatar
cstapels

Gold
Forum Posts: 60
sp_UserOfflineSmall Offline
2
October 12, 2017 - 5:37 pm
sp_Permalink sp_Print

Gilles,

There are several new examples available at ThingSpeak, just posted. There is a temperature example that uses the ThingSpeak Library to handle the HTTP stuff.

There is a moisture sensor example that uses an ESP8266-12 to post a voltage reading to a ThingSpeak channel.  This one does not use the communication library and does use deep sleep between readings.  

There are some differences depending on what version of the ESP8266 you have, let me know if you have trouble.

Avatar
chputney
New Member
Forum Posts: 1
sp_UserOfflineSmall Offline
3
October 14, 2017 - 8:03 am
sp_Permalink sp_Print

Here is what works for me:

 

#include <Esp.h>
#include <user_interface.h>
#include "display.h"
#include "button.h"
#include "onewire.h"
#include <ESP8266WiFi.h>

 

// boot log - connect putty to com port at 74880 baud to see boot log

// Thermostat GPIO pin defines
// pins to avoid GPIO0 (D3), GPIO2(D4), GPIO15(D8)

 

#define DS18B20 2 // D4 on mini = GPIO2 on ESP8266
#define SCL 12 // D6 on mini = GPIO12 on ESP8266
#define SDA 13 // D7 on mini = GPIO13 on ESP8266
#define BUTTON 14 // D5 on mini = GPIO14 on ESP8266

#define TIMEOUT 500 // 5 seconds

// ------------- wifi and http defines --------------------
#define ssid "xxxxx"
#define password "xxxxx"
#define THINGSPEAKIP "184.106.153.149" // thingspeak.com
#define host "api.thingspeak.com"

uint32_t RTC_Memory[10];
int Current_Temperature, Current_Setpoint;
struct rst_info *Reset_Info; // first int32 is the reason 5 = reset button, 6 = deep sleep
int SleepTime = TIMEOUT;
bool Temp_displayed = false;
bool Show_Setpoint_or_Temperature = false; // Setpoint = false, Temperature = true
String httpstring;
String ThingSpeak = "GET http://api.thingspeak.com/update?api_key=VPTOJBWMJSADEDO806RQM&field1=";

 

void setup()
{
Serial.begin(9600);
pinMode(SCL, OUTPUT);
pinMode(SDA, OUTPUT);
pinMode(DS18B20, OUTPUT);
pinMode(BUTTON, INPUT_PULLUP);
digitalWrite(BUILTIN_LED, HIGH); // make sure the LED is off

Reset_Info = ESP.getResetInfoPtr();
Serial.print("
System Reset Reason: ");
Serial.print(Reset_Info->reason);
Serial.print(" ");
Serial.println(ESP.getResetReason());

//get last temperature and setpoint
ESP.rtcUserMemoryRead(0, (uint32_t*) &RTC_Memory, 10) ;
Current_Temperature = (int)RTC_Memory[0];
if (Current_Temperature < 0 || Current_Temperature > 30) Current_Temperature = 19;
Current_Setpoint = (int)RTC_Memory[1];
if (Current_Setpoint < 10 || Current_Setpoint > 25) Current_Setpoint = 20;

// get the current temperature
Request_Temperature();
if (Reset_Info->reason == 5) // we have awoken from deep sleep
{
Report_Temperature(); // report the current temperature over wifi and go back to sleep
Goto_Sleep();
}
else // the user pushed the button - show the display

{
Display_init();
Display_clear();
Show_Setpoint_or_Temperature = true;
}

}

void loop()
{
if (Timer()) // do this every 10 milliseconds
{

ESP.wdtFeed(); // watchdog is reset

if (Temp_displayed == false)
{
Display_clear();
if (Show_Setpoint_or_Temperature) Show_Temperature(); else Show_Setpoint();
Temp_displayed = true;
}

byte but = Check_Button();
if (but > 0) SleepTime = TIMEOUT;
switch (but)
{

case 3: // 3 = Button was pressed for COUNT_UP
Current_Setpoint++;
if (Current_Setpoint > 25) Current_Setpoint = 25;
Temp_displayed = false;
Show_Setpoint_or_Temperature = false;
break;

case 4: // 4 = Button was pressed for COUNT_DOWN
Current_Setpoint--;
if (Current_Setpoint <15) Current_Setpoint = 15;
Temp_displayed = false;
Show_Setpoint_or_Temperature = false;
break;
}
SleepTime--;
if (SleepTime == 0) Goto_Sleep(); // shutdown

}
}

bool Timer(void)
{
static unsigned long Time;
if (millis() > Time)
{
Time += 10;
return true;
}
return false;
}

void Goto_Sleep(void) // sleep for 10 minutes
{
Display_off();
RTC_Memory[0] = (uint32_t)Current_Temperature;
RTC_Memory[1] = (uint32_t)Current_Setpoint;
ESP.rtcUserMemoryWrite(0, (uint32_t*) &RTC_Memory, 10) ; // save Setpoint
ESP.deepSleep(600e6, (RFMode)(2)); // no RF calibration on startup
}

void Show_Temperature(void)
{
Display_setfont(1);
Display_goto(30,0);
Display_chars("Temperature");
Display_setfont(4);
Display_goto(40,20);
Display_char(Current_Temperature/10+0x30);
Display_char(Current_Temperature%10+0x30);
Display_update();
}

void Show_Setpoint(void)
{
Display_setfont(1);
Display_goto(30,0);
Display_chars(" Setpoint ");
Display_setfont(4);
Display_goto(40,20);
Display_char(Current_Setpoint/10+0x30);
Display_char(Current_Setpoint%10+0x30);
Display_update();
}

void Show_Failure_Reason(const char *reason)
{
long timenow;
Display_init();
Display_clear();
Display_setfont(1);
Display_goto(20,26);
Display_chars(reason);
Display_update();
timenow = millis();
while ((millis() - timenow) < 3000); // show for 3 seconds
}

void Report_Temperature(void)
{
long timenow;
long WiFiTime; // time to establish wifi connection
long ClientTime; // time to connect to thingspeak server

// start wifi station

WiFi.persistent(false);
//WiFi.mode(WIFI_OFF);
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);

timenow = millis();
while (WiFi.isConnected() == false)
{
delay(100);
ESP.wdtFeed();
WiFiTime = millis() - timenow;
if (WiFiTime > 10000)
{
Serial.println("WiFi Connection Timeout !");
Show_Failure_Reason("WiFi Timeout");
Goto_Sleep(); // if no connection after 10 seconds - give up
}
}
Serial.print("WiFi Connection after ");
Serial.print(WiFiTime/1000);
Serial.print(".");
Serial.print(WiFiTime%1000);
Serial.println(" seconds");

Current_Temperature = Get_Temperature();
Serial.print("Current Temperature = ");
Serial.println(Current_Temperature);
WiFiClient client;

httpstring = ThingSpeak + Current_Setpoint + "&field2=" + Current_Temperature + "&field3=" + WiFiTime;

if (client.connect(host, 80))
{
Serial.println("connected");
Serial.println(httpstring);
client.println(httpstring);

timenow = millis();
while (client.connected())
{
if (client.available())
{
String line = client.readStringUntil('
');
Serial.println(line);
}
ClientTime = millis() - timenow;
if (ClientTime > 5000)
{
Serial.println("thingspeak Connection Timeout !");
Show_Failure_Reason("Server Timeout");
Goto_Sleep(); // if no connection after 5 seconds - give up
}
}
client.stop();
Serial.println("
Disconnected");
}
else
{
Serial.println("connection failed !");
client.stop();
}

client.stop();
WiFi.mode(WIFI_OFF);
}

Avatar
gillesroszak
New Member
Forum Posts: 3
sp_UserOfflineSmall Offline
4
October 16, 2017 - 2:43 pm
sp_Permalink sp_Print

OK Guys thanks a lot I progress well with your help!!!!

Another question please, can we send text and display it to Thingspeak ?

Avatar
cstapels

Gold
Forum Posts: 60
sp_UserOfflineSmall Offline
5
October 16, 2017 - 4:32 pm
sp_Permalink
Awaiting Moderation

Avatar
gillesroszak
New Member
Forum Posts: 3
sp_UserOfflineSmall Offline
6
October 17, 2017 - 3:34 pm
sp_Permalink sp_Print

Hi Guys , 

Can you help me one more time? 

I want to use this sketch to send data to thingspeak see below)

Which code line should i add to do this

 

[code]
/* Flame Sensor analog example.
Code by Reichenstein7 (thejamerson.com)

For use with a Rain Sensor with an analog out!

To test view the output, point a serial monitor such as Putty at your Arduino.

- If the Sensor Board is completely soaked; "case 0" will be activated and " Flood " will be sent to the serial monitor.
- If the Sensor Board has water droplets on it; "case 1" will be activated and " Rain Warning " will be sent to the serial monitor.
- If the Sensor Board is dry; "case 2" will be activated and " Not Raining " will be sent to the serial monitor.

*/

// lowest and highest sensor readings:
const int sensorMin = 0; // sensor minimum
const int sensorMax = 960; // sensor maximum

void setup() {
// initialize serial communication @ 9600 baud:
Serial.begin(9600);
}
void loop() {
// read the sensor on analog A0:
int sensorReading = analogRead(A0);
// map the sensor range (Six options):
// ex: 'long int map(long int, long int, long int, long int, long int)'
int range = map(sensorReading, sensorMin, sensorMax, 0, 15);

// range value:
switch (range) {
case 0: // Sensor getting wet
Serial.println("15");
break;
case 1: // Sensor getting wet
Serial.println("14");
break;
case 2: // Sensor getting wet
Serial.println("13");
break;
case 3: // Sensor getting wet
Serial.println("12");
break;
case 4: // Sensor getting wet
Serial.println("11");
break;
case 5: // Sensor getting wet
Serial.println("10");
break;
case 6: // Sensor getting wet
Serial.println("9");
break;
case 7: // Sensor getting wet
Serial.println("8");
break;
case 8: // Sensor getting wet
Serial.println("7");
break;
case 9: // Sensor getting wet
Serial.println("6");
break;
case 10: // Sensor getting wet
Serial.println("5");
break;
case 11: // Sensor getting wet
Serial.println("4");
break;
case 12: // Sensor getting wet
Serial.println("3");
break;
case 13: // Sensor getting wet
Serial.println("2");
break;
case 14: // Sensor getting wet
Serial.println("1");
break;
case 15: // Sensor dry - To shut this up delete the " Serial.println("Sec"); " below.
Serial.println("0");
break;
}
delay(2000); // delay between reads
}
[/code]

Forum Timezone: America/New_York

Most Users Ever Online: 114

Currently Online:
18 Guest(s)

Currently Browsing this Page:
1 Guest(s)

Top Posters:

rw950431: 250

Vinod: 152

piajola: 74

vespapierre: 63

cstapels: 60

Adarsh_Murthy: 59

Newest Members:

bharad

mikebradley

gilloliver

chputney

fibula

namansahore

Forum Stats:

Groups: 4

Forums: 17

Topics: 1261

Posts: 4400

 

Member Stats:

Guest Posters: 1

Members: 5588

Moderators: 0

Admins: 2

Administrators: Hans, lee