These are chat archives for esp8266/Arduino

19th
Apr 2016
geman220
@geman220
Apr 19 2016 00:13
Can anyone point me in the direction of a good tutorial to start learning how to program and use an ESP8266. I'm completely new, looking to have the ESP rotate a small servo to automate my blinds.
Stavros Korokithakis
@skorokithakis
Apr 19 2016 01:33
@geman220 i need to write one, really
@geman220 how comfortable are you with the command line?
geman220
@geman220
Apr 19 2016 01:53
I'm a decent programmer
Not by profession but I can get around
I bought 10x of these http://www.aliexpress.com/store/product/D1-mini-Mini-NodeMcu-4M-bytes-Lua-WIFI-Internet-of-Things-development-board-based-ESP8266/1331105_32529101036.html . I am going to use it with a LiOn solar battery Output:DC 5V/1A; 5V2.1A so I will need a Voltage regulator before I plug the USB into the micro-USB slot to power it correct?
Aditya Tannu
@AdySan
Apr 19 2016 01:56
Nah if it's 5v already should be good to power the nodemcu board
geman220
@geman220
Apr 19 2016 01:56
Ah okay
Aditya Tannu
@AdySan
Apr 19 2016 01:57
There's an onboard lm1117 to bring it down to 3.3V for the esp
geman220
@geman220
Apr 19 2016 01:58
Can you tell me how difficult what I'm trying to accomplish is? I want to hook up 10 of these to 1 servo each and a LiOn solar battery. I need to the ESP's to subscribe to an MQTT topic and rotate the servos so they can open/close my blinds. I don't mean pull the blinds up, just rotate the wheel that opens/closes the slats of the blinds.
On a scale of 1 - 10 what am I getting myself into
I plan to wire the servo directly off the LiOn battery not through the ESP. So I will have power running to the ESP and the servo.
Michael Miller
@Makuna
Apr 19 2016 06:56
@geman220 You will need a continuous rotation servo to do that. Further, you will need to sense the end stop (blind all the way up and all the way down)
@skorokithakis I believe there are writups on how to write an Arduino Library; plenty. Did you search and review any of those?
Stavros Korokithakis
@skorokithakis
Apr 19 2016 09:39
@Makuna no, I didn't know it was the same process. I'll have a look, thanks
@geman220 should be pretty easy to do what you want, I have code to do most of it
Faraz Ahmed
@faraz117
Apr 19 2016 10:00
I am getting this error I have been at it for weeks but cant figure it out ".pioenvs\esp12e\FrameworkArduinoVariant/pins_arduino.h:29:31: fatal error: ../generic/common.h: No such file or directory

include "../generic/common.h"

i am getting this error after i cloned the latest repo
I am using platformio
Ivan Grokhotkov
@igrr
Apr 19 2016 10:04
do you have the file variants/generic/common.h in your local copy of the repo?
Faraz Ahmed
@faraz117
Apr 19 2016 10:05
yes
But the file isn't been created in the .pioenvs when i build my project
Ivan Grokhotkov
@igrr
Apr 19 2016 10:09
It seems that platformio copies the current variant, instead of including it into search path.
Faraz Ahmed
@faraz117
Apr 19 2016 10:12
Yes .. can you suggest any solution for it ?
Ivan Grokhotkov
@igrr
Apr 19 2016 10:15
well, as a workaround, you can copy variants/generic/common.h into your .pioenvs\esp12e\FrameworkArduinoVariant directory and modify pins_arduino.h to include common.h instead of ../generic/common.h
regarding proper solution for this, i think it might be better to ask on platformio channel
Faraz Ahmed
@faraz117
Apr 19 2016 10:17
Alright thanks :)
marksev1
@marksev1
Apr 19 2016 10:19
Got a few hardware related questions. Here: https://community.openhab.org/t/mqtt-ir-transmiter-receiver/9433 he uses a esp-01, while I would use a wemos d1 mini. So he connects the IR-receiver to GPIO0, if I use same gpio (which i think also on wemos d1 mini has a pullup) I don't need to connect additional resistors. What about if I use different GPIO, do I need to connect resistor between gpio and 3.3v? On GPIO3 he connect an amplifier, why is that?
Faraz Ahmed
@faraz117
Apr 19 2016 10:27
I have tried to ask this in the platformio channel too . The work around doesnt work because platformio overwrite .pioenvs on every compilation
geman220
@geman220
Apr 19 2016 12:14
@skorokithakis Any chance you know of a good tutorial or read on how to get started on learning how to program something like this project?
Stavros Korokithakis
@skorokithakis
Apr 19 2016 12:15
@geman220 I don't know a tutorial, but here's my whole source file: https://www.pastery.net/nhmqbp/
i intend to break it up into a small framework so you can have your passwords in a small build script and then your setup and loop will be in a different file
the framework will take care of mqtt/wifi/whatever
so you can write setup/loop after the ESP has connected to the wifi and mqtt server
geman220
@geman220
Apr 19 2016 12:16
@skorokithakis Wow, thanks. This will certainly give me a head start.
Stavros Korokithakis
@skorokithakis
Apr 19 2016 12:17
no problem, it should be pretty much all you need
it'll connect to mqtt and receive commands
the commands will be like what i have there
"roll up"/"roll down" or whatever you want
i also have a command to do sensible logging to mqtt
in case you don't have a serial console connected
it'll print its IP address and time and things so you don't go hunting
geman220
@geman220
Apr 19 2016 12:18
Awesome! So quick question
 if (command == "forward") {
        motor(1, FORWARD, power);
        motor(2, FORWARD, power);
Is that the command that actually moves the servo?
Stavros Korokithakis
@skorokithakis
Apr 19 2016 12:20
yes but that's not a servo
i just output a HIGH on a pin
i have a custom bridge
you need a servo library for your thing
there's one built-in, i think
geman220
@geman220
Apr 19 2016 12:21
Yea, I think I read about a built-in one
Stavros Korokithakis
@skorokithakis
Apr 19 2016 12:21
by the way, you might find this useful: https://github.com/skorokithakis/tiny-ESP8266-breakout
geman220
@geman220
Apr 19 2016 12:22

I guess a more accurate question would be would I mainly be editing that area to turn the servo based on MQTT broker messages? Something like

 if (command == "open") {
        servo(1, 180, power);

Or whatever the actual syntax for the servo library is.

Stavros Korokithakis
@skorokithakis
Apr 19 2016 12:24
yeah
that just responds to mqtt messages
and has some assorted code to deal with crap i had to deal with during the ages
that's why i was asking @Makuna about the library
i want to make it into a library/framework and put it on github
so people don't have to rewrite all that crap every time
geman220
@geman220
Apr 19 2016 12:27
I see. So from what I've been reading so far I should be able to take my solar LiOn 5V2.1A or 5V1A split the power, hook power via USB into the ESP, wire hot/ground to the servo separate and connect 1 data to the servo from the ESP on any data pin I choose. Am I on the right track?
The battery is 10000mah so I believe it should run everything for a while without solar charging, but it has a solar panel on it and it will be facing outside of the window.
Stavros Korokithakis
@skorokithakis
Apr 19 2016 12:31
yeah, sounds good to me, as long as they share a ground
maybe you'll find this useful: https://www.stavros.io/notes/hardware/
geman220
@geman220
Apr 19 2016 12:33
Awesome, I'll read through this today
Stavros Korokithakis
@skorokithakis
Apr 19 2016 12:34
it's just two paragraphs, it'll take two seconds :smile:
geman220
@geman220
Apr 19 2016 12:34
Ha I already read that page. I meant the other content on your blog =)
Stavros Korokithakis
@skorokithakis
Apr 19 2016 12:34
oh right :P
geman220
@geman220
Apr 19 2016 12:35
I'm sure it's going to be a headache to get running at first, but it's always fun to learn something new and make something.
Stavros Korokithakis
@skorokithakis
Apr 19 2016 12:35
nah, it's trivial, get platformio
mkdir mything
cd mything
platformio init --board=esp12e
<edit your src/main.ino>
platformio run -t upload
done
geman220
@geman220
Apr 19 2016 12:37
Is there a way to emulate a program? Or will I have to wait until my ESPs and servo get here
Stavros Korokithakis
@skorokithakis
Apr 19 2016 12:44
you'll have to wait, i'm afraid
which nodemcu did you get?
Stavros Korokithakis
@skorokithakis
Apr 19 2016 12:44
ah, that's my favorite
geman220
@geman220
Apr 19 2016 12:44
I read online it was the best overall
Stavros Korokithakis
@skorokithakis
Apr 19 2016 12:44
i should probably buy a few more
yeah it is
geman220
@geman220
Apr 19 2016 12:45
I actually did SOME research =P
Stavros Korokithakis
@skorokithakis
Apr 19 2016 12:45
haha
geman220
@geman220
Apr 19 2016 12:48
Should platformio have made the src/main.ino?
Stavros Korokithakis
@skorokithakis
Apr 19 2016 12:48
i don't think it does
just use mine
geman220
@geman220
Apr 19 2016 12:48
Ah okay just making sure I didn't miss something
Stavros Korokithakis
@skorokithakis
Apr 19 2016 12:49
nope, i think it makes one if you use the blink template
you can use templates, i seem to recall
hey, @ivankravets, can i use a git repo as a template? i'd like to do something like platformio init --from-repo=git@github.com:skorokithakis/wifi-template.git
geman220
@geman220
Apr 19 2016 12:56

Where are these includes in your program coming from?

#include "PubSubClient.h"
#include <ESP8266WiFi.h>
#include <ESP8266mDNS.h>
#include <WiFiUdp.h>
#include <ArduinoOTA.h>
#include <DHT.h>

Do I need to physically get those? Are they just default Arudino IDE libraries? If so I can just look up the included servo library and add it correct?

Stavros Korokithakis
@skorokithakis
Apr 19 2016 12:59
oh yes, sec
add to your platformio.ini:
targets = upload
lib_install = 19,89
build_flags = -DMQTT_MAX_PACKET_SIZE=1024
upload_flags = -a OTAPASS
upload_port = 192.168.10.144
it'll download and install the libraries
change otapass to your otapass and the IP to your wifi ip
geman220
@geman220
Apr 19 2016 13:01
Sorry, OTA pass? Is that just the wireless password? I was confused about that in the program as well because there is WIFI_PASS is OTA_PASS
Stavros Korokithakis
@skorokithakis
Apr 19 2016 13:02
OTA pass is authentication for flashing over wifi
so random people can't just drive by and flash new firmware
running this will make your ESP flashable over wifi
so you don't need to have it connected to the pc
geman220
@geman220
Apr 19 2016 13:02
Oh, I see. Makes more sense now
Stavros Korokithakis
@skorokithakis
Apr 19 2016 13:03
i need to take some time and write all this up
geman220
@geman220
Apr 19 2016 13:03
upload_port = 192.168.10.144 is that generic or will I need to modify that for my subnet

And just so I'm clear in my own head...

[env:nodemcuv2]
platform = espressif
framework = arduino
board = nodemcuv2
targets = upload
lib_install = 19,89
build_flags = -DMQTT_MAX_PACKET_SIZE=1024
upload_flags = -a pass
upload_port = 192.168.10.144

This will download the libraries to the ESP, does this include all included Arduino IDE libraries? So I can add whatever library I want and it will download it to the ESP?

Ivan Kravets
@ivankravets
Apr 19 2016 13:12
@skorokithakis hi. We have an issue for that in PlatformIO 3.0
Stavros Korokithakis
@skorokithakis
Apr 19 2016 13:12
ah, fantastic
@geman220, you need to change the IP, yes
it'll download everything, yes
you can find libraries with platformio lib search i think
have a look at the docs
geman220
@geman220
Apr 19 2016 13:13
And that's the IP of what? Each ESP?
Ivan Kravets
@ivankravets
Apr 19 2016 13:13
platformio/platformio#446
geman220
@geman220
Apr 19 2016 13:14

I've added:

#include <Servo.h>

and

Servo myservo;  // create servo object to control a servo
int pos = 0;    // variable to store the servo position
void setup() {
  myservo.attach(9);  // attaches the servo on pin 9 to the servo object
}
Stavros Korokithakis
@skorokithakis
Apr 19 2016 13:17
seems reasonable
each ESP, yes
geman220
@geman220
Apr 19 2016 13:19

What's the most logical way to store this so I can just call it in the MQTT section?

void loop() {
  for (pos = 0; pos <= 180; pos += 1) { // goes from 0 degrees to 180 degrees
    // in steps of 1 degree
    myservo.write(pos);              // tell servo to go to position in variable 'pos'
    delay(15);                       // waits 15ms for the servo to reach the position
  }
}

So when this runs:

    if (command == "forward") {
        motor(1, FORWARD, power);
        motor(2, FORWARD, power);

I could have something like this:

  if (command == "open") {
        myservo(open180);
    }

Or am I off track?

geman220
@geman220
Apr 19 2016 14:00
I'll have to wait for my servo to get here but I think the code is
// Receive a message from MQTT and act on it.
void mqttCallback(char* chTopic, byte* chPayload, unsigned int length) {
    chPayload[length] = '\0';
    String payload = String((char*)chPayload);

    String command = getValue(payload, ' ', 0).c_str();
    int power = getValue(payload, ' ', 1).toInt();

    mqttPublish(LOG_TOPIC, String("Command ") + command + String(" received."));

    if (command == "open") {
        myservo.write(180);
    } else if (command == "close") {
        myservo.write(0);
    }
}
Stavros Korokithakis
@skorokithakis
Apr 19 2016 14:43
that looks reasonable, i've never used a servo so i don't know, but i assume the first goes to 180' and the second back to 0'
geman220
@geman220
Apr 19 2016 14:53
Yea what I've read online is the max setting is 180 and the lowest setting is 0. I can remove the potentiometer if I need to and setting it to 180 will essentially rotate it right forever and 0 will rotate it left forever. If I need to remove the potentiometer I'll have to add something like
    if (command == "open") {
        myservo.write(180);
        delay(2000);
    } else if (command == "close") {
        myservo.write(0);
        delay(2000);
    }
I think at least
Stavros Korokithakis
@skorokithakis
Apr 19 2016 15:09
@ystrem i've been wanting a gsm module, have you used it?
@geman220 if you only need to rotate to 180 degrees, i think keeping the potentiometer would be good
@geman220 the servos i got could rotate with accuracy forever
so they can rotate for 1000 degrees if you want
ystrem
@ystrem
Apr 19 2016 15:10
@skorokithakis nope
Stavros Korokithakis
@skorokithakis
Apr 19 2016 15:10
@ystrem hmm, i got one that's more ready-to-use, it had a sim card slot, antenna, etc
but that looks interesting
geman220
@geman220
Apr 19 2016 15:26
@skorokithakis I plan to leave the potentionmeter unless I need it to rotate more. Or I'll buy a new servo that has full rotation
Stavros Korokithakis
@skorokithakis
Apr 19 2016 15:26
@geman220 yeah, i think that's a better solution
@geman220 how many D1s did you get?
geman220
@geman220
Apr 19 2016 15:27
10
Stavros Korokithakis
@skorokithakis
Apr 19 2016 15:27
haha, great
i was going to tell you to get 3-4 because you might fry a few, but 10 is good
geman220
@geman220
Apr 19 2016 15:28
I need 6 to work on my first order so I can get downstairs completed. I need 10 total so if I'm lucky I don't fry any, if I'm unlucky I have 4 spare and I can make a second order

@skorokithakis so I hacked up your code and I do realize there is a lot of dead useless code now but I don't want to remove anything until I can test and go step by step through my "cleaning" here is what I have https://www.pastery.net/zegxhg/ . Notice anything that is glaringly wrong? Also to verify

WiFiClient wclient;
char COMMAND_TOPIC[] = "rccar/" NAME "/command";
char STATE_TOPIC[] = "rccar/" NAME "/state";
char LOG_TOPIC[] = "rccar/" NAME "/log";

Command_topic would be rccar/command so rccar is the name and command is the topic correct?

geman220
@geman220
Apr 19 2016 15:35
And don't I need a username / password to connect to the MQTT topic?
Stavros Korokithakis
@skorokithakis
Apr 19 2016 15:40
oh sorry
hmm
that's the old code
i replaced rccar
sec
that's the problem with not having a library
i have to hunt down the latest version of this code
project name is something like "shutters"
instance name is something like "living room"
mqtt auth will change depending on your setup, mine is on my lan, so i don't have it authed
hmm
geman220
@geman220
Apr 19 2016 15:42
I could set up MQTT on my LAN. Right now I use HASS and CloudMQTT but HASS has a built in local MQTT so I could just move to that
I only use MQTT currently for device tracking so no big deal
char COMMAND_TOPIC[] = PROJECT_NAME "blinds" INSTANCE_NAME "/command";
char STATE_TOPIC[] = PROJECT_NAME "blinds" INSTANCE_NAME "/state";
char LOG_TOPIC[] = PROJECT_NAME "blinds" INSTANCE_NAME "/log";
So in MQTT I make the name "blinds" and the topic inside of blinds command, state, and log. Correct?
geman220
@geman220
Apr 19 2016 15:48
Or I could just install Mosquitto
Stavros Korokithakis
@skorokithakis
Apr 19 2016 15:48
just define the vars
here:
stick that in build.sh
i use it to avoid defining my secrets in the file
basically this q/a is making me realize i should make this a proper framework with a readme that people can clone and use
geman220
@geman220
Apr 19 2016 15:51
But then you wouldn't have to sit in a chat all day answering Q/A! =)
Stavros Korokithakis
@skorokithakis
Apr 19 2016 15:54
haha
skorokithakis @skorokithakis runs off quickly to write the framework
geman220
@geman220
Apr 19 2016 15:56
Other than defining the var in build.sh to avoid having passwords / information in the main.ino is there any reason to do it? I ask because since I am just starting out I like to keep things together in one spot as best I can, it helps me understand the logic and flow. Once I am more comfortable with what I am doing I then split out things into includes etc.
When I was learning YAML for HASS I had one really long 2000+ line config for like 2 months until I was comfortable with what I was doing. Then I split everything up so it's actually manageable now, for example.
Stavros Korokithakis
@skorokithakis
Apr 19 2016 16:11
no, no other reason
i just like to publish my code and secrets in the code were non-great
plus this way i can reuse the code
geman220
@geman220
Apr 19 2016 16:19
Yea makes more sense. I'll clean it up once I'm more comfortable with what I'm doing.
I went ahead and setup Mosquitto to make things easier and migrated my device tracker over
I'm not certain but just publishing to a topic also creates the topic correct? So really now that I have Mosquitto up and running and working I don't need to modify anything I can just let the publisher create the topic blinds/command
Stavros Korokithakis
@skorokithakis
Apr 19 2016 16:56
yeah, just publish
you can also listen to things like
blinds/#
and it will listen to all messages under blinds
geman220
@geman220
Apr 19 2016 16:57
Okay, now the waiting game to get my ESPs =(
Stavros Korokithakis
@skorokithakis
Apr 19 2016 16:57
yeah, that's always the worst part :/
geman220
@geman220
Apr 19 2016 16:58
So if I remove the potentiometer just to do a basic proof of concept, I should be able to set a delay then stop the servo after the delay correct?
Stavros Korokithakis
@skorokithakis
Apr 19 2016 17:03
i am not sure at all about that
possibly?
geman220
@geman220
Apr 19 2016 17:03
Ha okay I'll look around online
Michael Miller
@Makuna
Apr 19 2016 17:37
@geman220 using timing is not a safe approach, you really need sensors and stop on sensor. Voltage variance will effect the speed of the servo.
geman220
@geman220
Apr 19 2016 17:38
=( more complexity
Michael Miller
@Makuna
Apr 19 2016 17:44
if you are using unmodified servos, then it has an internal sensor for position so you can set it and forget. If you disable the internal sensor for continuous mode, then you need external sensors.
geman220
@geman220
Apr 19 2016 17:51
Okay so I just need a continuous servo
Michael Miller
@Makuna
Apr 19 2016 17:56
continuous servos either ignore the internal sensor or have it disabled when shipped, so you then have to have an external sensor.
CHAE-PIL LIM
@chaeplin
Apr 19 2016 18:32
https://twitter.com/mydailyview/status/722476960761249793 It's direct image posting. Just for fun. esp-01 to twitter.
geman220
@geman220
Apr 19 2016 18:32
So essentially there is no way turn open/close blinds unless I get some sort of other sensor to tell it when to stop turning
Unless by some miracle 180 degrees is enough
Michael Miller
@Makuna
Apr 19 2016 18:41
@geman220 well, close to correct. More precisely, if a servo with internal sensor does not support the range of motion you need, then you will need external sensors. There are servos that do extended ranges and often most will do 200 degrees. To use them, you will need to then use the writeMicroseconds() method instead. The servo library is written to compatible with the normal Arduino library, so you can use their docs for reference.
geman220
@geman220
Apr 19 2016 18:42
Oh well that makes me feel much better =]. I'll probably look into getting servos that go up to 200 and use the method you mentioned.
Michael Miller
@Makuna
Apr 19 2016 21:57
@geman220 Yours may already go to 200, try them with the writeMicroseconds() first.
geman220
@geman220
Apr 19 2016 21:59
@Makuna so instead of myservo.write(180); do myservo.writeMicroseconds(200);
I just looked my servo up it moves 180
Stavros Korokithakis
@skorokithakis
Apr 19 2016 22:26
so i want to make this boilerplate framework for the ESP and i need a place to put my settings
what's a good place for that? something like settings.h?
i'm afraid my C knowledge isn't great
Stavros Korokithakis
@skorokithakis
Apr 19 2016 22:47
can i #ifdef an include?
Michael Miller
@Makuna
Apr 19 2016 22:58

@geman220 Review the Arduino Servo Reference. you are setting the microseconds, not angle. These are the helpful values ...

#define MIN_PULSE_WIDTH       544     // the shortest pulse sent to a servo  
#define MAX_PULSE_WIDTH      2400     // the longest pulse sent to a servo 
#define DEFAULT_PULSE_WIDTH  1500     // default pulse width when servo is attached

When you "attach" the Servo, you can provide your own min and max like

Servo.attach(pin, 500, 2500); // use an extended range
Keith Andrew Hill
@vic10us
Apr 19 2016 23:55
Anyone know a good place to get nema 17/23 steppers in or around Montreal?