Register a SA Forums Account here!
JOINING THE SA FORUMS WILL REMOVE THIS BIG AD, THE ANNOYING UNDERLINED ADS, AND STUPID INTERSTITIAL ADS!!!

You can: log in, read the tech support FAQ, or request your lost password. This dumb message (and those ads) will appear on every screen until you register! Get rid of this crap by registering your own SA Forums Account and joining roughly 150,000 Goons, for the one-time price of $9.95! We charge money because it costs us money per month for bills, and since we don't believe in showing ads to our users, we try to make the money back through forum registrations.
 
  • Post
  • Reply
Spazzle
Jul 5, 2003

Parts Kit posted:

It may just be me or the combo of stuff I was using on a past project but Adafruit's libraries seem really bloated.

I recall watching the output of both on my oscilloscope, and the neopixel bits were just way longer. Long enough that it cut into the frame rate with big chains. The fast led output was basically as short as could be dumped out of arduino.

Adbot
ADBOT LOVES YOU

Fanged Lawn Wormy
Jan 4, 2008

SQUEAK! SQUEAK! SQUEAK!
Fastled also has some very nice math functions which I often pull over for lighting projects, even if I'm not using pixels.

unpacked robinhood
Feb 18, 2013

by Fluffdaddy
I'm trying to output unmodulated IR codes when pressing a button, which works half the time.

I suspect it's got something to do with the state of the counter when hitting the button but so far I haven't been able to fix this.

This is what the output looks like:



Here's my code, trimmed for readability. Pressing the button calls transmit()

code:
void setupPCM () {
    TCCR0B = 1<<CS01;  // prescaler 8
    TCCR0A = 1<<COM0B0 |1<<WGM01; // CTC Mode, toggle OC0B on compare match
    OCR0A=0xFF;
}

void stopPCM () {
    TCCR0A &= ~(1<<WGM01);
    TCCR0A &= ~(1<<COM0B0);
}

void pulse (int high, int low) {
    OCR0A=high;
    for (char i=0; i<2; i++) {
        //wait for flag
        do ; while ((TIFR & 1<<OCF0A) == 0);
        TIFR = 1<<OCF0A;
        OCR0A = low;
    }
}

void sendCode (uint8_t code) {
    TCNT0=0;
    for (int Bit=7; Bit>-1; Bit--) {
        if (code & ((unsigned long) 1<<Bit)) pulse(82, 184); else pulse(80, 50);
    }
}

void transmit(){
    setupPCM ()
    sendCode(0x0F);
    stopPCM();
}


int main() {
        sei();
    while(1){
            //wait for button press then call transmit()    

    }
}
How do you correctly start and stop pulses at the correct time in this mode ?

edmund745
Jun 5, 2010

unpacked robinhood posted:

...
Here's my code, trimmed for readability. Pressing the button calls transmit()

code:
,,,,
void pulse (int high, int low) {
    OCR0A=high;
    for (char i=0; i<2; i++) {
        //wait for flag
        do ; while ((TIFR & 1<<OCF0A) == 0);
        TIFR = 1<<OCF0A;
        OCR0A = low;
    }
}
,,,
Why is your for-loop counter using a char variable as the counter? Is that supposed to be an int?

I know you can use a char as a loop counter in C, but you are assigning numeric values to it, and char(zero) is numeric value=48.... All the printing character values begin at ASCII value 32....

thegasman2000
Feb 12, 2005
Update my TFLC log? BOLLOCKS!
/
:backtowork:
Just ordered this kit
https://www.amazon.co.uk/dp/B01D8KOZF4/ref=cm_sw_r_cp_api_IzRTzb68JGWRJ

I will mostly be tinkering with sensors, leds and motors until I am up to speed. Then I am looking to make a reef fish tank controller. Oh and some basic home automation stuff. I really really want to ask alexa to make me a coffee in the morning!

SpartanIvy
May 18, 2007
Hair Elf

thegasman2000 posted:

Just ordered this kit
https://www.amazon.co.uk/dp/B01D8KOZF4/ref=cm_sw_r_cp_api_IzRTzb68JGWRJ

I will mostly be tinkering with sensors, leds and motors until I am up to speed. Then I am looking to make a reef fish tank controller. Oh and some basic home automation stuff. I really really want to ask alexa to make me a coffee in the morning!

I ordered this kit a couple days ago and although I've only made it to like lesson 5 in the book, it's been great so far.

I think the CD it comes with is blank, but I just downloaded everything off their website.

thegasman2000
Feb 12, 2005
Update my TFLC log? BOLLOCKS!
/
:backtowork:

SpartanIV posted:

I ordered this kit a couple days ago and although I've only made it to like lesson 5 in the book, it's been great so far.

I think the CD it comes with is blank, but I just downloaded everything off their website.

I replaced my MacBook DVD drive with another hard drive. I have not missed it but thanks for the heads up!

Really excited to get this up and running :)

unpacked robinhood
Feb 18, 2013

by Fluffdaddy

edmund745 posted:

Why is your for-loop counter using a char variable as the counter? Is that supposed to be an int?

I know you can use a char as a loop counter in C, but you are assigning numeric values to it, and char(zero) is numeric value=48.... All the printing character values begin at ASCII value 32....

I didn't wrote this part I think it was originally for some flavor of *uino. Isn't this a hack to have the compiler use 8 bits instead of whatever is an int on the platform ? I admit it doesn't really makes sense on an 8 bit platform anyway

Malcolm XML
Aug 8, 2009

I always knew it would end like this.
I want to clock out bits at a specific rate -- just OOK basically. My current solution is a timer interrupt that changes the pin state based on the data and a bits sent counter that's incremented. The timer stops firing when the last bit is clocked out.

Is there a nicer way to do this? I'd like to set up some hardware that to be like: here's a pointer here's the length, bit time is x usec , clock em out and interrupt when you're done

taqueso
Mar 8, 2004


:911:
:wookie: :thermidor: :wookie:
:dehumanize:

:pirate::hf::tinfoil:

edmund745 posted:

Why is your for-loop counter using a char variable as the counter? Is that supposed to be an int?

I know you can use a char as a loop counter in C, but you are assigning numeric values to it, and char(zero) is numeric value=48.... All the printing character values begin at ASCII value 32....

unpacked robinhood posted:

I didn't wrote this part I think it was originally for some flavor of *uino. Isn't this a hack to have the compiler use 8 bits instead of whatever is an int on the platform ? I admit it doesn't really makes sense on an 8 bit platform anyway

char / unsigned char will define an 8-bit integer variable. In C, there is nothing that makes a char special and only used for text characters, it is just an unfortunate name that implies it is special. Assigning 0 to a char is the same as assigning 0 to an int, you can do math in the normal way, etc. If you want to assign the ASCII character '0', you can do that with single quotes.

Using a char instead of int is a common way to save a byte in an embedded context if you, like in this case, know the variable will not ever be large enough to overflow. I wouldn't bother worrying about it until you are optimizing, you might as well use int everywhere. Anyway, I would expect a modern compiler to use 1 byte for an int in a situation like this (very clear the variable will not overflow a byte), if you are compiling for an 8-bit target.

code:
	char my_char = 0; 	// my_char has the value 0
	my_char = '0';		// my_char has the value 48
	my_char = 48;		// my_char is still 48, which is equivalent to '0'

Sagebrush
Feb 26, 2012

unpacked robinhood posted:

I didn't wrote this part I think it was originally for some flavor of *uino. Isn't this a hack to have the compiler use 8 bits instead of whatever is an int on the platform ? I admit it doesn't really makes sense on an 8 bit platform anyway

Recent versions of the C++ spec (which Arduino uses) allows you to explicitly define integer widths as follows:

code:
int8_t alpha = -69;      //"char"
uint8_t beta = 187;      //"byte"
int16_t gamma = -420;    //"int"
uint16_t delta = 31337;  //"unsigned int"
etc.

TheresaJayne
Jul 1, 2011

thegasman2000 posted:

Just ordered this kit
https://www.amazon.co.uk/dp/B01D8KOZF4/ref=cm_sw_r_cp_api_IzRTzb68JGWRJ

I will mostly be tinkering with sensors, leds and motors until I am up to speed. Then I am looking to make a reef fish tank controller. Oh and some basic home automation stuff. I really really want to ask alexa to make me a coffee in the morning!

I got it cheaper from Wish.com

http://www.wish.com/c/593e086ee389ac6e350a4037

I also got loads of stuff from there real cheap

http://www.wish.com/c/5821b1c69138567e1723fbcb

and

https://www.wish.com/c/576219ab82340962480489ce

They offer full refund if they do not work
(and you get to keep them)

edmund745
Jun 5, 2010

taqueso posted:

char / unsigned char will define an 8-bit integer variable. In C, there is nothing that makes a char special and only used for text characters, it is just an unfortunate name that implies it is special. ...
I've seen where people used a char as a counter, but it was usually because they wanted to "print" the char at some point. The posted code was char[0] to char[2], which is non-printing characters.

TheresaJayne posted:

I got it cheaper from Wish.com ...

...They offer full refund if they do not work
(and you get to keep them)
Well if they don't work then they're hardly worth keeping.
But I mainly use China clone Arduino boards and so far all of them worked 100%.

The only parts I've had that were bad were some Arduino prototyping shields that had some sloppy solder that shorted the 5v pin to a ground pin.
The Arduino boards (like the Unos, Nanos and Megas) get loaded with a blink sketch and tested, to confirm that they work.
The prototype shields have a couple buttons and LEDs on them, but they probably don't get tested at all. So you reeealllyy want to check the solder and traces on them before sticking them on a board and powering it up... (pop! goes the voltage regulator)

Where is cheapest to buy depends a lot on where you are.
In the US you can order stuff dirt cheap from China, because China has US most-favored-nation trading status, and so it gets a special extra-low mailing rate to send small packages to the US. The small-package rate from China is cheaper than the small-package rate inside the USA.
US sellers selling the same items (even at the same price) can get it to you faster (and sometimes I will pay more to buy something from a US-based seller for that reason) but they can't compete on price, just on shipping costs alone.

US mail-order companies universally hate the ePacket program.
To add insult to injury, the USPS won't even publish the actual list of China-to-US rates:
http://www.ecommercebytes.com/C/blog/blog.pl?/pl/2016/1/1453344622.html
I don't think ePacket will live forever, but I'm using it while it's here.

Probably all of the China-direct sites have a "keep it for free if not satisfied" because it can't be mail it back as cheap as it can get mailed to the US. The return postage cost makes it not worth doing.

(For US people) I tend to stick to Aliexpress, but there's banggood, dx and others.

thegasman2000
Feb 12, 2005
Update my TFLC log? BOLLOCKS!
/
:backtowork:
Yeah the kit was on Amazon prime deals for £20 so a decent enough price and peace of mind if anything is wrong.

For cheap poo poo I use Joom, gearbest and AliExpress to the uk but expect at least a 4 week lead time. I avoid eBay because it's a poo poo show if anything isn't working etc.

Malcolm XML
Aug 8, 2009

I always knew it would end like this.

edmund745 posted:

I've seen where people used a char as a counter, but it was usually because they wanted to "print" the char at some point. The posted code was char[0] to char[2], which is non-printing characters.

Well if they don't work then they're hardly worth keeping.
But I mainly use China clone Arduino boards and so far all of them worked 100%.

The only parts I've had that were bad were some Arduino prototyping shields that had some sloppy solder that shorted the 5v pin to a ground pin.
The Arduino boards (like the Unos, Nanos and Megas) get loaded with a blink sketch and tested, to confirm that they work.
The prototype shields have a couple buttons and LEDs on them, but they probably don't get tested at all. So you reeealllyy want to check the solder and traces on them before sticking them on a board and powering it up... (pop! goes the voltage regulator)

Where is cheapest to buy depends a lot on where you are.
In the US you can order stuff dirt cheap from China, because China has US most-favored-nation trading status, and so it gets a special extra-low mailing rate to send small packages to the US. The small-package rate from China is cheaper than the small-package rate inside the USA.
US sellers selling the same items (even at the same price) can get it to you faster (and sometimes I will pay more to buy something from a US-based seller for that reason) but they can't compete on price, just on shipping costs alone.

US mail-order companies universally hate the ePacket program.
To add insult to injury, the USPS won't even publish the actual list of China-to-US rates:
http://www.ecommercebytes.com/C/blog/blog.pl?/pl/2016/1/1453344622.html
I don't think ePacket will live forever, but I'm using it while it's here.

Probably all of the China-direct sites have a "keep it for free if not satisfied" because it can't be mail it back as cheap as it can get mailed to the US. The return postage cost makes it not worth doing.

(For US people) I tend to stick to Aliexpress, but there's banggood, dx and others.

Yeah epacket is effectively dumping


But cheap as hell electronics? Who cares??

VikingofRock
Aug 24, 2008




So I've been thinking a fun end-of-summer project would be to clone my garage door opener, which seems like it should just require a RF transmitter/receiver pair and a cheap microcontroller. Any recommendations for a low-cost microcontroller for this sort of thing? I don't have a ton of experience with embedded stuff (although I do have a lot of experience with C), so something beginner-friendly would be nice.

Sagebrush
Feb 26, 2012

To clone a garage door opener you can use any pair of little Arduino modules (Nano is small but also easy to work with) and the cheapo 433MHz/315MHz radio modules that are 10 for 10 dollars from eBay. No security other than obscurity, limited range, but it's easy.

babyeatingpsychopath
Oct 28, 2000
Forum Veteran


unpacked robinhood posted:

I'm trying to output unmodulated IR codes when pressing a button, which works half the time.

I suspect it's got something to do with the state of the counter when hitting the button but so far I haven't been able to fix this.

This is what the output looks like:



Here's my code, trimmed for readability. Pressing the button calls transmit()

How do you correctly start and stop pulses at the correct time in this mode ?

Here's my take on your code. I unwrapped the loop for clarity. I also like the "for(;test;);" construction instead of do;while(test);
C code:
void pulse(int high, int low){
	//inputs: high, low, number of clock ticks spent in each state.
	//outputs??? I have no idea when the output is set.
	OCR0A=high; 					//i assume we're setting some clock register
	//set some output pin high here?
	for(;(TIFR&1<<OCF0A) == 0;); 	//and then waiting for it to time out
	TIFR=1<<OCF0A; 					// then resetting the interrupt
	OCR0A=low; 						// then doing it
	//set some output pin low here?
	for(;(TIFR&1<<OCF0A) == 0;);	// over again
}
void sendCode(uint8_t code){
	//inputs: code, 8-bit code we're sending
	TCNT0=0;
	uint8_t mask=0x80;
	while(mask)
	{
		if(code&mask) // check the bit at the mask position
			pulse(82,184); 	// if 1, long pulse
		else
			pulse(80,50);  	// if 0, short pulse
		mask>>=1; 			//shift mask down.
	}
}

Lowen SoDium
Jun 5, 2003

Highen Fiber
Clapping Larry

Sagebrush posted:

To clone a garage door opener you can use any pair of little Arduino modules (Nano is small but also easy to work with) and the cheapo 433MHz/315MHz radio modules that are 10 for 10 dollars from eBay. No security other than obscurity, limited range, but it's easy.

Most garage door openers made in the last 15-20 years use some kind of rolling code or encryption.

edit: Honestly not sure about the encryption part. Genie says they use encryption and then immediately say rolling code in the same sentence.

Lowen SoDium fucked around with this message at 13:29 on Sep 13, 2017

sharkytm
Oct 9, 2003

Ba

By

Sharkytm doot doo do doot do doo


Fallen Rib
That's the same thing, rite? LOL at garage door company's security. The remotes are semi secure rolling code, but I'll bet :10bux: their internet based stuff is a joke.

VikingofRock
Aug 24, 2008




It's kind of irrelevant in my case because I'm pretty sure the opener I'm cloning is from the 80s.

unpacked robinhood
Feb 18, 2013

by Fluffdaddy

babyeatingpsychopath posted:

Here's my take on your code. I unwrapped the loop for clarity. I also like the "for(;test;);" construction instead of do;while(test);


Thanks, I'll try running it after the weekend when I'm home. I'm still wondering whether the OCR0A register should be updated outside of the appropriate interrupt routine.

thegasman2000
Feb 12, 2005
Update my TFLC log? BOLLOCKS!
/
:backtowork:
So I have been fiddling with my starter kit and also bought a nodeMCA ESP8266 to try a few IOT things. Now the issue is with the programming side of things as I dont understand what I need to do to turn on a relay with it. Its for connecting to my amazon alexa. I have a demo sketch with also included some NeoPixel code, which I have tried to erase to make life easier. here is what i have so far..

code:

#include <Arduino.h>
#include <ESP8266WiFi.h>
#include "fauxmoESP.h"

#define WIFI_SSID "cliche"
#define WIFI_PASS "meh"

#define SERIAL_BAUDRATE                 115200

fauxmoESP fauxmo;

#define RELAY_PIN 13

// -----------------------------------------------------------------------------
// Wifi
// -----------------------------------------------------------------------------


void wifiSetup() {
  // Set WIFI module to STA mode
  WiFi.mode(WIFI_STA);

  // Connect
  Serial.printf("[WIFI] Connecting to %s ", WIFI_SSID);
  WiFi.begin(WIFI_SSID, WIFI_PASS);

  // Wait
  while (WiFi.status() != WL_CONNECTED) {
      Serial.print(".");
      delay(100);
  }
  Serial.println();

  // Connected!
  Serial.printf("[WIFI] STATION Mode, SSID: %s, IP address: %s\n", WiFi.SSID().c_str(), WiFi.localIP().toString().c_str());
}


void callback(uint8_t device_id, const char * device_name, bool state) {
  Serial.printf("[MAIN] %s state: %s\n", device_name, state ? "ON" : "OFF");
  

  if ( (strcmp(device_name, "relay1") == 0) ) {
    // adjust the relay immediately!
    if (state) {
      digitalWrite(RELAY_PIN, HIGH);
    } else {
      digitalWrite(RELAY_PIN, LOW);
    }
  }
}

void setup() {
  
  pinMode(RELAY_PIN, OUTPUT);
  digitalWrite(RELAY_PIN, LOW);
  
    // Init serial port and clean garbage
    Serial.begin(SERIAL_BAUDRATE);
    Serial.println();
    Serial.println();
    Serial.println("FauxMo demo sketch");
    Serial.println("After connection, ask Alexa/Echo to 'turn pixels on' or 'off' or 'turn relay on' or 'off'");

  // Wifi
  wifiSetup();

  // Fauxmo
  fauxmo.addDevice("relay");
  fauxmo.onMessage(callback);
}

void loop() {
  fauxmo.handle();
  
  }


When I open the serial monitor I can get the output to display FauxMo demo sketch
After connection, ask Alexa/Echo to 'turn pixels on' or 'off' or 'turn relay on' or 'off'
[WIFI] Connecting to PrettyFlyForAWifi .........
[WIFI] STATION Mode, SSID: PrettyFlyForAWifi, IP address: 192.168.1.232
[MAIN] relay state: OFF
[MAIN] relay state: ON
Which seems to suggest I have the thing working but my relay, plugged into GPIO 13, just stays "ON" all the time.

thegasman2000 fucked around with this message at 14:35 on Sep 25, 2017

TheLastManStanding
Jan 14, 2008
Mash Buttons!
code:
if ( (strcmp(device_name, "relay1") == 0) ) {
    // adjust the relay immediately!
    if (state) {
      digitalWrite(RELAY_PIN, HIGH);
    } else {
      digitalWrite(RELAY_PIN, LOW);
    }
}
Stick some serial writes inside the if-else statements to see if the if statement is reacting properly. Also your code is comparing to 'relay1', but your sample console output just calls it 'relay'.

Splode
Jun 18, 2013

put some clothes on you little freak
Also edit out your Wi-Fi password

wolrah
May 8, 2006
what?

Splode posted:

Also edit out your Wi-Fi password
Also if you actually care about security change your SSID. The SSID is used as the salt when hashing the password, so there are rainbow tables for most common SSIDs. If it's a device default or something funny you've seen on the internet you can be pretty sure it's already in those tables. I'd be willing to bet that a purely alphabetical password is already in the tables, meaning your password would be cracked literally within fractions of a second of being captured.

thegasman2000
Feb 12, 2005
Update my TFLC log? BOLLOCKS!
/
:backtowork:
Yeah I really am not worried about my Wifi password being on their as my router makes 2 ssid and this one is for testing alone, but thanks!

So got it working. If anyone is in the least bit interested... Its a cheap NodeMCU running an 8 channel relay board making a smart power strip of sorts. This is then all controlled using my amazon echo. I plan on using it for a marine fish tank to allow hands free control while cleaning and pottering about along with some monitoring etc later such as temp sensing and low water levels turning on a top up pump.

code:

#include <Arduino.h>
#include <ESP8266WiFi.h>
#include "fauxmoESP.h"

#define WIFI_SSID "cliche funny wifi name"
#define WIFI_PASS "This could be anything... "

#define SERIAL_BAUDRATE                 115200

fauxmoESP fauxmo;

#define RELAY1 2
#define RELAY2 16
#define RELAY3 9
#define RELAY4 10
#define RELAY5 14
#define RELAY6 12
#define RELAY7 5
#define RELAY8 4
// -----------------------------------------------------------------------------
// Wifi
// -----------------------------------------------------------------------------


void wifiSetup() {
  // Set WIFI module to STA mode
  WiFi.mode(WIFI_STA);

  // Connect
  Serial.printf("[WIFI] Connecting to %s ", WIFI_SSID);
  WiFi.begin(WIFI_SSID, WIFI_PASS);

  // Wait
  while (WiFi.status() != WL_CONNECTED) {
      Serial.print(".");
      delay(100);
  }
  Serial.println();

  // Connected!
  Serial.printf("[WIFI] STATION Mode, SSID: %s, IP address: %s\n", WiFi.SSID().c_str(), WiFi.localIP().toString().c_str());
}


void callback(uint8_t device_id, const char * device_name, bool state) {
  Serial.printf("[MAIN] %s state: %s\n", device_name, state ? "ON" : "OFF");
  

  if ( (strcmp(device_name, "relay1") == 0) ) {
    // adjust the relay immediately!
    if (state) {
      digitalWrite(RELAY1, HIGH);
      Serial.write("high");
    } else {
      digitalWrite(RELAY1, LOW);
      Serial.write("Low");
    }
  }

  if ( (strcmp(device_name, "relay2") == 0) ) {
    // adjust the relay immediately!
    if (state) {
      digitalWrite(RELAY2, HIGH);
      Serial.write("high");
    } else {
      digitalWrite(RELAY2, LOW);
      Serial.write("Low");
    }
  }

  if ( (strcmp(device_name, "relay3") == 0) ) {
    // adjust the relay immediately!
    if (state) {
      digitalWrite(RELAY3, HIGH);
      Serial.write("high");
    } else {
      digitalWrite(RELAY3, LOW);
      Serial.write("Low");
    }
  }

  if ( (strcmp(device_name, "relay4") == 0) ) {
    // adjust the relay immediately!
    if (state) {
      digitalWrite(RELAY4, HIGH);
      Serial.write("high");
    } else {
      digitalWrite(RELAY4, LOW);
      Serial.write("Low");
    }
  }

  if ( (strcmp(device_name, "relay5") == 0) ) {
    // adjust the relay immediately!
    if (state) {
      digitalWrite(RELAY5, HIGH);
      Serial.write("high");
    } else {
      digitalWrite(RELAY5, LOW);
      Serial.write("Low");
    }
  }

  if ( (strcmp(device_name, "relay6") == 0) ) {
    // adjust the relay immediately!
    if (state) {
      digitalWrite(RELAY6, HIGH);
      Serial.write("high");
    } else {
      digitalWrite(RELAY6, LOW);
      Serial.write("Low");
    }
  }

  if ( (strcmp(device_name, "relay7") == 0) ) {
    // adjust the relay immediately!
    if (state) {
      digitalWrite(RELAY7, HIGH);
      Serial.write("high");
    } else {
      digitalWrite(RELAY7, LOW);
      Serial.write("Low");
    }
  }

  if ( (strcmp(device_name, "relay8") == 0) ) {
    // adjust the relay immediately!
    if (state) {
      digitalWrite(RELAY8, HIGH);
      Serial.write("high");
    } else {
      digitalWrite(RELAY8, LOW);
      Serial.write("Low");
    }
  }



}

void setup() {
  
  pinMode(RELAY1, OUTPUT);
  pinMode(RELAY2, OUTPUT);
  pinMode(RELAY3, OUTPUT);
  pinMode(RELAY4, OUTPUT);
  pinMode(RELAY5, OUTPUT);
  pinMode(RELAY6, OUTPUT);
  pinMode(RELAY7, OUTPUT);
  pinMode(RELAY8, OUTPUT);
  
  digitalWrite(RELAY1, LOW);
  digitalWrite(RELAY2, LOW);
  digitalWrite(RELAY3, LOW);
  digitalWrite(RELAY4, LOW);
  digitalWrite(RELAY5, LOW);
  digitalWrite(RELAY6, LOW);
  digitalWrite(RELAY7, LOW);
  digitalWrite(RELAY8, LOW);
  
    // Init serial port and clean garbage
    Serial.begin(SERIAL_BAUDRATE);


  // Wifi
  wifiSetup();

  // Fauxmo
  fauxmo.addDevice("relay1");
  fauxmo.addDevice("relay2");
  fauxmo.addDevice("relay3");
  fauxmo.addDevice("relay4");
  fauxmo.addDevice("relay5");
  fauxmo.addDevice("relay6");
  fauxmo.addDevice("relay7");
  fauxmo.addDevice("relay8");

  fauxmo.onMessage(callback);
}

void loop() {
  fauxmo.handle();
  
  }

I am sure their is a switch statement I could use instead of the 8 if statements but for the min this works, is fast and is is easy to debug.

Splode
Jun 18, 2013

put some clothes on you little freak
Post pictures of the physical set up!

You've made me want to try using IFTTT with an esp8266 now

thegasman2000
Feb 12, 2005
Update my TFLC log? BOLLOCKS!
/
:backtowork:
I started to solder it up, realised my soldering is not uk to scratch and ordered some female to female leads :bang:

My 8 channel relay board has some tiny surface mount leds to tell when the relay is switched off. I want to extend these so I can have a visual check on what's on and off at a glance. Should I extend these? Or add an led inline to the relay switch from the gpio?

Also the next phase is to integrate some web stuff so I can log in and see what's on and off. Potentially see how long etc. Any pointers on what to google? I have an rpi sat doing f all so might be easiest.

thegasman2000 fucked around with this message at 20:40 on Sep 27, 2017

thegasman2000
Feb 12, 2005
Update my TFLC log? BOLLOCKS!
/
:backtowork:
Sorry for the double post...

So I also need to apologise for what i suspect is really, really ugly code! I bastardised some tutorial code to make a webserver which gives the state of one relay.

code:

#include <ArduinoJson.h>
#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
#include "fauxmoESP.h"
fauxmoESP fauxmo;

#define SERIAL_BAUDRATE                 115200

//Relays
#define RELAY1 9
bool relay1State = digitalRead(9);
String relay1StateString;


#define WLAN_SSID     "meh"
#define WLAN_PASSWORD "meh"


ESP8266WebServer server(80);

const long interval = 2000;


// make variables for relay state
void callback(uint8_t device_id, const char * device_name, bool state) {
  Serial.printf("[MAIN] %s state: %s\n", device_name, state ? "ON" : "OFF");

  if ( (strcmp(device_name, "relay1") == 0) ) {
    // adjust the relay immediately!
    if (state) {
      digitalWrite(RELAY1, HIGH);
      Serial.write("high");
      
    } else {
      digitalWrite(RELAY1, LOW);
      Serial.write("Low");
    }
  }

}

void setup() {

  pinMode(RELAY1, OUTPUT);

  
  digitalWrite(RELAY1, LOW);

  
    // Init serial port and clean garbage
    Serial.begin(SERIAL_BAUDRATE);



  // Fauxmo
  fauxmo.addDevice("relay1");


  fauxmo.onMessage(callback);

    Serial.begin(115200);
    delay(10);



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

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

    Serial.println("WiFi connected");
    Serial.println("IP address: "); Serial.println(WiFi.localIP());

    server.on("/server", HTTP_GET, [](){
      if (relay1State == 1) {
        relay1StateString = "True"; }
        else {
          relay1StateString = "False"; }
      });

        String webString = "Relay 1 " + (relay1StateString);
        Serial.println(webString);
        server.send(200, "text/plain", webString);


    server.begin();
    Serial.println("HTTP server started! Waiting for clients!");
}

void loop() {
    server.handleClient();
}

Now it compiles (eventually) and loads on the node. I get this in my serial output

Connecting to meh
..
WiFi connected
IP address:
192.168.1.232
Relay 1
HTTP server started! Waiting for clients!

however when I got to 192.168.1.232/server I get nothing. The tutorial code worked fine so its not a network issue at all.

unpacked robinhood
Feb 18, 2013

by Fluffdaddy
My attiny thing is starting to work but there's still something weird.
I'm putting two eight bit values together and build a 16 bit pulse train with the following pattern: preamble - 16 bit pulse sequence - stop pulse.

However I had to use weird values for the loop index to get it to work, sendCode() is the function I'm having trouble with:

code:
const uint8_t JVC_VOLM = 0x71;
const uint8_t JVC_ADDRESS = 0xF1;

void sendCode (uint16_t code) {
	TCNT0=0;
	for (int Bit=17; Bit>-1; Bit--) {//weird loop indexes, I'd expect Bit=15; ...
		if (code & (1<<Bit)) {
			OCR0A=255;
			OCR0B=80;
		}
		else {
			OCR0A=130;
			OCR0B=80;
		};
		//wait for TIFR:OCF0B==1
		//The OCF0B bit is set when a Compare Match occurs between the Timer/Counter and the data in OCR0B
		do ; while ((TIFR & 1<<OCF0B) == 0);
		//clear OCF0B by writing a 1
		TIFR = 1<<OCF0B;
	}
	//stop bit (short pulse)
	OCR0A=130;
	OCR0B=80;
	do ; while ((TIFR & 1<<OCF0B) == 0);
	TIFR = 1<<OCF0B;
}

void transmit(uint8_t address,uint8_t code){
	preamble();
	setupPCM();//pwm settings
	sendCode((JVC_ADDRESS<<8)+code);
	stopPCM();//disable pwm
}

transmit(JVC_ADDRESS,JVC_VOLM):
This function correctly outputs the following signal:



However if I edit the loop inside sendCode() to

code:
for (int Bit=15; Bit>-1; Bit--)
The first two pulses of the address part (highlighted in orange) go missing. It makes no sense to me. What am I missing ?

unpacked robinhood fucked around with this message at 23:03 on Oct 2, 2017

babyeatingpsychopath
Oct 28, 2000
Forum Veteran


unpacked robinhood posted:


code:
for (int Bit=15; Bit>-1; Bit--)
The first two pulses of the address part (highlighted in orange) go missing. It makes no sense to me. What am I missing ?

try
code:
for (uint16_t Bit=0x8000;Bit;Bit=Bit>>1)
and see if it gives you the same result. 1 << 17 should roll off the end of a uint16_t.

unpacked robinhood
Feb 18, 2013

by Fluffdaddy

babyeatingpsychopath posted:

try
code:
for (uint16_t Bit=0x8000;Bit;Bit=Bit>>1)
and see if it gives you the same result. 1 << 17 should roll off the end of a uint16_t.

I edited in your loop declaration and changed the condition right after.
It's more elegant but it still either loses 1 or 2 pulses at the beginning, or introduces one short extra glitchy pulse at the end.

code:
void sendCode (uint16_t code) {
	TCNT0=0;
	for (uint16_t Bit=0x8000;Bit;Bit=Bit>>1){
		if (code & Bit) {
			OCR0A=255;
			OCR0B=80;
		}
		else {
			OCR0A=130;
			OCR0B=80;
		};
		//wait for TIFR:OCF0B==1
		do ; while ((TIFR & 1<<OCF0B) == 0);
		//clear OCF0B by writing a 1
		TIFR = 1<<OCF0B;
	}
	//stop bit (short pulse)
	OCR0A=130;
	OCR0B=80;
	do ; while ((TIFR & 1<<OCF0B) == 0);
	TIFR = 1<<OCF0B;
}
This isn't a big deal because my previous code still works better, but I admit it's not satisfying and hacky.

unpacked robinhood fucked around with this message at 14:48 on Oct 3, 2017

babyeatingpsychopath
Oct 28, 2000
Forum Veteran


unpacked robinhood posted:

I edited in your loop declaration and changed the condition right after.
It's more elegant but it still either loses 1 or 2 pulses at the beginning, or introduces one short extra glitchy pulse at the end.

This isn't a big deal because my previous code still works better, but I admit it's not satisfying and hacky.

That just means it's not the for loop initializing that's causing your bit problems. Your program is just not sending one or two bits to begin with. Starting upshifted 17 just means you don't send the bits you're missing, so you always start correctly. Look elsewhere for this problem.

thegasman2000
Feb 12, 2005
Update my TFLC log? BOLLOCKS!
/
:backtowork:
So my 2 year old tried to burn my house down and its given me the inspiration to make something actually useful. Here in the UK we wire our electric cookers into a box with a simple throw switch built in. It looks like this https://imgur.com/a/yFxuE My darling child turned on one of the heating elements (hobs) while a nicely oiled wooden chopping block was sat on top. Lots of smoke and a torched chopping board later and we are lucky my smoke alarm works. So the problem is to make the setup safer. Now we normally turn the cooker off at the wall, most people with no small kids leave it on all the time but safety first and all that, but forgot to after cooking dinner.

The solution. Well I think a simple solution of a large relay, 50A 240AC such as http://www.ebay.co.uk/itm/Solid-Sta...vkAAOSwbYZXex8v should surfice. I then think adding a small display such as http://www.ebay.co.uk/itm/I2C-OLED-...H7BzbjR65ULo-Bw and a decent button. When you press the button it adds 15 minutes to a timer, when the timer runs out it turns the cooker off. So you want to cook something for an hour? Press the button 5 times, the timer reads 1.15 and the oven turns on. Does this seem a reasonable solution to the problem?

I would need to embed a small arduino board and a power supply for said board all into a standard electrical box and mount the whole thing to a blank front.

Collateral Damage
Jun 13, 2009

You're describing an outlet auto-off timer, which you can buy off the shelf in any appliance store. You might have to shop around a bit (or talk to an electrician) to find one that can handle the draw of a cooktop though..

Something like this, assuming your cooktop draws less than 10A: https://www.amazon.co.uk/gp/product/B005C9S0I8/

e: vv What evil_bunnY said. Don't mix DIY and things that can burn your house down. Talk to an electrician.

Collateral Damage fucked around with this message at 12:30 on Oct 5, 2017

evil_bunnY
Apr 2, 2003

thegasman2000 posted:

So my 2 year old tried to burn my house down

I would need to embed a small arduino board and a power supply for said board all into a standard electrical box and mount the whole thing to a blank front.
If you're using arduino and safety in the same sentence you need to reconsider your life choices. Buy an off the shelf auto-off timer you can put in the wall. Make sure it's not accessible to a child. Make sure you hammer in that appliances are not OK to touch.

thegasman2000
Feb 12, 2005
Update my TFLC log? BOLLOCKS!
/
:backtowork:
I have never seen one of these things! Much easier and safer thanks!

Zarikov
Jun 20, 2004

Metal Gear? Metal Gear? Metal Gear!
Dinosaur Gum
A very long time ago I was in a closing Radioshack and purchased a cheap Arduino Uno R3 and promptly forgot about it until I moved.

I found it and a dictation machine pedal controller and thought that it would be fun to turn it into a USB keyboard controller for use in games because why not open my inventory in PUBG with my toe which is otherwise doing nothing.

Turns out the Uno R3 doesn't support working as an HID keyboard. Technically it can- I ran through a bunch of posts and wikis filled with dead links, and reflashed the USB controller to make it unto a USB keyboard and back again... problem is it doesn't actually support the Arduino Keyboard library and there's no complete documentation on how to get around THAT. So gently caress it.

What's the cheapest small Arduino or similar thing I can get that can take 3 push buttons and turn them into keyboard strokes on a PC that won't require me to do anything other than call a keyboard library?

Alternatively, has anyone turned an Uno R3 into a functioning keyboard device and actually have documentation that isn't lost to the sands of time?

Adbot
ADBOT LOVES YOU

Sagebrush
Feb 26, 2012

Any Arduino-compatible board with an ATMega32U4 will be able to do keyboard emulation with the native library. The Leonardo is the official Arduino board that can do it, while the Sparkfun Pro Micro is the same thing in a board the size of a stick of gum. You can get knockoff versions of both from eBay, Amazon, AliExpress, etc for cheap. Knockoff Pro Micros are like 4 dollars with free shipping if you're okay with it taking the slow boat.

  • 1
  • 2
  • 3
  • 4
  • 5
  • Post
  • Reply