Need help in code of pulse counter using esp8266 | General | Forum

Avatar

Please consider registering
Guest

sp_LogInOut Log In sp_Registration Register

Register | Lost password?
Advanced Search

— Forum Scope —






— Match —





— Forum Options —





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

sp_Feed sp_TopicIcon
Need help in code of pulse counter using esp8266
No permission to create posts
May 19, 2018
12:49 pm
Avatar
shivshankar9
New Member
Members
Forum Posts: 1
Member Since:
May 19, 2018
sp_UserOfflineSmall Offline

Hello everyone ,
I am write the code for nodemcu esp8266, its regarding pulse count, store and return back to nodemcu using raspberry pi as server. I am using 3 pins of nodemcu esp8266 for that. I am using D4 for pulse output, D5 pulse input and D6 use as switch ( means when only one pulse give that pin then pulse comes out from D4) when given pulse on d5 its store on server when only one pulse given on D6 then all store pulse on server its comes out from D4 pin.
But problem is,
When I am upload then code on nodemcu esp8266 and not given any power to that just given only pulse on pin D5 then its instant comes out from D4 pin ( I am check with LED), please help me.
Here is my code

#include
#include "Adafruit_MQTT.h"
#include "Adafruit_MQTT_Client.h"

int pulse = D5;
int pulse_back = D6;
int LED = D4;
int pulseState=0;
int pulse_backState=0;
int count=0;
char buf[10];
String buf2;
int realcount;
/************************* WiFi Access Point *********************************/

#define WLAN_SSID "iBall-Baton" // enter your WiFi SSID
#define WLAN_PASS "987654321" // this is your WiFi password

/************************* Adafruit.io Setup *********************************/

#define MQTT_SERVER "192.168.1.100" // change this to your Pi IP_address
#define MQTT_SERVERPORT 1883 // use 8883 for SSL
#define MQTT_USERNAME "" // empty
#define MQTT_KEY "" // empty

/************ Global State (you don't need to change this!) ******************/

WiFiClient client;

// Setup the MQTT client class by passing in the WiFi client and MQTT server and login details.
Adafruit_MQTT_Client mqtt(&client, MQTT_SERVER, MQTT_SERVERPORT, MQTT_USERNAME, MQTT_KEY);

/****************************** data stream publish/subscribe ***************************************/

Adafruit_MQTT_Publish pot_stream = Adafruit_MQTT_Publish(&mqtt, MQTT_USERNAME "/esp/pot"); // change the topic
Adafruit_MQTT_Subscribe onoffbutton = Adafruit_MQTT_Subscribe(&mqtt, MQTT_USERNAME "/feeds/N1");

/*************************** Sketch Code ************************************/
void MQTT_connect();

void setup() {
Serial.begin(9600);
delay(10);
pinMode(pulse,INPUT_PULLUP); //
pinMode(pulse_back,INPUT_PULLUP); //
pinMode(LED,OUTPUT);
digitalWrite(LED,HIGH);
Serial.println(F("Adafruit MQTT demo"));

Serial.println(); Serial.println();
Serial.print("Connecting to ");
Serial.println(WLAN_SSID);

WiFi.begin(WLAN_SSID, WLAN_PASS);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println();

Serial.println("WiFi connected");
Serial.println("IP address: "); Serial.println(WiFi.localIP());
mqtt.subscribe(&onoffbutton);

}

uint32_t x=0;

void loop()
{

MQTT_connect();

pulse_backState = digitalRead(pulse_back);
pulseState = digitalRead(pulse);

if (pulseState == LOW)
{
count=count + 10;
Serial.println(count);
pot_stream.publish(10);
delay(100);
}
if (pulse_backState == LOW)
{
pot_stream.publish("N1"); // publish to Raspberry Pi under topic "/esp/pot"
Serial.print("N1");
Adafruit_MQTT_Subscribe *subscription;
while ((subscription = mqtt.readSubscription(5000))) {
if (subscription == &onoffbutton)
{
Serial.print(F("On-Off button: "));
Serial.println((char *)onoffbutton.lastread);
buf2=(char *)onoffbutton.lastread;
Serial.println(buf2);
realcount=buf2.toInt();//integer
Serial.println(realcount);
for(int i=1;i<=realcount;i++)
{
digitalWrite(LED, HIGH);
delay(50);
digitalWrite(LED, LOW);
delay(50);
}
count=0;
realcount=0;

}
}
}
}

void MQTT_connect() {
int8_t ret;

if (mqtt.connected())
{
return;
}

Serial.print("Connecting to MQTT... ");

uint8_t retries = 3;
while ((ret = mqtt.connect()) != 0) { // connect will return 0 for connected
Serial.println(mqtt.connectErrorString(ret));
Serial.println("Retrying MQTT connection in 5 seconds...");
mqtt.disconnect();
delay(5000); // wait 5 seconds
retries--;
if (retries == 0)
{
while (1);
}
}
Serial.println("MQTT Connected!");
}

June 11, 2018
7:41 pm
Avatar
cstapels
Moderator
Members


Moderators
Forum Posts: 437
Member Since:
March 7, 2017
sp_UserOfflineSmall Offline

The node MCU pins do not map 1:1 with Arduino GPIO.
See https://iotbytes.wordpress.com/nodemcu-pinout/ for example.
D6 does not seem to be an available pin, that might explain the odd behavior. (d6 on the node MCU should be called as pin 12)
Let us know if that fixes the problem.

Forum Timezone: America/New_York

Most Users Ever Online: 114

Currently Online: deekshana
35 Guest(s)

Currently Browsing this Page:
1 Guest(s)

Top Posters:

rw950431: 261

Vinod: 196

piajola: 85

turgo: 70

vespapierre: 63

Adarsh_Murthy: 62

Member Stats:

Guest Posters: 1

Members: 5703

Moderators: 0

Admins: 2

Forum Stats:

Groups: 4

Forums: 17

Topics: 1313

Posts: 4565

Newest Members:

JavierLecy, gwenbk2, eloisedq3, AEtedesar, RichardWosus, Kellynapse

Administrators: Hans: 387, lee: 457